-move IPv4 header initialization to tun library
authorChristian Grothoff <christian@grothoff.org>
Tue, 17 Jan 2012 19:22:23 +0000 (19:22 +0000)
committerChristian Grothoff <christian@grothoff.org>
Tue, 17 Jan 2012 19:22:23 +0000 (19:22 +0000)
src/Makefile.am
src/dns/Makefile.am
src/dns/gnunet-service-dns.c
src/exit/Makefile.am
src/exit/gnunet-daemon-exit.c
src/include/gnunet_tun_lib.h
src/vpn/Makefile.am
src/vpn/gnunet-service-vpn.c

index 12862af38b5a56138ae45e52f8c2d448a5dd67c8..36e87f82bd9f9b46b0d17d430eda992214bf3e12 100644 (file)
@@ -10,6 +10,7 @@ SUBDIRS = \
   include $(INTLEMU_SUBDIRS) \
   util \
   hello \
+  tun \
   block \
   statistics \
   arm \
index f8bdae94e931faf5cc6d238bb975a2265b59756a..5f9f39116fafd235c4fd8d5d555db0146d916b99 100644 (file)
@@ -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)
index 175215634439c8da4d80f6b08693605e72fbc125..5dc6641e74d7c3758137230ee42c32baae01b064 100644 (file)
@@ -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;
index 736bb5fd319ee842846c6ccdec26f2ed361152a3..5a047a13a43c14175b584548539e63dae80c116d 100644 (file)
@@ -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)
index 7df72fb022e481b1fde5a1a31401de2f5e1e86db..a4a0f967d1a9fa89279d842d36ab28f1818a6b30 100644 (file)
@@ -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:
index dc6f01e1e6db056a65b1f54a1a4126747b07ed1c..ae16bc65f2fba4291280c1fefd0dee73ba998974 100644 (file)
@@ -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
index bb0993c66e59372bbb9349aa250fb24678e7fd88..786f94d6ad6751dfaeb3d4859fa27e38203a8e77 100644 (file)
@@ -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)
index 7aeabf457cfdc4ccee325515991d23475d4c7bbd..f0cdf6a3c353345275cceb538e3a6d4a1fba477b 100644 (file)
@@ -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);