data.c 20 KB

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