data.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /* $Id: data.c,v 1.14 2002/08/29 19:38:52 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2002 Willem Dijkstra
  4. * All rights reserved.
  5. *
  6. * The crc routine is from 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 'mon' is considered a 'source' of information. A single
  36. * data 'stream' of information has a particular type: <cpu|mem|if|io>. A
  37. * source can provide multiple 'streams' simultaniously. 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 <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 "data.h"
  49. #include "error.h"
  50. #include "lex.h"
  51. #include "xmalloc.h"
  52. __BEGIN_DECLS
  53. int bytelenvar(char);
  54. int checklen(int, int, int);
  55. struct stream *create_stream(int, char *);
  56. char *formatstrvar(char);
  57. char *rrdstrvar(char);
  58. int strlenvar(char);
  59. __END_DECLS
  60. /* Stream formats
  61. *
  62. * Format specifications are strings of characters:
  63. *
  64. * L = u_int64
  65. * l = u_int32
  66. * c = 3.2f <= u_int14 <= u_int16 (used in percentages)
  67. */
  68. struct {
  69. char type;
  70. char *rrdformat;
  71. char *strformat;
  72. int strlen;
  73. int bytelen;
  74. u_int64_t max;
  75. } streamvar[] = {
  76. {'L', ":%qu", " %20qu", 22, sizeof(u_int64_t), (u_int64_t) 0xffffffffffffffff},
  77. {'l', ":%lu", " %10lu", 12, sizeof(u_int32_t), (u_int64_t) 0xffffffff},
  78. {'c', ":%3.2f", " %3.2f", 7, sizeof(u_int16_t), (u_int64_t) 100},
  79. {'\0', NULL, NULL, 0, 0, 0}
  80. };
  81. /* streams of <type> have the packedstream <form> */
  82. struct {
  83. int type;
  84. char *form;
  85. } streamform[] = {
  86. {MT_IO, "LLL"},
  87. {MT_CPU, "ccccc"},
  88. {MT_MEM, "lllll"},
  89. {MT_IF, "llllllllll"},
  90. {MT_PF, "LLLLLLLLLLLLLLLLLLLLLL"},
  91. {MT_EOT, ""}
  92. };
  93. struct {
  94. int type;
  95. int token;
  96. } streamtoken[] = {
  97. {MT_IO, LXT_IO},
  98. {MT_CPU, LXT_CPU},
  99. {MT_MEM, LXT_MEM},
  100. {MT_IF, LXT_IF},
  101. {MT_PF, LXT_PF},
  102. {MT_EOT, LXT_BADTOKEN}
  103. };
  104. /* parallel crc32 table */
  105. u_int32_t
  106. crc32_table[256];
  107. /* Convert lexical entities to stream entities */
  108. const int
  109. token2type(const int token)
  110. {
  111. int i;
  112. for (i=0; streamtoken[i].type < MT_EOT; i++)
  113. if (streamtoken[i].token == token)
  114. return streamtoken[i].type;
  115. fatal("%s:%d: internal error: token (%d) could not be translated into a stream type",
  116. __FILE__, __LINE__, token);
  117. /* NOT REACHED */
  118. return 0;
  119. }
  120. /* Convert stream entities to their ascii representation */
  121. const char *
  122. type2str(const int streamtype)
  123. {
  124. int i;
  125. for (i=0; streamtoken[i].type < MT_EOT; i++)
  126. if (streamtoken[i].type == streamtype)
  127. return parse_opcode(streamtoken[i].token);
  128. fatal("%s:%d: internal error: type (%d) could not be translated into ascii representation",
  129. __FILE__, __LINE__, streamtype);
  130. /* NOT REACHED */
  131. return 0;
  132. }
  133. /* Return the maximum lenght of the ascii representation of type <type> */
  134. int
  135. strlentype(int type)
  136. {
  137. int i = 0;
  138. int sum = 0;
  139. while (streamform[type].form[i])
  140. sum += strlenvar(streamform[type].form[i++]);
  141. return sum;
  142. }
  143. /* Return the maximum lenght of the ascii representation of streamvar <var> */
  144. int
  145. strlenvar(char var)
  146. {
  147. int i;
  148. for (i=0; streamvar[i].type > '\0'; i++)
  149. if (streamvar[i].type == var)
  150. return streamvar[i].strlen;
  151. fatal("%s:%d: internal error: type spefication for stream var '%c' not found",
  152. __FILE__, __LINE__, var);
  153. /* NOT REACHED */
  154. return 0;
  155. }
  156. /* Return the maximum lenght of the network representation of streamvar <var> */
  157. int
  158. bytelenvar(char var)
  159. {
  160. int i;
  161. for (i=0; streamvar[i].type > '\0'; i++)
  162. if (streamvar[i].type == var)
  163. return streamvar[i].bytelen;
  164. fatal("%s:%d: internal error: type spefication for stream var '%c' not found",
  165. __FILE__, __LINE__, var);
  166. /* NOT REACHED */
  167. return 0;
  168. }
  169. /* Return the ascii format string for streamvar <var> */
  170. char *
  171. formatstrvar(char var)
  172. {
  173. int i;
  174. for (i=0; streamvar[i].type > '\0'; i++)
  175. if (streamvar[i].type == var)
  176. return streamvar[i].strformat;
  177. fatal("%s:%d: internal error: type spefication for stream var '%c' not found",
  178. __FILE__, __LINE__, var);
  179. /* NOT REACHED */
  180. return "";
  181. }
  182. /* Return the rrd format string for streamvar <var> */
  183. char *
  184. rrdstrvar(char var)
  185. {
  186. int i;
  187. for (i=0; streamvar[i].type > '\0'; i++)
  188. if (streamvar[i].type == var)
  189. return streamvar[i].rrdformat;
  190. fatal("internal error: type spefication for stream var '%c' not found", var);
  191. /* NOT REACHED */
  192. return "";
  193. }
  194. /* Check whether <extra> more bytes fit in <maxlen> when we are already at <start> */
  195. int
  196. checklen(int maxlen, int current, int extra)
  197. {
  198. if ((current + extra) < maxlen) {
  199. return 0;
  200. } else {
  201. warning("buffer overflow: max=%d, current=%d, extra=%d",
  202. maxlen, current, extra);
  203. return 1;
  204. }
  205. }
  206. /*
  207. * Pack multiple arguments of a MT_TYPE into a network order bytestream.
  208. * snpack returns the number of bytes actually stored.
  209. */
  210. int
  211. snpack(char *buf, int maxlen, char *id, int type, ...)
  212. {
  213. va_list ap;
  214. u_int16_t c;
  215. u_int32_t l;
  216. u_int64_t q;
  217. int i = 0;
  218. int offset = 0;
  219. if (type > MT_EOT) {
  220. warning("stream type (%d) out of range", type);
  221. return 0;
  222. }
  223. if ( maxlen < 2 ) {
  224. fatal("%s:%d: maxlen too small", __FILE__, __LINE__);
  225. } else {
  226. buf[offset++] = type & 0xff;
  227. }
  228. if (id) {
  229. if ((strlen(id) + 1) >= maxlen) {
  230. return 0;
  231. } else {
  232. strncpy(&buf[offset], id, maxlen-1);
  233. offset += strlen(id);
  234. }
  235. }
  236. buf[offset++] = '\0';
  237. va_start(ap, type);
  238. while (streamform[type].form[i] != '\0'){
  239. /* check for buffer overflow */
  240. if (checklen(maxlen, offset, bytelenvar(streamform[type].form[i])))
  241. return offset;
  242. switch (streamform[type].form[i]) {
  243. case 'c':
  244. c = va_arg(ap, int); /* int instead of u_int16_t to avoid just
  245. getting the first 2 bytes if the compiler
  246. generated an int on the stack -- cheers
  247. dhartmei@ */
  248. c = htons(c);
  249. bcopy(&c, buf + offset, sizeof(u_int16_t));
  250. offset += sizeof(u_int16_t);
  251. break;
  252. case 'l':
  253. l = va_arg(ap, u_int32_t);
  254. l = htonl(l);
  255. bcopy(&l, buf + offset, sizeof(u_int32_t));
  256. offset += sizeof(u_int32_t);
  257. break;
  258. case 'L':
  259. q = va_arg(ap, u_int64_t);
  260. q = htonq(q);
  261. bcopy(&q, buf + offset, sizeof(u_int64_t));
  262. offset += sizeof(u_int64_t);
  263. break;
  264. default:
  265. warning("unknown stream format identifier");
  266. return 0;
  267. }
  268. i++;
  269. }
  270. va_end(ap);
  271. return offset;
  272. }
  273. /*
  274. * Unpack a packedstream in buf into a struct packetstream. Returns the number
  275. * of bytes actually read.
  276. *
  277. * Note that this function does "automatic" bounds checking; it uses a
  278. * description of the packedstream (streamform) to parse the actual bytes. This
  279. * description corresponds to the amount of bytes that will fit inside the
  280. * packedstream structure. */
  281. int
  282. sunpack(char *buf, struct packedstream *ps)
  283. {
  284. char *in, *out;
  285. int i=0;
  286. int type;
  287. u_int16_t c;
  288. u_int32_t l;
  289. u_int64_t q;
  290. bzero(ps, sizeof(struct packedstream));
  291. in = buf;
  292. if ((*in) > MT_EOT) {
  293. warning("unpack failure: stream type (%d) out of range", (*in));
  294. return -1;
  295. }
  296. type = ps->type = (*in);
  297. in++;
  298. if ((*in) != '\0') {
  299. strncpy(ps->args, in, sizeof(ps->args));
  300. ps->args[sizeof(ps->args)-1]='\0';
  301. in += strlen(ps->args);
  302. } else {
  303. ps->args[0] = '\0';
  304. }
  305. in++;
  306. out = (char *)(&ps->data);
  307. while (streamform[type].form[i] != '\0') {
  308. switch (streamform[type].form[i]) {
  309. case 'c':
  310. bcopy((void *)in, &c, sizeof(u_int16_t));
  311. c = ntohs(c);
  312. bcopy(&c, (void *)out, sizeof(u_int16_t));
  313. in += sizeof(u_int16_t);
  314. out += sizeof(u_int16_t);
  315. break;
  316. case 'l':
  317. bcopy((void *)in, &l, sizeof(u_int32_t));
  318. l = ntohl(l);
  319. bcopy(&l, (void *)out, sizeof(u_int32_t));
  320. in += sizeof(u_int32_t);
  321. out += sizeof(u_int32_t);
  322. break;
  323. case 'L':
  324. bcopy((void *)in, &q, sizeof(u_int64_t));
  325. q = ntohq(q);
  326. bcopy(&q, (void *)out, sizeof(u_int64_t));
  327. in += sizeof(u_int64_t);
  328. out += sizeof(u_int64_t);
  329. break;
  330. default:
  331. warning("unknown stream format identifier");
  332. return 0;
  333. }
  334. i++;
  335. }
  336. return (in - buf);
  337. }
  338. /* Get the RRD or 'pretty' ascii representation of packedstream */
  339. int
  340. ps2strn(struct packedstream *ps, char *buf, const int maxlen, int pretty)
  341. {
  342. float f;
  343. u_int16_t c;
  344. u_int64_t q;
  345. u_int32_t l;
  346. int i=0;
  347. char *formatstr;
  348. char *in, *out;
  349. char vartype;
  350. in = (char *)(&ps->data);
  351. out = (char *)buf;
  352. while ((vartype = streamform[ps->type].form[i]) != '\0') {
  353. /* check buffer overflow */
  354. if (checklen(maxlen, (out-buf), strlenvar(vartype)))
  355. return 0;
  356. switch (pretty) {
  357. case PS2STR_PRETTY:
  358. formatstr = formatstrvar(vartype);
  359. break;
  360. case PS2STR_RRD:
  361. formatstr = rrdstrvar(vartype);
  362. break;
  363. default:
  364. warning("%s:%d: unknown pretty identifier", __FILE__, __LINE__);
  365. return 0;
  366. }
  367. switch (vartype) {
  368. case 'c':
  369. bcopy(in, &c, sizeof(u_int16_t));
  370. f = (float)c / 10.0;
  371. snprintf(out, strlenvar(vartype), formatstr, f);
  372. in += sizeof(u_int16_t);
  373. break;
  374. case 'l':
  375. bcopy(in, &l, sizeof(u_int32_t));
  376. snprintf(out, strlenvar(vartype), formatstr, l);
  377. in += sizeof(u_int32_t);
  378. break;
  379. case 'L':
  380. bcopy(in, &q, sizeof(u_int64_t));
  381. snprintf(out, strlenvar(vartype), formatstr, q);
  382. in += sizeof(u_int64_t);
  383. break;
  384. default:
  385. warning("Unknown stream format identifier");
  386. return 0;
  387. }
  388. out += strlen(out);
  389. i++;
  390. }
  391. return (out - buf);
  392. }
  393. struct stream *
  394. create_stream(int type, char *args)
  395. {
  396. struct stream *p;
  397. if (type < 0 || type >= MT_EOT)
  398. fatal("%s:%d: internal error: stream type unknown", __FILE__, __LINE__);
  399. p = (struct stream *)xmalloc(sizeof(struct stream));
  400. bzero(p, sizeof(struct stream));
  401. p->type = type;
  402. if (args != NULL)
  403. p->args = xstrdup(args);
  404. return p;
  405. }
  406. /* Find the stream handle in a source */
  407. struct stream *
  408. find_source_stream(struct source *source, int type, char *args)
  409. {
  410. struct stream *p;
  411. if (source == NULL)
  412. return NULL;
  413. SLIST_FOREACH(p, &source->sl, streams) {
  414. if ((p->type == type)
  415. && (((void *)args != (void *)p != NULL)
  416. && strncmp(args, p->args, _POSIX2_LINE_MAX) == 0))
  417. return p;
  418. }
  419. return NULL;
  420. }
  421. /* Add a stream to a source */
  422. struct stream *
  423. add_source_stream(struct source *source, int type, char *args)
  424. {
  425. struct stream *p;
  426. if (source == NULL)
  427. return NULL;
  428. if (find_source_stream(source, type, args) != NULL)
  429. return NULL;
  430. p = create_stream(type, args);
  431. SLIST_INSERT_HEAD(&source->sl, p, streams);
  432. return p;
  433. }
  434. /* Find a stream in a mux */
  435. struct stream *
  436. find_mux_stream(struct mux *mux, int type, char *args)
  437. {
  438. struct stream *p;
  439. if (mux == NULL)
  440. return NULL;
  441. SLIST_FOREACH(p, &mux->sl, streams) {
  442. if ((p->type == type)
  443. && (((void *)args != (void *)p != NULL)
  444. && strncmp(args, p->args, _POSIX2_LINE_MAX) == 0))
  445. return p;
  446. }
  447. return NULL;
  448. }
  449. /* Add a stream to a mux */
  450. struct stream *
  451. add_mux_stream(struct mux *mux, int type, char *args)
  452. {
  453. struct stream *p;
  454. if (mux == NULL)
  455. return NULL;
  456. if (find_mux_stream(mux, type, args) != NULL)
  457. return NULL;
  458. p = create_stream(type, args);
  459. SLIST_INSERT_HEAD(&mux->sl, p, streams);
  460. return p;
  461. }
  462. /* Find a source by name in a sourcelist */
  463. struct source *
  464. find_source(struct sourcelist *sol, char *name)
  465. {
  466. struct source *p;
  467. if (sol == NULL || SLIST_EMPTY(sol))
  468. return NULL;
  469. SLIST_FOREACH(p, sol, sources) {
  470. if (((void *)name != (void *)p != NULL)
  471. && strncmp(name, p->name, _POSIX2_LINE_MAX) == 0)
  472. return p;
  473. }
  474. return NULL;
  475. }
  476. /* Find a source by ip in a sourcelist */
  477. struct source *
  478. find_source_ip(struct sourcelist *sol, u_int32_t ip)
  479. {
  480. struct source *p;
  481. if (sol == NULL || SLIST_EMPTY(sol))
  482. return NULL;
  483. SLIST_FOREACH(p, sol, sources) {
  484. if (p->ip == ip)
  485. return p;
  486. }
  487. return NULL;
  488. }
  489. /* Add a source with to a sourcelist */
  490. struct source *
  491. add_source(struct sourcelist *sol, char *name)
  492. {
  493. struct source* p;
  494. if (sol == NULL)
  495. return NULL;
  496. if (find_source(sol, name) != NULL)
  497. return NULL;
  498. p = (struct source *)xmalloc(sizeof(struct source));
  499. bzero(p, sizeof(struct source));
  500. p->name = xstrdup(name);
  501. SLIST_INSERT_HEAD(sol, p, sources);
  502. return p;
  503. }
  504. /* Find a mux by name in a muxlist */
  505. struct mux *
  506. find_mux(struct muxlist *mul, char *name)
  507. {
  508. struct mux *p;
  509. if (mul == NULL || SLIST_EMPTY(mul))
  510. return NULL;
  511. SLIST_FOREACH(p, mul, muxes) {
  512. if (((void *)name != (void *)p != NULL)
  513. && strncmp(name, p->name, _POSIX2_LINE_MAX) == 0)
  514. return p;
  515. }
  516. return NULL;
  517. }
  518. /* Add a mux to a muxlist */
  519. struct mux *
  520. add_mux(struct muxlist *mul, char *name)
  521. {
  522. struct mux* p;
  523. if (mul == NULL)
  524. return NULL;
  525. if (find_mux(mul, name) != NULL)
  526. return NULL;
  527. p = (struct mux *)xmalloc(sizeof(struct mux));
  528. bzero(p, sizeof(struct mux));
  529. p->name = xstrdup(name);
  530. SLIST_INSERT_HEAD(mul, p, muxes);
  531. return p;
  532. }
  533. /* Rename a mux */
  534. struct mux *
  535. rename_mux(struct muxlist *mul, struct mux *mux, char *name)
  536. {
  537. if (mul == NULL || mux == NULL)
  538. return NULL;
  539. if (find_mux(mul, name) != NULL)
  540. return NULL;
  541. if (mux->name != NULL)
  542. xfree(mux->name);
  543. mux->name = xstrdup(name);
  544. return mux;
  545. }
  546. void
  547. free_muxlist(struct muxlist *mul)
  548. {
  549. struct mux *p, *np;
  550. if (mul == NULL || SLIST_EMPTY(mul))
  551. return;
  552. p = SLIST_FIRST(mul);
  553. while ( p ) {
  554. np = SLIST_NEXT(p, muxes);
  555. if (p->name != NULL) xfree(p->name);
  556. close(p->clientsocket);
  557. close(p->monsocket);
  558. free_streamlist(&p->sl);
  559. xfree(p);
  560. p = np;
  561. }
  562. }
  563. void
  564. free_streamlist(struct streamlist *sl)
  565. {
  566. struct stream *p, *np;
  567. if (sl == NULL || SLIST_EMPTY(sl))
  568. return;
  569. p = SLIST_FIRST(sl);
  570. while ( p ) {
  571. np = SLIST_NEXT(p, streams);
  572. if (p->args != NULL) xfree(p->args);
  573. if (p->file != NULL) xfree(p->file);
  574. xfree(p);
  575. p = np;
  576. }
  577. }
  578. void
  579. free_sourcelist(struct sourcelist *sol)
  580. {
  581. struct source *p, *np;
  582. if (sol == NULL || SLIST_EMPTY(sol))
  583. return;
  584. p = SLIST_FIRST(sol);
  585. while ( p ) {
  586. np = SLIST_NEXT(p, sources);
  587. if (p->name != NULL) xfree(p->name);
  588. free_streamlist(&p->sl);
  589. xfree(p);
  590. p = np;
  591. }
  592. }
  593. /* Calculate maximum buffer space needed for a single mon hit */
  594. int
  595. calculate_churnbuffer(struct sourcelist *sol) {
  596. struct source *source;
  597. struct stream *stream;
  598. int prefixlen;
  599. int maxlen;
  600. int len;
  601. int n;
  602. /* determine length of a timestamp + ip as strings */
  603. prefixlen = (sizeof(time_t)*3) + strlen(":") + 15 + strlen(":");
  604. len = n = 0;
  605. source = NULL;
  606. stream = NULL;
  607. maxlen = 0;
  608. /* determine maximum string size for a single source */
  609. SLIST_FOREACH(source, sol, sources) {
  610. len = prefixlen;
  611. SLIST_FOREACH(stream, &source->sl, streams) {
  612. len += strlen(type2str(stream->type)) + strlen(":");
  613. len += strlen(stream->args) + strlen(":");
  614. len += strlentype(stream->type);
  615. n++;
  616. }
  617. if (len > maxlen) maxlen = len;
  618. }
  619. return maxlen;
  620. }
  621. /* Big endian CRC32 */
  622. u_int32_t
  623. crc32(const void *buf, unsigned int len)
  624. {
  625. u_int8_t *p;
  626. u_int32_t crc;
  627. crc = 0xffffffff;
  628. for (p = (u_int8_t *) buf; len > 0; ++p, --len)
  629. crc = (crc << 8) ^ crc32_table[(crc >> 24) ^ *p];
  630. return ~crc;
  631. }
  632. /* Init table for CRC32 */
  633. void
  634. init_crc32()
  635. {
  636. unsigned int i, j;
  637. u_int32_t c;
  638. for (i = 0; i < 256; ++i) {
  639. c = i << 24;
  640. for (j = 8; j > 0; --j)
  641. c = c & 0x80000000 ? (c << 1) ^ MON_CRCPOLY : (c << 1);
  642. crc32_table[i] = c;
  643. }
  644. }