more code cleanup
[oweals/gnunet.git] / src / transport / gnunet-nat-server-windows.c
index a312fa2ca7bccd2845c201c55ea75d68194705ad..ceff3fb8d9b5c2b0ffd894406a506eaa0342ebe6 100644 (file)
  */
 #define VERBOSE 0
 
+/**
+ * Must match IP given in the client.
+ */
+#define DUMMY_IP "192.0.2.86"
+
 /**
  * TTL to use for our outgoing messages.
  */
 
 #define ICMP_TIME_EXCEEDED      11      /* Time Exceeded */
 
-/**
- * Must match IP given in the client.
- */
-#define DUMMY_IP "192.0.2.86"
-
 /**
  * How often do we send our ICMP messages to receive replies?
  */
@@ -87,52 +87,52 @@ struct ip_packet
   /**
    * Version (4 bits) + Internet header length (4 bits) 
    */
-  u_char vers_ihl; 
+  uint8_t vers_ihl; 
 
   /**
    * Type of service
    */
-  u_char tos;  
+  uint8_t tos;  
 
   /**
    * Total length
    */
-  u_short pkt_len;  
+  uint16_t pkt_len;  
 
   /**
    * Identification
    */
-  u_short id;    
+  uint16_t id;    
 
   /**
    * Flags (3 bits) + Fragment offset (13 bits)
    */
-  u_short flags_frag_offset; 
+  uint16_t flags_frag_offset; 
 
   /**
    * Time to live
    */
-  u_char  ttl;   
+  uint8_t  ttl;   
 
   /**
    * Protocol       
    */
-  u_char  proto; 
+  uint8_t  proto; 
 
   /**
    * Header checksum
    */
-  u_short checksum; 
+  uint16_t checksum; 
 
   /**
    * Source address
    */
-  u_long  src_ip;  
+  uint32_t  src_ip;  
 
   /**
    * Destination address 
    */
-  u_long  dst_ip;  
+  uint32_t  dst_ip;  
 };
 
 /**
@@ -141,8 +141,11 @@ struct ip_packet
 struct icmp_packet 
 {
   uint8_t type;
+
   uint8_t code;
+
   uint16_t checksum;
+
   uint32_t reserved;
 };
 
@@ -158,14 +161,28 @@ struct udp_packet
   uint32_t length;
 };
 
+/**
+ * Socket we use to receive "fake" ICMP replies.
+ */
 static SOCKET icmpsock;
 
+/**
+ * Socket we use to send our ICMP requests.
+ */
 static SOCKET rawsock;
 
+/**
+ * Target "dummy" address.
+ */
 static struct in_addr dummy;
 
+
 /**
  * CRC-16 for IP/ICMP headers.
+ *
+ * @param data what to calculate the CRC over
+ * @param bytes number of bytes in data (must be multiple of 2)
+ * @return the CRC 16.
  */
 static uint16_t 
 calc_checksum(const uint16_t *data, 
@@ -182,12 +199,14 @@ calc_checksum(const uint16_t *data,
   return sum;
 }
 
+
 /**
  * Convert IPv4 address from text to binary form.
  *
  * @param af address family
  * @param cp the address to print
  * @param buf where to write the address result
+ * @return 1 on success
  */
 static int 
 inet_pton (int af, 
@@ -270,6 +289,9 @@ send_icmp_echo (const struct in_addr *my_ip)
 }
 
 
+/**
+ * We've received an ICMP response.  Process it.
+ */
 static void
 process_icmp_response ()
 {
@@ -281,7 +303,6 @@ process_icmp_response ()
   struct udp_packet udp_pkt;
   size_t off;
   int have_port;
-  int have_udp;
   uint32_t port;
 
   have = read (icmpsock, buf, sizeof (buf));
@@ -328,7 +349,6 @@ process_icmp_response ()
         sizeof (sip));
   memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
   off += sizeof (ip_pkt);
-  have_udp = (ip_pkt.proto == IPPROTO_UDP);
 
   if (have_port)
     {
@@ -347,7 +367,7 @@ process_icmp_response ()
               buf, 
               port);
     }
-  else if (have_udp)
+  else if (ip_pkt.proto == IPPROTO_UDP)
     {
       memcpy(&udp_pkt,
             &buf[off],
@@ -361,7 +381,7 @@ process_icmp_response ()
       fprintf (stdout, 
               "%s:%d\n", 
               buf, 
-              ntohs((int)udp_pkt.length));
+              ntohs((uint16_t)udp_pkt.length));
     }
   else
     {
@@ -379,6 +399,11 @@ process_icmp_response ()
 }
 
 
+/**
+ * Create an ICMP raw socket for reading.
+ *
+ * @return INVALID_SOCKET on error
+ */
 static SOCKET
 make_icmp_socket ()
 {
@@ -390,12 +415,17 @@ make_icmp_socket ()
       fprintf (stderr,
               "Error opening RAW socket: %s\n",
               strerror (errno));
-      return -1;
+      return INVALID_SOCKET;
     }  
   return ret;
 }
 
 
+/**
+ * Create an ICMP raw socket for writing.
+ *
+ * @return INVALID_SOCKET on error
+ */
 static SOCKET
 make_raw_socket ()
 {
@@ -470,11 +500,11 @@ main (int argc,
       fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
       return 2;
     }
-  if (-1 == (icmpsock = make_icmp_socket()))
+  if (INVALID_SOCKET == (icmpsock = make_icmp_socket()))
     {
       return 3; 
     }
-  if (-1 == (make_raw_socket()))
+  if (INVALID_SOCKET == (make_raw_socket()))
     {
       closesocket (icmpsock);
       return 3; 
@@ -485,7 +515,7 @@ main (int argc,
       FD_SET (icmpsock, &rs);
       tv.tv_sec = 0;
       tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000; 
-      if (0 != select (icmpsock + 1, &rs, NULL, NULL, &tv))
+      if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
        {
          if (errno == EINTR)
            continue;
@@ -498,10 +528,11 @@ main (int argc,
         process_icmp_response ();
       send_icmp_echo (&external);
     }
+  /* select failed (internal error or OS out of resources) */
   closesocket(icmpsock);
   closesocket(rawsock);
   WSACleanup ();
-  return 4; /* select failed! */
+  return 4; 
 }