From: Christian Grothoff Date: Tue, 17 Jan 2012 19:30:12 +0000 (+0000) Subject: -move IPv6 header initialization to tun library X-Git-Tag: initial-import-from-subversion-38251~15248 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=e0d8c3f1c05ab0cb218d7182c723298f7e981b58;p=oweals%2Fgnunet.git -move IPv6 header initialization to tun library --- diff --git a/src/dns/gnunet-service-dns.c b/src/dns/gnunet-service-dns.c index 5dc6641e7..288304600 100644 --- a/src/dns/gnunet-service-dns.c +++ b/src/dns/gnunet-service-dns.c @@ -419,18 +419,11 @@ request_done (struct RequestRecord *rr) spt = dst->sin6_port; dpt = src->sin6_port; - ip.traffic_class_h = 0; - ip.version = 6; /* is there a named constant? I couldn't find one */ - ip.traffic_class_l = 0; - ip.flow_label = 0; - ip.payload_length = htons ((uint16_t) reply_len); - ip.next_header = IPPROTO_UDP; - ip.hop_limit = 255; /* or lower? */ - ip.source_address = dst->sin6_addr; - ip.destination_address = src->sin6_addr; - udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum, - &ip.source_address, - sizeof (struct in6_addr) * 2); + GNUNET_TUN_initialize_ipv6_header (&ip, + IPPROTO_UDP, + reply_len - sizeof (struct GNUNET_TUN_IPv6Header), + &dst->sin6_addr, + &src->sin6_addr); { uint32_t tmp; diff --git a/src/exit/gnunet-daemon-exit.c b/src/exit/gnunet-daemon-exit.c index a4a0f967d..664a86f23 100644 --- a/src/exit/gnunet-daemon-exit.c +++ b/src/exit/gnunet-daemon-exit.c @@ -1159,12 +1159,11 @@ prepare_ipv6_packet (const void *payload, size_t payload_length, return; } - pkt6->version = 6; - pkt6->next_header = protocol; - pkt6->payload_length = htons ((uint16_t) (len + sizeof (struct GNUNET_TUN_IPv6Header))); - pkt6->hop_limit = 255; - pkt6->destination_address = dst_address->address.ipv6; - pkt6->source_address = src_address->address.ipv6; + GNUNET_TUN_initialize_ipv6_header (pkt6, + protocol, + len, + &dst_address->address.ipv6, + &src_address->address.ipv6); switch (protocol) { diff --git a/src/include/gnunet_tun_lib.h b/src/include/gnunet_tun_lib.h index ae16bc65f..89cfdd2b3 100644 --- a/src/include/gnunet_tun_lib.h +++ b/src/include/gnunet_tun_lib.h @@ -165,4 +165,21 @@ GNUNET_TUN_initialize_ipv4_header (struct GNUNET_TUN_IPv4Header *ip, const struct in_addr *dst); +/** + * Initialize an IPv6 header. + * + * @param ip header to initialize + * @param protocol protocol to use (i.e. IPPROTO_UDP) + * @param payload_length number of bytes of payload that follow (excluding IPv4 header) + * @param src source IP address to use + * @param dst destination IP address to use + */ +void +GNUNET_TUN_initialize_ipv6_header (struct GNUNET_TUN_IPv6Header *ip, + uint8_t protocol, + uint16_t payload_length, + const struct in6_addr *src, + const struct in6_addr *dst); + + #endif diff --git a/src/tun/tun.c b/src/tun/tun.c index 97b7a730e..005c7013d 100644 --- a/src/tun/tun.c +++ b/src/tun/tun.c @@ -48,6 +48,7 @@ GNUNET_TUN_initialize_ipv4_header (struct GNUNET_TUN_IPv4Header *ip, const struct in_addr *src, const struct in_addr *dst) { + GNUNET_assert (payload_length <= UINT16_MAX - sizeof (struct GNUNET_TUN_IPv4Header)); ip->header_length = sizeof (struct GNUNET_TUN_IPv4Header) / 4; ip->version = 4; ip->diff_serv = 0; @@ -65,5 +66,34 @@ GNUNET_TUN_initialize_ipv4_header (struct GNUNET_TUN_IPv4Header *ip, } +/** + * Initialize an IPv6 header. + * + * @param ip header to initialize + * @param protocol protocol to use (i.e. IPPROTO_UDP), technically "next_header" for IPv6 + * @param payload_length number of bytes of payload that follow (excluding IPv4 header) + * @param src source IP address to use + * @param dst destination IP address to use + */ +void +GNUNET_TUN_initialize_ipv6_header (struct GNUNET_TUN_IPv6Header *ip, + uint8_t protocol, + uint16_t payload_length, + const struct in6_addr *src, + const struct in6_addr *dst) +{ + GNUNET_assert (payload_length <= UINT16_MAX - sizeof (struct GNUNET_TUN_IPv6Header)); + ip->traffic_class_h = 0; + ip->version = 6; + ip->traffic_class_l = 0; + ip->flow_label = 0; + ip->next_header = protocol; + ip->payload_length = htons ((uint16_t) (payload_length + sizeof (struct GNUNET_TUN_IPv6Header))); + ip->hop_limit = FRESH_TTL; + ip->destination_address = *dst; + ip->source_address = *src; +} + + /* end of tun.c */ diff --git a/src/vpn/gnunet-service-vpn.c b/src/vpn/gnunet-service-vpn.c index f0cdf6a3c..b352e216b 100644 --- a/src/vpn/gnunet-service-vpn.c +++ b/src/vpn/gnunet-service-vpn.c @@ -1636,15 +1636,11 @@ receive_tcp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel, msg->size = htons (size); tun->flags = htons (0); tun->proto = htons (ETH_P_IPV6); - ipv6->traffic_class_h = 0; - ipv6->version = 6; - ipv6->traffic_class_l = 0; - ipv6->flow_label = 0; - ipv6->payload_length = htons (sizeof (struct GNUNET_TUN_TcpHeader) + sizeof (struct GNUNET_TUN_IPv6Header) + mlen); - ipv6->next_header = IPPROTO_TCP; - ipv6->hop_limit = 255; - ipv6->source_address = ts->destination_ip.v6; - ipv6->destination_address = ts->source_ip.v6; + GNUNET_TUN_initialize_ipv6_header (ipv6, + IPPROTO_TCP, + sizeof (struct GNUNET_TUN_TcpHeader) + mlen, + &ts->destination_ip.v6, + &ts->source_ip.v6); tcp->spt = htons (ts->destination_port); tcp->dpt = htons (ts->source_port); tcp->crc = 0;