data.c 27 KB

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