-move IPv6-TCP checksum calculation to tun library
authorChristian Grothoff <christian@grothoff.org>
Tue, 17 Jan 2012 19:39:52 +0000 (19:39 +0000)
committerChristian Grothoff <christian@grothoff.org>
Tue, 17 Jan 2012 19:39:52 +0000 (19:39 +0000)
src/exit/gnunet-daemon-exit.c
src/include/gnunet_tun_lib.h
src/tun/tun.c
src/vpn/gnunet-service-vpn.c

index 664a86f23522afebc9131eb6de6e5e1c61a7e029..482eaae7e5e8ea87d8b0d2e8b0b790d4e82dc807 100644 (file)
@@ -1091,6 +1091,7 @@ prepare_ipv4_packet (const void *payload, size_t payload_length,
       memcpy (&pkt4_tcp[1], payload, payload_length);
       pkt4_tcp->spt = htons (src_address->port);
       pkt4_tcp->dpt = htons (dst_address->port);
+      
       pkt4_tcp->crc = 0;
       uint32_t sum = 0;
       sum = GNUNET_CRYPTO_crc16_step (sum, 
@@ -1194,19 +1195,12 @@ prepare_ipv6_packet (const void *payload, size_t payload_length,
       struct GNUNET_TUN_TcpHeader *pkt6_tcp = (struct GNUNET_TUN_TcpHeader *) pkt6;
       
       memcpy (pkt6_tcp, payload, payload_length);
-      pkt6_tcp->crc = 0;
       pkt6_tcp->spt = htons (src_address->port);
       pkt6_tcp->dpt = htons (dst_address->port);
-
-      uint32_t sum = 0;
-      sum = GNUNET_CRYPTO_crc16_step (sum, &pkt6->source_address, 
-                                     sizeof (struct in6_addr) * 2);
-      uint32_t tmp = htonl (len);
-      sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
-      tmp = htonl (pkt6->next_header);
-      sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));      
-      sum = GNUNET_CRYPTO_crc16_step (sum,  pkt6_tcp, len);
-      pkt6_tcp->crc = GNUNET_CRYPTO_crc16_finish (sum);
+      GNUNET_TUN_calculate_tcp6_checksum (pkt6,
+                                         pkt6_tcp,
+                                         payload,
+                                         payload_length);
     }
     break;
   default:
index 89cfdd2b3330b68ad31111e5a894a7a9df138784..b30b2b03a62468d662c00295994111717eb0714e 100644 (file)
@@ -181,5 +181,19 @@ GNUNET_TUN_initialize_ipv6_header (struct GNUNET_TUN_IPv6Header *ip,
                                   const struct in6_addr *src,
                                   const struct in6_addr *dst);
 
+/**
+ * Calculate IPv6 TCP checksum.
+ *
+ * @param ipv6 header fully initialized
+ * @param tcp header (initialized except for CRC)
+ * @param payload the TCP payload
+ * @param payload_length number of bytes of TCP payload
+ */
+void
+GNUNET_TUN_calculate_tcp6_checksum (const struct GNUNET_TUN_IPv6Header *ip,
+                                   struct GNUNET_TUN_TcpHeader *tcp,
+                                   const void *payload,
+                                   uint16_t payload_length);
+
 
 #endif
index 005c7013ddd1fdb6bc8a1c113b76190a83540c7c..c30861cce80578c34f1684898306f79b52c3cf39 100644 (file)
@@ -95,5 +95,37 @@ GNUNET_TUN_initialize_ipv6_header (struct GNUNET_TUN_IPv6Header *ip,
 }
 
 
+/**
+ * Calculate IPv6 TCP checksum.
+ *
+ * @param ipv6 header fully initialized
+ * @param tcp header (initialized except for CRC)
+ * @param payload the TCP payload
+ * @param payload_length number of bytes of TCP payload
+ */
+void
+GNUNET_TUN_calculate_tcp6_checksum (const struct GNUNET_TUN_IPv6Header *ip,
+                                   struct GNUNET_TUN_TcpHeader *tcp,
+                                   const void *payload,
+                                   uint16_t payload_length)
+{
+  uint32_t sum;
+  uint32_t tmp;
+
+  GNUNET_assert (payload_length + sizeof (struct GNUNET_TUN_IPv6Header) + sizeof (struct GNUNET_TUN_TcpHeader) ==
+                ntohs (ip->payload_length));
+  tcp->crc = 0;
+  sum = GNUNET_CRYPTO_crc16_step (0, &ip->source_address, 2 * sizeof (struct in6_addr));
+  tmp = htonl (sizeof (struct GNUNET_TUN_TcpHeader) + payload_length);
+  sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
+  tmp = htonl (IPPROTO_TCP);
+  sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
+  sum = GNUNET_CRYPTO_crc16_step (sum, tcp,
+                                 sizeof (struct GNUNET_TUN_TcpHeader));
+  sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length);
+  tcp->crc = GNUNET_CRYPTO_crc16_finish (sum);
+}
+
+
 
 /* end of tun.c */
index b352e216b1ff7520c201c0bafdc2f41bac3e1555..0d236975ef5b6b8a9103c3422cb7f4ff5d9b9ead 100644 (file)
@@ -1643,20 +1643,10 @@ receive_tcp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
                                           &ts->source_ip.v6);
        tcp->spt = htons (ts->destination_port);
        tcp->dpt = htons (ts->source_port);
-       tcp->crc = 0;
-       {
-         uint32_t sum = 0;
-         uint32_t tmp;
-
-         sum = GNUNET_CRYPTO_crc16_step (sum, &ipv6->source_address, 2 * sizeof (struct in6_addr));
-         tmp = htonl (sizeof (struct GNUNET_TUN_TcpHeader) + mlen);
-         sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
-         tmp = htonl (IPPROTO_TCP);
-         sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
-         sum = GNUNET_CRYPTO_crc16_step (sum, tcp,
-                                         sizeof (struct GNUNET_TUN_TcpHeader) + mlen);
-         tcp->crc = GNUNET_CRYPTO_crc16_finish (sum);
-       }
+       GNUNET_TUN_calculate_tcp6_checksum (ipv6,
+                                           tcp,
+                                           &tcp[1],
+                                           mlen);
        (void) GNUNET_HELPER_send (helper_handle,
                                   msg,
                                   GNUNET_YES,