data.c 21 KB

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