convert sqlite peerstore to using libgnunetsq
[oweals/gnunet.git] / src / nat / gnunet-helper-nat-server.c
index aacae376a9360de18d6398b48dfcb78fe4b49d9f..ef8e97482771d0054713251e7f23c35fc028ed0d 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2010 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2010 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -264,8 +264,11 @@ send_icmp_echo (const struct in_addr *my_ip)
   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 (struct ip_header)));
-  memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
+      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;
@@ -273,9 +276,12 @@ send_icmp_echo (const struct in_addr *my_ip)
   icmp_echo.checksum = 0;
   icmp_echo.reserved = 0;
   icmp_echo.checksum =
-      htons (calc_checksum
-             ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header)));
-  memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header));
+    htons (calc_checksum
+          ((uint16_t *) & 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));
@@ -284,17 +290,24 @@ send_icmp_echo (const struct in_addr *my_ip)
   dst.sin_len = sizeof (struct sockaddr_in);
 #endif
   dst.sin_addr = dummy;
-  err =
-      sendto (rawsock, packet, off, 0, (struct sockaddr *) &dst, sizeof (dst));
+  err = sendto (rawsock,
+               packet,
+               off,
+               0,
+               (struct sockaddr *) &dst,
+               sizeof (dst));
   if (err < 0)
   {
 #if VERBOSE
-    fprintf (stderr, "sendto failed: %s\n", strerror (errno));
+    fprintf (stderr,
+            "sendto failed: %s\n",
+            strerror (errno));
 #endif
   }
   else if (sizeof (packet) != err)
   {
-    fprintf (stderr, "Error: partial send of ICMP message\n");
+    fprintf (stderr,
+            "Error: partial send of ICMP message\n");
   }
 }
 
@@ -315,16 +328,24 @@ send_udp ()
 #endif
   dst.sin_addr = dummy;
   dst.sin_port = htons (NAT_TRAV_PORT);
-  err = sendto (udpsock, NULL, 0, 0, (struct sockaddr *) &dst, sizeof (dst));
+  err = sendto (udpsock,
+               NULL,
+               0,
+               0,
+               (struct sockaddr *) &dst,
+               sizeof (dst));
   if (err < 0)
   {
 #if VERBOSE
-    fprintf (stderr, "sendto failed: %s\n", strerror (errno));
+    fprintf (stderr,
+            "sendto failed: %s\n",
+            strerror (errno));
 #endif
   }
   else if (0 != err)
   {
-    fprintf (stderr, "Error: partial send of ICMP message\n");
+    fprintf (stderr,
+            "Error: partial send of ICMP message\n");
   }
 }
 
@@ -348,11 +369,15 @@ process_icmp_response ()
   have = read (icmpsock, buf, sizeof (buf));
   if (-1 == have)
   {
-    fprintf (stderr, "Error reading raw socket: %s\n", strerror (errno));
+    fprintf (stderr,
+            "Error reading raw socket: %s\n",
+            strerror (errno));
     return;
   }
 #if VERBOSE
-  fprintf (stderr, "Received message of %u bytes\n", (unsigned int) have);
+  fprintf (stderr,
+          "Received message of %u bytes\n",
+          (unsigned int) have);
 #endif
   if (have <
       (ssize_t) (sizeof (struct ip_header) +
@@ -433,15 +458,27 @@ setup_raw_socket ()
   const int one = 1;
 
   if (-1 ==
-      setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &one, sizeof (one)))
+      setsockopt (rawsock,
+                 SOL_SOCKET,
+                 SO_BROADCAST,
+                 (char *) &one,
+                 sizeof (one)))
   {
-    fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
+    fprintf (stderr,
+            "setsockopt failed: %s\n",
+            strerror (errno));
     return -1;
   }
   if (-1 ==
-      setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof (one)))
+      setsockopt (rawsock,
+                 IPPROTO_IP,
+                 IP_HDRINCL,
+                 (char *) &one,
+                 sizeof (one)))
   {
-    fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
+    fprintf (stderr,
+            "setsockopt failed: %s\n",
+            strerror (errno));
     return -1;
   }
   return 0;
@@ -492,7 +529,8 @@ make_udp_socket (const struct in_addr *my_ip)
 
 
 int
-main (int argc, char *const *argv)
+main (int argc,
+      char *const *argv)
 {
   struct in_addr external;
   fd_set rs;
@@ -504,11 +542,15 @@ main (int argc, char *const *argv)
   int global_ret;
 
   /* Create an ICMP raw socket for reading (we'll check errors later) */
-  icmpsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
+  icmpsock = socket (AF_INET,
+                    SOCK_RAW,
+                    IPPROTO_ICMP);
   icmp_eno = errno;
 
   /* Create an (ICMP) raw socket for writing (we'll check errors later) */
-  rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
+  rawsock = socket (AF_INET,
+                   SOCK_RAW,
+                   IPPROTO_RAW);
   raw_eno = errno;
   udpsock = -1;
 
@@ -517,14 +559,18 @@ main (int argc, char *const *argv)
 #ifdef HAVE_SETRESUID
   if (0 != setresuid (uid, uid, uid))
   {
-    fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
+    fprintf (stderr,
+            "Failed to setresuid: %s\n",
+            strerror (errno));
     global_ret = 1;
     goto error_exit;
   }
 #else
   if (0 != (setuid (uid) | seteuid (uid)))
   {
-    fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
+    fprintf (stderr,
+            "Failed to setuid: %s\n",
+            strerror (errno));
     global_ret = 2;
     goto error_exit;
   }
@@ -540,13 +586,16 @@ main (int argc, char *const *argv)
   }
   if (1 != inet_pton (AF_INET, argv[1], &external))
   {
-    fprintf (stderr, "Error parsing IPv4 address: %s\n", strerror (errno));
+    fprintf (stderr,
+            "Error parsing IPv4 address: %s\n",
+            strerror (errno));
     global_ret = 4;
     goto error_exit;
   }
   if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
   {
-    fprintf (stderr, "Internal error converting dummy IP to binary.\n");
+    fprintf (stderr,
+            "Internal error converting dummy IP to binary.\n");
     global_ret = 5;
     goto error_exit;
   }
@@ -554,7 +603,9 @@ main (int argc, char *const *argv)
   /* error checking icmpsock */
   if (-1 == icmpsock)
   {
-    fprintf (stderr, "Error opening RAW socket: %s\n", strerror (icmp_eno));
+    fprintf (stderr,
+            "Error opening RAW socket: %s\n",
+            strerror (icmp_eno));
     global_ret = 6;
     goto error_exit;
   }
@@ -562,7 +613,9 @@ main (int argc, char *const *argv)
   {
     /* this could happen if we were started with a large number of already-open
        file descriptors... */
-    fprintf (stderr, "Socket number too large (%d > %u)\n", icmpsock,
+    fprintf (stderr,
+            "Socket number too large (%d > %u)\n",
+            icmpsock,
              (unsigned int) FD_SETSIZE);
     global_ret = 7;
     goto error_exit;
@@ -571,7 +624,9 @@ main (int argc, char *const *argv)
   /* error checking rawsock */
   if (-1 == rawsock)
   {
-    fprintf (stderr, "Error opening RAW socket: %s\n", strerror (raw_eno));
+    fprintf (stderr,
+            "Error opening RAW socket: %s\n",
+            strerror (raw_eno));
     global_ret = 8;
     goto error_exit;
   }
@@ -601,7 +656,9 @@ main (int argc, char *const *argv)
     {
       if (errno == EINTR)
         continue;
-      fprintf (stderr, "select failed: %s\n", strerror (errno));
+      fprintf (stderr,
+              "select failed: %s\n",
+              strerror (errno));
       break;
     }
     if (1 == getppid ())        /* Check the parent process id, if 1 the parent has died, so we should die too */