From 1355ed0acd88356beccc25dfc710ef0d458b5b96 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 21 Aug 2010 22:34:17 +0000 Subject: [PATCH] cleaning --- src/transport/gnunet-nat-client-windows.c | 201 ++++++++++-------- src/transport/gnunet-nat-client.c | 31 +-- src/transport/gnunet-nat-server-windows.c | 236 ++++++++++++---------- src/transport/gnunet-nat-server.c | 15 +- 4 files changed, 263 insertions(+), 220 deletions(-) diff --git a/src/transport/gnunet-nat-client-windows.c b/src/transport/gnunet-nat-client-windows.c index aae487b76..3d3127157 100644 --- a/src/transport/gnunet-nat-client-windows.c +++ b/src/transport/gnunet-nat-client-windows.c @@ -70,7 +70,7 @@ /** * IPv4 header. */ -struct ip_packet +struct ip_header { /** @@ -101,12 +101,12 @@ struct ip_packet /** * Time to live */ - uint8_t ttl; + uint8_t ttl; /** * Protocol */ - uint8_t proto; + uint8_t proto; /** * Header checksum @@ -116,19 +116,19 @@ struct ip_packet /** * Source address */ - uint32_t src_ip; + uint32_t src_ip; /** * Destination address */ - uint32_t dst_ip; + uint32_t dst_ip; }; /** * Format of ICMP packet. */ -struct icmp_packet +struct icmp_ttl_exceeded_header { uint8_t type; @@ -136,28 +136,34 @@ struct icmp_packet uint16_t checksum; - uint32_t reserved; + uint32_t unused; + + /* followed by original payload */ }; -struct icmp_echo_packet +struct icmp_echo_header { uint8_t type; + uint8_t code; + uint16_t checksum; + uint32_t reserved; - uint32_t data; }; /** * Beginning of UDP packet. */ -struct udp_packet +struct udp_header { uint16_t src_port; uint16_t dst_port; - uint32_t length; + uint16_t length; + + uint16_t crc; }; @@ -237,13 +243,13 @@ static void send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) { - struct ip_packet ip_pkt; - struct icmp_packet icmp_pkt; - struct udp_packet udp_pkt; - + char packet[sizeof(struct ip_header) * 2 + + sizeof(struct icmp_ttl_exceeded_header) + + sizeof(struct udp_header)]; + struct ip_header ip_pkt; + struct icmp_ttl_exceeded_header icmp_pkt; + struct udp_header udp_pkt; struct sockaddr_in dst; - char packet[sizeof(ip_pkt) * 2 + sizeof(icmp_pkt) * 2 + sizeof(uint32_t)]; - size_t off; int err; @@ -259,27 +265,27 @@ send_icmp_udp (const struct in_addr *my_ip, ip_pkt.checksum = 0; ip_pkt.src_ip = my_ip->s_addr; ip_pkt.dst_ip = other->s_addr; - ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt))); - memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt)); - off += sizeof(ip_pkt); - - /* ip header of the presumably 'lost' udp packet */ - ip_pkt.vers_ihl = 0x45; - ip_pkt.tos = 0; - ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet)); - - icmp_pkt.type = 11; /* TTL exceeded */ + ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, + sizeof (struct ip_header))); + memcpy(&packet[off], + &ip_pkt, + sizeof(struct ip_header)); + off += sizeof(struct ip_header); + + icmp_pkt.type = ICMP_TIME_EXCEEDED; icmp_pkt.code = 0; icmp_pkt.checksum = 0; - icmp_pkt.reserved = 0; - memcpy(&packet[off], &icmp_pkt, sizeof(icmp_pkt)); - off += sizeof(icmp_pkt); + icmp_pkt.unused = 0; + memcpy(&packet[off], + &icmp_pkt, + sizeof(struct icmp_ttl_exceeded_header)); + off += sizeof(struct icmp_ttl_exceeded_header); - /* build inner IP header */ - memset(&ip_pkt, 0, sizeof(ip_pkt)); + /* ip header of the presumably 'lost' udp packet */ ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; - ip_pkt.pkt_len = htons(sizeof (ip_pkt) + sizeof(udp_pkt)); + ip_pkt.pkt_len = htons(sizeof (struct ip_header) + + sizeof (struct udp_header)); ip_pkt.id = htons(0); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = 128; @@ -287,25 +293,31 @@ send_icmp_udp (const struct in_addr *my_ip, ip_pkt.checksum = 0; ip_pkt.src_ip = other->s_addr; ip_pkt.dst_ip = dummy.s_addr; - ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt))); - memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt)); - off += sizeof(ip_pkt); + ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, + sizeof (struct ip_header))); + memcpy(&packet[off], + &ip_pkt, + sizeof(struct ip_header)); + off += sizeof(struct ip_header); /* build UDP header */ udp_pkt.src_port = htons(NAT_TRAV_PORT); - udp_pkt.dst_port = htons(NAT_TRAV_PORT); - - memset(&udp_pkt.length, 0, sizeof(uint32_t)); + udp_pkt.dst_port = htons(NAT_TRAV_PORT); udp_pkt.length = htons (port); - memcpy(&packet[off], &udp_pkt, sizeof(udp_pkt)); - off += sizeof(udp_pkt); + udp_pkt.crc = 0; + memcpy(&packet[off], + &udp_pkt, + sizeof(struct udp_header)); + off += sizeof(struct udp_header); /* no go back to calculate ICMP packet checksum */ - off = sizeof (ip_pkt); icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[off], - sizeof (icmp_pkt) + sizeof(ip_pkt) + sizeof(udp_pkt))); - memcpy (&packet[off], &icmp_pkt, sizeof (icmp_pkt)); - + sizeof (struct icmp_ttl_exceeded_header) + + sizeof (struct ip_header) + + sizeof (struct udp_header))); + memcpy (&packet[sizeof (struct ip_header)], + &icmp_pkt, + sizeof (struct icmp_ttl_exceeded_header)); memset (&dst, 0, sizeof (dst)); dst.sin_family = AF_INET; @@ -315,13 +327,12 @@ send_icmp_udp (const struct in_addr *my_ip, sizeof (packet), 0, (struct sockaddr*)&dst, sizeof(dst)); - if (err < 0) { fprintf(stderr, "sendto failed: %s\n", strerror(errno)); } - else if (err != sizeof (packet)) + else if (sizeof (packet) != (size_t) err) { fprintf(stderr, "Error: partial send of ICMP message\n"); @@ -339,61 +350,62 @@ static void send_icmp (const struct in_addr *my_ip, const struct in_addr *other) { - struct ip_packet ip_pkt; - struct icmp_packet *icmp_pkt; - struct icmp_echo_packet icmp_echo; + struct ip_header ip_pkt; + struct icmp_ttl_exceeded_header icmp_ttl; + struct icmp_echo_header icmp_echo; struct sockaddr_in dst; - char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet) + sizeof(struct icmp_echo_packet)]; - + char packet[sizeof (struct ip_header) * 2 + + sizeof (struct icmp_ttl_exceeded_header) + + sizeof(struct icmp_echo_header)]; size_t off; int err; - memset(packet, 0, sizeof(packet)); /* ip header: send to (known) ip address */ off = 0; - memset(&ip_pkt, 0, sizeof(ip_pkt)); ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; - ip_pkt.pkt_len = sizeof (packet); /* huh? */ - ip_pkt.id = 1; + ip_pkt.pkt_len = htons (sizeof (packet)); + ip_pkt.id = htons(256); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = IPDEFTTL; ip_pkt.proto = IPPROTO_ICMP; ip_pkt.checksum = 0; ip_pkt.src_ip = my_ip->s_addr; ip_pkt.dst_ip = other->s_addr; - ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet))); - - memcpy (packet, &ip_pkt, sizeof (struct ip_packet)); + ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, + sizeof (struct ip_header))); + memcpy (&packet[off], + &ip_pkt, + sizeof (struct ip_header)); off += sizeof (ip_pkt); - memset (&dst, 0, sizeof (dst)); - dst.sin_family = AF_INET; - dst.sin_addr = *other; /* icmp reply: time exceeded */ - icmp_pkt = (struct icmp_packet*) &packet[off]; - memset(icmp_pkt, 0, sizeof(struct icmp_packet)); - icmp_pkt->type = ICMP_TIME_EXCEEDED; - icmp_pkt->code = 0; - icmp_pkt->reserved = 0; - icmp_pkt->checksum = 0; - - off += sizeof (struct icmp_packet); + icmp_ttl.type = ICMP_TIME_EXCEEDED; + icmp_ttl.code = 0; + icmp_ttl.reserved = 0; + icmp_ttl.checksum = 0; + memcpy (&packet[off], + &icmp_ttl, + sizeof (struct icmp_ttl_exceeded_header)); + off += sizeof (struct icmp_ttl_exceeded_header); /* ip header of the presumably 'lost' udp packet */ ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; - ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet)); - ip_pkt.id = 1; + ip_pkt.pkt_len = (sizeof (struct ip_header) + sizeof (struct icmp_echo_header)); + ip_pkt.id = htons (256); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */ ip_pkt.proto = IPPROTO_ICMP; ip_pkt.src_ip = other->s_addr; ip_pkt.dst_ip = dummy.s_addr; ip_pkt.checksum = 0; - ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet))); - memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet)); - off += sizeof (struct ip_packet); + ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, + sizeof (struct ip_header))); + memcpy (&packet[off], + &ip_pkt, + sizeof (struct ip_header)); + off += sizeof (struct ip_header); icmp_echo.type = ICMP_ECHO; icmp_echo.code = 0; @@ -401,26 +413,37 @@ send_icmp (const struct in_addr *my_ip, icmp_echo.checksum = 0; icmp_echo.data = htons(port); icmp_echo.checksum = htons(calc_checksum((uint16_t*) &icmp_echo, - sizeof (struct icmp_echo_packet))); + sizeof (struct icmp_echo_header))); + memcpy (&packet[off], + &icmp_echo, + sizeof(struct icmp_echo_header)); - memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_echo_packet)); - off += sizeof (struct icmp_echo_packet); + /* no go back to calculate ICMP packet checksum */ + off = sizeof (struct ip_header); + icmp_ttl.checksum = htons(calc_checksum((uint16_t*) &packet[off], + sizeof (struct icmp_ttl_exceeded_header) + + sizeof (struct ip_header) + + sizeof (struct icmp_echo_header))); + memcpy (&packet[off], + &icmp_ttl, + sizeof (struct icmp_ttl_exceeded_header)); - icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt, - sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet))); + memset (&dst, 0, sizeof (dst)); + dst.sin_family = AF_INET; + dst.sin_addr = *other; err = sendto(rawsock, - &packet[0], - off, 0, + packet, + sizeof (packet), 0, (struct sockaddr*)&dst, - sizeof(dst)); /* or sizeof 'struct sockaddr'? */ + sizeof(dst)); if (err < 0) { fprintf(stderr, "sendto failed: %s\n", strerror(errno)); } - else if (err != off) + else if (sizeof (packet) != (size_t) err) { fprintf(stderr, "Error: partial send of ICMP message\n"); @@ -448,7 +471,7 @@ make_raw_socket () strerror (errno)); return INVALID_SOCKET; } - if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen) != 0) + if (0 != setsockopt(ret, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen)) { fprintf(stderr, "Error setting SO_BROADCAST to ON: %s\n", @@ -457,7 +480,7 @@ make_raw_socket () return INVALID_SOCKET; } - if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL, (char*)&bOptVal, bOptLen) != 0) + if (0 != setsockopt(ret, IPPROTO_IP, IP_HDRINCL, (char*)&bOptVal, bOptLen)) { fprintf(stderr, "Error setting IP_HDRINCL to ON: %s\n", @@ -493,8 +516,8 @@ main (int argc, char *const *argv) return 1; } if ( (1 != sscanf (argv[3], "%u", &p) ) || - (p == 0) || - (p > 0xFFFF) ) + (0 == p) || + (0xFFFF < p) ) { fprintf (stderr, "Error parsing port value `%s'\n", @@ -503,7 +526,7 @@ main (int argc, char *const *argv) } port = (uint16_t) p; - if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0) + if (0 != WSAStartup (MAKEWORD (2, 1), &wsaData)) { fprintf (stderr, "Failed to find Winsock 2.1 or better.\n"); return 2; diff --git a/src/transport/gnunet-nat-client.c b/src/transport/gnunet-nat-client.c index 0ab29dff8..bff86cc93 100644 --- a/src/transport/gnunet-nat-client.c +++ b/src/transport/gnunet-nat-client.c @@ -228,7 +228,7 @@ send_icmp_udp (const struct in_addr *my_ip, off = 0; ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; - ip_pkt.pkt_len = htons(sizeof (packet)); + ip_pkt.pkt_len = htons (sizeof (packet)); ip_pkt.id = htons(256); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = 128; @@ -256,7 +256,7 @@ send_icmp_udp (const struct in_addr *my_ip, ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; ip_pkt.pkt_len = htons(sizeof (struct ip_header) + - sizeof(struct udp_header)); + sizeof (struct udp_header)); ip_pkt.id = htons(0); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = 128; @@ -289,6 +289,7 @@ send_icmp_udp (const struct in_addr *my_ip, memcpy (&packet[sizeof(struct ip_header)], &icmp_pkt, sizeof (struct icmp_ttl_exceeded_header)); + memset (&dst, 0, sizeof (dst)); dst.sin_family = AF_INET; #if HAVE_SOCKADDR_IN_SIN_LEN @@ -297,7 +298,7 @@ send_icmp_udp (const struct in_addr *my_ip, dst.sin_addr = *other; err = sendto(rawsock, packet, - off, 0, + sizeof (packet), 0, (struct sockaddr*)&dst, sizeof(dst)); if (err < 0) @@ -305,7 +306,7 @@ send_icmp_udp (const struct in_addr *my_ip, fprintf(stderr, "sendto failed: %s\n", strerror(errno)); } - else if (off != (size_t) err) + else if (sizeof (packet) != (size_t) err) { fprintf(stderr, "Error: partial send of ICMP message\n"); @@ -337,8 +338,8 @@ send_icmp (const struct in_addr *my_ip, off = 0; ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; - ip_pkt.pkt_len = sizeof (packet); - ip_pkt.id = 1; + ip_pkt.pkt_len = htons (sizeof (packet)); + ip_pkt.id = htons (256); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = IPDEFTTL; ip_pkt.proto = IPPROTO_ICMP; @@ -365,8 +366,8 @@ send_icmp (const struct in_addr *my_ip, /* ip header of the presumably 'lost' udp packet */ ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; - ip_pkt.pkt_len = (sizeof (struct ip_header) + sizeof (struct icmp_echo_header)); - ip_pkt.id = 1; + ip_pkt.pkt_len = htons (sizeof (struct ip_header) + sizeof (struct icmp_echo_header)); + ip_pkt.id = htons (256); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */ ip_pkt.proto = IPPROTO_ICMP; @@ -394,8 +395,8 @@ send_icmp (const struct in_addr *my_ip, off = sizeof (struct ip_header); icmp_ttl.checksum = htons(calc_checksum((uint16_t*) &packet[off], sizeof (struct icmp_ttl_exceeded_header) + - sizeof(struct ip_header) + - sizeof(struct icmp_echo_header))); + sizeof (struct ip_header) + + sizeof (struct icmp_echo_header))); memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header)); @@ -411,7 +412,7 @@ send_icmp (const struct in_addr *my_ip, packet, sizeof (packet), 0, (struct sockaddr*)&dst, - sizeof(struct sockaddr_in)); + sizeof(dst)); if (err < 0) { fprintf(stderr, @@ -444,8 +445,8 @@ make_raw_socket () strerror (errno)); return -1; } - if (-1 == setsockopt(ret, SOL_SOCKET, SO_BROADCAST, - (char *)&one, sizeof(one))) + if (0 != setsockopt(ret, SOL_SOCKET, SO_BROADCAST, + (char *)&one, sizeof(one))) { fprintf(stderr, "setsockopt failed: %s\n", @@ -453,8 +454,8 @@ make_raw_socket () close (ret); return -1; } - if (-1 == setsockopt(ret, IPPROTO_IP, IP_HDRINCL, - (char *)&one, sizeof(one))) + if (0 != setsockopt(ret, IPPROTO_IP, IP_HDRINCL, + (char *)&one, sizeof(one))) { fprintf(stderr, "setsockopt failed: %s\n", diff --git a/src/transport/gnunet-nat-server-windows.c b/src/transport/gnunet-nat-server-windows.c index bb135ed08..11c63aa9d 100644 --- a/src/transport/gnunet-nat-server-windows.c +++ b/src/transport/gnunet-nat-server-windows.c @@ -72,7 +72,7 @@ #define ICMP_ECHO 8 -#define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */ +#define ICMP_TIME_EXCEEDED 11 /** * How often do we send our ICMP messages to receive replies? @@ -82,7 +82,7 @@ /** * IPv4 header. */ -struct ip_packet +struct ip_header { /** @@ -139,7 +139,20 @@ struct ip_packet /** * Format of ICMP packet. */ -struct icmp_packet +struct icmp_ttl_exceeded_header +{ + uint8_t type; + + uint8_t code; + + uint16_t checksum; + + uint32_t unused; + + /* followed by original payload */ +}; + +struct icmp_echo_header { uint8_t type; @@ -153,13 +166,15 @@ struct icmp_packet /** * Beginning of UDP packet. */ -struct udp_packet +struct udp_header { uint16_t src_port; uint16_t dst_port; - uint32_t length; + uint16_t length; + + uint16_t crc; }; /** @@ -240,19 +255,17 @@ inet_pton (int af, static void send_icmp_echo (const struct in_addr *my_ip) { - struct icmp_packet icmp_echo; + char packet[sizeof (struct ip_header) + sizeof (struct icmp_echo_header)]; + struct icmp_ttl_exceeded_header icmp_echo; + struct ip_header ip_pkt; struct sockaddr_in dst; size_t off; int err; - struct ip_packet ip_pkt; - struct icmp_packet icmp_pkt; - char packet[sizeof (ip_pkt) + sizeof (icmp_pkt)]; off = 0; - memset(&ip_pkt, 0, sizeof(ip_pkt)); ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; - ip_pkt.pkt_len = sizeof (packet); + ip_pkt.pkt_len = htons (sizeof (packet)); ip_pkt.id = htons (256); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = IPDEFTTL; @@ -260,18 +273,23 @@ send_icmp_echo (const struct in_addr *my_ip) ip_pkt.checksum = 0; ip_pkt.src_ip = my_ip->s_addr; ip_pkt.dst_ip = dummy.s_addr; - ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt))); - memcpy (&packet[off], &ip_pkt, sizeof (ip_pkt)); - off += sizeof (ip_pkt); + ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, + sizeof (struct ip_header))); + memcpy (&packet[off], + &ip_pkt, + sizeof (struct ip_header)); + off += sizeof (struct ip_header); icmp_echo.type = ICMP_ECHO; icmp_echo.code = 0; icmp_echo.reserved = 0; icmp_echo.checksum = 0; icmp_echo.checksum = htons(calc_checksum((uint16_t*) &icmp_echo, - sizeof (struct icmp_packet))); - memcpy (&packet[off], &icmp_echo, sizeof (icmp_echo)); - off += sizeof (icmp_echo); + sizeof (struct icmp_echo_header))); + memcpy (&packet[off], + &icmp_echo, + sizeof (struct icmp_echo_header)); + off += sizeof (struct icmp_echo_header); memset (&dst, 0, sizeof (dst)); dst.sin_family = AF_INET; @@ -297,11 +315,9 @@ send_icmp_echo (const struct in_addr *my_ip) /** * Send a UDP message to the dummy IP. - * - * @param my_ip source address (our ip address) */ static void -send_udp (const struct in_addr *my_ip) +send_udp () { struct sockaddr_in dst; ssize_t err; @@ -321,7 +337,7 @@ send_udp (const struct in_addr *my_ip) "sendto failed: %s\n", strerror(errno)); #endif } - else if (err != 0) + else if (0 != err) { fprintf(stderr, "Error: partial send of ICMP message\n"); @@ -337,13 +353,14 @@ process_icmp_response () { char buf[65536]; ssize_t have; - struct in_addr sip; - struct ip_packet ip_pkt; - struct icmp_packet icmp_pkt; - struct udp_packet udp_pkt; + struct in_addr source_ip; + struct ip_header ip_pkt; + struct icmp_ttl_exceeded_header icmp_pkt; + struct icmp_echo_header icmp_echo; + struct udp_header udp_pkt; size_t off; - int have_port; - uint32_t port; + uint16_t port; + DWORD ssize; have = read (icmpsock, buf, sizeof (buf)); if (have == -1) @@ -359,82 +376,85 @@ process_icmp_response () "Received message of %u bytes\n", (unsigned int) have); #endif - if (have == sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2 + sizeof(uint32_t)) + if (have < (ssize_t) (sizeof (struct ip_header) + sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct ip_header))) { - have_port = 1; - } - else if (have != sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2) - { -#if VERBOSE - fprintf (stderr, - "Received ICMP message of unexpected size: %u bytes\n", - (unsigned int) have); -#endif + /* malformed */ return; } off = 0; - memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt)); - off += sizeof (ip_pkt); - memcpy (&icmp_pkt, &buf[off], sizeof (icmp_pkt)); - off += sizeof (icmp_pkt); - if ( ((ip_pkt.proto != IPPROTO_ICMP) && (ip_pkt.proto != IPPROTO_UDP)) || - (icmp_pkt.type != ICMP_TIME_EXCEEDED) || - (icmp_pkt.code != 0) ) - { - /* maybe we got an actual reply back... */ - return; - } - memcpy(&sip, + memcpy (&ip_pkt, + &buf[off], + sizeof (struct ip_header)); + off += sizeof (struct ip_header); + memcpy(&source_ip, &ip_pkt.src_ip, - sizeof (sip)); - memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt)); - off += sizeof (ip_pkt); - - if (have_port) + sizeof (source_ip)); + memcpy (&icmp_ttl, + &buf[off], + sizeof (struct icmp_ttl_exceeded_header)); + off += sizeof (struct icmp_ttl_exceeded_header); + if ( (ICMP_TIME_EXCEEDED != icmp_ttl.type) || + (0 != icmp_ttl.code) ) { - memcpy(&port, - &buf[sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2], - sizeof(uint32_t)); - port = ntohs(port); - DWORD ssize = sizeof(buf); - WSAAddressToString((LPSOCKADDR)&sip, - sizeof(sip), - NULL, - buf, - &ssize); - fprintf (stdout, - "%s:%d\n", - buf, - port); + /* different type than what we want */ + return; } - else if (ip_pkt.proto == IPPROTO_UDP) + /* skip 2nd IP header */ + memcpy (&ip_pkt, + &buf[off], + sizeof (struct ip_header)); + off += sizeof (struct ip_header); + + switch (ip_pkt.proto) { - memcpy(&udp_pkt, - &buf[off], - sizeof(udp_pkt)); - DWORD ssize = sizeof(buf); - WSAAddressToString((LPSOCKADDR)&sip, - sizeof(sip), - NULL, - buf, - &ssize); - fprintf (stdout, - "%s:%d\n", - buf, - ntohs((uint16_t)udp_pkt.length)); + case IPPROTO_ICMP: + if (have != (sizeof (struct ip_header) * 2 + + sizeof (struct icmp_ttl_exceeded_header) + + sizeof (struct icmp_echo_header)) ) + { + /* malformed */ + return; + } + /* grab ICMP ECHO content */ + memcpy (&icmp_echo, + &buf[off], + sizeof (struct icmp_echo_header)); + port = (uint16_t) ntohl (icmp_echo.reserved); + break; + case IPPROTO_UDP: + if (have != (sizeof (struct ip_header) * 2 + + sizeof (struct icmp_ttl_exceeded_header) + + sizeof (struct udp_header)) ) + { + /* malformed */ + return; + } + /* grab UDP content */ + memcpy (&udp_pkt, + &buf[off], + sizeof (struct udp_header)); + port = ntohs (udp_pkt.length); + break; + default: + /* different type than what we want */ + return; } + + ssize = sizeof(buf); + WSAAddressToString((LPSOCKADDR)&source_ip, + sizeof(source_ip), + NULL, + buf, + &ssize); + if (port == 0) + fprintf (stdout, + "%s\n", + buf); else - { - DWORD ssize = sizeof(buf); - WSAAddressToString((LPSOCKADDR)&sip, - sizeof(sip), - NULL, - buf, - &ssize); - fprintf (stdout, - "%s\n", - buf); - } + fprintf (stdout, + "%s:%u\n", + buf, + (unsigned int) port); fflush (stdout); } @@ -481,10 +501,10 @@ make_raw_socket () return INVALID_SOCKET; } - if (setsockopt(rawsock, - SOL_SOCKET, - SO_BROADCAST, - (char*)&bOptVal, bOptLen) != 0) + if (0 != setsockopt(rawsock, + SOL_SOCKET, + SO_BROADCAST, + (char*)&bOptVal, bOptLen)) { fprintf(stderr, "Error setting SO_BROADCAST to ON: %s\n", @@ -492,10 +512,10 @@ make_raw_socket () closesocket(rawsock); return INVALID_SOCKET; } - if (setsockopt(rawsock, - IPPROTO_IP, - IP_HDRINCL, - (char*)&bOptVal, bOptLen) != 0) + if (0 != setsockopt(rawsock, + IPPROTO_IP, + IP_HDRINCL, + (char*)&bOptVal, bOptLen)) { fprintf(stderr, "Error setting IP_HDRINCL to ON: %s\n", @@ -510,10 +530,11 @@ make_raw_socket () /** * Create a UDP socket for writinging. * - * @return -1 on error + * @param my_ip source address (our ip address) + * @return INVALID_SOCKET on error */ static SOCKET -make_udp_socket () +make_udp_socket (const struct in_addr *my_ip) { SOCKET ret; struct sockaddr_in addr; @@ -528,9 +549,8 @@ make_udp_socket () } memset (&addr, 0, sizeof (addr)); addr.sin_family = AF_INET; - /* addr.sin_addr zero == ours (hopefully...) */ + addr.sin_addr = *my_ip; addr.sin_port = htons (NAT_TRAV_PORT); - if (0 != bind (ret, &addr, sizeof(addr))) @@ -555,7 +575,7 @@ main (int argc, WSADATA wsaData; unsigned int alt; - if (argc != 2) + if (2 != argc) { fprintf (stderr, "This program must be started with our (internal NAT) IP as the only argument.\n"); @@ -588,7 +608,7 @@ main (int argc, closesocket (icmpsock); return 3; } - if (INVALID_SOCKET == (udpsock = make_udp_socket())) + if (INVALID_SOCKET == (udpsock = make_udp_socket(&external))) { closesocket (icmpsock); closesocket (rawsock); @@ -614,7 +634,7 @@ main (int argc, if (0 == (++alt % 2)) send_icmp_echo (&external); else - send_udp (&external); + send_udp (); } /* select failed (internal error or OS out of resources) */ closesocket(icmpsock); diff --git a/src/transport/gnunet-nat-server.c b/src/transport/gnunet-nat-server.c index 905a08337..5cedb35e3 100644 --- a/src/transport/gnunet-nat-server.c +++ b/src/transport/gnunet-nat-server.c @@ -244,8 +244,8 @@ send_icmp_echo (const struct in_addr *my_ip) off = 0; ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; - ip_pkt.pkt_len = sizeof (packet); - ip_pkt.id = 1; + ip_pkt.pkt_len = htons (sizeof (packet)); + ip_pkt.id = htons (256); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = IPDEFTTL; ip_pkt.proto = IPPROTO_ICMP; @@ -279,7 +279,7 @@ send_icmp_echo (const struct in_addr *my_ip) err = sendto(rawsock, packet, off, 0, (struct sockaddr*)&dst, - sizeof(struct sockaddr_in)); + sizeof(dst)); if (err < 0) { #if VERBOSE @@ -297,8 +297,6 @@ send_icmp_echo (const struct in_addr *my_ip) /** * Send a UDP message to the dummy IP. - * - * @param my_ip source address (our ip address) */ static void send_udp () @@ -346,7 +344,7 @@ process_icmp_response () struct icmp_echo_header icmp_echo; struct udp_header udp_pkt; size_t off; - uint32_t port; + uint16_t port; have = read (icmpsock, buf, sizeof (buf)); if (-1 == have) @@ -434,12 +432,12 @@ process_icmp_response () sizeof (buf))); else fprintf (stdout, - "%s:%d\n", + "%s:%u\n", inet_ntop (AF_INET, &source_ip, buf, sizeof (buf)), - port); + (unsigned int) port); fflush (stdout); } @@ -523,6 +521,7 @@ make_raw_socket () /** * Create a UDP socket for writinging. * + * @param my_ip source address (our ip address) * @return -1 on error */ static int -- 2.25.1