X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Ftun%2Ftun.c;h=b55de1ea9487f6c15e2a9a02df3dd414972cc3f7;hb=a015c3df2059a98a8692e7f2b06038e066bbc916;hp=3b0e9b94ab79eaf3fbc69d7a82b99f401bc82a46;hpb=ec9338c28ca046638191baba9a7cc05a7a4dd28d;p=oweals%2Fgnunet.git diff --git a/src/tun/tun.c b/src/tun/tun.c index 3b0e9b94a..b55de1ea9 100644 --- a/src/tun/tun.c +++ b/src/tun/tun.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - (C) 2010, 2011, 2012 Christian Grothoff + Copyright (C) 2010, 2011, 2012 Christian Grothoff GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -14,8 +14,8 @@ You should have received a copy of the GNU General Public License along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. */ /** @@ -55,7 +55,7 @@ GNUNET_TUN_initialize_ipv4_header (struct GNUNET_TUN_IPv4Header *ip, ip->header_length = sizeof (struct GNUNET_TUN_IPv4Header) / 4; ip->version = 4; ip->total_length = htons (sizeof (struct GNUNET_TUN_IPv4Header) + payload_length); - ip->identification = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, + ip->identification = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 65536); ip->ttl = FRESH_TTL; ip->protocol = protocol; @@ -70,7 +70,7 @@ GNUNET_TUN_initialize_ipv4_header (struct GNUNET_TUN_IPv4Header *ip, * * @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 payload_length number of bytes of payload that follow (excluding IPv6 header) * @param src source IP address to use * @param dst destination IP address to use */ @@ -89,7 +89,7 @@ GNUNET_TUN_initialize_ipv6_header (struct GNUNET_TUN_IPv6Header *ip, ip->payload_length = htons ((uint16_t) payload_length); ip->hop_limit = FRESH_TTL; ip->destination_address = *dst; - ip->source_address = *src; + ip->source_address = *src; } @@ -116,7 +116,7 @@ GNUNET_TUN_calculate_tcp4_checksum (const struct GNUNET_TUN_IPv4Header *ip, GNUNET_assert (IPPROTO_TCP == ip->protocol); tcp->crc = 0; - sum = GNUNET_CRYPTO_crc16_step (0, + sum = GNUNET_CRYPTO_crc16_step (0, &ip->source_address, sizeof (struct in_addr) * 2); tmp = htons (IPPROTO_TCP); @@ -186,21 +186,21 @@ GNUNET_TUN_calculate_udp4_checksum (const struct GNUNET_TUN_IPv4Header *ip, GNUNET_assert (IPPROTO_UDP == ip->protocol); udp->crc = 0; /* technically optional, but we calculate it anyway, just to be sure */ - sum = GNUNET_CRYPTO_crc16_step (0, + sum = GNUNET_CRYPTO_crc16_step (0, &ip->source_address, sizeof (struct in_addr) * 2); tmp = htons (IPPROTO_UDP); - sum = GNUNET_CRYPTO_crc16_step (sum, - &tmp, + sum = GNUNET_CRYPTO_crc16_step (sum, + &tmp, sizeof (uint16_t)); tmp = htons (sizeof (struct GNUNET_TUN_UdpHeader) + payload_length); - sum = GNUNET_CRYPTO_crc16_step (sum, - &tmp, + sum = GNUNET_CRYPTO_crc16_step (sum, + &tmp, sizeof (uint16_t)); - sum = GNUNET_CRYPTO_crc16_step (sum, - udp, + sum = GNUNET_CRYPTO_crc16_step (sum, + udp, sizeof (struct GNUNET_TUN_UdpHeader)); - sum = GNUNET_CRYPTO_crc16_step (sum, + sum = GNUNET_CRYPTO_crc16_step (sum, payload, payload_length); udp->crc = GNUNET_CRYPTO_crc16_finish (sum); @@ -268,4 +268,44 @@ GNUNET_TUN_calculate_icmp_checksum (struct GNUNET_TUN_IcmpHeader *icmp, } +/** + * Check if two sockaddrs are equal. + * + * @param sa one address + * @param sb another address + * @param include_port also check ports + * @return #GNUNET_YES if they are equal + */ +int +GNUNET_TUN_sockaddr_cmp (const struct sockaddr *sa, + const struct sockaddr *sb, + int include_port) +{ + if (sa->sa_family != sb->sa_family) + return GNUNET_NO; + + switch (sa->sa_family) + { + case AF_INET: + { + const struct sockaddr_in *sa4 = (const struct sockaddr_in *) sa; + const struct sockaddr_in *sb4 = (const struct sockaddr_in *) sb; + return (sa4->sin_addr.s_addr == sb4->sin_addr.s_addr); + } + case AF_INET6: + { + const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *) sa; + const struct sockaddr_in6 *sb6 = (const struct sockaddr_in6 *) sb; + + return (0 == memcmp(&sa6->sin6_addr, + &sb6->sin6_addr, + sizeof (struct in6_addr))); + } + default: + GNUNET_break (0); + return GNUNET_SYSERR; + } +} + + /* end of tun.c */