c2d7ae4dbea521025b04690ef4480f79b06476eb
2 * copyright (c) 2001-2011 Emil Mikulic.
4 * hosts_db.c: database of hosts, ports, protocols.
6 * You may use, modify and redistribute this file under the terms of the
7 * GNU General Public License version 2. (see COPYING.GPL)
23 #include <netdb.h> /* struct addrinfo */
28 #include <string.h> /* memset(), strcmp() */
32 int hosts_db_show_macs
= 0;
34 /* FIXME: specify somewhere more sane/tunable */
35 #define MAX_ENTRIES 30 /* in an HTML table rendered from a hashtable */
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
*,
47 uint8_t bits
; /* size of hashtable in bits */
49 uint32_t count
, count_max
, count_keep
; /* items in table */
50 uint32_t coeff
; /* coefficient for Fibonacci hashing */
51 struct bucket
**table
;
54 uint64_t inserts
, searches
, deletions
, rehashes
;
57 hash_func_t
*hash_func
;
58 /* returns hash value of given key (passed as void*) */
60 free_func_t
*free_func
;
61 /* free of bucket payload */
64 /* returns pointer to key of bucket (to pass to hash_func) */
66 find_func_t
*find_func
;
67 /* returns true if given bucket matches key (passed as void*) */
69 make_func_t
*make_func
;
70 /* returns bucket containing new record with key (passed as void*) */
72 format_cols_func_t
*format_cols_func
;
73 /* append table columns to str */
75 format_row_func_t
*format_row_func
;
76 /* format record and append to str */
79 static void hashtable_reduce(struct hashtable
*ht
);
80 static void hashtable_free(struct hashtable
*h
);
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 */
86 /* We only use one hosts_db hashtable and this is it. */
87 static struct hashtable
*hosts_db
= NULL
;
89 /* phi^-1 (reciprocal of golden ratio) = (sqrt(5) - 1) / 2 */
90 static const double phi_1
=
91 0.61803398874989490252573887119069695472717285156250;
93 /* Co-prime of u, using phi^-1 */
94 inline static uint32_t
95 coprime(const uint32_t u
)
97 return ( (uint32_t)( (double)(u
) * phi_1
) | 1U );
101 * This is the "recommended" IPv4 hash function, as seen in FreeBSD's
102 * src/sys/netinet/tcp_hostcache.c 1.1
104 inline static uint32_t
105 ipv4_hash(const struct addr
*const a
)
107 uint32_t ip
= a
->ip
.v4
;
108 return ( (ip
) ^ ((ip
) >> 7) ^ ((ip
) >> 17) );
114 * http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/common/netinet/in.h#130
116 # define s6_addr32 _S6_un._S6_u32
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
125 * This is the IPv6 hash function used by FreeBSD in the same file as above,
128 inline static uint32_t
129 ipv6_hash(const struct addr
*const a
)
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] );
136 /* ---------------------------------------------------------------------------
137 * hash_func collection
140 hash_func_host(const struct hashtable
*h _unused_
, const void *key
)
142 const struct addr
*a
= key
;
143 if (a
->family
== IPv4
)
144 return (ipv4_hash(a
));
146 assert(a
->family
== IPv6
);
147 return (ipv6_hash(a
));
151 #define CASTKEY(type) (*((const type *)key))
154 hash_func_short(const struct hashtable
*h
, const void *key
)
156 return (CASTKEY(uint16_t) * h
->coeff
);
160 hash_func_byte(const struct hashtable
*h
, const void *key
)
162 return (CASTKEY(uint8_t) * h
->coeff
);
165 /* ---------------------------------------------------------------------------
166 * key_func collection
170 key_func_host(const struct bucket
*b
)
172 return &(b
->u
.host
.addr
);
176 key_func_port_tcp(const struct bucket
*b
)
178 return &(b
->u
.port_tcp
.port
);
182 key_func_port_udp(const struct bucket
*b
)
184 return &(b
->u
.port_udp
.port
);
188 key_func_ip_proto(const struct bucket
*b
)
190 return &(b
->u
.ip_proto
.proto
);
193 /* ---------------------------------------------------------------------------
194 * find_func collection
198 find_func_host(const struct bucket
*b
, const void *key
)
200 return (addr_equal(key
, &(b
->u
.host
.addr
)));
204 find_func_port_tcp(const struct bucket
*b
, const void *key
)
206 return (b
->u
.port_tcp
.port
== CASTKEY(uint16_t));
210 find_func_port_udp(const struct bucket
*b
, const void *key
)
212 return (b
->u
.port_udp
.port
== CASTKEY(uint16_t));
216 find_func_ip_proto(const struct bucket
*b
, const void *key
)
218 return (b
->u
.ip_proto
.proto
== CASTKEY(uint8_t));
221 /* ---------------------------------------------------------------------------
222 * make_func collection
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;
234 static struct bucket
*
235 make_func_host(const void *key
)
237 MAKE_BUCKET(b
, h
, host
);
238 h
->addr
= CASTKEY(struct addr
);
240 h
->last_seen_mono
= 0;
241 memset(&h
->mac_addr
, 0, sizeof(h
->mac_addr
));
249 free_func_host(struct bucket
*b
)
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
);
258 static struct bucket
*
259 make_func_port_tcp(const void *key
)
261 MAKE_BUCKET(b
, p
, port_tcp
);
262 p
->port
= CASTKEY(uint16_t);
267 static struct bucket
*
268 make_func_port_udp(const void *key
)
270 MAKE_BUCKET(b
, p
, port_udp
);
271 p
->port
= CASTKEY(uint16_t);
275 static struct bucket
*
276 make_func_ip_proto(const void *key
)
278 MAKE_BUCKET(b
, p
, ip_proto
);
279 p
->proto
= CASTKEY(uint8_t);
284 free_func_simple(struct bucket
*b _unused_
)
289 /* ---------------------------------------------------------------------------
290 * format_func collection (ordered by struct)
294 format_cols_host(struct str
*buf
)
296 /* FIXME: don't clobber parts of the query string
297 * specifically "full" and "start"
298 * when setting sort direction
304 " <th>Hostname</th>\n");
305 if (hosts_db_show_macs
) str_append(buf
,
306 " <th>MAC Address</th>\n");
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");
318 format_row_host(struct str
*buf
, const struct bucket
*b
,
319 const char *css_class
)
321 const char *ip
= addr_to_str(&(b
->u
.host
.addr
));
324 "<tr class=\"%s\">\n"
325 " <td><a href=\"./%s/\">%s</a></td>\n"
329 (b
->u
.host
.dns
== NULL
) ? "" : b
->u
.host
.dns
);
331 if (hosts_db_show_macs
)
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]);
342 " <td class=\"num\">%'qu</td>\n"
343 " <td class=\"num\">%'qu</td>\n"
344 " <td class=\"num\">%'qu</td>\n",
345 b
->in
, b
->out
, b
->total
);
347 if (opt_want_lastseen
) {
348 long last
= b
->u
.host
.last_seen_mono
;
349 struct str
*last_str
= NULL
;
351 if ((now_mono() >= last
) && (last
> 0))
352 last_str
= length_of_time(now_mono() - last
);
354 str_append(buf
, " <td class=\"num\">");
355 if (last_str
== NULL
) {
357 str_append(buf
, "(never)");
359 str_append(buf
, "(clock error)");
361 str_appendstr(buf
, last_str
);
364 str_append(buf
, "</td>");
367 str_appendf(buf
, "</tr>\n");
369 /* Only resolve hosts "on demand" */
370 if (b
->u
.host
.dns
== NULL
)
371 dns_queue(&(b
->u
.host
.addr
));
375 format_cols_port_tcp(struct str
*buf
)
381 " <th>Service</td>\n"
391 format_row_port_tcp(struct str
*buf
, const struct bucket
*b
,
392 const char *css_class
)
394 const struct port_tcp
*p
= &(b
->u
.port_tcp
);
397 "<tr class=\"%s\">\n"
398 " <td class=\"num\">%u</td>\n"
400 " <td class=\"num\">%'qu</td>\n"
401 " <td class=\"num\">%'qu</td>\n"
402 " <td class=\"num\">%'qu</td>\n"
403 " <td class=\"num\">%'qu</td>\n"
406 p
->port
, getservtcp(p
->port
), b
->in
, b
->out
, b
->total
, p
->syn
411 format_cols_port_udp(struct str
*buf
)
417 " <th>Service</td>\n"
426 format_row_port_udp(struct str
*buf
, const struct bucket
*b
,
427 const char *css_class
)
429 const struct port_udp
*p
= &(b
->u
.port_udp
);
432 "<tr class=\"%s\">\n"
433 " <td class=\"num\">%u</td>\n"
435 " <td class=\"num\">%'qu</td>\n"
436 " <td class=\"num\">%'qu</td>\n"
437 " <td class=\"num\">%'qu</td>\n"
440 p
->port
, getservudp(p
->port
), b
->in
, b
->out
, b
->total
445 format_cols_ip_proto(struct str
*buf
)
451 " <th>Protocol</td>\n"
460 format_row_ip_proto(struct str
*buf
, const struct bucket
*b
,
461 const char *css_class
)
463 const struct ip_proto
*p
= &(b
->u
.ip_proto
);
466 "<tr class=\"%s\">\n"
467 " <td class=\"num\">%u</td>\n"
469 " <td class=\"num\">%'qu</td>\n"
470 " <td class=\"num\">%'qu</td>\n"
471 " <td class=\"num\">%'qu</td>\n"
474 p
->proto
, getproto(p
->proto
),
475 b
->in
, b
->out
, b
->total
479 /* ---------------------------------------------------------------------------
480 * Initialise a hashtable.
482 static struct hashtable
*
483 hashtable_make(const uint8_t bits
,
484 const unsigned int count_max
,
485 const unsigned int count_keep
,
486 hash_func_t
*hash_func
,
487 free_func_t
*free_func
,
488 key_func_t
*key_func
,
489 find_func_t
*find_func
,
490 make_func_t
*make_func
,
491 format_cols_func_t
*format_cols_func
,
492 format_row_func_t
*format_row_func
)
494 struct hashtable
*hash
;
497 hash
= xmalloc(sizeof(*hash
));
499 hash
->count_max
= count_max
;
500 hash
->count_keep
= count_keep
;
501 hash
->size
= 1U << bits
;
502 hash
->mask
= hash
->size
- 1;
503 hash
->coeff
= coprime(hash
->size
);
504 hash
->hash_func
= hash_func
;
505 hash
->free_func
= free_func
;
506 hash
->key_func
= key_func
;
507 hash
->find_func
= find_func
;
508 hash
->make_func
= make_func
;
509 hash
->format_cols_func
= format_cols_func
;
510 hash
->format_row_func
= format_row_func
;
512 hash
->table
= xcalloc(hash
->size
, sizeof(*hash
->table
));
513 memset(&(hash
->stats
), 0, sizeof(hash
->stats
));
517 /* ---------------------------------------------------------------------------
518 * Initialise global hosts_db.
523 assert(hosts_db
== NULL
);
524 hosts_db
= hashtable_make(HOST_BITS
, opt_hosts_max
, opt_hosts_keep
,
525 hash_func_host
, free_func_host
, key_func_host
, find_func_host
,
526 make_func_host
, format_cols_host
, format_row_host
);
530 hashtable_rehash(struct hashtable
*h
, const uint8_t bits
)
532 struct bucket
**old_table
, **new_table
;
533 uint32_t i
, old_size
;
539 old_table
= h
->table
;
542 h
->size
= 1U << bits
;
543 h
->mask
= h
->size
- 1;
544 h
->coeff
= coprime(h
->size
);
545 new_table
= xcalloc(h
->size
, sizeof(*new_table
));
547 for (i
=0; i
<old_size
; i
++) {
548 struct bucket
*next
, *b
= old_table
[i
];
550 uint32_t pos
= h
->hash_func(h
, h
->key_func(b
)) & h
->mask
;
552 b
->next
= new_table
[pos
];
559 h
->table
= new_table
;
563 hashtable_insert(struct hashtable
*h
, struct bucket
*b
)
568 assert(b
->next
== NULL
);
570 /* Rehash on 80% occupancy */
571 if ((h
->count
> h
->size
) ||
572 ((h
->size
- h
->count
) < h
->size
/ 5))
573 hashtable_rehash(h
, h
->bits
+1);
575 pos
= h
->hash_func(h
, h
->key_func(b
)) & h
->mask
;
576 if (h
->table
[pos
] == NULL
)
579 /* Insert at top of chain. */
580 b
->next
= h
->table
[pos
];
587 /* Return bucket matching key, or NULL if no such entry. */
588 static struct bucket
*
589 hashtable_search(struct hashtable
*h
, const void *key
)
595 pos
= h
->hash_func(h
, key
) & h
->mask
;
598 if (h
->find_func(b
, key
))
606 typedef enum { NO_REDUCE
= 0, ALLOW_REDUCE
= 1 } reduce_bool
;
607 /* Search for a key. If it's not there, make and insert a bucket for it. */
608 static struct bucket
*
609 hashtable_find_or_insert(struct hashtable
*h
, const void *key
,
610 const reduce_bool allow_reduce
)
612 struct bucket
*b
= hashtable_search(h
, key
);
615 /* Not found, so insert after checking occupancy. */
616 if (allow_reduce
&& (h
->count
>= h
->count_max
))
618 b
= h
->make_func(key
);
619 hashtable_insert(h
, b
);
625 * Frees the hashtable and the buckets. The contents are assumed to be
626 * "simple" -- i.e. no "destructor" action is required beyond simply freeing
630 hashtable_free(struct hashtable
*h
)
636 for (i
=0; i
<h
->size
; i
++) {
637 struct bucket
*tmp
, *b
= h
->table
[i
];
649 /* ---------------------------------------------------------------------------
650 * Return existing host or insert a new one.
653 host_get(const struct addr
*const a
)
655 return (hashtable_find_or_insert(hosts_db
, a
, NO_REDUCE
));
658 /* ---------------------------------------------------------------------------
659 * Find host, returns NULL if not in DB.
662 host_find(const struct addr
*const a
)
664 return (hashtable_search(hosts_db
, a
));
667 /* ---------------------------------------------------------------------------
668 * Find host, returns NULL if not in DB.
670 static struct bucket
*
671 host_search(const char *ipstr
)
674 struct addrinfo hints
, *ai
;
676 memset(&hints
, 0, sizeof(hints
));
677 hints
.ai_family
= AF_UNSPEC
;
678 hints
.ai_flags
= AI_NUMERICHOST
;
680 if (getaddrinfo(ipstr
, NULL
, &hints
, &ai
))
681 return (NULL
); /* invalid addr */
683 if (ai
->ai_family
== AF_INET
) {
685 a
.ip
.v4
= ((const struct sockaddr_in
*)ai
->ai_addr
)->sin_addr
.s_addr
;
687 else if (ai
->ai_family
== AF_INET6
) {
690 ((struct sockaddr_in6
*)ai
->ai_addr
)->sin6_addr
.s6_addr
,
694 return (NULL
); /* unknown family */
698 verbosef("search(%s) turned into %s", ipstr
, addr_to_str(&a
));
699 return (hashtable_search(hosts_db
, &a
));
702 /* ---------------------------------------------------------------------------
703 * Reduce a hashtable to the top <keep> entries.
706 hashtable_reduce(struct hashtable
*ht
)
708 uint32_t i
, pos
, rmd
;
709 const struct bucket
**table
;
712 assert(ht
->count_keep
< ht
->count
);
714 /* Fill table with pointers to buckets in hashtable. */
715 table
= xcalloc(ht
->count
, sizeof(*table
));
716 for (pos
=0, i
=0; i
<ht
->size
; i
++) {
717 struct bucket
*b
= ht
->table
[i
];
723 assert(pos
== ht
->count
);
724 qsort_buckets(table
, ht
->count
, 0, ht
->count_keep
, TOTAL
);
725 cutoff
= table
[ht
->count_keep
]->total
;
728 /* Remove all elements with total <= cutoff. */
730 for (i
=0; i
<ht
->size
; i
++) {
731 struct bucket
*last
= NULL
, *next
, *b
= ht
->table
[i
];
734 if (b
->total
<= cutoff
) {
735 /* Remove this one. */
750 verbosef("hashtable_reduce: removed %u buckets, left %u",
752 hashtable_rehash(ht
, ht
->bits
); /* is this needed? */
755 /* Reduce hosts_db if needed. */
756 void hosts_db_reduce(void)
758 if (hosts_db
->count
>= hosts_db
->count_max
)
759 hashtable_reduce(hosts_db
);
762 /* ---------------------------------------------------------------------------
763 * Reset hosts_db to empty.
770 for (i
=0; i
<hosts_db
->size
; i
++) {
771 struct bucket
*next
, *b
= hosts_db
->table
[i
];
774 hosts_db
->free_func(b
);
778 hosts_db
->table
[i
] = NULL
;
780 verbosef("hosts_db reset to empty, freed %u hosts", hosts_db
->count
);
784 /* ---------------------------------------------------------------------------
785 * Deallocate hosts_db.
787 void hosts_db_free(void)
791 assert(hosts_db
!= NULL
);
792 for (i
=0; i
<hosts_db
->size
; i
++) {
793 struct bucket
*tmp
, *b
= hosts_db
->table
[i
];
797 hosts_db
->free_func(tmp
);
801 free(hosts_db
->table
);
806 /* ---------------------------------------------------------------------------
807 * Find or create a port_tcp inside a host.
810 host_get_port_tcp(struct bucket
*host
, const uint16_t port
)
812 struct host
*h
= &host
->u
.host
;
814 if (h
->ports_tcp
== NULL
)
815 h
->ports_tcp
= hashtable_make(PORT_BITS
, opt_ports_max
, opt_ports_keep
,
816 hash_func_short
, free_func_simple
, key_func_port_tcp
,
817 find_func_port_tcp
, make_func_port_tcp
,
818 format_cols_port_tcp
, format_row_port_tcp
);
819 return (hashtable_find_or_insert(h
->ports_tcp
, &port
, ALLOW_REDUCE
));
822 /* ---------------------------------------------------------------------------
823 * Find or create a port_udp inside a host.
826 host_get_port_udp(struct bucket
*host
, const uint16_t port
)
828 struct host
*h
= &host
->u
.host
;
830 if (h
->ports_udp
== NULL
)
831 h
->ports_udp
= hashtable_make(PORT_BITS
, opt_ports_max
, opt_ports_keep
,
832 hash_func_short
, free_func_simple
, key_func_port_udp
,
833 find_func_port_udp
, make_func_port_udp
,
834 format_cols_port_udp
, format_row_port_udp
);
835 return (hashtable_find_or_insert(h
->ports_udp
, &port
, ALLOW_REDUCE
));
838 /* ---------------------------------------------------------------------------
839 * Find or create an ip_proto inside a host.
842 host_get_ip_proto(struct bucket
*host
, const uint8_t proto
)
844 struct host
*h
= &host
->u
.host
;
845 static const unsigned int PROTOS_MAX
= 512, PROTOS_KEEP
= 256;
847 if (h
->ip_protos
== NULL
)
848 h
->ip_protos
= hashtable_make(PROTO_BITS
, PROTOS_MAX
, PROTOS_KEEP
,
849 hash_func_byte
, free_func_simple
, key_func_ip_proto
,
850 find_func_ip_proto
, make_func_ip_proto
,
851 format_cols_ip_proto
, format_row_ip_proto
);
852 return (hashtable_find_or_insert(h
->ip_protos
, &proto
, ALLOW_REDUCE
));
855 static struct str
*html_hosts_main(const char *qs
);
856 static struct str
*html_hosts_detail(const char *ip
);
858 /* ---------------------------------------------------------------------------
859 * Web interface: delegate the /hosts/ space.
862 html_hosts(const char *uri
, const char *query
)
864 unsigned int i
, num_elems
;
865 char **elem
= split('/', uri
, &num_elems
);
866 struct str
*buf
= NULL
;
868 assert(num_elems
>= 1);
869 assert(strcmp(elem
[0], "hosts") == 0);
873 buf
= html_hosts_main(query
);
874 else if (num_elems
== 2)
875 /* /hosts/<IP of host>/ */
876 buf
= html_hosts_detail(elem
[1]);
878 for (i
=0; i
<num_elems
; i
++)
881 return (buf
); /* FIXME: a NULL here becomes 404 Not Found, we might want
882 other codes to be possible */
885 /* ---------------------------------------------------------------------------
886 * Format hashtable into HTML.
889 format_table(struct str
*buf
, struct hashtable
*ht
, unsigned int start
,
890 const enum sort_dir sort
, const int full
)
892 const struct bucket
**table
;
893 unsigned int i
, pos
, end
;
896 if ((ht
== NULL
) || (ht
->count
== 0)) {
897 str_append(buf
, "<p>The table is empty.</p>\n");
901 /* Fill table with pointers to buckets in hashtable. */
902 table
= xcalloc(ht
->count
, sizeof(*table
));
903 for (pos
=0, i
=0; i
<ht
->size
; i
++) {
904 struct bucket
*b
= ht
->table
[i
];
910 assert(pos
== ht
->count
);
913 /* full report overrides start and end */
917 end
= MIN(ht
->count
, (uint32_t)start
+MAX_ENTRIES
);
919 str_appendf(buf
, "(%u-%u of %u)<br>\n", start
+1, end
, ht
->count
);
920 qsort_buckets(table
, ht
->count
, start
, end
, sort
);
921 ht
->format_cols_func(buf
);
923 for (i
=start
; i
<end
; i
++) {
924 ht
->format_row_func(buf
, table
[i
], alt
? "alt1" : "alt2");
925 alt
= !alt
; /* alternate class for table rows */
928 str_append(buf
, "</table>\n");
931 /* ---------------------------------------------------------------------------
932 * Web interface: sorted table of hosts.
935 html_hosts_main(const char *qs
)
937 struct str
*buf
= str_make();
938 char *qs_start
, *qs_sort
, *qs_full
, *ep
;
943 /* parse query string */
944 qs_start
= qs_get(qs
, "start");
945 qs_sort
= qs_get(qs
, "sort");
946 qs_full
= qs_get(qs
, "full");
947 if (qs_full
!= NULL
) {
953 if (qs_sort
== NULL
) sort
= TOTAL
;
954 else if (strcmp(qs_sort
, "total") == 0) sort
= TOTAL
;
955 else if (strcmp(qs_sort
, "in") == 0) sort
= IN
;
956 else if (strcmp(qs_sort
, "out") == 0) sort
= OUT
;
957 else if (strcmp(qs_sort
, "lastseen") == 0) sort
= LASTSEEN
;
959 str_append(buf
, "Error: invalid value for \"sort\".\n");
964 if (qs_start
== NULL
)
967 start
= (int)strtoul(qs_start
, &ep
, 10);
969 str_append(buf
, "Error: \"start\" is not a number.\n");
972 if ((errno
== ERANGE
) ||
973 (start
< 0) || (start
>= (int)hosts_db
->count
)) {
974 str_append(buf
, "Error: \"start\" is out of bounds.\n");
979 #define PREV "<<< prev page"
980 #define NEXT "next page >>>"
981 #define FULL "full table"
983 html_open(buf
, "Hosts", /*path_depth=*/1, /*want_graph_js=*/0);
984 format_table(buf
, hosts_db
, start
, sort
, full
);
986 /* <prev | full | stats | next> */
988 if (sortstr
== NULL
) sortstr
= "total";
990 int prev
= start
- MAX_ENTRIES
;
993 str_appendf(buf
, "<a href=\"?start=%d&sort=%s\">" PREV
"</a>",
996 str_append(buf
, PREV
);
999 str_append(buf
, " | " FULL
);
1001 str_appendf(buf
, " | <a href=\"?full=yes&sort=%s\">" FULL
"</a>",
1004 if (start
+MAX_ENTRIES
< (int)hosts_db
->count
)
1005 str_appendf(buf
, " | <a href=\"?start=%d&sort=%s\">" NEXT
"</a>",
1006 start
+MAX_ENTRIES
, sortstr
);
1008 str_append(buf
, " | " NEXT
);
1010 str_append(buf
, "<br>\n");
1014 if (qs_start
!= NULL
) free(qs_start
);
1015 if (qs_sort
!= NULL
) free(qs_sort
);
1022 /* ---------------------------------------------------------------------------
1023 * Web interface: detailed view of a single host.
1025 static struct str
*html_hosts_detail(const char *ip
) {
1027 struct str
*buf
, *ls_len
;
1029 const char *canonical
;
1032 h
= host_search(ip
);
1034 return (NULL
); /* no such host */
1036 canonical
= addr_to_str(&(h
->u
.host
.addr
));
1040 html_open(buf
, ip
, /*path_depth=*/2, /*want_graph_js=*/0);
1041 if (strcmp(ip
, canonical
) != 0)
1042 str_appendf(buf
, "(canonically <b>%s</b>)\n", canonical
);
1045 "<b>Hostname:</b> %s<br>\n",
1046 (h
->u
.host
.dns
== NULL
)?"(resolving...)":h
->u
.host
.dns
);
1048 /* Resolve host "on demand" */
1049 if (h
->u
.host
.dns
== NULL
)
1050 dns_queue(&(h
->u
.host
.addr
));
1052 if (hosts_db_show_macs
)
1054 "<b>MAC Address:</b> "
1055 "<tt>%x:%x:%x:%x:%x:%x</tt><br>\n",
1056 h
->u
.host
.mac_addr
[0],
1057 h
->u
.host
.mac_addr
[1],
1058 h
->u
.host
.mac_addr
[2],
1059 h
->u
.host
.mac_addr
[3],
1060 h
->u
.host
.mac_addr
[4],
1061 h
->u
.host
.mac_addr
[5]);
1066 "<b>Last seen:</b> ");
1068 last_real
= mono_to_real(h
->u
.host
.last_seen_mono
);
1069 if (strftime(ls_when
, sizeof(ls_when
),
1070 "%Y-%m-%d %H:%M:%S %Z%z", localtime(&last_real
)) != 0)
1071 str_append(buf
, ls_when
);
1073 if (h
->u
.host
.last_seen_mono
<= now_mono()) {
1074 ls_len
= length_of_time(now_mono() - h
->u
.host
.last_seen_mono
);
1075 str_append(buf
, " (");
1076 str_appendstr(buf
, ls_len
);
1078 str_append(buf
, " ago)");
1080 str_append(buf
, " (in the future, possible clock problem)");
1086 " <b>In:</b> %'qu<br>\n"
1087 " <b>Out:</b> %'qu<br>\n"
1088 " <b>Total:</b> %'qu<br>\n"
1090 h
->in
, h
->out
, h
->total
);
1092 str_append(buf
, "<h3>TCP ports</h3>\n");
1093 format_table(buf
, h
->u
.host
.ports_tcp
, 0,TOTAL
,0);
1095 str_append(buf
, "<h3>UDP ports</h3>\n");
1096 format_table(buf
, h
->u
.host
.ports_udp
, 0,TOTAL
,0);
1098 str_append(buf
, "<h3>IP protocols</h3>\n");
1099 format_table(buf
, h
->u
.host
.ip_protos
, 0,TOTAL
,0);
1105 /* ---------------------------------------------------------------------------
1106 * Database import and export code:
1107 * Initially written and contributed by Ben Stewart.
1108 * copyright (c) 2007-2011 Ben Stewart, Emil Mikulic.
1110 static int hosts_db_export_ip(const struct hashtable
*h
, const int fd
);
1111 static int hosts_db_export_tcp(const struct hashtable
*h
, const int fd
);
1112 static int hosts_db_export_udp(const struct hashtable
*h
, const int fd
);
1115 export_proto_ip
= 'P',
1116 export_proto_tcp
= 'T',
1117 export_proto_udp
= 'U';
1119 static const unsigned char
1120 export_tag_host_ver1
[] = {'H', 'S', 'T', 0x01},
1121 export_tag_host_ver2
[] = {'H', 'S', 'T', 0x02},
1122 export_tag_host_ver3
[] = {'H', 'S', 'T', 0x03};
1124 /* ---------------------------------------------------------------------------
1125 * Load a host's ip_proto table from a file.
1126 * Returns 0 on failure, 1 on success.
1129 hosts_db_import_ip(const int fd
, struct bucket
*host
)
1133 if (!expect8(fd
, export_proto_ip
)) return 0;
1134 if (!read8(fd
, &count
)) return 0;
1136 for (i
=0; i
<count
; i
++) {
1141 if (!read8(fd
, &proto
)) return 0;
1142 if (!read64(fd
, &in
)) return 0;
1143 if (!read64(fd
, &out
)) return 0;
1146 b
= host_get_ip_proto(host
, proto
);
1149 b
->total
= in
+ out
;
1150 assert(b
->u
.ip_proto
.proto
== proto
); /* should be done by make fn */
1155 /* ---------------------------------------------------------------------------
1156 * Load a host's port_tcp table from a file.
1157 * Returns 0 on failure, 1 on success.
1160 hosts_db_import_tcp(const int fd
, struct bucket
*host
)
1164 if (!expect8(fd
, export_proto_tcp
)) return 0;
1165 if (!read16(fd
, &count
)) return 0;
1167 for (i
=0; i
<count
; i
++) {
1170 uint64_t in
, out
, syn
;
1172 if (!read16(fd
, &port
)) return 0;
1173 if (!read64(fd
, &syn
)) return 0;
1174 if (!read64(fd
, &in
)) return 0;
1175 if (!read64(fd
, &out
)) return 0;
1178 b
= host_get_port_tcp(host
, port
);
1181 b
->total
= in
+ out
;
1182 assert(b
->u
.port_tcp
.port
== port
); /* done by make_func_port_tcp */
1183 b
->u
.port_tcp
.syn
= syn
;
1188 /* ---------------------------------------------------------------------------
1189 * Load a host's port_tcp table from a file.
1190 * Returns 0 on failure, 1 on success.
1193 hosts_db_import_udp(const int fd
, struct bucket
*host
)
1197 if (!expect8(fd
, export_proto_udp
)) return 0;
1198 if (!read16(fd
, &count
)) return 0;
1200 for (i
=0; i
<count
; i
++) {
1205 if (!read16(fd
, &port
)) return 0;
1206 if (!read64(fd
, &in
)) return 0;
1207 if (!read64(fd
, &out
)) return 0;
1210 b
= host_get_port_udp(host
, port
);
1213 b
->total
= in
+ out
;
1214 assert(b
->u
.port_udp
.port
== port
); /* done by make_func */
1219 /* ---------------------------------------------------------------------------
1220 * Load all hosts from a file.
1221 * Returns 0 on failure, 1 on success.
1224 hosts_db_import_host(const int fd
)
1226 struct bucket
*host
;
1228 uint8_t hostname_len
;
1230 unsigned int pos
= xtell(fd
);
1234 if (!readn(fd
, hdr
, sizeof(hdr
))) return 0;
1235 if (memcmp(hdr
, export_tag_host_ver3
, sizeof(hdr
)) == 0)
1237 else if (memcmp(hdr
, export_tag_host_ver2
, sizeof(hdr
)) == 0)
1239 else if (memcmp(hdr
, export_tag_host_ver1
, sizeof(hdr
)) == 0)
1242 warnx("bad host header: %02x%02x%02x%02x",
1243 hdr
[0], hdr
[1], hdr
[2], hdr
[3]);
1248 if (!readaddr(fd
, &a
))
1251 assert((ver
== 1) || (ver
== 2));
1252 if (!readaddr_ipv4(fd
, &a
))
1255 verbosef("at file pos %u, importing host %s", pos
, addr_to_str(&a
));
1256 host
= host_get(&a
);
1257 assert(addr_equal(&(host
->u
.host
.addr
), &a
));
1261 if (!read64(fd
, &t
)) return 0;
1262 host
->u
.host
.last_seen_mono
= real_to_mono(t
);
1265 assert(sizeof(host
->u
.host
.mac_addr
) == 6);
1266 if (!readn(fd
, host
->u
.host
.mac_addr
, sizeof(host
->u
.host
.mac_addr
)))
1270 assert(host
->u
.host
.dns
== NULL
); /* make fn? */
1271 if (!read8(fd
, &hostname_len
)) return 0;
1272 if (hostname_len
> 0) {
1273 host
->u
.host
.dns
= xmalloc(hostname_len
+ 1);
1274 host
->u
.host
.dns
[0] = '\0';
1276 /* At this point, the hostname is attached to a host which is in our
1277 * hosts_db, so if we bail out due to an import error, this pointer
1278 * isn't lost and leaked, it can be cleaned up in hosts_db_{free,reset}
1281 if (!readn(fd
, host
->u
.host
.dns
, hostname_len
)) return 0;
1282 host
->u
.host
.dns
[hostname_len
] = '\0';
1285 if (!read64(fd
, &in
)) return 0;
1286 if (!read64(fd
, &out
)) return 0;
1290 host
->total
= in
+ out
;
1292 /* Host's port and proto subtables: */
1293 if (!hosts_db_import_ip(fd
, host
)) return 0;
1294 if (!hosts_db_import_tcp(fd
, host
)) return 0;
1295 if (!hosts_db_import_udp(fd
, host
)) return 0;
1299 /* ---------------------------------------------------------------------------
1300 * Database Import: Grab hosts_db from a file provided by the caller.
1302 * This function will retrieve the data sans the header. We expect the caller
1303 * to have validated the header of the hosts_db segment, and left the file
1304 * sitting at the start of the data.
1306 int hosts_db_import(const int fd
)
1308 uint32_t host_count
, i
;
1310 if (!read32(fd
, &host_count
)) return 0;
1312 for (i
=0; i
<host_count
; i
++)
1313 if (!hosts_db_import_host(fd
)) return 0;
1318 /* ---------------------------------------------------------------------------
1319 * Database Export: Dump hosts_db into a file provided by the caller.
1320 * The caller is responsible for writing out export_tag_hosts_ver1 first.
1322 int hosts_db_export(const int fd
)
1327 if (!write32(fd
, hosts_db
->count
)) return 0;
1329 for (i
= 0; i
<hosts_db
->size
; i
++)
1330 for (b
= hosts_db
->table
[i
]; b
!= NULL
; b
= b
->next
) {
1331 /* For each host: */
1332 if (!writen(fd
, export_tag_host_ver3
, sizeof(export_tag_host_ver3
)))
1335 if (!writeaddr(fd
, &(b
->u
.host
.addr
)))
1338 if (!write64(fd
, (uint64_t)mono_to_real(b
->u
.host
.last_seen_mono
)))
1341 assert(sizeof(b
->u
.host
.mac_addr
) == 6);
1342 if (!writen(fd
, b
->u
.host
.mac_addr
, sizeof(b
->u
.host
.mac_addr
)))
1346 if (b
->u
.host
.dns
== NULL
) {
1347 if (!write8(fd
, 0)) return 0;
1349 int dnslen
= strlen(b
->u
.host
.dns
);
1352 warnx("found a very long hostname: \"%s\"\n"
1353 "wasn't expecting one longer than 255 chars (this one is %d)",
1354 b
->u
.host
.dns
, dnslen
);
1358 if (!write8(fd
, (uint8_t)dnslen
)) return 0;
1359 if (!writen(fd
, b
->u
.host
.dns
, dnslen
)) return 0;
1362 if (!write64(fd
, b
->in
)) return 0;
1363 if (!write64(fd
, b
->out
)) return 0;
1365 if (!hosts_db_export_ip(b
->u
.host
.ip_protos
, fd
)) return 0;
1366 if (!hosts_db_export_tcp(b
->u
.host
.ports_tcp
, fd
)) return 0;
1367 if (!hosts_db_export_udp(b
->u
.host
.ports_udp
, fd
)) return 0;
1372 /* ---------------------------------------------------------------------------
1373 * Dump the ip_proto table of a host.
1376 hosts_db_export_ip(const struct hashtable
*h
, const int fd
)
1378 uint32_t i
, written
= 0;
1382 if (!write8(fd
, export_proto_ip
)) return 0;
1384 /* If no data, write a IP Proto count of 0 and we're done. */
1386 if (!write8(fd
, 0)) return 0;
1390 assert(h
->count
< 256);
1391 if (!write8(fd
, (uint8_t)h
->count
)) return 0;
1393 for (i
= 0; i
<h
->size
; i
++)
1394 for (b
= h
->table
[i
]; b
!= NULL
; b
= b
->next
) {
1395 /* For each ip_proto bucket: */
1397 if (!write8(fd
, b
->u
.ip_proto
.proto
)) return 0;
1398 if (!write64(fd
, b
->in
)) return 0;
1399 if (!write64(fd
, b
->out
)) return 0;
1402 assert(written
== h
->count
);
1406 /* ---------------------------------------------------------------------------
1407 * Dump the port_tcp table of a host.
1410 hosts_db_export_tcp(const struct hashtable
*h
, const int fd
)
1413 uint32_t i
, written
= 0;
1416 if (!write8(fd
, export_proto_tcp
)) return 0;
1418 /* If no data, write a count of 0 and we're done. */
1420 if (!write16(fd
, 0)) return 0;
1424 assert(h
->count
< 65536);
1425 if (!write16(fd
, (uint16_t)h
->count
)) return 0;
1427 for (i
= 0; i
<h
->size
; i
++)
1428 for (b
= h
->table
[i
]; b
!= NULL
; b
= b
->next
) {
1429 if (!write16(fd
, b
->u
.port_tcp
.port
)) return 0;
1430 if (!write64(fd
, b
->u
.port_tcp
.syn
)) return 0;
1431 if (!write64(fd
, b
->in
)) return 0;
1432 if (!write64(fd
, b
->out
)) return 0;
1435 assert(written
== h
->count
);
1439 /* ---------------------------------------------------------------------------
1440 * Dump the port_udp table of a host.
1443 hosts_db_export_udp(const struct hashtable
*h
, const int fd
)
1446 uint32_t i
, written
= 0;
1449 if (!write8(fd
, export_proto_udp
)) return 0;
1451 /* If no data, write a count of 0 and we're done. */
1453 if (!write16(fd
, 0)) return 0;
1457 assert(h
->count
< 65536);
1458 if (!write16(fd
, (uint16_t)h
->count
)) return 0;
1460 for (i
= 0; i
<h
->size
; i
++)
1461 for (b
= h
->table
[i
]; b
!= NULL
; b
= b
->next
) {
1462 if (!write16(fd
, b
->u
.port_udp
.port
)) return 0;
1463 if (!write64(fd
, b
->in
)) return 0;
1464 if (!write64(fd
, b
->out
)) return 0;
1467 assert(written
== h
->count
);
1471 /* vim:set ts=3 sw=3 tw=78 expandtab: */