data.c 26 KB

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