gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r12706 - gnunet/src/transport


From: gnunet
Subject: [GNUnet-SVN] r12706 - gnunet/src/transport
Date: Sun, 22 Aug 2010 00:34:17 +0200

Author: grothoff
Date: 2010-08-22 00:34:17 +0200 (Sun, 22 Aug 2010)
New Revision: 12706

Modified:
   gnunet/src/transport/gnunet-nat-client-windows.c
   gnunet/src/transport/gnunet-nat-client.c
   gnunet/src/transport/gnunet-nat-server-windows.c
   gnunet/src/transport/gnunet-nat-server.c
Log:
cleaning

Modified: gnunet/src/transport/gnunet-nat-client-windows.c
===================================================================
--- gnunet/src/transport/gnunet-nat-client-windows.c    2010-08-21 22:04:27 UTC 
(rev 12705)
+++ gnunet/src/transport/gnunet-nat-client-windows.c    2010-08-21 22:34:17 UTC 
(rev 12706)
@@ -70,7 +70,7 @@
 /**
  * IPv4 header.
  */
-struct ip_packet 
+struct ip_header 
 {
 
   /**
@@ -101,12 +101,12 @@
   /**
    * Time to live
    */
-  uint8_t  ttl;   
+  uint8_t ttl;   
 
   /**
    * Protocol       
    */
-  uint8_t  proto; 
+  uint8_t proto; 
 
   /**
    * Header checksum
@@ -116,19 +116,19 @@
   /**
    * 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 @@
 
   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 @@
 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 @@
   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_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);
 
-  /* 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 */
+  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 @@
   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 @@
                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 @@
 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;
+  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);
 
-  off += sizeof (struct icmp_packet);
-
   /* 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 @@
   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 @@
               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 @@
       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 @@
       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 @@
     }
   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;

Modified: gnunet/src/transport/gnunet-nat-client.c
===================================================================
--- gnunet/src/transport/gnunet-nat-client.c    2010-08-21 22:04:27 UTC (rev 
12705)
+++ gnunet/src/transport/gnunet-nat-client.c    2010-08-21 22:34:17 UTC (rev 
12706)
@@ -228,7 +228,7 @@
   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 @@
   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 @@
   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 @@
   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 @@
       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 @@
   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 @@
   /* 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 @@
   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 @@
               packet, 
               sizeof (packet), 0, 
               (struct sockaddr*)&dst, 
-              sizeof(struct sockaddr_in));
+              sizeof(dst));
   if (err < 0) 
     {
       fprintf(stderr,
@@ -444,8 +445,8 @@
               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 @@
       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",

Modified: gnunet/src/transport/gnunet-nat-server-windows.c
===================================================================
--- gnunet/src/transport/gnunet-nat-server-windows.c    2010-08-21 22:04:27 UTC 
(rev 12705)
+++ gnunet/src/transport/gnunet-nat-server-windows.c    2010-08-21 22:34:17 UTC 
(rev 12706)
@@ -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,7 @@
 /**
  * Format of ICMP packet.
  */
-struct icmp_packet 
+struct icmp_ttl_exceeded_header 
 {
   uint8_t type;
 
@@ -147,19 +147,34 @@
 
   uint16_t checksum;
 
+  uint32_t unused;
+
+  /* followed by original payload */
+};
+
+struct icmp_echo_header
+{
+  uint8_t type;
+
+  uint8_t code;
+
+  uint16_t checksum;
+
   uint32_t reserved;
 };
 
 /**
  * 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 @@
 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 @@
   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 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 @@
              "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 @@
 {
   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 @@
            "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) )
+  memcpy (&ip_pkt,
+         &buf[off], 
+         sizeof (struct ip_header));
+  off += sizeof (struct ip_header);
+  memcpy(&source_ip, 
+        &ip_pkt.src_ip, 
+        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) )
     {
-      /* maybe we got an actual reply back... */
-      return;    
+      /* different type than what we want */
+      return;
     }
-  memcpy(&sip, 
-        &ip_pkt.src_ip, 
-        sizeof (sip));
-  memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
-  off += sizeof (ip_pkt);
+  /* skip 2nd IP header */
+  memcpy (&ip_pkt,
+         &buf[off], 
+         sizeof (struct ip_header));
+  off += sizeof (struct ip_header);
 
-  if (have_port)
+  switch (ip_pkt.proto)
     {
-      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);
+    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;
     }
-  else if (ip_pkt.proto == IPPROTO_UDP)
-    {
-      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));
-    }
+  
+  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 @@
       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 @@
       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 @@
 /**
  * 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 @@
     }
   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 @@
   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 @@
       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 @@
       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);

Modified: gnunet/src/transport/gnunet-nat-server.c
===================================================================
--- gnunet/src/transport/gnunet-nat-server.c    2010-08-21 22:04:27 UTC (rev 
12705)
+++ gnunet/src/transport/gnunet-nat-server.c    2010-08-21 22:34:17 UTC (rev 
12706)
@@ -244,8 +244,8 @@
   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 @@
   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 a UDP message to the dummy IP.
- *
- * @param my_ip source address (our ip address)
  */
 static void
 send_udp ()
@@ -346,7 +344,7 @@
   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 @@
                        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 @@
 /**
  * Create a UDP socket for writinging.
  *
+ * @param my_ip source address (our ip address)
  * @return -1 on error
  */
 static int




reply via email to

[Prev in Thread] Current Thread [Next in Thread]