data.c 21 KB

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