data.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. /* $Id: data.c,v 1.31 2007/02/11 20:07:31 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. 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. 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. int result;
  276. va_list ap;
  277. /* default to v2 packets */
  278. va_start(ap, type);
  279. result = snpackx(SYMON_PS_ARGLENV2, buf, maxlen, id, type, ap);
  280. va_end(ap);
  281. return result;
  282. }
  283. int
  284. snpack1(char *buf, int maxlen, char *id, int type, ...)
  285. {
  286. int result;
  287. va_list ap;
  288. va_start(ap, type);
  289. result = snpackx(SYMON_PS_ARGLENV1, buf, maxlen, id, type, ap);
  290. va_end(ap);
  291. return result;
  292. }
  293. int
  294. snpack2(char *buf, int maxlen, char *id, int type, ...)
  295. {
  296. int result;
  297. va_list ap;
  298. va_start(ap, type);
  299. result = snpackx(SYMON_PS_ARGLENV2, buf, maxlen, id, type, ap);
  300. va_end(ap);
  301. return result;
  302. }
  303. int
  304. snpackx(size_t maxarglen, char *buf, int maxlen, char *id, int type, va_list ap)
  305. {
  306. u_int16_t b;
  307. u_int16_t s;
  308. u_int16_t c;
  309. u_int32_t l;
  310. u_int64_t q;
  311. int64_t d;
  312. double D;
  313. int i = 0;
  314. int offset = 0;
  315. int arglen = 0;
  316. if (type > MT_EOT) {
  317. warning("stream type (%d) out of range", type);
  318. return 0;
  319. }
  320. if (maxlen < 2) {
  321. fatal("%s:%d: maxlen too small", __FILE__, __LINE__);
  322. } else {
  323. buf[offset++] = type & 0xff;
  324. }
  325. if (id) {
  326. arglen = MIN(strlen(id), SYMON_PS_ARGLENV2 - 1);
  327. } else {
  328. id = "\0";
  329. arglen = 1;
  330. }
  331. if (checklen(maxlen, offset, arglen)) {
  332. return offset;
  333. } else {
  334. strncpy(&buf[offset], id, arglen);
  335. offset += arglen + 1;
  336. }
  337. while (streamform[type].form[i] != '\0') {
  338. if (checklen(maxlen, offset, bytelenvar(streamform[type].form[i])))
  339. return offset;
  340. /*
  341. * all values smaller than 32 bytes are transferred using ints on the
  342. * stack. This is to ensure that we get the correct value, if the
  343. * compiler decided to upgrade our short to a 32bit int. -- cheers
  344. * dhartmei@openbsd.org
  345. */
  346. switch (streamform[type].form[i]) {
  347. case 'b':
  348. b = va_arg(ap, int);
  349. buf[offset++] = b;
  350. break;
  351. case 'c':
  352. D = va_arg(ap, double);
  353. c = (u_int16_t) (D * 100.0);
  354. c = htons(c);
  355. bcopy(&c, buf + offset, sizeof(u_int16_t));
  356. offset += sizeof(u_int16_t);
  357. break;
  358. case 's':
  359. s = va_arg(ap, int);
  360. s = htons(s);
  361. bcopy(&s, buf + offset, sizeof(u_int16_t));
  362. offset += sizeof(u_int16_t);
  363. break;
  364. case 'l':
  365. l = va_arg(ap, u_int32_t);
  366. l = htonl(l);
  367. bcopy(&l, buf + offset, sizeof(u_int32_t));
  368. offset += sizeof(u_int32_t);
  369. break;
  370. case 'L':
  371. q = va_arg(ap, u_int64_t);
  372. q = htonq(q);
  373. bcopy(&q, buf + offset, sizeof(u_int64_t));
  374. offset += sizeof(u_int64_t);
  375. break;
  376. case 'D':
  377. D = va_arg(ap, double);
  378. d = (int64_t) (D * 1000 * 1000);
  379. d = htonq(d);
  380. bcopy(&d, buf + offset, sizeof(int64_t));
  381. offset += sizeof(int64_t);
  382. break;
  383. default:
  384. warning("unknown stream format identifier %c in type %d",
  385. streamform[type].form[i],
  386. type);
  387. return 0;
  388. }
  389. i++;
  390. }
  391. return offset;
  392. }
  393. /*
  394. * Unpack a packedstream in buf into a struct packetstream. Returns the number
  395. * of bytes actually read.
  396. *
  397. * Note that this function does "automatic" bounds checking; it uses a
  398. * description of the packedstream (streamform) to parse the actual bytes. This
  399. * description corresponds to the amount of bytes that will fit inside the
  400. * packedstream structure. */
  401. int
  402. sunpack(char *buf, struct packedstream *ps)
  403. {
  404. /* default to version 2 */
  405. return sunpackx(SYMON_PS_ARGLENV2, buf, ps);
  406. }
  407. int
  408. sunpack1(char *buf, struct packedstream *ps)
  409. {
  410. return sunpackx(SYMON_PS_ARGLENV1, buf, ps);
  411. }
  412. int
  413. sunpack2(char *buf, struct packedstream *ps)
  414. {
  415. return sunpackx(SYMON_PS_ARGLENV2, buf, ps);
  416. }
  417. int
  418. sunpackx(size_t arglen, char *buf, struct packedstream *ps)
  419. {
  420. char *in, *out;
  421. int i = 0;
  422. int type;
  423. u_int16_t s;
  424. u_int16_t c;
  425. u_int32_t l;
  426. u_int64_t q;
  427. int64_t d;
  428. bzero(ps, sizeof(struct packedstream));
  429. in = buf;
  430. if ((*in) > MT_EOT) {
  431. warning("unpack failure: stream type (%d) out of range", (*in));
  432. return -1;
  433. }
  434. type = ps->type = (*in);
  435. in++;
  436. if ((*in) != '\0') {
  437. strncpy(ps->arg, in, arglen);
  438. ps->arg[arglen - 1] = '\0';
  439. in += strlen(ps->arg) + 1;
  440. } else {
  441. ps->arg[0] = '\0';
  442. in++;
  443. }
  444. out = (char *) (&ps->data);
  445. while (streamform[type].form[i] != '\0') {
  446. switch (streamform[type].form[i]) {
  447. case 'b':
  448. bcopy((void *) in, (void *) out, sizeof(u_int8_t));
  449. in++;
  450. out++;
  451. break;
  452. case 'c':
  453. bcopy((void *) in, &c, sizeof(u_int16_t));
  454. c = ntohs(c);
  455. bcopy(&c, (void *) out, sizeof(u_int16_t));
  456. in += sizeof(u_int16_t);
  457. out += sizeof(u_int16_t);
  458. break;
  459. case 's':
  460. bcopy((void *) in, &s, sizeof(u_int16_t));
  461. s = ntohs(s);
  462. bcopy(&s, (void *) out, sizeof(u_int16_t));
  463. in += sizeof(u_int16_t);
  464. out += sizeof(u_int16_t);
  465. break;
  466. case 'l':
  467. bcopy((void *) in, &l, sizeof(u_int32_t));
  468. l = ntohl(l);
  469. bcopy(&l, (void *) out, sizeof(u_int32_t));
  470. in += sizeof(u_int32_t);
  471. out += sizeof(u_int32_t);
  472. break;
  473. case 'L':
  474. bcopy((void *) in, &q, sizeof(u_int64_t));
  475. q = ntohq(q);
  476. bcopy(&q, (void *) out, sizeof(u_int64_t));
  477. in += sizeof(u_int64_t);
  478. out += sizeof(u_int64_t);
  479. break;
  480. case 'D':
  481. bcopy((void *) in, &d, sizeof(int64_t));
  482. d = ntohq(d);
  483. bcopy(&d, (void *) out, sizeof(int64_t));
  484. in += sizeof(int64_t);
  485. out += sizeof(int64_t);
  486. break;
  487. default:
  488. warning("unknown stream format identifier %c in type %d",
  489. streamform[type].form[i],
  490. type);
  491. return 0;
  492. }
  493. i++;
  494. }
  495. return (in - buf);
  496. }
  497. /* Get the RRD or 'pretty' ascii representation of packedstream */
  498. int
  499. ps2strn(struct packedstream * ps, char *buf, const int maxlen, int pretty)
  500. {
  501. u_int16_t b;
  502. u_int16_t s;
  503. u_int16_t c;
  504. u_int64_t q;
  505. u_int32_t l;
  506. int64_t d;
  507. double D;
  508. int i = 0;
  509. char *formatstr;
  510. char *in, *out;
  511. char vartype;
  512. in = (char *) (&ps->data);
  513. out = (char *) buf;
  514. while ((vartype = streamform[ps->type].form[i]) != '\0') {
  515. /* check buffer overflow */
  516. if (checklen(maxlen, (out - buf), strlenvar(vartype)))
  517. return 0;
  518. switch (pretty) {
  519. case PS2STR_PRETTY:
  520. formatstr = formatstrvar(vartype);
  521. break;
  522. case PS2STR_RRD:
  523. formatstr = rrdstrvar(vartype);
  524. break;
  525. default:
  526. warning("%s:%d: unknown pretty identifier", __FILE__, __LINE__);
  527. return 0;
  528. }
  529. switch (vartype) {
  530. case 'b':
  531. bcopy(in, &b, sizeof(u_int8_t));
  532. snprintf(out, strlenvar(vartype), formatstr, b);
  533. in++;
  534. break;
  535. case 'c':
  536. bcopy(in, &c, sizeof(u_int16_t));
  537. D = (double) c / 100.0;
  538. snprintf(out, strlenvar(vartype), formatstr, D);
  539. in += sizeof(u_int16_t);
  540. break;
  541. case 's':
  542. bcopy(in, &s, sizeof(u_int16_t));
  543. snprintf(out, strlenvar(vartype), formatstr, s);
  544. in += sizeof(u_int16_t);
  545. break;
  546. case 'l':
  547. bcopy(in, &l, sizeof(u_int32_t));
  548. snprintf(out, strlenvar(vartype), formatstr, l);
  549. in += sizeof(u_int32_t);
  550. break;
  551. case 'L':
  552. bcopy(in, &q, sizeof(u_int64_t));
  553. snprintf(out, strlenvar(vartype), formatstr, q);
  554. in += sizeof(u_int64_t);
  555. break;
  556. case 'D':
  557. bcopy(in, &d, sizeof(int64_t));
  558. D = (double) (d / 1000.0 / 1000.0);
  559. snprintf(out, strlenvar(vartype), formatstr, D);
  560. in += sizeof(int64_t);
  561. break;
  562. default:
  563. warning("unknown stream format identifier %c", vartype);
  564. return 0;
  565. }
  566. out += strlen(out);
  567. i++;
  568. }
  569. return (out - buf);
  570. }
  571. struct stream *
  572. create_stream(int type, char *args)
  573. {
  574. struct stream *p;
  575. if (type < 0 || type >= MT_EOT)
  576. fatal("%s:%d: internal error: stream type unknown", __FILE__, __LINE__);
  577. p = (struct stream *) xmalloc(sizeof(struct stream));
  578. bzero(p, sizeof(struct stream));
  579. p->type = type;
  580. if (args != NULL)
  581. p->arg = xstrdup(args);
  582. return p;
  583. }
  584. /* Find the stream handle in a source */
  585. struct stream *
  586. find_source_stream(struct source * source, int type, char *args)
  587. {
  588. struct stream *p;
  589. if (source == NULL || args == NULL)
  590. return NULL;
  591. SLIST_FOREACH(p, &source->sl, streams) {
  592. if (((void *) p != NULL) && (p->type == type)
  593. && (((void *) args != (void *) p)
  594. && strncmp(args, p->arg, _POSIX2_LINE_MAX) == 0))
  595. return p;
  596. }
  597. return NULL;
  598. }
  599. /* Add a stream to a source */
  600. struct stream *
  601. add_source_stream(struct source * source, int type, char *args)
  602. {
  603. struct stream *p;
  604. if (source == NULL)
  605. return NULL;
  606. if (find_source_stream(source, type, args) != NULL)
  607. return NULL;
  608. p = create_stream(type, args);
  609. SLIST_INSERT_HEAD(&source->sl, p, streams);
  610. return p;
  611. }
  612. /* Find a stream in a mux */
  613. struct stream *
  614. find_mux_stream(struct mux * mux, int type, char *args)
  615. {
  616. struct stream *p;
  617. if (mux == NULL || args == NULL)
  618. return NULL;
  619. SLIST_FOREACH(p, &mux->sl, streams) {
  620. if (((void *) p != NULL) && (p->type == type)
  621. && (((void *) args != (void *) p)
  622. && strncmp(args, p->arg, _POSIX2_LINE_MAX) == 0))
  623. return p;
  624. }
  625. return NULL;
  626. }
  627. /* Add a stream to a mux */
  628. struct stream *
  629. add_mux_stream(struct mux * mux, int type, char *args)
  630. {
  631. struct stream *p;
  632. if (mux == NULL)
  633. return NULL;
  634. if (find_mux_stream(mux, type, args) != NULL)
  635. return NULL;
  636. p = create_stream(type, args);
  637. SLIST_INSERT_HEAD(&mux->sl, p, streams);
  638. return p;
  639. }
  640. /* Find a source by name in a sourcelist */
  641. struct source *
  642. find_source(struct sourcelist * sol, char *name)
  643. {
  644. struct source *p;
  645. if (sol == NULL || SLIST_EMPTY(sol) || name == NULL)
  646. return NULL;
  647. SLIST_FOREACH(p, sol, sources) {
  648. if (((void *) p != NULL) && ((void *) name != (void *) p)
  649. && strncmp(name, p->addr, _POSIX2_LINE_MAX) == 0)
  650. return p;
  651. }
  652. return NULL;
  653. }
  654. /* Find a source by ip in a sourcelist */
  655. struct source *
  656. find_source_sockaddr(struct sourcelist * sol, struct sockaddr * addr)
  657. {
  658. struct source *p;
  659. if (sol == NULL || SLIST_EMPTY(sol))
  660. return NULL;
  661. SLIST_FOREACH(p, sol, sources) {
  662. if (cmpsock_addr((struct sockaddr *) & p->sockaddr, addr))
  663. return p;
  664. }
  665. return NULL;
  666. }
  667. /* Add a source with to a sourcelist */
  668. struct source *
  669. add_source(struct sourcelist * sol, char *name)
  670. {
  671. struct source *p;
  672. if (sol == NULL)
  673. return NULL;
  674. if (find_source(sol, name) != NULL)
  675. return NULL;
  676. p = (struct source *) xmalloc(sizeof(struct source));
  677. bzero(p, sizeof(struct source));
  678. p->addr = xstrdup(name);
  679. SLIST_INSERT_HEAD(sol, p, sources);
  680. return p;
  681. }
  682. /* Find a mux by name in a muxlist */
  683. struct mux *
  684. find_mux(struct muxlist * mul, char *name)
  685. {
  686. struct mux *p;
  687. if (mul == NULL || SLIST_EMPTY(mul) || name == NULL)
  688. return NULL;
  689. SLIST_FOREACH(p, mul, muxes) {
  690. if (((void *) p != NULL) && ((void *) name != (void *) p)
  691. && strncmp(name, p->name, _POSIX2_LINE_MAX) == 0)
  692. return p;
  693. }
  694. return NULL;
  695. }
  696. /* Add a mux to a muxlist */
  697. struct mux *
  698. add_mux(struct muxlist * mul, char *name)
  699. {
  700. struct mux *p;
  701. if (mul == NULL)
  702. return NULL;
  703. if (find_mux(mul, name) != NULL)
  704. return NULL;
  705. p = (struct mux *) xmalloc(sizeof(struct mux));
  706. bzero(p, sizeof(struct mux));
  707. p->name = xstrdup(name);
  708. SLIST_INSERT_HEAD(mul, p, muxes);
  709. SLIST_INIT(&p->sol);
  710. return p;
  711. }
  712. /* Rename a mux */
  713. struct mux *
  714. rename_mux(struct muxlist * mul, struct mux * mux, char *name)
  715. {
  716. if (mul == NULL || mux == NULL)
  717. return NULL;
  718. if (find_mux(mul, name) != NULL)
  719. return NULL;
  720. if (mux->name != NULL)
  721. xfree(mux->name);
  722. mux->name = xstrdup(name);
  723. return mux;
  724. }
  725. void
  726. free_muxlist(struct muxlist * mul)
  727. {
  728. struct mux *p, *np;
  729. int i;
  730. if (mul == NULL || SLIST_EMPTY(mul))
  731. return;
  732. p = SLIST_FIRST(mul);
  733. while (p) {
  734. np = SLIST_NEXT(p, muxes);
  735. if (p->name != NULL)
  736. xfree(p->name);
  737. if (p->addr != NULL)
  738. xfree(p->addr);
  739. if (p->port != NULL)
  740. xfree(p->port);
  741. close(p->clientsocket);
  742. close(p->symuxsocket);
  743. for (i = 0; i < AF_MAX; i++)
  744. if (p->symonsocket[i])
  745. close(p->symonsocket[i]);
  746. free_streamlist(&p->sl);
  747. free_sourcelist(&p->sol);
  748. xfree(p);
  749. p = np;
  750. }
  751. }
  752. void
  753. free_streamlist(struct streamlist * sl)
  754. {
  755. struct stream *p, *np;
  756. if (sl == NULL || SLIST_EMPTY(sl))
  757. return;
  758. p = SLIST_FIRST(sl);
  759. while (p) {
  760. np = SLIST_NEXT(p, streams);
  761. if (p->arg != NULL)
  762. xfree(p->arg);
  763. if (p->file != NULL)
  764. xfree(p->file);
  765. xfree(p);
  766. p = np;
  767. }
  768. }
  769. void
  770. free_sourcelist(struct sourcelist * sol)
  771. {
  772. struct source *p, *np;
  773. if (sol == NULL || SLIST_EMPTY(sol))
  774. return;
  775. p = SLIST_FIRST(sol);
  776. while (p) {
  777. np = SLIST_NEXT(p, sources);
  778. if (p->addr != NULL)
  779. xfree(p->addr);
  780. free_streamlist(&p->sl);
  781. xfree(p);
  782. p = np;
  783. }
  784. }
  785. /* Calculate maximum buffer space needed for a single symon hit */
  786. int
  787. calculate_churnbuffer(struct sourcelist * sol)
  788. {
  789. char buf[_POSIX2_LINE_MAX];
  790. struct source *source;
  791. struct stream *stream;
  792. int maxlen;
  793. int len;
  794. int n;
  795. len = n = 0;
  796. source = NULL;
  797. stream = NULL;
  798. maxlen = 0;
  799. /* determine maximum string size for a single source */
  800. SLIST_FOREACH(source, sol, sources) {
  801. len = snprintf(&buf[0], _POSIX2_LINE_MAX, "%s;", source->addr);
  802. SLIST_FOREACH(stream, &source->sl, streams) {
  803. len += strlen(type2str(stream->type)) + strlen(":");
  804. len += strlen(stream->arg) + strlen(":");
  805. len += (sizeof(time_t) * 3) + strlen(":"); /* 3 > ln(255) / ln(10) */
  806. len += strlentype(stream->type);
  807. n++;
  808. }
  809. if (len > maxlen)
  810. maxlen = len;
  811. }
  812. return maxlen;
  813. }
  814. /* Big endian CRC32 */
  815. u_int32_t
  816. crc32(const void *buf, unsigned int len)
  817. {
  818. u_int8_t *p;
  819. u_int32_t crc;
  820. crc = 0xffffffff;
  821. for (p = (u_int8_t *) buf; len > 0; ++p, --len)
  822. crc = (crc << 8) ^ crc32_table[(crc >> 24) ^ *p];
  823. return ~crc;
  824. }
  825. /* Init table for CRC32 */
  826. void
  827. init_crc32()
  828. {
  829. unsigned int i, j;
  830. u_int32_t c;
  831. for (i = 0; i < 256; ++i) {
  832. c = i << 24;
  833. for (j = 8; j > 0; --j)
  834. c = c & 0x80000000 ? (c << 1) ^ SYMON_CRCPOLY : (c << 1);
  835. crc32_table[i] = c;
  836. }
  837. }