data.c 26 KB

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