From 40261ecc5bf242437f12cf91d035c138cf089a1e Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 17 Jan 2012 19:22:23 +0000 Subject: [PATCH] -move IPv4 header initialization to tun library --- src/Makefile.am | 1 + src/dns/Makefile.am | 1 + src/dns/gnunet-service-dns.c | 28 +++++++--------------- src/exit/Makefile.am | 1 + src/exit/gnunet-daemon-exit.c | 20 ++++------------ src/include/gnunet_tun_lib.h | 17 ++++++++++++++ src/vpn/Makefile.am | 1 + src/vpn/gnunet-service-vpn.c | 44 ++++++++--------------------------- 8 files changed, 45 insertions(+), 68 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 12862af38..36e87f82b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,6 +10,7 @@ SUBDIRS = \ include $(INTLEMU_SUBDIRS) \ util \ hello \ + tun \ block \ statistics \ arm \ diff --git a/src/dns/Makefile.am b/src/dns/Makefile.am index f8bdae94e..5f9f39116 100644 --- a/src/dns/Makefile.am +++ b/src/dns/Makefile.am @@ -70,6 +70,7 @@ gnunet_dns_redirector_DEPENDENCIES = \ gnunet_service_dns_SOURCES = \ gnunet-service-dns.c gnunet_service_dns_LDADD = \ + $(top_builddir)/src/tun/libgnunettun.la \ $(top_builddir)/src/statistics/libgnunetstatistics.la \ $(top_builddir)/src/util/libgnunetutil.la \ $(GN_LIBINTL) diff --git a/src/dns/gnunet-service-dns.c b/src/dns/gnunet-service-dns.c index 175215634..5dc6641e7 100644 --- a/src/dns/gnunet-service-dns.c +++ b/src/dns/gnunet-service-dns.c @@ -32,9 +32,6 @@ #include "gnunet_statistics_service.h" #include "gnunet_tun_lib.h" -#ifndef IPVERSION -#define IPVERSION 4 -#endif /** * Phases each request goes through. @@ -387,20 +384,13 @@ request_done (struct RequestRecord *rr) spt = dst->sin_port; dpt = src->sin_port; - ip.header_length = sizeof (struct GNUNET_TUN_IPv4Header) / 4; - ip.version = IPVERSION; /* aka 4 */ - ip.diff_serv = 0; - ip.total_length = htons ((uint16_t) reply_len - off); - ip.identification = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, - 65536); - ip.flags = 0; - ip.fragmentation_offset = 0; - ip.ttl = 255; /* or lower? */ - ip.protocol = IPPROTO_UDP; - ip.checksum = 0; /* checksum is optional */ - ip.source_address = dst->sin_addr; - ip.destination_address = src->sin_addr; - ip.checksum = GNUNET_CRYPTO_crc16_n (&ip, sizeof (ip)); + GNUNET_TUN_initialize_ipv4_header (&ip, + IPPROTO_UDP, + reply_len - off - sizeof (struct GNUNET_TUN_IPv4Header), + &dst->sin_addr, + &src->sin_addr); + + udp_crc_sum = GNUNET_CRYPTO_crc16_step (udp_crc_sum, &ip.source_address, @@ -1062,7 +1052,7 @@ process_helper_messages (void *cls GNUNET_UNUSED, void *client, case ETH_P_IPV4: ip4 = (const struct GNUNET_TUN_IPv4Header *) &tun[1]; if ( (msize < sizeof (struct GNUNET_TUN_IPv4Header)) || - (ip4->version != IPVERSION) || + (ip4->version != 4) || (ip4->header_length != sizeof (struct GNUNET_TUN_IPv4Header) / 4) || (ntohs(ip4->total_length) != msize) || (ip4->protocol != IPPROTO_UDP) ) @@ -1119,7 +1109,7 @@ process_helper_messages (void *cls GNUNET_UNUSED, void *client, /* setup new request */ rr->phase = RP_INIT; - if (ip4->version == IPVERSION) + if (ip4->version == 4) { srca4 = (struct sockaddr_in*) &rr->src_addr; dsta4 = (struct sockaddr_in*) &rr->dst_addr; diff --git a/src/exit/Makefile.am b/src/exit/Makefile.am index 736bb5fd3..5a047a13a 100644 --- a/src/exit/Makefile.am +++ b/src/exit/Makefile.am @@ -37,6 +37,7 @@ gnunet_daemon_exit_SOURCES = \ gnunet_daemon_exit_LDADD = \ $(top_builddir)/src/core/libgnunetcore.la \ $(top_builddir)/src/statistics/libgnunetstatistics.la \ + $(top_builddir)/src/tun/libgnunettun.la \ $(top_builddir)/src/util/libgnunetutil.la \ $(top_builddir)/src/mesh/libgnunetmesh.la \ $(GN_LIBINTL) diff --git a/src/exit/gnunet-daemon-exit.c b/src/exit/gnunet-daemon-exit.c index 7df72fb02..a4a0f967d 100644 --- a/src/exit/gnunet-daemon-exit.c +++ b/src/exit/gnunet-daemon-exit.c @@ -1065,21 +1065,11 @@ prepare_ipv4_packet (const void *payload, size_t payload_length, return; } - pkt4->version = 4; - pkt4->header_length = sizeof (struct GNUNET_TUN_IPv4Header) / 4; - pkt4->diff_serv = 0; - pkt4->total_length = htons ((uint16_t) (sizeof (struct GNUNET_TUN_IPv4Header) + len)); - pkt4->identification = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, - UINT16_MAX + 1); - pkt4->flags = 0; - pkt4->fragmentation_offset = 0; - pkt4->ttl = 255; - pkt4->protocol = protocol; - pkt4->checksum = 0; - pkt4->destination_address = dst_address->address.ipv4; - pkt4->source_address = src_address->address.ipv4; - pkt4->checksum = GNUNET_CRYPTO_crc16_n (pkt4, sizeof (struct GNUNET_TUN_IPv4Header)); - + GNUNET_TUN_initialize_ipv4_header (pkt4, + protocol, + len, + &src_address->address.ipv4, + &dst_address->address.ipv4); switch (protocol) { case IPPROTO_UDP: diff --git a/src/include/gnunet_tun_lib.h b/src/include/gnunet_tun_lib.h index dc6f01e1e..ae16bc65f 100644 --- a/src/include/gnunet_tun_lib.h +++ b/src/include/gnunet_tun_lib.h @@ -148,4 +148,21 @@ struct GNUNET_TUN_DnsHeader GNUNET_NETWORK_STRUCT_END +/** + * Initialize an IPv4 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_ipv4_header (struct GNUNET_TUN_IPv4Header *ip, + uint8_t protocol, + uint16_t payload_length, + const struct in_addr *src, + const struct in_addr *dst); + + #endif diff --git a/src/vpn/Makefile.am b/src/vpn/Makefile.am index bb0993c66..786f94d6a 100644 --- a/src/vpn/Makefile.am +++ b/src/vpn/Makefile.am @@ -40,6 +40,7 @@ gnunet_service_vpn_SOURCES = \ gnunet-service-vpn.c gnunet_service_vpn_LDADD = \ $(top_builddir)/src/statistics/libgnunetstatistics.la \ + $(top_builddir)/src/tun/libgnunettun.la \ $(top_builddir)/src/util/libgnunetutil.la \ $(top_builddir)/src/mesh/libgnunetmesh.la \ $(GN_LIBINTL) diff --git a/src/vpn/gnunet-service-vpn.c b/src/vpn/gnunet-service-vpn.c index 7aeabf457..f0cdf6a3c 100644 --- a/src/vpn/gnunet-service-vpn.c +++ b/src/vpn/gnunet-service-vpn.c @@ -1422,23 +1422,11 @@ receive_udp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel, msg->size = htons (size); tun->flags = htons (0); tun->proto = htons (ETH_P_IPV4); - ipv4->version = 4; - ipv4->header_length = sizeof (struct GNUNET_TUN_IPv4Header) / 4; - ipv4->diff_serv = 0; - ipv4->total_length = htons (sizeof (struct GNUNET_TUN_IPv4Header) + - sizeof (struct GNUNET_TUN_UdpHeader) + - mlen); - ipv4->identification = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, - UINT16_MAX + 1); - ipv4->flags = 0; - ipv4->fragmentation_offset = 0; - ipv4->ttl = 255; - ipv4->protocol = IPPROTO_UDP; - ipv4->checksum = 0; - ipv4->source_address = ts->destination_ip.v4; - ipv4->destination_address = ts->source_ip.v4; - ipv4->checksum = - GNUNET_CRYPTO_crc16_n (ipv4, sizeof (struct GNUNET_TUN_IPv4Header)); + GNUNET_TUN_initialize_ipv4_header (ipv4, + IPPROTO_UDP, + sizeof (struct GNUNET_TUN_UdpHeader) + mlen, + &ts->destination_ip.v4, + &ts->source_ip.v4); if (0 == ntohs (reply->source_port)) udp->spt = htons (ts->destination_port); else @@ -1600,23 +1588,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_IPV4); - ipv4->version = 4; - ipv4->header_length = sizeof (struct GNUNET_TUN_IPv4Header) / 4; - ipv4->diff_serv = 0; - ipv4->total_length = htons (sizeof (struct GNUNET_TUN_IPv4Header) + - sizeof (struct GNUNET_TUN_TcpHeader) + - mlen); - ipv4->identification = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, - UINT16_MAX + 1); - ipv4->flags = 0; - ipv4->fragmentation_offset = 0; - ipv4->ttl = 255; - ipv4->protocol = IPPROTO_TCP; - ipv4->checksum = 0; - ipv4->source_address = ts->destination_ip.v4; - ipv4->destination_address = ts->source_ip.v4; - ipv4->checksum = - GNUNET_CRYPTO_crc16_n (ipv4, sizeof (struct GNUNET_TUN_IPv4Header)); + GNUNET_TUN_initialize_ipv4_header (ipv4, + IPPROTO_TCP, + sizeof (struct GNUNET_TUN_TcpHeader) + mlen, + &ts->destination_ip.v4, + &ts->source_ip.v4); *tcp = data->tcp_header; tcp->spt = htons (ts->destination_port); tcp->dpt = htons (ts->source_port); -- 2.25.1