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

index 5dc6641e74d7c3758137230ee42c32baae01b064..288304600a5f53747fb460a0d0b3e52cac8a6133 100644 (file)
@@ -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;
          
index a4a0f967d1a9fa89279d842d36ab28f1818a6b30..664a86f23522afebc9131eb6de6e5e1c61a7e029 100644 (file)
@@ -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)
   {
index ae16bc65f2fba4291280c1fefd0dee73ba998974..89cfdd2b3330b68ad31111e5a894a7a9df138784 100644 (file)
@@ -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
index 97b7a730ea2101e6f3ff7db0ed5661247af36280..005c7013ddd1fdb6bc8a1c113b76190a83540c7c 100644 (file)
@@ -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 */
index f0cdf6a3c353345275cceb538e3a6d4a1fba477b..b352e216b1ff7520c201c0bafdc2f41bac3e1555 100644 (file)
@@ -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;