data.c 20 KB

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