data.c 26 KB

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