(no commit message)
[oweals/gnunet.git] / src / transport / gnunet-nat-server.c
index e44847519f474d7b3a467067aa529ae72ee94a90..13a54058b71f46358fc6d9977119e5ff28db232f 100644 (file)
@@ -60,9 +60,9 @@
 /**
  * Must match IP given in the client.
  */
-#define DUMMY_IP "1.2.3.4"
+#define DUMMY_IP "192.0.2.86"
 
-#define VERBOSE GNUNET_NO
+#define VERBOSE 0
 
 /**
  * How often do we send our ICMP messages to receive replies?
@@ -91,6 +91,14 @@ struct icmp_packet
   uint32_t reserved;
 };
 
+struct udp_packet
+{
+  uint16_t src_port;
+
+  uint16_t dst_port;
+
+  uint32_t length;
+};
 
 static int icmpsock;
 
@@ -192,7 +200,11 @@ process_icmp_response ()
   struct in_addr sip;
   struct ip_packet ip_pkt;
   struct icmp_packet icmp_pkt;
+  struct udp_packet udp_pkt;
   size_t off;
+  int have_port;
+  int have_udp;
+  uint32_t port;
   
   have = read (icmpsock, buf, sizeof (buf));
   if (have == -1)
@@ -202,7 +214,13 @@ process_icmp_response ()
               strerror (errno));
       return; 
     }
-  if (have != sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2)
+  have_port = 0;
+
+  if (have == sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2 + sizeof(uint32_t))
+    {
+      have_port = 1;
+    }
+  else if (have != sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2)
     {
 #if VERBOSE
       fprintf (stderr,
@@ -216,7 +234,7 @@ process_icmp_response ()
   off += sizeof (ip_pkt);
   memcpy (&icmp_pkt, &buf[off], sizeof (icmp_pkt));
   off += sizeof (icmp_pkt);
-  if ( (ip_pkt.proto != IPPROTO_ICMP) ||
+  if ( ((ip_pkt.proto != IPPROTO_ICMP) && (ip_pkt.proto != IPPROTO_UDP)) ||
        (icmp_pkt.type != ICMP_TIME_EXCEEDED) || 
        (icmp_pkt.code != 0) )
     {
@@ -226,12 +244,46 @@ process_icmp_response ()
   memcpy(&sip, 
         &ip_pkt.src_ip, 
         sizeof (sip));
-  fprintf (stdout,
-          "%s\n",
-          inet_ntop (AF_INET,
-                     &sip,
-                     buf,
-                     sizeof (buf)));
+
+  memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
+  off += sizeof (ip_pkt);
+
+  have_udp = 0;
+  if (ip_pkt.proto == IPPROTO_UDP)
+    {
+      have_udp = 1;
+    }
+
+  if (have_port)
+    {
+      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);
+    }
+  else if (have_udp)
+    {
+      memcpy(&udp_pkt, &buf[off], sizeof(udp_pkt));
+      fprintf (stdout,
+               "%s:%d\n",
+               inet_ntop (AF_INET,
+                          &sip,
+                          buf,
+                          sizeof (buf)), ntohl(udp_pkt.length));
+    }
+  else
+    {
+      fprintf (stdout,
+              "%s\n",
+              inet_ntop (AF_INET,
+                         &sip,
+                         buf,
+                         sizeof (buf)));
+    }
   fflush (stdout);
 }
 
@@ -313,7 +365,7 @@ main (int argc, char *const *argv)
   if (argc != 2)
     {
       fprintf (stderr,
-              "This program must be started with our external IP as the only argument.\n");
+              "This program must be started with our (internal NAT) IP as the only argument.\n");
       return 1;
     }
   if (1 != inet_pton (AF_INET, argv[1], &external))