From 5e442d9cf826adb58d4595722c7dd7b2532096c9 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Fri, 20 Jan 2012 23:18:49 +0000 Subject: [PATCH] -adding ICMP support to TUN library --- src/include/gnunet_tun_lib.h | 70 ++++++++++++++++++++++++++++++++++++ src/tun/tun.c | 23 ++++++++++++ 2 files changed, 93 insertions(+) diff --git a/src/include/gnunet_tun_lib.h b/src/include/gnunet_tun_lib.h index 5fddf70b2..01358299d 100644 --- a/src/include/gnunet_tun_lib.h +++ b/src/include/gnunet_tun_lib.h @@ -145,6 +145,63 @@ struct GNUNET_TUN_DnsHeader uint16_t arcount GNUNET_PACKED; }; +#define GNUNET_TUN_ICMPTYPE_ECHO_REPLY 0 +#define GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE 3 +#define GNUNET_TUN_ICMPTYPE_SOURCE_QUENCH 4 +#define GNUNET_TUN_ICMPTYPE_REDIRECT_MESSAGE 5 +#define GNUNET_TUN_ICMPTYPE_ECHO_REQUEST 8 +#define GNUNET_TUN_ICMPTYPE_ROUTER_ADVERTISEMENT 9 +#define GNUNET_TUN_ICMPTYPE_ROUTER_SOLICITATION 10 +#define GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED 11 + +#define GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE 1 +#define GNUNET_TUN_ICMPTYPE6_PACKET_TOO_BIG 2 +#define GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED 3 +#define GNUNET_TUN_ICMPTYPE6_PARAMETER_PROBLEM 4 +#define GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST 128 +#define GNUNET_TUN_ICMPTYPE6_ECHO_REPLY 129 + +/** + * ICMP header. + */ +struct GNUNET_TUN_IcmpHeader { + uint8_t type; + uint8_t code; + uint16_t crc; + + union { + /** + * ICMP Echo (request/reply) + */ + struct { + uint16_t identifier; + uint16_t sequence_number; + } echo; + + /** + * ICMP Destination Unreachable (RFC 1191) + */ + struct ih_pmtu { + uint16_t empty; + uint16_t next_hop_mtu; + /* followed by original IP header + first 8 bytes of original IP datagram */ + } destination_unreachable; + + /** + * ICMP Redirect + */ + struct in_addr redirect_gateway_address; + + /** + * Placeholder. + */ + int32_t present; + + } quench; + +}; + + GNUNET_NETWORK_STRUCT_END @@ -239,4 +296,17 @@ GNUNET_TUN_calculate_udp6_checksum (const struct GNUNET_TUN_IPv6Header *ip, uint16_t payload_length); +/** + * Calculate ICMP checksum. + * + * @param icmp IMCP header (initialized except for CRC) + * @param payload the ICMP payload + * @param payload_length number of bytes of ICMP payload + */ +void +GNUNET_TUN_calculate_icmp_checksum (struct GNUNET_TUN_IcmpHeader *icmp, + const void *payload, + uint16_t payload_length); + + #endif diff --git a/src/tun/tun.c b/src/tun/tun.c index 009fdd952..4c2ddf07a 100644 --- a/src/tun/tun.c +++ b/src/tun/tun.c @@ -242,4 +242,27 @@ GNUNET_TUN_calculate_udp6_checksum (const struct GNUNET_TUN_IPv6Header *ip, } +/** + * Calculate ICMP checksum. + * + * @param icmp IMCP header (initialized except for CRC) + * @param payload the ICMP payload + * @param payload_length number of bytes of ICMP payload + */ +void +GNUNET_TUN_calculate_icmp_checksum (struct GNUNET_TUN_IcmpHeader *icmp, + const void *payload, + uint16_t payload_length) +{ + uint32_t sum; + + icmp->crc = 0; + sum = GNUNET_CRYPTO_crc16_step (0, + icmp, + sizeof (struct GNUNET_TUN_IcmpHeader)); + sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length); + icmp->crc = GNUNET_CRYPTO_crc16_finish (sum); +} + + /* end of tun.c */ -- 2.25.1