gnunet-svn
[Top][All Lists]
Advanced

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

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


From: gnunet
Subject: [GNUnet-SVN] r12701 - gnunet/src/transport
Date: Sat, 21 Aug 2010 23:44:01 +0200

Author: grothoff
Date: 2010-08-21 23:44:01 +0200 (Sat, 21 Aug 2010)
New Revision: 12701

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

Modified: gnunet/src/transport/gnunet-nat-client.c
===================================================================
--- gnunet/src/transport/gnunet-nat-client.c    2010-08-21 17:05:52 UTC (rev 
12700)
+++ gnunet/src/transport/gnunet-nat-client.c    2010-08-21 21:44:01 UTC (rev 
12701)
@@ -70,7 +70,7 @@
 /**
  * IPv4 header.
  */
-struct ip_packet 
+struct ip_header 
 {
 
   /**
@@ -127,7 +127,7 @@
 /**
  * Format of ICMP packet.
  */
-struct icmp_packet 
+struct icmp_ttl_exceeded_header 
 {
   uint8_t type;
 
@@ -135,10 +135,12 @@
 
   uint16_t checksum;
 
-  uint32_t reserved;
+  uint32_t unused;
+
+  /* followed by original payload */
 };
 
-struct icmp_echo_packet
+struct icmp_echo_header
 {
   uint8_t type;
 
@@ -147,20 +149,20 @@
   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;
 };
 
 /**
@@ -187,8 +189,8 @@
  * @return the CRC 16.
  */
 static uint16_t 
-calc_checksum(const uint16_t *data, 
-             unsigned int bytes)
+calc_checksum (const uint16_t *data, 
+              unsigned int bytes)
 {
   uint32_t sum;
   unsigned int i;
@@ -212,19 +214,18 @@
 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;
 
   /* 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 = htons(sizeof (packet));
@@ -235,27 +236,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;
@@ -263,40 +264,48 @@
   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);
+  udp_pkt.length = htons (sizeof (struct udp_header));
+  udp_pkt.crc = htons (port);
+  memcpy(&packet[off], 
+        &udp_pkt,
+        sizeof(struct udp_header));
+  off += sizeof(struct udp_header);
 
-  memset(&udp_pkt.length, 0, sizeof(uint32_t));
-  udp_pkt.length = htons (port);
-  memcpy(&packet[off], &udp_pkt, sizeof(udp_pkt));
-  off += sizeof(udp_pkt);
-
   /* set ICMP checksum */
-  icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(ip_pkt)],
-                            sizeof (icmp_pkt) + sizeof(ip_pkt) + 
sizeof(udp_pkt)));
-  memcpy (&packet[sizeof(ip_pkt)], &icmp_pkt, sizeof (icmp_pkt));
-
-
+  icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(struct 
ip_header)],
+                                         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;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+  dst.sin_len = sizeof (struct sockaddr_in);
+#endif
   dst.sin_addr = *other;
   err = sendto(rawsock,
                packet,
                off, 0,
                (struct sockaddr*)&dst,
                sizeof(dst));
-
   if (err < 0)
     {
       fprintf(stderr,
               "sendto failed: %s\n", strerror(errno));
     }
-  else if (err != off)
+  else if (off != (size_t) err)
     {
       fprintf(stderr,
               "Error: partial send of ICMP message\n");
@@ -314,12 +323,13 @@
 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_pkt;
+  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;
 
@@ -327,7 +337,7 @@
   off = 0;
   ip_pkt.vers_ihl = 0x45;
   ip_pkt.tos = 0;
-  ip_pkt.pkt_len = sizeof (packet); /* huh? */
+  ip_pkt.pkt_len = sizeof (packet);
   ip_pkt.id = 1; 
   ip_pkt.flags_frag_offset = 0;
   ip_pkt.ttl = IPDEFTTL;
@@ -335,24 +345,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 (struct 
ip_packet)));
-  memcpy (&packet[off], &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);
 
   /* icmp reply: time exceeded */
   icmp_pkt.type = ICMP_TIME_EXCEEDED;
   icmp_pkt.code = 0; 
-  icmp_pkt.reserved = 0;
   icmp_pkt.checksum = 0;
+  icmp_pkt.unused = 0;
   memcpy (&packet[off],
          &icmp_pkt,
-         sizeof (struct icmp_packet));
-  off += sizeof (struct icmp_packet);
+         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.pkt_len = (sizeof (struct ip_header) + sizeof (struct 
icmp_echo_header));
   ip_pkt.id = 1; 
   ip_pkt.flags_frag_offset = 0;
   ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
@@ -360,28 +373,32 @@
   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;
-  icmp_echo.reserved = 0;
+  icmp_echo.reserved = htonl (port);
   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_packet));
+         sizeof(struct icmp_echo_header));
 
   /* no go back to calculate ICMP packet checksum */
-  off = sizeof (ip_pkt);
+  off = sizeof (struct ip_header);
   icmp_pkt.checksum = htons(calc_checksum((uint16_t*) &packet[off],
-                                         sizeof (struct icmp_packet) + 
sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet)));
+                                         sizeof (struct 
icmp_ttl_exceeded_header) + 
+                                         sizeof(struct ip_header) + 
+                                         sizeof(struct icmp_echo_header)));
   memcpy (&packet[off],
          &icmp_pkt,
-         sizeof (struct icmp_packet));
+         sizeof (struct icmp_ttl_exceeded_header));
 
   /* prepare for transmission */
   memset (&dst, 0, sizeof (dst));
@@ -400,7 +417,7 @@
       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");
@@ -427,8 +444,8 @@
               strerror (errno));
       return -1;
     }  
-  if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
-                (char *)&one, sizeof(one)) == -1)
+  if (-1 == setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
+                      (char *)&one, sizeof(one)))
     {
       fprintf(stderr,
              "setsockopt failed: %s\n",
@@ -436,8 +453,8 @@
       close (ret);
       return -1;
     }
-  if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
-                (char *)&one, sizeof(one)) == -1)
+  if (-1 == setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
+                      (char *)&one, sizeof(one)))
     {
       fprintf(stderr,
              "setsockopt failed: %s\n",
@@ -457,7 +474,7 @@
   uid_t uid;
   unsigned int p;
 
-  if (argc != 4)
+  if (4 != argc)
     {
       fprintf (stderr,
               "This program must be started with our IP, the targets external 
IP, and our port as arguments.\n");
@@ -472,8 +489,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",

Modified: gnunet/src/transport/gnunet-nat-server.c
===================================================================
--- gnunet/src/transport/gnunet-nat-server.c    2010-08-21 17:05:52 UTC (rev 
12700)
+++ gnunet/src/transport/gnunet-nat-server.c    2010-08-21 21:44:01 UTC (rev 
12701)
@@ -86,7 +86,7 @@
 /**
  * IPv4 header.
  */
-struct ip_packet 
+struct ip_header 
 {
 
   /**
@@ -143,7 +143,7 @@
 /**
  * Format of ICMP packet.
  */
-struct icmp_packet 
+struct icmp_ttl_exceeded_header 
 {
   uint8_t type;
 
@@ -151,19 +151,35 @@
 
   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;
 };
 
 /**
@@ -218,16 +234,14 @@
 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_echo_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);
@@ -238,18 +252,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, &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.checksum = 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;
@@ -260,7 +279,7 @@
   err = sendto(rawsock, 
               packet, off, 0, 
               (struct sockaddr*)&dst, 
-              sizeof(dst));
+              sizeof(struct sockaddr_in));
   if (err < 0) 
     {
 #if VERBOSE
@@ -268,7 +287,7 @@
              "sendto failed: %s\n", strerror(errno));
 #endif
     }
-  else if (err != sizeof (packet)) 
+  else if (sizeof (packet) != err) 
     {
       fprintf(stderr,
              "Error: partial send of ICMP message\n");
@@ -282,7 +301,7 @@
  * @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;
@@ -305,7 +324,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");
@@ -313,7 +332,6 @@
 }
 
 
-
 /**
  * We've received an ICMP response.  Process it.
  */
@@ -322,95 +340,103 @@
 {
   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_pkt;
+  struct udp_header udp_pkt;
   size_t off;
-  int have_port;
   uint32_t port;
   
   have = read (icmpsock, buf, sizeof (buf));
-  if (have == -1)
+  if (-1 == have)
     {
       fprintf (stderr,
               "Error reading raw socket: %s\n",
               strerror (errno));
       return; 
     }
-  have_port = 0;
 #if VERBOSE
   fprintf (stderr,
            "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 < 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_pkt, 
+         &buf[off], 
+         sizeof (struct icmp_ttl_exceeded_header));
+  off += sizeof (struct icmp_ttl_exceeded_header);
+  if ( (ICMP_TIME_EXCEEDED != icmp_pkt.type) || 
+       (0 != icmp_pkt.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 */
+  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);
-      fprintf (stdout,
-              "%s:%d\n",
-              inet_ntop (AF_INET,
-                         &sip,
-                         buf,
-                         sizeof (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_pkt,
+             &buf[off],
+             sizeof (struct icmp_echo_header));
+      port = (uint16_t) htonl (icmp_echo_pkt.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.crc);
+      break;
+    default:   
+      /* different type than what we want */
+      return;
     }
-  else if (ip_pkt.proto == IPPROTO_UDP)
-    {
-      memcpy(&udp_pkt, 
-            &buf[off], 
-            sizeof(udp_pkt));
-      fprintf (stdout,
-               "%s:%d\n",
-               inet_ntop (AF_INET,
-                          &sip,
-                          buf,
-                          sizeof (buf)), 
-              ntohs((uint16_t) udp_pkt.length));
-    }
+
+  if (port == 0)
+    fprintf (stdout,
+            "%s\n",
+            inet_ntop (AF_INET,
+                       &source_ip,
+                       buf,
+                       sizeof (buf)));
   else
-    {
-      fprintf (stdout,
-              "%s\n",
-              inet_ntop (AF_INET,
-                         &sip,
-                         buf,
-                         sizeof (buf)));
-    }
+    fprintf (stdout,
+            "%s:%d\n",
+            inet_ntop (AF_INET,
+                       &source_ip,
+                       buf,
+                       sizeof (buf)), 
+            port);
   fflush (stdout);
 }
 
@@ -465,11 +491,10 @@
               strerror (errno));
       return -1;
     }  
-  if (setsockopt(ret, 
-                SOL_SOCKET, 
-                SO_BROADCAST,
-                (char *)&one, 
-                sizeof(one)) == -1)
+  if (-1 == setsockopt(ret, 
+                      SOL_SOCKET, 
+                      SO_BROADCAST,
+                      (char *)&one, sizeof(one)))
     {
       fprintf(stderr,
              "setsockopt failed: %s\n",
@@ -477,10 +502,10 @@
       close (ret);
       return -1;
     }
-  if (setsockopt(ret, 
-                IPPROTO_IP, 
-                IP_HDRINCL,
-                (char *)&one, sizeof(one)) == -1)
+  if (-1 == setsockopt(ret, 
+                      IPPROTO_IP, 
+                      IP_HDRINCL,
+                      (char *)&one, sizeof(one)))
     {
       fprintf(stderr,
              "setsockopt failed: %s\n",
@@ -498,7 +523,7 @@
  * @return -1 on error
  */
 static int
-make_udp_socket ()
+make_udp_socket (const struct in_addr *my_ip)
 {
   int ret;
   struct sockaddr_in addr;
@@ -511,12 +536,14 @@
               strerror (errno));
       return -1;
     }
-  memset (&addr, 0, sizeof (addr));
+  memset (&addr, 
+         0, 
+         sizeof (addr));
   addr.sin_family = AF_INET;
 #if HAVE_SOCKADDR_IN_SIN_LEN
   addr.sin_len = sizeof (struct sockaddr_in);
 #endif
-  /* addr.sin_addr zero == ours (hopefully...) */
+  addr.sin_addr = *my_ip;
   addr.sin_port = htons (NAT_TRAV_PORT);
 
   if (0 != bind (ret,
@@ -543,7 +570,7 @@
   uid_t uid;
   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");
@@ -579,7 +606,7 @@
               strerror (errno));    
       /* not critical, continue anyway */
     }
-  if (-1 == (udpsock = make_udp_socket()))
+  if (-1 == (udpsock = make_udp_socket(&external)))
     {
       close (icmpsock);
       close (rawsock);
@@ -606,7 +633,7 @@
       if (0 == (++alt % 2))
        send_icmp_echo (&external);
       else
-       send_udp (&external);
+       send_udp ();
     }  
   /* select failed (internal error or OS out of resources) */
   close (icmpsock);




reply via email to

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