2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Copyright 2014 Tobias Klauser
5 * Subject to the GPL, version 2.
15 static struct hash_table lookup_port_tables
[PORTS_MAX
];
16 static const char * const lookup_port_files
[] = {
17 [PORTS_UDP
] = ETCDIRE_STRING
"/udp.conf",
18 [PORTS_TCP
] = ETCDIRE_STRING
"/tcp.conf",
19 [PORTS_ETHER
] = ETCDIRE_STRING
"/ether.conf",
28 void lookup_init_ports(enum ports which
)
31 char buff
[128], *ptr
, *end
;
33 struct hash_table
*table
;
37 bug_on(which
>= PORTS_MAX
);
38 table
= &lookup_port_tables
[which
];
39 file
= lookup_port_files
[which
];
41 fp
= fopen(file
, "r");
43 panic("No %s found!\n", file
);
45 memset(buff
, 0, sizeof(buff
));
47 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
48 buff
[sizeof(buff
) - 1] = 0;
51 p
= xmalloc(sizeof(*p
));
52 p
->id
= strtol(ptr
, &end
, 0);
53 /* not a valid line, skip */
54 if (p
->id
== 0 && end
== ptr
) {
59 ptr
= strstr(buff
, ", ");
67 ptr
= strtrim_right(ptr
, '\n');
68 ptr
= strtrim_right(ptr
, ' ');
70 p
->port
= xstrdup(ptr
);
73 pos
= insert_hash(p
->id
, p
, table
);
79 memset(buff
, 0, sizeof(buff
));
85 static int __lookup_cleanup_single(void *ptr
)
87 struct port
*tmp
, *p
= ptr
;
92 while ((tmp
= p
->next
)) {
104 void lookup_cleanup_ports(enum ports which
)
106 struct hash_table
*table
;
108 bug_on(which
>= PORTS_MAX
);
109 table
= &lookup_port_tables
[which
];
111 for_each_hash(table
, __lookup_cleanup_single
);
115 #define __do_lookup_inline(id, struct_name, hash_ptr, struct_member) \
117 struct struct_name *entry = lookup_hash(id, hash_ptr); \
119 while (entry && id != entry->id) \
120 entry = entry->next; \
122 (entry && id == entry->id ? entry->struct_member : NULL); \
125 char *lookup_ether_type(unsigned int id
)
127 return __do_lookup_inline(id
, port
, &lookup_port_tables
[PORTS_ETHER
], port
);
130 char *lookup_port_udp(unsigned int id
)
132 return __do_lookup_inline(id
, port
, &lookup_port_tables
[PORTS_UDP
], port
);
135 char *lookup_port_tcp(unsigned int id
)
137 return __do_lookup_inline(id
, port
, &lookup_port_tables
[PORTS_TCP
], port
);