data.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /* $Id: data.c,v 1.24 2004/02/26 22:48:08 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2004 Willem Dijkstra
  4. * All rights reserved.
  5. *
  6. * The crc routine is by Rob Warnock <rpw3@sgi.com>, from the
  7. * comp.compression FAQ.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials provided
  18. * with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. * */
  33. /* Terminology:
  34. *
  35. * A host carrying a 'symon' is considered a 'source' of information. A single
  36. * data 'stream' of information has a particular type: <cpu|mem|if|io>. A
  37. * source can provide multiple 'streams' simultaniously. A source spools
  38. * information towards a 'mux'. A 'stream' that has been converted to network
  39. * representation is called a 'packedstream'.
  40. */
  41. #include <assert.h>
  42. #include <limits.h>
  43. #include <stdarg.h>
  44. #include <string.h>
  45. #include <stdio.h>
  46. #include <time.h>
  47. #include <unistd.h>
  48. #include "data.h"
  49. #include "error.h"
  50. #include "lex.h"
  51. #include "net.h"
  52. #include "xmalloc.h"
  53. __BEGIN_DECLS
  54. int bytelenvar(char);
  55. int checklen(int, int, int);
  56. struct stream *create_stream(int, char *);
  57. char *formatstrvar(char);
  58. char *rrdstrvar(char);
  59. int strlenvar(char);
  60. __END_DECLS
  61. /* Stream formats
  62. *
  63. * Format specifications are strings of characters:
  64. *
  65. * L = u_int64
  66. * D = 7.6f <= int64
  67. * l = u_int32
  68. * s = u_int16
  69. * c = 3.2f <= u_int14 <= u_int16 (used in percentages)
  70. * b = u_int8
  71. */
  72. struct {
  73. char type;
  74. char *rrdformat;
  75. char *strformat;
  76. int strlen;
  77. int bytelen;
  78. u_int64_t max;
  79. } streamvar[] = {
  80. { 'L', ":%llu", " %20llu", 22, sizeof(u_int64_t), (u_int64_t) 0xffffffffffffffff },
  81. { 'D', ":%7.6f", " %7.6f", 23, sizeof(int64_t), (u_int64_t) 0xffffffffffffffff },
  82. { 'l', ":%lu", " %10lu", 12, sizeof(u_int32_t), (u_int64_t) 0xffffffff },
  83. { 's', ":%u", " %5u", 7, sizeof(u_int16_t), (u_int64_t) 0xffff },
  84. { 'c', ":%3.2f", " %3.2f", 8, sizeof(u_int16_t), (u_int64_t) 100 },
  85. { 'b', ":%3u", " %3u", 5, sizeof(u_int8_t), (u_int64_t) 255 },
  86. { '\0', NULL, NULL, 0, 0, 0 }
  87. };
  88. /* streams of <type> have the packedstream <form> */
  89. struct {
  90. int type;
  91. char *form;
  92. } streamform[] = {
  93. { MT_IO1, "LLL" },
  94. { MT_CPU, "ccccc" },
  95. { MT_MEM, "lllll" },
  96. { MT_IF, "llllllllll" },
  97. { MT_PF, "LLLLLLLLLLLLLLLLLLLLLL" },
  98. { MT_DEBUG, "llllllllllllllllllll" },
  99. { MT_PROC, "lLLLlcll" },
  100. { MT_MBUF, "lllllllllllllll" },
  101. { MT_SENSOR, "D" },
  102. { MT_IO2, "LLLLL" },
  103. { MT_TEST, "LLLLDDDDllllssssccccbbbb" },
  104. { MT_EOT, "" }
  105. };
  106. struct {
  107. int type;
  108. int token;
  109. } streamtoken[] = {
  110. { MT_IO1, LXT_IO1 },
  111. { MT_CPU, LXT_CPU },
  112. { MT_MEM, LXT_MEM },
  113. { MT_IF, LXT_IF },
  114. { MT_PF, LXT_PF },
  115. { MT_DEBUG, LXT_DEBUG },
  116. { MT_PROC, LXT_PROC },
  117. { MT_MBUF, LXT_MBUF },
  118. { MT_SENSOR, LXT_SENSOR },
  119. { MT_IO2, LXT_IO },
  120. { MT_EOT, LXT_BADTOKEN }
  121. };
  122. /* parallel crc32 table */
  123. u_int32_t
  124. crc32_table[256];
  125. /* Convert lexical entities to stream entities */
  126. const int
  127. token2type(const int token)
  128. {
  129. int i;
  130. for (i = 0; streamtoken[i].type < MT_EOT; i++)
  131. if (streamtoken[i].token == token)
  132. return streamtoken[i].type;
  133. fatal("%s:%d: internal error: token (%d) could not be translated into a stream type",
  134. __FILE__, __LINE__, token);
  135. /* NOT REACHED */
  136. return 0;
  137. }
  138. /* Convert stream entities to their ascii representation */
  139. const char *
  140. type2str(const int streamtype)
  141. {
  142. int i;
  143. for (i = 0; streamtoken[i].type < MT_EOT; i++)
  144. if (streamtoken[i].type == streamtype)
  145. return parse_opcode(streamtoken[i].token);
  146. fatal("%s:%d: internal error: type (%d) could not be translated into ascii representation",
  147. __FILE__, __LINE__, streamtype);
  148. /* NOT REACHED */
  149. return 0;
  150. }
  151. /* Return the maximum lenght of the ascii representation of type <type> */
  152. int
  153. strlentype(int type)
  154. {
  155. int i = 0;
  156. int sum = 0;
  157. while (streamform[type].form[i])
  158. sum += strlenvar(streamform[type].form[i++]);
  159. return sum;
  160. }
  161. /* Return the maximum lenght of the ascii representation of streamvar <var> */
  162. int
  163. strlenvar(char var)
  164. {
  165. int i;
  166. for (i = 0; streamvar[i].type > '\0'; i++)
  167. if (streamvar[i].type == var)
  168. return streamvar[i].strlen;
  169. fatal("%s:%d: internal error: type spefication for stream var '%c' not found",
  170. __FILE__, __LINE__, var);
  171. /* NOT REACHED */
  172. return 0;
  173. }
  174. /* Return the maximum lenght of the network representation of streamvar <var> */
  175. int
  176. bytelenvar(char var)
  177. {
  178. int i;
  179. for (i = 0; streamvar[i].type > '\0'; i++)
  180. if (streamvar[i].type == var)
  181. return streamvar[i].bytelen;
  182. fatal("%s:%d: internal error: type spefication for stream var '%c' not found",
  183. __FILE__, __LINE__, var);
  184. /* NOT REACHED */
  185. return 0;
  186. }
  187. /* Return the ascii format string for streamvar <var> */
  188. char *
  189. formatstrvar(char var)
  190. {
  191. int i;
  192. for (i = 0; streamvar[i].type > '\0'; i++)
  193. if (streamvar[i].type == var)
  194. return streamvar[i].strformat;
  195. fatal("%s:%d: internal error: type spefication for stream var '%c' not found",
  196. __FILE__, __LINE__, var);
  197. /* NOT REACHED */
  198. return "";
  199. }
  200. /* Return the rrd format string for streamvar <var> */
  201. char *
  202. rrdstrvar(char var)
  203. {
  204. int i;
  205. for (i = 0; streamvar[i].type > '\0'; i++)
  206. if (streamvar[i].type == var)
  207. return streamvar[i].rrdformat;
  208. fatal("internal error: type spefication for stream var '%c' not found", var);
  209. /* NOT REACHED */
  210. return "";
  211. }
  212. /* Check whether <extra> more bytes fit in <maxlen> when we are already at <start> */
  213. int
  214. checklen(int maxlen, int current, int extra)
  215. {
  216. if ((current + extra) < maxlen) {
  217. return 0;
  218. } else {
  219. warning("buffer overflow: max=%d, current=%d, extra=%d",
  220. maxlen, current, extra);
  221. return 1;
  222. }
  223. }
  224. int
  225. setheader(char *buf, struct symonpacketheader *hph)
  226. {
  227. struct symonpacketheader nph;
  228. char *p;
  229. nph.timestamp = htonq(hph->timestamp);
  230. nph.crc = htonl(hph->crc);
  231. nph.length = htons(hph->length);
  232. nph.symon_version = hph->symon_version;
  233. p = buf;
  234. bcopy(&nph.crc, p, sizeof(u_int32_t));
  235. p += sizeof(u_int32_t);
  236. bcopy(&nph.timestamp, p, sizeof(u_int64_t));
  237. p += sizeof(u_int64_t);
  238. bcopy(&nph.length, p, sizeof(u_int16_t));
  239. p += sizeof(u_int16_t);
  240. bcopy(&nph.symon_version, p, sizeof(u_int8_t));
  241. p += sizeof(u_int8_t);
  242. return (p - buf);
  243. }
  244. int
  245. getheader(char *buf, struct symonpacketheader *hph)
  246. {
  247. char *p;
  248. p = buf;
  249. bcopy(p, &hph->crc, sizeof(u_int32_t));
  250. p += sizeof(u_int32_t);
  251. bcopy(p, &hph->timestamp, sizeof(u_int64_t));
  252. p += sizeof(u_int64_t);
  253. bcopy(p, &hph->length, sizeof(u_int16_t));
  254. p += sizeof(u_int16_t);
  255. bcopy(p, &hph->symon_version, sizeof(u_int8_t));
  256. p += sizeof(u_int8_t);
  257. hph->timestamp = ntohq(hph->timestamp);
  258. hph->crc = ntohl(hph->crc);
  259. hph->length = ntohs(hph->length);
  260. return (p - buf);
  261. }
  262. /*
  263. * Pack multiple arguments of a MT_TYPE into a network order bytestream.
  264. * snpack returns the number of bytes actually stored.
  265. */
  266. int
  267. snpack(char *buf, int maxlen, char *id, int type,...)
  268. {
  269. va_list ap;
  270. u_int16_t b;
  271. u_int16_t s;
  272. u_int16_t c;
  273. u_int32_t l;
  274. u_int64_t q;
  275. int64_t d;
  276. double D;
  277. int i = 0;
  278. int offset = 0;
  279. if (type > MT_EOT) {
  280. warning("stream type (%d) out of range", type);
  281. return 0;
  282. }
  283. if (maxlen < 2) {
  284. fatal("%s:%d: maxlen too small", __FILE__, __LINE__);
  285. } else {
  286. buf[offset++] = type & 0xff;
  287. }
  288. if (id) {
  289. if ((strlen(id) + 1) >= maxlen) {
  290. return 0;
  291. } else {
  292. strncpy(&buf[offset], id, maxlen - 1);
  293. offset += strlen(id);
  294. }
  295. }
  296. buf[offset++] = '\0';
  297. va_start(ap, type);
  298. while (streamform[type].form[i] != '\0') {
  299. /* check for buffer overflow */
  300. if (checklen(maxlen, offset, bytelenvar(streamform[type].form[i])))
  301. return offset;
  302. /*
  303. * all values smaller than 32 bytes are transferred using ints on the
  304. * stack. This is to ensure that we get the correct value, if the
  305. * compiler decided to upgrade our short to a 32bit int. -- cheers
  306. * dhartmei@
  307. */
  308. switch (streamform[type].form[i]) {
  309. case 'b':
  310. b = va_arg(ap, int);
  311. buf[offset++] = b;
  312. break;
  313. case 'c':
  314. D = va_arg(ap, double);
  315. c = (u_int16_t) (D * 100.0);
  316. c = htons(c);
  317. bcopy(&c, buf + offset, sizeof(u_int16_t));
  318. offset += sizeof(u_int16_t);
  319. break;
  320. case 's':
  321. s = va_arg(ap, int);
  322. s = htons(s);
  323. bcopy(&s, buf + offset, sizeof(u_int16_t));
  324. offset += sizeof(u_int16_t);
  325. break;
  326. case 'l':
  327. l = va_arg(ap, u_int32_t);
  328. l = htonl(l);
  329. bcopy(&l, buf + offset, sizeof(u_int32_t));
  330. offset += sizeof(u_int32_t);
  331. break;
  332. case 'L':
  333. q = va_arg(ap, u_int64_t);
  334. q = htonq(q);
  335. bcopy(&q, buf + offset, sizeof(u_int64_t));
  336. offset += sizeof(u_int64_t);
  337. break;
  338. case 'D':
  339. D = va_arg(ap, double);
  340. d = (int64_t) (D * 1000 * 1000);
  341. d = htonq(d);
  342. bcopy(&d, buf + offset, sizeof(int64_t));
  343. offset += sizeof(int64_t);
  344. break;
  345. default:
  346. warning("unknown stream format identifier %c in type %d",
  347. streamform[type].form[i],
  348. type);
  349. return 0;
  350. }
  351. i++;
  352. }
  353. va_end(ap);
  354. return offset;
  355. }
  356. /*
  357. * Unpack a packedstream in buf into a struct packetstream. Returns the number
  358. * of bytes actually read.
  359. *
  360. * Note that this function does "automatic" bounds checking; it uses a
  361. * description of the packedstream (streamform) to parse the actual bytes. This
  362. * description corresponds to the amount of bytes that will fit inside the
  363. * packedstream structure. */
  364. int
  365. sunpack(char *buf, struct packedstream * ps)
  366. {
  367. char *in, *out;
  368. int i = 0;
  369. int type;
  370. u_int16_t s;
  371. u_int16_t c;
  372. u_int32_t l;
  373. u_int64_t q;
  374. int64_t d;
  375. bzero(ps, sizeof(struct packedstream));
  376. in = buf;
  377. if ((*in) > MT_EOT) {
  378. warning("unpack failure: stream type (%d) out of range", (*in));
  379. return -1;
  380. }
  381. type = ps->type = (*in);
  382. in++;
  383. if ((*in) != '\0') {
  384. strncpy(ps->args, in, sizeof(ps->args));
  385. ps->args[sizeof(ps->args) - 1] = '\0';
  386. in += strlen(ps->args);
  387. } else {
  388. ps->args[0] = '\0';
  389. }
  390. in++;
  391. out = (char *) (&ps->data);
  392. while (streamform[type].form[i] != '\0') {
  393. switch (streamform[type].form[i]) {
  394. case 'b':
  395. bcopy((void *) in, (void *) out, sizeof(u_int8_t));
  396. in++;
  397. out++;
  398. break;
  399. case 'c':
  400. bcopy((void *) in, &c, sizeof(u_int16_t));
  401. c = ntohs(c);
  402. bcopy(&c, (void *) out, sizeof(u_int16_t));
  403. in += sizeof(u_int16_t);
  404. out += sizeof(u_int16_t);
  405. break;
  406. case 's':
  407. bcopy((void *) in, &s, sizeof(u_int16_t));
  408. s = ntohs(s);
  409. bcopy(&s, (void *) out, sizeof(u_int16_t));
  410. in += sizeof(u_int16_t);
  411. out += sizeof(u_int16_t);
  412. break;
  413. case 'l':
  414. bcopy((void *) in, &l, sizeof(u_int32_t));
  415. l = ntohl(l);
  416. bcopy(&l, (void *) out, sizeof(u_int32_t));
  417. in += sizeof(u_int32_t);
  418. out += sizeof(u_int32_t);
  419. break;
  420. case 'L':
  421. bcopy((void *) in, &q, sizeof(u_int64_t));
  422. q = ntohq(q);
  423. bcopy(&q, (void *) out, sizeof(u_int64_t));
  424. in += sizeof(u_int64_t);
  425. out += sizeof(u_int64_t);
  426. break;
  427. case 'D':
  428. bcopy((void *) in, &d, sizeof(int64_t));
  429. d = ntohq(d);
  430. bcopy(&d, (void *) out, sizeof(int64_t));
  431. in += sizeof(int64_t);
  432. out += sizeof(int64_t);
  433. break;
  434. default:
  435. warning("unknown stream format identifier %c in type %d",
  436. streamform[type].form[i],
  437. type);
  438. return 0;
  439. }
  440. i++;
  441. }
  442. return (in - buf);
  443. }
  444. /* Get the RRD or 'pretty' ascii representation of packedstream */
  445. int
  446. ps2strn(struct packedstream * ps, char *buf, const int maxlen, int pretty)
  447. {
  448. u_int16_t b;
  449. u_int16_t s;
  450. u_int16_t c;
  451. u_int64_t q;
  452. u_int32_t l;
  453. int64_t d;
  454. double D;
  455. int i = 0;
  456. char *formatstr;
  457. char *in, *out;
  458. char vartype;
  459. in = (char *) (&ps->data);
  460. out = (char *) buf;
  461. while ((vartype = streamform[ps->type].form[i]) != '\0') {
  462. /* check buffer overflow */
  463. if (checklen(maxlen, (out - buf), strlenvar(vartype)))
  464. return 0;
  465. switch (pretty) {
  466. case PS2STR_PRETTY:
  467. formatstr = formatstrvar(vartype);
  468. break;
  469. case PS2STR_RRD:
  470. formatstr = rrdstrvar(vartype);
  471. break;
  472. default:
  473. warning("%s:%d: unknown pretty identifier", __FILE__, __LINE__);
  474. return 0;
  475. }
  476. switch (vartype) {
  477. case 'b':
  478. bcopy(in, &b, sizeof(u_int8_t));
  479. snprintf(out, strlenvar(vartype), formatstr, b);
  480. in++;
  481. break;
  482. case 'c':
  483. bcopy(in, &c, sizeof(u_int16_t));
  484. D = (double) c / 100.0;
  485. snprintf(out, strlenvar(vartype), formatstr, D);
  486. in += sizeof(u_int16_t);
  487. break;
  488. case 's':
  489. bcopy(in, &s, sizeof(u_int16_t));
  490. snprintf(out, strlenvar(vartype), formatstr, s);
  491. in += sizeof(u_int16_t);
  492. break;
  493. case 'l':
  494. bcopy(in, &l, sizeof(u_int32_t));
  495. snprintf(out, strlenvar(vartype), formatstr, l);
  496. in += sizeof(u_int32_t);
  497. break;
  498. case 'L':
  499. bcopy(in, &q, sizeof(u_int64_t));
  500. snprintf(out, strlenvar(vartype), formatstr, q);
  501. in += sizeof(u_int64_t);
  502. break;
  503. case 'D':
  504. bcopy(in, &d, sizeof(int64_t));
  505. D = (double) (d / 1000.0 / 1000.0);
  506. snprintf(out, strlenvar(vartype), formatstr, D);
  507. in += sizeof(int64_t);
  508. break;
  509. default:
  510. warning("unknown stream format identifier %c", vartype);
  511. return 0;
  512. }
  513. out += strlen(out);
  514. i++;
  515. }
  516. return (out - buf);
  517. }
  518. struct stream *
  519. create_stream(int type, char *args)
  520. {
  521. struct stream *p;
  522. if (type < 0 || type >= MT_EOT)
  523. fatal("%s:%d: internal error: stream type unknown", __FILE__, __LINE__);
  524. p = (struct stream *) xmalloc(sizeof(struct stream));
  525. bzero(p, sizeof(struct stream));
  526. p->type = type;
  527. if (args != NULL)
  528. p->args = xstrdup(args);
  529. return p;
  530. }
  531. /* Find the stream handle in a source */
  532. struct stream *
  533. find_source_stream(struct source * source, int type, char *args)
  534. {
  535. struct stream *p;
  536. if (source == NULL)
  537. return NULL;
  538. SLIST_FOREACH(p, &source->sl, streams) {
  539. if ((p->type == type)
  540. && (((void *) args != (void *) p != NULL)
  541. && strncmp(args, p->args, _POSIX2_LINE_MAX) == 0))
  542. return p;
  543. }
  544. return NULL;
  545. }
  546. /* Add a stream to a source */
  547. struct stream *
  548. add_source_stream(struct source * source, int type, char *args)
  549. {
  550. struct stream *p;
  551. if (source == NULL)
  552. return NULL;
  553. if (find_source_stream(source, type, args) != NULL)
  554. return NULL;
  555. p = create_stream(type, args);
  556. SLIST_INSERT_HEAD(&source->sl, p, streams);
  557. return p;
  558. }
  559. /* Find a stream in a mux */
  560. struct stream *
  561. find_mux_stream(struct mux * mux, int type, char *args)
  562. {
  563. struct stream *p;
  564. if (mux == NULL)
  565. return NULL;
  566. SLIST_FOREACH(p, &mux->sl, streams) {
  567. if ((p->type == type)
  568. && (((void *) args != (void *) p != NULL)
  569. && strncmp(args, p->args, _POSIX2_LINE_MAX) == 0))
  570. return p;
  571. }
  572. return NULL;
  573. }
  574. /* Add a stream to a mux */
  575. struct stream *
  576. add_mux_stream(struct mux * mux, int type, char *args)
  577. {
  578. struct stream *p;
  579. if (mux == NULL)
  580. return NULL;
  581. if (find_mux_stream(mux, type, args) != NULL)
  582. return NULL;
  583. p = create_stream(type, args);
  584. SLIST_INSERT_HEAD(&mux->sl, p, streams);
  585. return p;
  586. }
  587. /* Find a source by name in a sourcelist */
  588. struct source *
  589. find_source(struct sourcelist * sol, char *name)
  590. {
  591. struct source *p;
  592. if (sol == NULL || SLIST_EMPTY(sol))
  593. return NULL;
  594. SLIST_FOREACH(p, sol, sources) {
  595. if (((void *) name != (void *) p != NULL)
  596. && strncmp(name, p->addr, _POSIX2_LINE_MAX) == 0)
  597. return p;
  598. }
  599. return NULL;
  600. }
  601. /* Find a source by ip in a sourcelist */
  602. struct source *
  603. find_source_sockaddr(struct sourcelist * sol, struct sockaddr * addr)
  604. {
  605. struct source *p;
  606. if (sol == NULL || SLIST_EMPTY(sol))
  607. return NULL;
  608. SLIST_FOREACH(p, sol, sources) {
  609. if (cmpsock_addr((struct sockaddr *) & p->sockaddr, addr))
  610. return p;
  611. }
  612. return NULL;
  613. }
  614. /* Add a source with to a sourcelist */
  615. struct source *
  616. add_source(struct sourcelist * sol, char *name)
  617. {
  618. struct source *p;
  619. if (sol == NULL)
  620. return NULL;
  621. if (find_source(sol, name) != NULL)
  622. return NULL;
  623. p = (struct source *) xmalloc(sizeof(struct source));
  624. bzero(p, sizeof(struct source));
  625. p->addr = xstrdup(name);
  626. SLIST_INSERT_HEAD(sol, p, sources);
  627. return p;
  628. }
  629. /* Find a mux by name in a muxlist */
  630. struct mux *
  631. find_mux(struct muxlist * mul, char *name)
  632. {
  633. struct mux *p;
  634. if (mul == NULL || SLIST_EMPTY(mul))
  635. return NULL;
  636. SLIST_FOREACH(p, mul, muxes) {
  637. if (((void *) name != (void *) p != NULL)
  638. && strncmp(name, p->name, _POSIX2_LINE_MAX) == 0)
  639. return p;
  640. }
  641. return NULL;
  642. }
  643. /* Add a mux to a muxlist */
  644. struct mux *
  645. add_mux(struct muxlist * mul, char *name)
  646. {
  647. struct mux *p;
  648. if (mul == NULL)
  649. return NULL;
  650. if (find_mux(mul, name) != NULL)
  651. return NULL;
  652. p = (struct mux *) xmalloc(sizeof(struct mux));
  653. bzero(p, sizeof(struct mux));
  654. p->name = xstrdup(name);
  655. SLIST_INSERT_HEAD(mul, p, muxes);
  656. SLIST_INIT(&p->sol);
  657. return p;
  658. }
  659. /* Rename a mux */
  660. struct mux *
  661. rename_mux(struct muxlist * mul, struct mux * mux, char *name)
  662. {
  663. if (mul == NULL || mux == NULL)
  664. return NULL;
  665. if (find_mux(mul, name) != NULL)
  666. return NULL;
  667. if (mux->name != NULL)
  668. xfree(mux->name);
  669. mux->name = xstrdup(name);
  670. return mux;
  671. }
  672. void
  673. free_muxlist(struct muxlist * mul)
  674. {
  675. struct mux *p, *np;
  676. int i;
  677. if (mul == NULL || SLIST_EMPTY(mul))
  678. return;
  679. p = SLIST_FIRST(mul);
  680. while (p) {
  681. np = SLIST_NEXT(p, muxes);
  682. if (p->name != NULL)
  683. xfree(p->name);
  684. if (p->addr != NULL)
  685. xfree(p->addr);
  686. if (p->port != NULL)
  687. xfree(p->port);
  688. close(p->clientsocket);
  689. close(p->symuxsocket);
  690. for (i = 0; i < AF_MAX; i++)
  691. if (p->symonsocket[i])
  692. close(p->symonsocket[i]);
  693. free_streamlist(&p->sl);
  694. free_sourcelist(&p->sol);
  695. xfree(p);
  696. p = np;
  697. }
  698. }
  699. void
  700. free_streamlist(struct streamlist * sl)
  701. {
  702. struct stream *p, *np;
  703. if (sl == NULL || SLIST_EMPTY(sl))
  704. return;
  705. p = SLIST_FIRST(sl);
  706. while (p) {
  707. np = SLIST_NEXT(p, streams);
  708. if (p->args != NULL)
  709. xfree(p->args);
  710. if (p->file != NULL)
  711. xfree(p->file);
  712. xfree(p);
  713. p = np;
  714. }
  715. }
  716. void
  717. free_sourcelist(struct sourcelist * sol)
  718. {
  719. struct source *p, *np;
  720. if (sol == NULL || SLIST_EMPTY(sol))
  721. return;
  722. p = SLIST_FIRST(sol);
  723. while (p) {
  724. np = SLIST_NEXT(p, sources);
  725. if (p->addr != NULL)
  726. xfree(p->addr);
  727. free_streamlist(&p->sl);
  728. xfree(p);
  729. p = np;
  730. }
  731. }
  732. /* Calculate maximum buffer space needed for a single symon hit */
  733. int
  734. calculate_churnbuffer(struct sourcelist * sol)
  735. {
  736. char buf[_POSIX2_LINE_MAX];
  737. struct source *source;
  738. struct stream *stream;
  739. int maxlen;
  740. int len;
  741. int n;
  742. len = n = 0;
  743. source = NULL;
  744. stream = NULL;
  745. maxlen = 0;
  746. /* determine maximum string size for a single source */
  747. SLIST_FOREACH(source, sol, sources) {
  748. len = snprintf(&buf[0], _POSIX2_LINE_MAX, "%s;", source->addr);
  749. SLIST_FOREACH(stream, &source->sl, streams) {
  750. len += strlen(type2str(stream->type)) + strlen(":");
  751. len += strlen(stream->args) + strlen(":");
  752. len += (sizeof(time_t) * 3) + strlen(":"); /* 3 > ln(255) / ln(10) */
  753. len += strlentype(stream->type);
  754. n++;
  755. }
  756. if (len > maxlen)
  757. maxlen = len;
  758. }
  759. return maxlen;
  760. }
  761. /* Big endian CRC32 */
  762. u_int32_t
  763. crc32(const void *buf, unsigned int len)
  764. {
  765. u_int8_t *p;
  766. u_int32_t crc;
  767. crc = 0xffffffff;
  768. for (p = (u_int8_t *) buf; len > 0; ++p, --len)
  769. crc = (crc << 8) ^ crc32_table[(crc >> 24) ^ *p];
  770. return ~crc;
  771. }
  772. /* Init table for CRC32 */
  773. void
  774. init_crc32()
  775. {
  776. unsigned int i, j;
  777. u_int32_t c;
  778. for (i = 0; i < 256; ++i) {
  779. c = i << 24;
  780. for (j = 8; j > 0; --j)
  781. c = c & 0x80000000 ? (c << 1) ^ SYMON_CRCPOLY : (c << 1);
  782. crc32_table[i] = c;
  783. }
  784. }