summaryrefslogtreecommitdiff
path: root/lookup.c
diff options
context:
space:
mode:
Diffstat (limited to 'lookup.c')
-rw-r--r--lookup.c48
1 files changed, 25 insertions, 23 deletions
diff --git a/lookup.c b/lookup.c
index 874a9b1..f903fe6 100644
--- a/lookup.c
+++ b/lookup.c
@@ -1,11 +1,13 @@
/*
* netsniff-ng - the packet sniffing beast
* Copyright 2009, 2010 Daniel Borkmann.
- * Copyright 2014 Tobias Klauser
+ * Copyright 2014, 2015 Tobias Klauser
* Subject to the GPL, version 2.
*/
#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
#include <string.h>
#include "hash.h"
@@ -13,29 +15,29 @@
#include "lookup.h"
#include "xmalloc.h"
-static struct hash_table lookup_port_tables[PORTS_MAX];
+static struct hash_table lookup_port_tables[LT_MAX];
static const char * const lookup_port_files[] = {
- [PORTS_UDP] = ETCDIRE_STRING "/udp.conf",
- [PORTS_TCP] = ETCDIRE_STRING "/tcp.conf",
- [PORTS_ETHER] = ETCDIRE_STRING "/ether.conf",
+ [LT_PORTS_UDP] = ETCDIRE_STRING "/udp.conf",
+ [LT_PORTS_TCP] = ETCDIRE_STRING "/tcp.conf",
+ [LT_ETHERTYPES] = ETCDIRE_STRING "/ether.conf",
};
-struct port {
+struct lookup_entry {
unsigned int id;
- char *port;
- struct port *next;
+ char *str;
+ struct lookup_entry *next;
};
-void lookup_init_ports(enum ports which)
+void lookup_init(enum lookup_type which)
{
FILE *fp;
char buff[128], *ptr, *end;
const char *file;
struct hash_table *table;
- struct port *p;
+ struct lookup_entry *p;
void **pos;
- bug_on(which >= PORTS_MAX);
+ bug_on(which >= LT_MAX);
table = &lookup_port_tables[which];
file = lookup_port_files[which];
@@ -72,7 +74,7 @@ void lookup_init_ports(enum ports which)
ptr = strtrim_right(ptr, '\n');
ptr = strtrim_right(ptr, ' ');
- p->port = xstrdup(ptr);
+ p->str = xstrdup(ptr);
p->next = NULL;
pos = insert_hash(p->id, p, table);
@@ -89,55 +91,55 @@ void lookup_init_ports(enum ports which)
static int __lookup_cleanup_single(void *ptr)
{
- struct port *tmp, *p = ptr;
+ struct lookup_entry *tmp, *p = ptr;
if (!ptr)
return 0;
while ((tmp = p->next)) {
- xfree(p->port);
+ xfree(p->str);
xfree(p);
p = tmp;
}
- xfree(p->port);
+ xfree(p->str);
xfree(p);
return 0;
}
-void lookup_cleanup_ports(enum ports which)
+void lookup_cleanup(enum lookup_type which)
{
struct hash_table *table;
- bug_on(which >= PORTS_MAX);
+ bug_on(which >= LT_MAX);
table = &lookup_port_tables[which];
for_each_hash(table, __lookup_cleanup_single);
free_hash(table);
}
-#define __do_lookup_inline(id, struct_name, hash_ptr, struct_member) \
+#define __do_lookup_inline(id, hash_ptr) \
({ \
- struct struct_name *entry = lookup_hash(id, hash_ptr); \
+ struct lookup_entry *entry = lookup_hash(id, hash_ptr); \
\
while (entry && id != entry->id) \
entry = entry->next; \
\
- (entry && id == entry->id ? entry->struct_member : NULL); \
+ (entry && id == entry->id ? entry->str : NULL); \
})
char *lookup_ether_type(unsigned int id)
{
- return __do_lookup_inline(id, port, &lookup_port_tables[PORTS_ETHER], port);
+ return __do_lookup_inline(id, &lookup_port_tables[LT_ETHERTYPES]);
}
char *lookup_port_udp(unsigned int id)
{
- return __do_lookup_inline(id, port, &lookup_port_tables[PORTS_UDP], port);
+ return __do_lookup_inline(id, &lookup_port_tables[LT_PORTS_UDP]);
}
char *lookup_port_tcp(unsigned int id)
{
- return __do_lookup_inline(id, port, &lookup_port_tables[PORTS_TCP], port);
+ return __do_lookup_inline(id, &lookup_port_tables[LT_PORTS_TCP]);
}