- changes
[oweals/gnunet.git] / src / tun / tun.c
index 1ce6605dc1867c28e612c5641b79837d0433cac1..3b0e9b94ab79eaf3fbc69d7a82b99f401bc82a46 100644 (file)
@@ -49,8 +49,9 @@ GNUNET_TUN_initialize_ipv4_header (struct GNUNET_TUN_IPv4Header *ip,
                                   const struct in_addr *src,
                                   const struct in_addr *dst)
 {
+  GNUNET_assert (20 == sizeof (struct GNUNET_TUN_IPv4Header));
   GNUNET_assert (payload_length <= UINT16_MAX - sizeof (struct GNUNET_TUN_IPv4Header));
-  memset (&ip, 0, sizeof (struct GNUNET_TUN_IPv4Header));
+  memset (ip, 0, sizeof (struct GNUNET_TUN_IPv4Header));
   ip->header_length =  sizeof (struct GNUNET_TUN_IPv4Header) / 4;
   ip->version = 4;
   ip->total_length = htons (sizeof (struct GNUNET_TUN_IPv4Header) + payload_length);
@@ -80,8 +81,9 @@ GNUNET_TUN_initialize_ipv6_header (struct GNUNET_TUN_IPv6Header *ip,
                                   const struct in6_addr *src,
                                   const struct in6_addr *dst)
 {
+  GNUNET_assert (40 == sizeof (struct GNUNET_TUN_IPv6Header));
   GNUNET_assert (payload_length <= UINT16_MAX - sizeof (struct GNUNET_TUN_IPv6Header));
-  memset (&ip, 0, sizeof (struct GNUNET_TUN_IPv6Header));
+  memset (ip, 0, sizeof (struct GNUNET_TUN_IPv6Header));
   ip->version = 6;
   ip->next_header = protocol;
   ip->payload_length = htons ((uint16_t) payload_length);
@@ -106,8 +108,9 @@ GNUNET_TUN_calculate_tcp4_checksum (const struct GNUNET_TUN_IPv4Header *ip,
                                    uint16_t payload_length)
 {
   uint32_t sum;
-  uint32_t tmp;
+  uint16_t tmp;
 
+  GNUNET_assert (20 == sizeof (struct GNUNET_TUN_TcpHeader));
   GNUNET_assert (payload_length + sizeof (struct GNUNET_TUN_IPv4Header) + sizeof (struct GNUNET_TUN_TcpHeader) ==
                 ntohs (ip->total_length));
   GNUNET_assert (IPPROTO_TCP == ip->protocol);
@@ -116,8 +119,10 @@ GNUNET_TUN_calculate_tcp4_checksum (const struct GNUNET_TUN_IPv4Header *ip,
   sum = GNUNET_CRYPTO_crc16_step (0, 
                                  &ip->source_address,
                                  sizeof (struct in_addr) * 2);
-  tmp = htonl ((IPPROTO_TCP << 16) | (payload_length + sizeof (struct GNUNET_TUN_TcpHeader)));
-  sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
+  tmp = htons (IPPROTO_TCP);
+  sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint16_t));
+  tmp = htons (payload_length + sizeof (struct GNUNET_TUN_TcpHeader));
+  sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint16_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);
@@ -141,6 +146,7 @@ GNUNET_TUN_calculate_tcp6_checksum (const struct GNUNET_TUN_IPv6Header *ip,
   uint32_t sum;
   uint32_t tmp;
 
+  GNUNET_assert (20 == sizeof (struct GNUNET_TUN_TcpHeader));
   GNUNET_assert (payload_length + sizeof (struct GNUNET_TUN_TcpHeader) ==
                 ntohs (ip->payload_length));
   GNUNET_assert (IPPROTO_TCP == ip->next_header);
@@ -174,6 +180,7 @@ GNUNET_TUN_calculate_udp4_checksum (const struct GNUNET_TUN_IPv4Header *ip,
   uint32_t sum;
   uint16_t tmp;
 
+  GNUNET_assert (8 == sizeof (struct GNUNET_TUN_UdpHeader));
   GNUNET_assert (payload_length + sizeof (struct GNUNET_TUN_IPv4Header) + sizeof (struct GNUNET_TUN_UdpHeader) ==
                 ntohs (ip->total_length));
   GNUNET_assert (IPPROTO_UDP == ip->protocol);
@@ -251,6 +258,7 @@ GNUNET_TUN_calculate_icmp_checksum (struct GNUNET_TUN_IcmpHeader *icmp,
 {
   uint32_t sum;
 
+  GNUNET_assert (8 == sizeof (struct GNUNET_TUN_IcmpHeader));
   icmp->crc = 0;
   sum = GNUNET_CRYPTO_crc16_step (0,
                                  icmp,