data.c 21 KB

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