From 0d32c5c070f817229110f92d7b31df9a3e4eeec5 Mon Sep 17 00:00:00 2001 From: Jozsef Kadlecsik Date: Sun, 24 Oct 2010 21:42:48 +0200 Subject: Fixes, cleanups, comments - More comments added to the code - ICMP and ICMPv6 support added to the hash:ip,port, hash:ip,port,ip and hash:ip,port,net types - hash:net and hash:ip,port,net types are reworked - hash:net,port type added - Wrong direction parameters fixed in hash:ip,port - Helps and manpage are updated - More tests added - Ugly macros are rewritten to functions in parse.c (Holger Eitzenberger) - resize related bug in hash types fixed (Holger Eitzenberger) - autoreconf patches by Jan Engelhardt applied - netlink patch minimalized: dumping can be initialized by a second parsing of the message (thanks to David and Patrick for the suggestion) - IPv4/IPv6 address attributes are introduced in order to fix the context (suggested by David) --- lib/icmp.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 lib/icmp.c (limited to 'lib/icmp.c') diff --git a/lib/icmp.c b/lib/icmp.c new file mode 100644 index 0000000..93276e2 --- /dev/null +++ b/lib/icmp.c @@ -0,0 +1,79 @@ +/* Copyright 2007-2010 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ +#include /* STRNEQ */ +#include /* prototypes */ + +struct icmp_names { + const char *name; + uint8_t type, code; +}; + +static const struct icmp_names icmp_typecodes[] = { + { "echo-reply", 0, 0 }, + { "pong", 0, 0 }, + { "network-unreachable", 3, 0 }, + { "host-unreachable", 3, 1 }, + { "protocol-unreachable", 3, 2 }, + { "port-unreachable", 3, 3 }, + { "fragmentation-needed", 3, 4 }, + { "source-route-failed", 3, 5 }, + { "network-unknown", 3, 6 }, + { "host-unknown", 3, 7 }, + { "network-prohibited", 3, 9 }, + { "host-prohibited", 3, 10 }, + { "TOS-network-unreachable", 3, 11 }, + { "TOS-host-unreachable", 3, 12 }, + { "communication-prohibited", 3, 13 }, + { "host-precedence-violation", 3, 14 }, + { "precedence-cutoff", 3, 15 }, + { "source-quench", 4, 0 }, + { "network-redirect", 5, 0 }, + { "host-redirect", 5, 1 }, + { "TOS-network-redirect", 5, 2 }, + { "TOS-host-redirect", 5, 3 }, + { "echo-request", 8, 0 }, + { "ping", 8, 0 }, + { "router-advertisement", 9, 0 }, + { "router-solicitation", 10, 0 }, + { "ttl-zero-during-transit", 11, 0 }, + { "ttl-zero-during-reassembly", 11, 1 }, + { "ip-header-bad", 12, 0 }, + { "required-option-missing", 12, 1 }, + { "timestamp-request", 13, 0 }, + { "timestamp-reply", 14, 0 }, + { "address-mask-request", 17, 0 }, + { "address-mask-reply", 18, 0 }, +}; + +const char * id_to_icmp(uint8_t id) +{ + return id < ARRAY_SIZE(icmp_typecodes) ? icmp_typecodes[id].name : NULL; +} + +const char * icmp_to_name(uint8_t type, uint8_t code) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(icmp_typecodes); i++) + if (icmp_typecodes[i].type == type && icmp_typecodes[i].code == code) + return icmp_typecodes[i].name; + + return NULL; +} + +int name_to_icmp(const char *str, uint16_t *typecode) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(icmp_typecodes); i++) + if (STRNEQ(icmp_typecodes[i].name, str, strlen(str))) { + *typecode = (icmp_typecodes[i].type << 8) | icmp_typecodes[i].code; + return 0; + } + + return -1; +} -- cgit v1.2.3