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,
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:
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
}
+/**
+ * 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 */
&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,