3058d650b2d60b366be2a19568d1d5f0e8ef8e19
[darkstat-debian] / hosts_db.c
1 /* darkstat 3
2 * copyright (c) 2001-2014 Emil Mikulic.
3 *
4 * hosts_db.c: database of hosts, ports, protocols.
5 *
6 * You may use, modify and redistribute this file under the terms of the
7 * GNU General Public License version 2. (see COPYING.GPL)
8 */
9
10 #include "cdefs.h"
11 #include "conv.h"
12 #include "decode.h"
13 #include "dns.h"
14 #include "err.h"
15 #include "hosts_db.h"
16 #include "db.h"
17 #include "html.h"
18 #include "ncache.h"
19 #include "now.h"
20 #include "opt.h"
21 #include "str.h"
22
23 #include <netdb.h> /* struct addrinfo */
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h> /* memset(), strcmp() */
29 #include <time.h>
30 #include <unistd.h>
31
32 int hosts_db_show_macs = 0;
33
34 /* FIXME: specify somewhere more sane/tunable */
35 #define MAX_ENTRIES 30 /* in an HTML table rendered from a hashtable */
36
37 typedef uint32_t (hash_func_t)(const struct hashtable *, const void *);
38 typedef void (free_func_t)(struct bucket *);
39 typedef const void * (key_func_t)(const struct bucket *);
40 typedef int (find_func_t)(const struct bucket *, const void *);
41 typedef struct bucket * (make_func_t)(const void *);
42 typedef void (format_cols_func_t)(struct str *);
43 typedef void (format_row_func_t)(struct str *, const struct bucket *,
44 const char *);
45
46 struct hashtable {
47 uint8_t bits; /* size of hashtable in bits */
48 uint32_t size, mask;
49 uint32_t count, count_max, count_keep; /* items in table */
50 uint32_t coeff; /* coefficient for Fibonacci hashing */
51 struct bucket **table;
52
53 struct {
54 uint64_t inserts, searches, deletions, rehashes;
55 } stats;
56
57 hash_func_t *hash_func;
58 /* returns hash value of given key (passed as void*) */
59
60 free_func_t *free_func;
61 /* free of bucket payload */
62
63 key_func_t *key_func;
64 /* returns pointer to key of bucket (to pass to hash_func) */
65
66 find_func_t *find_func;
67 /* returns true if given bucket matches key (passed as void*) */
68
69 make_func_t *make_func;
70 /* returns bucket containing new record with key (passed as void*) */
71
72 format_cols_func_t *format_cols_func;
73 /* append table columns to str */
74
75 format_row_func_t *format_row_func;
76 /* format record and append to str */
77 };
78
79 static void hashtable_reduce(struct hashtable *ht);
80 static void hashtable_free(struct hashtable *h);
81
82 #define HOST_BITS 1 /* initial size of hosts table */
83 #define PORT_BITS 1 /* initial size of ports tables */
84 #define PROTO_BITS 1 /* initial size of proto table */
85
86 /* We only use one hosts_db hashtable and this is it. */
87 static struct hashtable *hosts_db = NULL;
88
89 /* phi^-1 (reciprocal of golden ratio) = (sqrt(5) - 1) / 2 */
90 static const double phi_1 =
91 0.61803398874989490252573887119069695472717285156250;
92
93 /* Co-prime of u, using phi^-1 */
94 inline static uint32_t
95 coprime(const uint32_t u)
96 {
97 return ( (uint32_t)( (double)(u) * phi_1 ) | 1U );
98 }
99
100 /*
101 * This is the "recommended" IPv4 hash function, as seen in FreeBSD's
102 * src/sys/netinet/tcp_hostcache.c 1.1
103 */
104 inline static uint32_t
105 ipv4_hash(const struct addr *const a)
106 {
107 uint32_t ip = a->ip.v4;
108 return ( (ip) ^ ((ip) >> 7) ^ ((ip) >> 17) );
109 }
110
111 #ifndef s6_addr32
112 # ifdef sun
113 /*
114 * http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/common/netinet/in.h#130
115 */
116 # define s6_addr32 _S6_un._S6_u32
117 # else
118 /* Covers OpenBSD and FreeBSD. The macro __USE_GNU has
119 * taken care of GNU/Linux and GNU/kfreebsd. */
120 # define s6_addr32 __u6_addr.__u6_addr32
121 # endif
122 #endif
123
124 /*
125 * This is the IPv6 hash function used by FreeBSD in the same file as above,
126 * svn rev 122922.
127 */
128 inline static uint32_t
129 ipv6_hash(const struct addr *const a)
130 {
131 const struct in6_addr *const ip6 = &(a->ip.v6);
132 return ( ip6->s6_addr32[0] ^ ip6->s6_addr32[1] ^
133 ip6->s6_addr32[2] ^ ip6->s6_addr32[3] );
134 }
135
136 /* ---------------------------------------------------------------------------
137 * hash_func collection
138 */
139 static uint32_t
140 hash_func_host(const struct hashtable *h _unused_, const void *key)
141 {
142 const struct addr *a = key;
143 if (a->family == IPv4)
144 return (ipv4_hash(a));
145 else {
146 assert(a->family == IPv6);
147 return (ipv6_hash(a));
148 }
149 }
150
151 #define CASTKEY(type) (*((const type *)key))
152
153 static uint32_t
154 hash_func_short(const struct hashtable *h, const void *key)
155 {
156 return (CASTKEY(uint16_t) * h->coeff);
157 }
158
159 static uint32_t
160 hash_func_byte(const struct hashtable *h, const void *key)
161 {
162 return (CASTKEY(uint8_t) * h->coeff);
163 }
164
165 /* ---------------------------------------------------------------------------
166 * key_func collection
167 */
168
169 static const void *
170 key_func_host(const struct bucket *b)
171 {
172 return &(b->u.host.addr);
173 }
174
175 static const void *
176 key_func_port_tcp(const struct bucket *b)
177 {
178 return &(b->u.port_tcp.port);
179 }
180
181 static const void *
182 key_func_port_udp(const struct bucket *b)
183 {
184 return &(b->u.port_udp.port);
185 }
186
187 static const void *
188 key_func_ip_proto(const struct bucket *b)
189 {
190 return &(b->u.ip_proto.proto);
191 }
192
193 /* ---------------------------------------------------------------------------
194 * find_func collection
195 */
196
197 static int
198 find_func_host(const struct bucket *b, const void *key)
199 {
200 return (addr_equal(key, &(b->u.host.addr)));
201 }
202
203 static int
204 find_func_port_tcp(const struct bucket *b, const void *key)
205 {
206 return (b->u.port_tcp.port == CASTKEY(uint16_t));
207 }
208
209 static int
210 find_func_port_udp(const struct bucket *b, const void *key)
211 {
212 return (b->u.port_udp.port == CASTKEY(uint16_t));
213 }
214
215 static int
216 find_func_ip_proto(const struct bucket *b, const void *key)
217 {
218 return (b->u.ip_proto.proto == CASTKEY(uint8_t));
219 }
220
221 /* ---------------------------------------------------------------------------
222 * make_func collection
223 */
224
225 #define MAKE_BUCKET(name_bucket, name_content, type) struct { \
226 struct bucket *next; \
227 uint64_t in, out, total; \
228 union { struct type t; } u; } _custom_bucket; \
229 struct bucket *name_bucket = xcalloc(1, sizeof(_custom_bucket)); \
230 struct type *name_content = &(name_bucket->u.type); \
231 name_bucket->next = NULL; \
232 name_bucket->in = name_bucket->out = name_bucket->total = 0;
233
234 static struct bucket *
235 make_func_host(const void *key)
236 {
237 MAKE_BUCKET(b, h, host);
238 h->addr = CASTKEY(struct addr);
239 h->dns = NULL;
240 h->last_seen_mono = 0;
241 memset(&h->mac_addr, 0, sizeof(h->mac_addr));
242 h->ports_tcp = NULL;
243 h->ports_udp = NULL;
244 h->ip_protos = NULL;
245 return (b);
246 }
247
248 static void
249 free_func_host(struct bucket *b)
250 {
251 struct host *h = &(b->u.host);
252 if (h->dns != NULL) free(h->dns);
253 hashtable_free(h->ports_tcp);
254 hashtable_free(h->ports_udp);
255 hashtable_free(h->ip_protos);
256 }
257
258 static struct bucket *
259 make_func_port_tcp(const void *key)
260 {
261 MAKE_BUCKET(b, p, port_tcp);
262 p->port = CASTKEY(uint16_t);
263 p->syn = 0;
264 return (b);
265 }
266
267 static struct bucket *
268 make_func_port_udp(const void *key)
269 {
270 MAKE_BUCKET(b, p, port_udp);
271 p->port = CASTKEY(uint16_t);
272 return (b);
273 }
274
275 static struct bucket *
276 make_func_ip_proto(const void *key)
277 {
278 MAKE_BUCKET(b, p, ip_proto);
279 p->proto = CASTKEY(uint8_t);
280 return (b);
281 }
282
283 static void
284 free_func_simple(struct bucket *b _unused_)
285 {
286 /* nop */
287 }
288
289 /* ---------------------------------------------------------------------------
290 * format_func collection (ordered by struct)
291 */
292
293 static void
294 format_cols_host(struct str *buf)
295 {
296 /* FIXME: don't clobber parts of the query string
297 * specifically "full" and "start"
298 * when setting sort direction
299 */
300 str_append(buf,
301 "<table>\n"
302 "<tr>\n"
303 " <th>IP</th>\n"
304 " <th>Hostname</th>\n");
305 if (hosts_db_show_macs) str_append(buf,
306 " <th>MAC Address</th>\n");
307 str_append(buf,
308 " <th><a href=\"?sort=in\">In</a></th>\n"
309 " <th><a href=\"?sort=out\">Out</a></th>\n"
310 " <th><a href=\"?sort=total\">Total</a></th>\n");
311 if (opt_want_lastseen) str_append(buf,
312 " <th><a href=\"?sort=lastseen\">Last seen</a></th>\n");
313 str_append(buf,
314 "</tr>\n");
315 }
316
317 static void
318 format_row_host(struct str *buf, const struct bucket *b,
319 const char *css_class)
320 {
321 const char *ip = addr_to_str(&(b->u.host.addr));
322
323 str_appendf(buf,
324 "<tr class=\"%s\">\n"
325 " <td><a href=\"./%s/\">%s</a></td>\n"
326 " <td>%s</td>\n",
327 css_class,
328 ip, ip,
329 (b->u.host.dns == NULL) ? "" : b->u.host.dns);
330
331 if (hosts_db_show_macs)
332 str_appendf(buf,
333 " <td><tt>%x:%x:%x:%x:%x:%x</tt></td>\n",
334 b->u.host.mac_addr[0],
335 b->u.host.mac_addr[1],
336 b->u.host.mac_addr[2],
337 b->u.host.mac_addr[3],
338 b->u.host.mac_addr[4],
339 b->u.host.mac_addr[5]);
340
341 str_appendf(buf,
342 " <td class=\"num\">%'qu</td>\n"
343 " <td class=\"num\">%'qu</td>\n"
344 " <td class=\"num\">%'qu</td>\n",
345 (qu)b->in,
346 (qu)b->out,
347 (qu)b->total);
348
349 if (opt_want_lastseen) {
350 time_t last = b->u.host.last_seen_mono;
351 struct str *last_str = NULL;
352
353 if ((now_mono() >= last) && (last > 0))
354 last_str = length_of_time(now_mono() - last);
355
356 str_append(buf, " <td class=\"num\">");
357 if (last_str == NULL) {
358 if (last == 0)
359 str_append(buf, "(never)");
360 else
361 str_appendf(buf, "(clock error: now = %qu, last = %qu)",
362 (qu)now_mono(),
363 (qu)last);
364 } else {
365 str_appendstr(buf, last_str);
366 str_free(last_str);
367 }
368 str_append(buf, "</td>");
369 }
370
371 str_appendf(buf, "</tr>\n");
372
373 /* Only resolve hosts "on demand" */
374 if (b->u.host.dns == NULL)
375 dns_queue(&(b->u.host.addr));
376 }
377
378 static void
379 format_cols_port_tcp(struct str *buf)
380 {
381 str_append(buf,
382 "<table>\n"
383 "<tr>\n"
384 " <th>Port</td>\n"
385 " <th>Service</td>\n"
386 " <th>In</td>\n"
387 " <th>Out</td>\n"
388 " <th>Total</td>\n"
389 " <th>SYNs</td>\n"
390 "</tr>\n"
391 );
392 }
393
394 static void
395 format_row_port_tcp(struct str *buf, const struct bucket *b,
396 const char *css_class)
397 {
398 const struct port_tcp *p = &(b->u.port_tcp);
399
400 str_appendf(buf,
401 "<tr class=\"%s\">\n"
402 " <td class=\"num\">%u</td>\n"
403 " <td>%s</td>\n"
404 " <td class=\"num\">%'qu</td>\n"
405 " <td class=\"num\">%'qu</td>\n"
406 " <td class=\"num\">%'qu</td>\n"
407 " <td class=\"num\">%'qu</td>\n"
408 "</tr>\n",
409 css_class,
410 p->port,
411 getservtcp(p->port),
412 (qu)b->in,
413 (qu)b->out,
414 (qu)b->total,
415 (qu)p->syn
416 );
417 }
418
419 static void
420 format_cols_port_udp(struct str *buf)
421 {
422 str_append(buf,
423 "<table>\n"
424 "<tr>\n"
425 " <th>Port</td>\n"
426 " <th>Service</td>\n"
427 " <th>In</td>\n"
428 " <th>Out</td>\n"
429 " <th>Total</td>\n"
430 "</tr>\n"
431 );
432 }
433
434 static void
435 format_row_port_udp(struct str *buf, const struct bucket *b,
436 const char *css_class)
437 {
438 const struct port_udp *p = &(b->u.port_udp);
439
440 str_appendf(buf,
441 "<tr class=\"%s\">\n"
442 " <td class=\"num\">%u</td>\n"
443 " <td>%s</td>\n"
444 " <td class=\"num\">%'qu</td>\n"
445 " <td class=\"num\">%'qu</td>\n"
446 " <td class=\"num\">%'qu</td>\n"
447 "</tr>\n",
448 css_class,
449 p->port,
450 getservudp(p->port),
451 (qu)b->in,
452 (qu)b->out,
453 (qu)b->total
454 );
455 }
456
457 static void
458 format_cols_ip_proto(struct str *buf)
459 {
460 str_append(buf,
461 "<table>\n"
462 "<tr>\n"
463 " <th>#</td>\n"
464 " <th>Protocol</td>\n"
465 " <th>In</td>\n"
466 " <th>Out</td>\n"
467 " <th>Total</td>\n"
468 "</tr>\n"
469 );
470 }
471
472 static void
473 format_row_ip_proto(struct str *buf, const struct bucket *b,
474 const char *css_class)
475 {
476 const struct ip_proto *p = &(b->u.ip_proto);
477
478 str_appendf(buf,
479 "<tr class=\"%s\">\n"
480 " <td class=\"num\">%u</td>\n"
481 " <td>%s</td>\n"
482 " <td class=\"num\">%'qu</td>\n"
483 " <td class=\"num\">%'qu</td>\n"
484 " <td class=\"num\">%'qu</td>\n"
485 "</tr>\n",
486 css_class,
487 p->proto,
488 getproto(p->proto),
489 (qu)b->in,
490 (qu)b->out,
491 (qu)b->total
492 );
493 }
494
495 /* ---------------------------------------------------------------------------
496 * Initialise a hashtable.
497 */
498 static struct hashtable *
499 hashtable_make(const uint8_t bits,
500 const unsigned int count_max,
501 const unsigned int count_keep,
502 hash_func_t *hash_func,
503 free_func_t *free_func,
504 key_func_t *key_func,
505 find_func_t *find_func,
506 make_func_t *make_func,
507 format_cols_func_t *format_cols_func,
508 format_row_func_t *format_row_func)
509 {
510 struct hashtable *hash;
511 assert(bits > 0);
512
513 hash = xmalloc(sizeof(*hash));
514 hash->bits = bits;
515 hash->count_max = count_max;
516 hash->count_keep = count_keep;
517 hash->size = 1U << bits;
518 hash->mask = hash->size - 1;
519 hash->coeff = coprime(hash->size);
520 hash->hash_func = hash_func;
521 hash->free_func = free_func;
522 hash->key_func = key_func;
523 hash->find_func = find_func;
524 hash->make_func = make_func;
525 hash->format_cols_func = format_cols_func;
526 hash->format_row_func = format_row_func;
527 hash->count = 0;
528 hash->table = xcalloc(hash->size, sizeof(*hash->table));
529 memset(&(hash->stats), 0, sizeof(hash->stats));
530 return (hash);
531 }
532
533 /* ---------------------------------------------------------------------------
534 * Initialise global hosts_db.
535 */
536 void
537 hosts_db_init(void)
538 {
539 assert(hosts_db == NULL);
540 hosts_db = hashtable_make(HOST_BITS, opt_hosts_max, opt_hosts_keep,
541 hash_func_host, free_func_host, key_func_host, find_func_host,
542 make_func_host, format_cols_host, format_row_host);
543 }
544
545 static void
546 hashtable_rehash(struct hashtable *h, const uint8_t bits)
547 {
548 struct bucket **old_table, **new_table;
549 uint32_t i, old_size;
550 assert(h != NULL);
551 assert(bits > 0);
552
553 h->stats.rehashes++;
554 old_size = h->size;
555 old_table = h->table;
556
557 h->bits = bits;
558 h->size = 1U << bits;
559 h->mask = h->size - 1;
560 h->coeff = coprime(h->size);
561 new_table = xcalloc(h->size, sizeof(*new_table));
562
563 for (i=0; i<old_size; i++) {
564 struct bucket *next, *b = old_table[i];
565 while (b != NULL) {
566 uint32_t pos = h->hash_func(h, h->key_func(b)) & h->mask;
567 next = b->next;
568 b->next = new_table[pos];
569 new_table[pos] = b;
570 b = next;
571 }
572 }
573
574 free(h->table);
575 h->table = new_table;
576 }
577
578 static void
579 hashtable_insert(struct hashtable *h, struct bucket *b)
580 {
581 uint32_t pos;
582 assert(h != NULL);
583 assert(b != NULL);
584 assert(b->next == NULL);
585
586 /* Rehash on 80% occupancy */
587 if ((h->count > h->size) ||
588 ((h->size - h->count) < h->size / 5))
589 hashtable_rehash(h, h->bits+1);
590
591 pos = h->hash_func(h, h->key_func(b)) & h->mask;
592 if (h->table[pos] == NULL)
593 h->table[pos] = b;
594 else {
595 /* Insert at top of chain. */
596 b->next = h->table[pos];
597 h->table[pos] = b;
598 }
599 h->count++;
600 h->stats.inserts++;
601 }
602
603 /* Return bucket matching key, or NULL if no such entry. */
604 static struct bucket *
605 hashtable_search(struct hashtable *h, const void *key)
606 {
607 uint32_t pos;
608 struct bucket *b;
609
610 h->stats.searches++;
611 pos = h->hash_func(h, key) & h->mask;
612 b = h->table[pos];
613 while (b != NULL) {
614 if (h->find_func(b, key))
615 return (b);
616 else
617 b = b->next;
618 }
619 return (NULL);
620 }
621
622 typedef enum { NO_REDUCE = 0, ALLOW_REDUCE = 1 } reduce_bool;
623 /* Search for a key. If it's not there, make and insert a bucket for it. */
624 static struct bucket *
625 hashtable_find_or_insert(struct hashtable *h, const void *key,
626 const reduce_bool allow_reduce)
627 {
628 struct bucket *b = hashtable_search(h, key);
629
630 if (b == NULL) {
631 /* Not found, so insert after checking occupancy. */
632 if (allow_reduce && (h->count >= h->count_max))
633 hashtable_reduce(h);
634 b = h->make_func(key);
635 hashtable_insert(h, b);
636 }
637 return (b);
638 }
639
640 /*
641 * Frees the hashtable and the buckets. The contents are assumed to be
642 * "simple" -- i.e. no "destructor" action is required beyond simply freeing
643 * the bucket.
644 */
645 static void
646 hashtable_free(struct hashtable *h)
647 {
648 uint32_t i;
649
650 if (h == NULL)
651 return;
652 for (i=0; i<h->size; i++) {
653 struct bucket *tmp, *b = h->table[i];
654 while (b != NULL) {
655 tmp = b;
656 b = b->next;
657 h->free_func(tmp);
658 free(tmp);
659 }
660 }
661 free(h->table);
662 free(h);
663 }
664
665 /* ---------------------------------------------------------------------------
666 * Return existing host or insert a new one.
667 */
668 struct bucket *
669 host_get(const struct addr *const a)
670 {
671 return (hashtable_find_or_insert(hosts_db, a, NO_REDUCE));
672 }
673
674 /* ---------------------------------------------------------------------------
675 * Find host, returns NULL if not in DB.
676 */
677 struct bucket *
678 host_find(const struct addr *const a)
679 {
680 return (hashtable_search(hosts_db, a));
681 }
682
683 /* ---------------------------------------------------------------------------
684 * Find host, returns NULL if not in DB.
685 */
686 static struct bucket *
687 host_search(const char *ipstr)
688 {
689 struct addr a;
690 struct addrinfo hints, *ai;
691
692 memset(&hints, 0, sizeof(hints));
693 hints.ai_family = AF_UNSPEC;
694 hints.ai_flags = AI_NUMERICHOST;
695
696 if (getaddrinfo(ipstr, NULL, &hints, &ai))
697 return (NULL); /* invalid addr */
698
699 if (ai->ai_family == AF_INET) {
700 a.family = IPv4;
701 a.ip.v4 = ((const struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr;
702 }
703 else if (ai->ai_family == AF_INET6) {
704 a.family = IPv6;
705 memcpy(&(a.ip.v6),
706 ((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr.s6_addr,
707 sizeof(a.ip.v6));
708 } else {
709 freeaddrinfo(ai);
710 return (NULL); /* unknown family */
711 }
712 freeaddrinfo(ai);
713
714 verbosef("search(%s) turned into %s", ipstr, addr_to_str(&a));
715 return (hashtable_search(hosts_db, &a));
716 }
717
718 /* ---------------------------------------------------------------------------
719 * Reduce a hashtable to the top <keep> entries.
720 */
721 static void
722 hashtable_reduce(struct hashtable *ht)
723 {
724 uint32_t i, pos, rmd;
725 const struct bucket **table;
726 uint64_t cutoff;
727
728 assert(ht->count_keep < ht->count);
729
730 /* Fill table with pointers to buckets in hashtable. */
731 table = xcalloc(ht->count, sizeof(*table));
732 for (pos=0, i=0; i<ht->size; i++) {
733 struct bucket *b = ht->table[i];
734 while (b != NULL) {
735 table[pos++] = b;
736 b = b->next;
737 }
738 }
739 assert(pos == ht->count);
740 qsort_buckets(table, ht->count, 0, ht->count_keep, TOTAL);
741 cutoff = table[ht->count_keep]->total;
742 free(table);
743
744 /* Remove all elements with total <= cutoff. */
745 rmd = 0;
746 for (i=0; i<ht->size; i++) {
747 struct bucket *last = NULL, *next, *b = ht->table[i];
748 while (b != NULL) {
749 next = b->next;
750 if (b->total <= cutoff) {
751 /* Remove this one. */
752 ht->free_func(b);
753 free(b);
754 if (last == NULL)
755 ht->table[i] = next;
756 else
757 last->next = next;
758 rmd++;
759 ht->count--;
760 } else {
761 last = b;
762 }
763 b = next;
764 }
765 }
766 verbosef("hashtable_reduce: removed %u buckets, left %u",
767 rmd, ht->count);
768 hashtable_rehash(ht, ht->bits); /* is this needed? */
769 }
770
771 /* Reduce hosts_db if needed. */
772 void hosts_db_reduce(void)
773 {
774 if (hosts_db->count >= hosts_db->count_max)
775 hashtable_reduce(hosts_db);
776 }
777
778 /* ---------------------------------------------------------------------------
779 * Reset hosts_db to empty.
780 */
781 void
782 hosts_db_reset(void)
783 {
784 unsigned int i;
785
786 for (i=0; i<hosts_db->size; i++) {
787 struct bucket *next, *b = hosts_db->table[i];
788 while (b != NULL) {
789 next = b->next;
790 hosts_db->free_func(b);
791 free(b);
792 b = next;
793 }
794 hosts_db->table[i] = NULL;
795 }
796 verbosef("hosts_db reset to empty, freed %u hosts", hosts_db->count);
797 hosts_db->count = 0;
798 }
799
800 /* ---------------------------------------------------------------------------
801 * Deallocate hosts_db.
802 */
803 void hosts_db_free(void)
804 {
805 uint32_t i;
806
807 assert(hosts_db != NULL);
808 for (i=0; i<hosts_db->size; i++) {
809 struct bucket *tmp, *b = hosts_db->table[i];
810 while (b != NULL) {
811 tmp = b;
812 b = b->next;
813 hosts_db->free_func(tmp);
814 free(tmp);
815 }
816 }
817 free(hosts_db->table);
818 free(hosts_db);
819 hosts_db = NULL;
820 }
821
822 /* ---------------------------------------------------------------------------
823 * Find or create a port_tcp inside a host.
824 */
825 struct bucket *
826 host_get_port_tcp(struct bucket *host, const uint16_t port)
827 {
828 struct host *h = &host->u.host;
829 assert(h != NULL);
830 if (h->ports_tcp == NULL)
831 h->ports_tcp = hashtable_make(PORT_BITS, opt_ports_max, opt_ports_keep,
832 hash_func_short, free_func_simple, key_func_port_tcp,
833 find_func_port_tcp, make_func_port_tcp,
834 format_cols_port_tcp, format_row_port_tcp);
835 return (hashtable_find_or_insert(h->ports_tcp, &port, ALLOW_REDUCE));
836 }
837
838 /* ---------------------------------------------------------------------------
839 * Find or create a port_udp inside a host.
840 */
841 struct bucket *
842 host_get_port_udp(struct bucket *host, const uint16_t port)
843 {
844 struct host *h = &host->u.host;
845 assert(h != NULL);
846 if (h->ports_udp == NULL)
847 h->ports_udp = hashtable_make(PORT_BITS, opt_ports_max, opt_ports_keep,
848 hash_func_short, free_func_simple, key_func_port_udp,
849 find_func_port_udp, make_func_port_udp,
850 format_cols_port_udp, format_row_port_udp);
851 return (hashtable_find_or_insert(h->ports_udp, &port, ALLOW_REDUCE));
852 }
853
854 /* ---------------------------------------------------------------------------
855 * Find or create an ip_proto inside a host.
856 */
857 struct bucket *
858 host_get_ip_proto(struct bucket *host, const uint8_t proto)
859 {
860 struct host *h = &host->u.host;
861 static const unsigned int PROTOS_MAX = 512, PROTOS_KEEP = 256;
862 assert(h != NULL);
863 if (h->ip_protos == NULL)
864 h->ip_protos = hashtable_make(PROTO_BITS, PROTOS_MAX, PROTOS_KEEP,
865 hash_func_byte, free_func_simple, key_func_ip_proto,
866 find_func_ip_proto, make_func_ip_proto,
867 format_cols_ip_proto, format_row_ip_proto);
868 return (hashtable_find_or_insert(h->ip_protos, &proto, ALLOW_REDUCE));
869 }
870
871 static struct str *html_hosts_main(const char *qs);
872 static struct str *html_hosts_detail(const char *ip);
873
874 /* ---------------------------------------------------------------------------
875 * Web interface: delegate the /hosts/ space.
876 */
877 struct str *
878 html_hosts(const char *uri, const char *query)
879 {
880 unsigned int i, num_elems;
881 char **elem = split('/', uri, &num_elems);
882 struct str *buf = NULL;
883
884 assert(num_elems >= 1);
885 assert(strcmp(elem[0], "hosts") == 0);
886
887 if (num_elems == 1)
888 /* /hosts/ */
889 buf = html_hosts_main(query);
890 else if (num_elems == 2)
891 /* /hosts/<IP of host>/ */
892 buf = html_hosts_detail(elem[1]);
893
894 for (i=0; i<num_elems; i++)
895 free(elem[i]);
896 free(elem);
897 return (buf); /* FIXME: a NULL here becomes 404 Not Found, we might want
898 other codes to be possible */
899 }
900
901 /* ---------------------------------------------------------------------------
902 * Format hashtable into HTML.
903 */
904 static void
905 format_table(struct str *buf, struct hashtable *ht, unsigned int start,
906 const enum sort_dir sort, const int full)
907 {
908 const struct bucket **table;
909 unsigned int i, pos, end;
910 int alt = 0;
911
912 if ((ht == NULL) || (ht->count == 0)) {
913 str_append(buf, "<p>The table is empty.</p>\n");
914 return;
915 }
916
917 /* Fill table with pointers to buckets in hashtable. */
918 table = xcalloc(ht->count, sizeof(*table));
919 for (pos=0, i=0; i<ht->size; i++) {
920 struct bucket *b = ht->table[i];
921 while (b != NULL) {
922 table[pos++] = b;
923 b = b->next;
924 }
925 }
926 assert(pos == ht->count);
927
928 if (full) {
929 /* full report overrides start and end */
930 start = 0;
931 end = ht->count;
932 } else
933 end = MIN(ht->count, (uint32_t)start+MAX_ENTRIES);
934
935 str_appendf(buf, "(%u-%u of %u)<br>\n", start+1, end, ht->count);
936 qsort_buckets(table, ht->count, start, end, sort);
937 ht->format_cols_func(buf);
938
939 for (i=start; i<end; i++) {
940 ht->format_row_func(buf, table[i], alt ? "alt1" : "alt2");
941 alt = !alt; /* alternate class for table rows */
942 }
943 free(table);
944 str_append(buf, "</table>\n");
945 }
946
947 /* ---------------------------------------------------------------------------
948 * Web interface: sorted table of hosts.
949 */
950 static struct str *
951 html_hosts_main(const char *qs)
952 {
953 struct str *buf = str_make();
954 char *qs_start, *qs_sort, *qs_full, *ep;
955 const char *sortstr;
956 int start, full = 0;
957 enum sort_dir sort;
958
959 /* parse query string */
960 qs_start = qs_get(qs, "start");
961 qs_sort = qs_get(qs, "sort");
962 qs_full = qs_get(qs, "full");
963 if (qs_full != NULL) {
964 full = 1;
965 free(qs_full);
966 }
967
968 /* validate sort */
969 if (qs_sort == NULL) sort = TOTAL;
970 else if (strcmp(qs_sort, "total") == 0) sort = TOTAL;
971 else if (strcmp(qs_sort, "in") == 0) sort = IN;
972 else if (strcmp(qs_sort, "out") == 0) sort = OUT;
973 else if (strcmp(qs_sort, "lastseen") == 0) sort = LASTSEEN;
974 else {
975 str_append(buf, "Error: invalid value for \"sort\".\n");
976 goto done;
977 }
978
979 /* parse start */
980 if (qs_start == NULL)
981 start = 0;
982 else {
983 start = (int)strtoul(qs_start, &ep, 10);
984 if (*ep != '\0') {
985 str_append(buf, "Error: \"start\" is not a number.\n");
986 goto done;
987 }
988 if ((errno == ERANGE) ||
989 (start < 0) || (start >= (int)hosts_db->count)) {
990 str_append(buf, "Error: \"start\" is out of bounds.\n");
991 goto done;
992 }
993 }
994
995 #define PREV "&lt;&lt;&lt; prev page"
996 #define NEXT "next page &gt;&gt;&gt;"
997 #define FULL "full table"
998
999 html_open(buf, "Hosts", /*path_depth=*/1, /*want_graph_js=*/0);
1000 format_table(buf, hosts_db, start, sort, full);
1001
1002 /* <prev | full | stats | next> */
1003 sortstr = qs_sort;
1004 if (sortstr == NULL) sortstr = "total";
1005 if (start > 0) {
1006 int prev = start - MAX_ENTRIES;
1007 if (prev < 0)
1008 prev = 0;
1009 str_appendf(buf, "<a href=\"?start=%d&sort=%s\">" PREV "</a>",
1010 prev, sortstr);
1011 } else
1012 str_append(buf, PREV);
1013
1014 if (full)
1015 str_append(buf, " | " FULL);
1016 else
1017 str_appendf(buf, " | <a href=\"?full=yes&sort=%s\">" FULL "</a>",
1018 sortstr);
1019
1020 if (start+MAX_ENTRIES < (int)hosts_db->count)
1021 str_appendf(buf, " | <a href=\"?start=%d&sort=%s\">" NEXT "</a>",
1022 start+MAX_ENTRIES, sortstr);
1023 else
1024 str_append(buf, " | " NEXT);
1025
1026 str_append(buf, "<br>\n");
1027
1028 html_close(buf);
1029 done:
1030 if (qs_start != NULL) free(qs_start);
1031 if (qs_sort != NULL) free(qs_sort);
1032 return buf;
1033 #undef PREV
1034 #undef NEXT
1035 #undef FULL
1036 }
1037
1038 /* ---------------------------------------------------------------------------
1039 * Web interface: detailed view of a single host.
1040 */
1041 static struct str *html_hosts_detail(const char *ip) {
1042 struct bucket *h;
1043 struct str *buf, *ls_len;
1044 char ls_when[100];
1045 const char *canonical;
1046 time_t last_seen_real;
1047
1048 h = host_search(ip);
1049 if (h == NULL)
1050 return (NULL); /* no such host */
1051
1052 canonical = addr_to_str(&(h->u.host.addr));
1053
1054 /* Overview. */
1055 buf = str_make();
1056 html_open(buf, ip, /*path_depth=*/2, /*want_graph_js=*/0);
1057 if (strcmp(ip, canonical) != 0)
1058 str_appendf(buf, "(canonically <b>%s</b>)\n", canonical);
1059 str_appendf(buf,
1060 "<p>\n"
1061 "<b>Hostname:</b> %s<br>\n",
1062 (h->u.host.dns == NULL)?"(resolving...)":h->u.host.dns);
1063
1064 /* Resolve host "on demand" */
1065 if (h->u.host.dns == NULL)
1066 dns_queue(&(h->u.host.addr));
1067
1068 if (hosts_db_show_macs)
1069 str_appendf(buf,
1070 "<b>MAC Address:</b> "
1071 "<tt>%x:%x:%x:%x:%x:%x</tt><br>\n",
1072 h->u.host.mac_addr[0],
1073 h->u.host.mac_addr[1],
1074 h->u.host.mac_addr[2],
1075 h->u.host.mac_addr[3],
1076 h->u.host.mac_addr[4],
1077 h->u.host.mac_addr[5]);
1078
1079 str_append(buf,
1080 "</p>\n"
1081 "<p>\n"
1082 "<b>Last seen:</b> ");
1083
1084 last_seen_real = mono_to_real(h->u.host.last_seen_mono);
1085 if (strftime(ls_when, sizeof(ls_when),
1086 "%Y-%m-%d %H:%M:%S %Z%z", localtime(&last_seen_real)) != 0)
1087 str_append(buf, ls_when);
1088
1089 if (h->u.host.last_seen_mono <= now_mono()) {
1090 ls_len = length_of_time(now_mono() - h->u.host.last_seen_mono);
1091 str_append(buf, " (");
1092 str_appendstr(buf, ls_len);
1093 str_free(ls_len);
1094 str_append(buf, " ago)");
1095 } else {
1096 str_append(buf, " (in the future, possible clock problem)");
1097 }
1098
1099 str_appendf(buf,
1100 "</p>\n"
1101 "<p>\n"
1102 " <b>In:</b> %'qu<br>\n"
1103 " <b>Out:</b> %'qu<br>\n"
1104 " <b>Total:</b> %'qu<br>\n"
1105 "</p>\n",
1106 (qu)h->in,
1107 (qu)h->out,
1108 (qu)h->total);
1109
1110 str_append(buf, "<h3>TCP ports</h3>\n");
1111 format_table(buf, h->u.host.ports_tcp, 0,TOTAL,0);
1112
1113 str_append(buf, "<h3>UDP ports</h3>\n");
1114 format_table(buf, h->u.host.ports_udp, 0,TOTAL,0);
1115
1116 str_append(buf, "<h3>IP protocols</h3>\n");
1117 format_table(buf, h->u.host.ip_protos, 0,TOTAL,0);
1118
1119 html_close(buf);
1120 return buf;
1121 }
1122
1123 /* ---------------------------------------------------------------------------
1124 * Database import and export code:
1125 * Initially written and contributed by Ben Stewart.
1126 * copyright (c) 2007-2011 Ben Stewart, Emil Mikulic.
1127 */
1128 static int hosts_db_export_ip(const struct hashtable *h, const int fd);
1129 static int hosts_db_export_tcp(const struct hashtable *h, const int fd);
1130 static int hosts_db_export_udp(const struct hashtable *h, const int fd);
1131
1132 static const char
1133 export_proto_ip = 'P',
1134 export_proto_tcp = 'T',
1135 export_proto_udp = 'U';
1136
1137 static const unsigned char
1138 export_tag_host_ver1[] = {'H', 'S', 'T', 0x01},
1139 export_tag_host_ver2[] = {'H', 'S', 'T', 0x02},
1140 export_tag_host_ver3[] = {'H', 'S', 'T', 0x03};
1141
1142 /* ---------------------------------------------------------------------------
1143 * Load a host's ip_proto table from a file.
1144 * Returns 0 on failure, 1 on success.
1145 */
1146 static int
1147 hosts_db_import_ip(const int fd, struct bucket *host)
1148 {
1149 uint8_t count, i;
1150
1151 if (!expect8(fd, export_proto_ip)) return 0;
1152 if (!read8(fd, &count)) return 0;
1153
1154 for (i=0; i<count; i++) {
1155 struct bucket *b;
1156 uint8_t proto;
1157 uint64_t in, out;
1158
1159 if (!read8(fd, &proto)) return 0;
1160 if (!read64(fd, &in)) return 0;
1161 if (!read64(fd, &out)) return 0;
1162
1163 /* Store data */
1164 b = host_get_ip_proto(host, proto);
1165 b->in = in;
1166 b->out = out;
1167 b->total = in + out;
1168 assert(b->u.ip_proto.proto == proto); /* should be done by make fn */
1169 }
1170 return 1;
1171 }
1172
1173 /* ---------------------------------------------------------------------------
1174 * Load a host's port_tcp table from a file.
1175 * Returns 0 on failure, 1 on success.
1176 */
1177 static int
1178 hosts_db_import_tcp(const int fd, struct bucket *host)
1179 {
1180 uint16_t count, i;
1181
1182 if (!expect8(fd, export_proto_tcp)) return 0;
1183 if (!read16(fd, &count)) return 0;
1184
1185 for (i=0; i<count; i++) {
1186 struct bucket *b;
1187 uint16_t port;
1188 uint64_t in, out, syn;
1189
1190 if (!read16(fd, &port)) return 0;
1191 if (!read64(fd, &syn)) return 0;
1192 if (!read64(fd, &in)) return 0;
1193 if (!read64(fd, &out)) return 0;
1194
1195 /* Store data */
1196 b = host_get_port_tcp(host, port);
1197 b->in = in;
1198 b->out = out;
1199 b->total = in + out;
1200 assert(b->u.port_tcp.port == port); /* done by make_func_port_tcp */
1201 b->u.port_tcp.syn = syn;
1202 }
1203 return 1;
1204 }
1205
1206 /* ---------------------------------------------------------------------------
1207 * Load a host's port_tcp table from a file.
1208 * Returns 0 on failure, 1 on success.
1209 */
1210 static int
1211 hosts_db_import_udp(const int fd, struct bucket *host)
1212 {
1213 uint16_t count, i;
1214
1215 if (!expect8(fd, export_proto_udp)) return 0;
1216 if (!read16(fd, &count)) return 0;
1217
1218 for (i=0; i<count; i++) {
1219 struct bucket *b;
1220 uint16_t port;
1221 uint64_t in, out;
1222
1223 if (!read16(fd, &port)) return 0;
1224 if (!read64(fd, &in)) return 0;
1225 if (!read64(fd, &out)) return 0;
1226
1227 /* Store data */
1228 b = host_get_port_udp(host, port);
1229 b->in = in;
1230 b->out = out;
1231 b->total = in + out;
1232 assert(b->u.port_udp.port == port); /* done by make_func */
1233 }
1234 return 1;
1235 }
1236
1237 /* ---------------------------------------------------------------------------
1238 * Load all hosts from a file.
1239 * Returns 0 on failure, 1 on success.
1240 */
1241 static int
1242 hosts_db_import_host(const int fd)
1243 {
1244 struct bucket *host;
1245 struct addr a;
1246 uint8_t hostname_len;
1247 uint64_t in, out;
1248 unsigned int pos = xtell(fd);
1249 char hdr[4];
1250 int ver = 0;
1251
1252 if (!readn(fd, hdr, sizeof(hdr))) return 0;
1253 if (memcmp(hdr, export_tag_host_ver3, sizeof(hdr)) == 0)
1254 ver = 3;
1255 else if (memcmp(hdr, export_tag_host_ver2, sizeof(hdr)) == 0)
1256 ver = 2;
1257 else if (memcmp(hdr, export_tag_host_ver1, sizeof(hdr)) == 0)
1258 ver = 1;
1259 else {
1260 warnx("bad host header: %02x%02x%02x%02x",
1261 hdr[0], hdr[1], hdr[2], hdr[3]);
1262 return 0;
1263 }
1264
1265 if (ver == 3) {
1266 if (!readaddr(fd, &a))
1267 return 0;
1268 } else {
1269 assert((ver == 1) || (ver == 2));
1270 if (!readaddr_ipv4(fd, &a))
1271 return 0;
1272 }
1273 verbosef("at file pos %u, importing host %s", pos, addr_to_str(&a));
1274 host = host_get(&a);
1275 assert(addr_equal(&(host->u.host.addr), &a));
1276
1277 if (ver > 1) {
1278 uint64_t t;
1279 if (!read64(fd, &t)) return 0;
1280 host->u.host.last_seen_mono = real_to_mono(t);
1281 }
1282
1283 assert(sizeof(host->u.host.mac_addr) == 6);
1284 if (!readn(fd, host->u.host.mac_addr, sizeof(host->u.host.mac_addr)))
1285 return 0;
1286
1287 /* HOSTNAME */
1288 assert(host->u.host.dns == NULL); /* make fn? */
1289 if (!read8(fd, &hostname_len)) return 0;
1290 if (hostname_len > 0) {
1291 host->u.host.dns = xmalloc(hostname_len + 1);
1292 host->u.host.dns[0] = '\0';
1293
1294 /* At this point, the hostname is attached to a host which is in our
1295 * hosts_db, so if we bail out due to an import error, this pointer
1296 * isn't lost and leaked, it can be cleaned up in hosts_db_{free,reset}
1297 */
1298
1299 if (!readn(fd, host->u.host.dns, hostname_len)) return 0;
1300 host->u.host.dns[hostname_len] = '\0';
1301 }
1302
1303 if (!read64(fd, &in)) return 0;
1304 if (!read64(fd, &out)) return 0;
1305
1306 host->in = in;
1307 host->out = out;
1308 host->total = in + out;
1309
1310 /* Host's port and proto subtables: */
1311 if (!hosts_db_import_ip(fd, host)) return 0;
1312 if (!hosts_db_import_tcp(fd, host)) return 0;
1313 if (!hosts_db_import_udp(fd, host)) return 0;
1314 return 1;
1315 }
1316
1317 /* ---------------------------------------------------------------------------
1318 * Database Import: Grab hosts_db from a file provided by the caller.
1319 *
1320 * This function will retrieve the data sans the header. We expect the caller
1321 * to have validated the header of the hosts_db segment, and left the file
1322 * sitting at the start of the data.
1323 */
1324 int hosts_db_import(const int fd)
1325 {
1326 uint32_t host_count, i;
1327
1328 if (!read32(fd, &host_count)) return 0;
1329
1330 for (i=0; i<host_count; i++)
1331 if (!hosts_db_import_host(fd)) return 0;
1332
1333 return 1;
1334 }
1335
1336 /* ---------------------------------------------------------------------------
1337 * Database Export: Dump hosts_db into a file provided by the caller.
1338 * The caller is responsible for writing out export_tag_hosts_ver1 first.
1339 */
1340 int hosts_db_export(const int fd)
1341 {
1342 uint32_t i;
1343 struct bucket *b;
1344
1345 if (!write32(fd, hosts_db->count)) return 0;
1346
1347 for (i = 0; i<hosts_db->size; i++)
1348 for (b = hosts_db->table[i]; b != NULL; b = b->next) {
1349 /* For each host: */
1350 if (!writen(fd, export_tag_host_ver3, sizeof(export_tag_host_ver3)))
1351 return 0;
1352
1353 if (!writeaddr(fd, &(b->u.host.addr)))
1354 return 0;
1355
1356 if (!write64(fd, (uint64_t)mono_to_real(b->u.host.last_seen_mono)))
1357 return 0;
1358
1359 assert(sizeof(b->u.host.mac_addr) == 6);
1360 if (!writen(fd, b->u.host.mac_addr, sizeof(b->u.host.mac_addr)))
1361 return 0;
1362
1363 /* HOSTNAME */
1364 if (b->u.host.dns == NULL) {
1365 if (!write8(fd, 0)) return 0;
1366 } else {
1367 int dnslen = strlen(b->u.host.dns);
1368
1369 if (dnslen > 255) {
1370 warnx("found a very long hostname: \"%s\"\n"
1371 "wasn't expecting one longer than 255 chars (this one is %d)",
1372 b->u.host.dns, dnslen);
1373 dnslen = 255;
1374 }
1375
1376 if (!write8(fd, (uint8_t)dnslen)) return 0;
1377 if (!writen(fd, b->u.host.dns, dnslen)) return 0;
1378 }
1379
1380 if (!write64(fd, b->in)) return 0;
1381 if (!write64(fd, b->out)) return 0;
1382
1383 if (!hosts_db_export_ip(b->u.host.ip_protos, fd)) return 0;
1384 if (!hosts_db_export_tcp(b->u.host.ports_tcp, fd)) return 0;
1385 if (!hosts_db_export_udp(b->u.host.ports_udp, fd)) return 0;
1386 }
1387 return 1;
1388 }
1389
1390 /* ---------------------------------------------------------------------------
1391 * Dump the ip_proto table of a host.
1392 */
1393 static int
1394 hosts_db_export_ip(const struct hashtable *h, const int fd)
1395 {
1396 uint32_t i, written = 0;
1397 struct bucket *b;
1398
1399 /* IP DATA */
1400 if (!write8(fd, export_proto_ip)) return 0;
1401
1402 /* If no data, write a IP Proto count of 0 and we're done. */
1403 if (h == NULL) {
1404 if (!write8(fd, 0)) return 0;
1405 return 1;
1406 }
1407
1408 assert(h->count < 256);
1409 if (!write8(fd, (uint8_t)h->count)) return 0;
1410
1411 for (i = 0; i<h->size; i++)
1412 for (b = h->table[i]; b != NULL; b = b->next) {
1413 /* For each ip_proto bucket: */
1414
1415 if (!write8(fd, b->u.ip_proto.proto)) return 0;
1416 if (!write64(fd, b->in)) return 0;
1417 if (!write64(fd, b->out)) return 0;
1418 written++;
1419 }
1420 assert(written == h->count);
1421 return 1;
1422 }
1423
1424 /* ---------------------------------------------------------------------------
1425 * Dump the port_tcp table of a host.
1426 */
1427 static int
1428 hosts_db_export_tcp(const struct hashtable *h, const int fd)
1429 {
1430 struct bucket *b;
1431 uint32_t i, written = 0;
1432
1433 /* TCP DATA */
1434 if (!write8(fd, export_proto_tcp)) return 0;
1435
1436 /* If no data, write a count of 0 and we're done. */
1437 if (h == NULL) {
1438 if (!write16(fd, 0)) return 0;
1439 return 1;
1440 }
1441
1442 assert(h->count < 65536);
1443 if (!write16(fd, (uint16_t)h->count)) return 0;
1444
1445 for (i = 0; i<h->size; i++)
1446 for (b = h->table[i]; b != NULL; b = b->next) {
1447 if (!write16(fd, b->u.port_tcp.port)) return 0;
1448 if (!write64(fd, b->u.port_tcp.syn)) return 0;
1449 if (!write64(fd, b->in)) return 0;
1450 if (!write64(fd, b->out)) return 0;
1451 written++;
1452 }
1453 assert(written == h->count);
1454 return 1;
1455 }
1456
1457 /* ---------------------------------------------------------------------------
1458 * Dump the port_udp table of a host.
1459 */
1460 static int
1461 hosts_db_export_udp(const struct hashtable *h, const int fd)
1462 {
1463 struct bucket *b;
1464 uint32_t i, written = 0;
1465
1466 /* UDP DATA */
1467 if (!write8(fd, export_proto_udp)) return 0;
1468
1469 /* If no data, write a count of 0 and we're done. */
1470 if (h == NULL) {
1471 if (!write16(fd, 0)) return 0;
1472 return 1;
1473 }
1474
1475 assert(h->count < 65536);
1476 if (!write16(fd, (uint16_t)h->count)) return 0;
1477
1478 for (i = 0; i<h->size; i++)
1479 for (b = h->table[i]; b != NULL; b = b->next) {
1480 if (!write16(fd, b->u.port_udp.port)) return 0;
1481 if (!write64(fd, b->in)) return 0;
1482 if (!write64(fd, b->out)) return 0;
1483 written++;
1484 }
1485 assert(written == h->count);
1486 return 1;
1487 }
1488
1489 /* vim:set ts=3 sw=3 tw=78 expandtab: */