Delete struct ifr
[tinc] / src / net.c
index bc32b7c..8d92cc1 100644 (file)
--- a/src/net.c
+++ b/src/net.c
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: net.c,v 1.35.4.68 2000/11/07 21:43:28 guus Exp $
+    $Id: net.c,v 1.35.4.76 2000/11/16 22:11:40 zarq Exp $
 */
 
 #include "config.h"
 
-#include <arpa/inet.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <linux/sockios.h>
-#include <net/if.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/signal.h>
-#include <sys/socket.h>
 #include <sys/time.h>
 #include <sys/types.h>
 #include <syslog.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
-#include <openssl/rand.h>
-#include <openssl/evp.h>
-#include <openssl/err.h>
+/* SunOS really wants sys/socket.h BEFORE net/if.h,
+   and FreeBSD wants these lines below the rest. */
+#include <arpa/inet.h>
+#include <sys/socket.h>
+#include <net/if.h>
+
+#ifdef HAVE_OPENSSL_RAND_H
+# include <openssl/rand.h>
+#else
+# include <rand.h>
+#endif
+
+#ifdef HAVE_OPENSSL_EVP_H
+# include <openssl/evp.h>
+#else
+# include <evp.h>
+#endif
+
+#ifdef HAVE_OPENSSL_ERR_H
+# include <openssl/err.h>
+#else
+# include <err.h>
+#endif
 
 #ifdef HAVE_TUNTAP
 #include LINUX_IF_TUN_H
 
 #include "conf.h"
 #include "connlist.h"
+#include "list.h"
 #include "meta.h"
 #include "net.h"
 #include "netutl.h"
+#include "process.h"
 #include "protocol.h"
 #include "subnet.h"
 
@@ -77,55 +95,6 @@ char *unknown = NULL;
 
 subnet_t mymac;
 
-/*
-  Execute the given script.
-  This function doesn't really belong here.
-*/
-int execute_script(const char *name)
-{
-  char *scriptname;
-  pid_t pid;
-  char *s;
-
-  if((pid = fork()) < 0)
-    {
-      syslog(LOG_ERR, _("System call `%s' failed: %m"),
-            "fork");
-      return -1;
-    }
-
-  if(pid)
-    {
-      return 0;
-    }
-
-  /* Child here */
-
-  if(netname)
-    {
-      asprintf(&s, "NETNAME=%s", netname);
-      putenv(s);       /* Don't free s! see man 3 putenv */
-    }
-  else
-    {
-      unsetenv("NETNAME");
-    }
-
-  chdir(confbase);     /* This cannot fail since we already read config files from this directory. */
-  
-  asprintf(&scriptname, "%s/%s", confbase, name);
-  execl(scriptname, NULL);
-
-  /* No return on success */
-  
-  if(errno != ENOENT)  /* Ignore if the file does not exist */
-    syslog(LOG_WARNING, _("Error executing `%s': %m"), scriptname);
-
-  /* No need to free things */
-
-  exit(0);
-}
-
 int xsend(conn_list_t *cl, vpn_packet_t *inpkt)
 {
   vpn_packet_t outpkt;
@@ -415,7 +384,6 @@ int setup_tap_fd(void)
   int nfd;
   const char *tapfname;
   config_t const *cfg;
-  struct ifreq ifr;
 
 cp  
   if((cfg = get_config_val(config, config_tapdevice)))
@@ -483,6 +451,7 @@ cp
 
   if(setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
     {
+      close(nfd);
       syslog(LOG_ERR, _("System call `%s' failed: %m"),
             "setsockopt");
       return -1;
@@ -490,6 +459,7 @@ cp
 
   if(setsockopt(nfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one)))
     {
+      close(nfd);
       syslog(LOG_ERR, _("System call `%s' failed: %m"),
             "setsockopt");
       return -1;
@@ -498,6 +468,7 @@ cp
   flags = fcntl(nfd, F_GETFL);
   if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
     {
+      close(nfd);
       syslog(LOG_ERR, _("System call `%s' failed: %m"),
             "fcntl");
       return -1;
@@ -507,6 +478,7 @@ cp
     {
       if(setsockopt(nfd, SOL_SOCKET, SO_KEEPALIVE, cfg->data.ptr, strlen(cfg->data.ptr)))
         {
+          close(nfd);
           syslog(LOG_ERR, _("Unable to bind listen socket to interface %s: %m"), cfg->data.ptr);
           return -1;
         }
@@ -523,12 +495,14 @@ cp
 
   if(bind(nfd, (struct sockaddr *)&a, sizeof(struct sockaddr)))
     {
+      close(nfd);
       syslog(LOG_ERR, _("Can't bind to port %hd/tcp: %m"), port);
       return -1;
     }
 
   if(listen(nfd, 3))
     {
+      close(nfd);
       syslog(LOG_ERR, _("System call `%s' failed: %m"),
             "listen");
       return -1;
@@ -549,12 +523,14 @@ int setup_vpn_in_socket(int port)
 cp
   if((nfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
     {
+      close(nfd);
       syslog(LOG_ERR, _("Creating socket failed: %m"));
       return -1;
     }
 
   if(setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
     {
+      close(nfd);
       syslog(LOG_ERR, _("System call `%s' failed: %m"),
             "setsockopt");
       return -1;
@@ -563,6 +539,7 @@ cp
   flags = fcntl(nfd, F_GETFL);
   if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
     {
+      close(nfd);
       syslog(LOG_ERR, _("System call `%s' failed: %m"),
             "fcntl");
       return -1;
@@ -575,6 +552,7 @@ cp
 
   if(bind(nfd, (struct sockaddr *)&a, sizeof(struct sockaddr)))
     {
+      close(nfd);
       syslog(LOG_ERR, _("Can't bind to port %hd/udp: %m"), port);
       return -1;
     }
@@ -613,6 +591,7 @@ cp
 
   if(connect(cl->meta_socket, (struct sockaddr *)&a, sizeof(a)) == -1)
     {
+      close(cl->meta_socket);
       syslog(LOG_ERR, _("%s port %hd: %m"), cl->hostname, cl->port);
       return -1;
     }
@@ -620,6 +599,7 @@ cp
   flags = fcntl(cl->meta_socket, F_GETFL);
   if(fcntl(cl->meta_socket, F_SETFL, flags | O_NONBLOCK) < 0)
     {
+      close(cl->meta_socket);
       syslog(LOG_ERR, _("fcntl for %s port %d: %m"),
              cl->hostname, cl->port);
       return -1;
@@ -823,6 +803,8 @@ cp
   myself->status.active = 1;
 
   syslog(LOG_NOTICE, _("Ready: listening on port %hd"), myself->port);
+
+  child_pids = list_new();
 cp
   return 0;
 }
@@ -962,6 +944,7 @@ cp
 
   if(setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
     {
+      close(nfd);
       syslog(LOG_ERR, _("System call `%s' failed: %m"),
             "setsockopt");
       return -1;
@@ -970,6 +953,7 @@ cp
   flags = fcntl(nfd, F_GETFL);
   if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
     {
+      close(nfd);
       syslog(LOG_ERR, _("System call `%s' failed: %m"),
             "fcntl");
       return -1;
@@ -982,6 +966,7 @@ cp
 
   if(bind(nfd, (struct sockaddr *)&a, sizeof(struct sockaddr)))
     {
+      close(nfd);
       syslog(LOG_ERR, _("Can't bind to port %hd/udp: %m"), myself->port);
       return -1;
     }
@@ -992,6 +977,7 @@ cp
 
   if(connect(nfd, (struct sockaddr *)&a, sizeof(a)) == -1)
     {
+      close(nfd);
       syslog(LOG_ERR, _("Connecting to %s port %d failed: %m"),
             cl->hostname, cl->port);
       return -1;
@@ -1000,6 +986,7 @@ cp
   flags = fcntl(nfd, F_GETFL);
   if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
     {
+      close(nfd);
       syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m %s (%s)"), __FILE__, __LINE__, nfd,
              cl->name, cl->hostname);
       return -1;
@@ -1023,7 +1010,7 @@ conn_list_t *create_new_connection(int sfd)
 cp
   p = new_conn_list();
 
-  if(getpeername(sfd, &ci, &len) < 0)
+  if(getpeername(sfd, (struct sockaddr *) &ci, (socklen_t *) &len) < 0)
     {
       syslog(LOG_ERR, _("System call `%s' failed: %m"),
             "getpeername");
@@ -1406,6 +1393,8 @@ cp
           if(FD_ISSET(tap_fd, &fset))
            handle_tap_input();
         }
+
+      check_children();
     }
 cp
 }