No need to constantly re-open a socket. Just open it up front and keep
it around.

diff --git if.c if.c
index 9c6acd0e049..e04b6027fae 100644
--- if.c
+++ if.c
@@ -65,6 +65,8 @@ static void get_iflist(char **buf, size_t *size);
 static void parse_iflist(struct if_msghdr ***ifmlist_p, char *buf,
     size_t bufsize);
 
+extern int ioctl_sock;
+
 static void
 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
 {
@@ -111,23 +113,18 @@ if_nametosdl(char *name)
 int
 if_getmtu(char *name)
 {
-       int             s, save_errno;
        struct ifreq    ifr;
        u_long          mtu = 0;
 
-       if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) >= 0) {
-               memset(&ifr, 0, sizeof(ifr));
-               ifr.ifr_addr.sa_family = AF_INET6;
-               if (strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)) >=
-                   sizeof(ifr.ifr_name))
-                       fatalx("strlcpy");
-               if (ioctl(s, SIOCGIFMTU, (char *)&ifr) >= 0)
-                       mtu = ifr.ifr_mtu;
-               save_errno = errno;
-               close(s);
-               errno = save_errno;
-       }
-
+       memset(&ifr, 0, sizeof(ifr));
+       ifr.ifr_addr.sa_family = AF_INET6;
+       if (strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)) >=
+           sizeof(ifr.ifr_name))
+               fatalx("strlcpy");
+       if (ioctl(ioctl_sock, SIOCGIFMTU, (char *)&ifr) >= 0)
+               mtu = ifr.ifr_mtu;
+       else
+               log_warn("s: %d", ioctl_sock);
        return (mtu);
 }
 
@@ -136,20 +133,12 @@ int
 if_getflags(int ifindex, int oifflags)
 {
        struct ifreq ifr;
-       int s;
-
-       if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
-               log_warn("socket");
-               return (oifflags & ~IFF_UP);
-       }
 
        if_indextoname(ifindex, ifr.ifr_name);
-       if (ioctl(s, SIOCGIFFLAGS, (char *)&ifr) < 0) {
+       if (ioctl(ioctl_sock, SIOCGIFFLAGS, (char *)&ifr) < 0) {
                log_warn("ioctl:SIOCGIFFLAGS: failed for %s", ifr.ifr_name);
-               close(s);
                return (oifflags & ~IFF_UP);
        }
-       close(s);
        return (ifr.ifr_flags);
 }
 
diff --git rtadvd.c rtadvd.c
index 5214a8bb6ec..c1c85207b28 100644
--- rtadvd.c
+++ rtadvd.c
@@ -79,6 +79,7 @@ struct sockaddr_in6 from;
 struct sockaddr_in6 sin6_allnodes = {sizeof(sin6_allnodes), AF_INET6};
 int sock;
 int rtsock = -1;
+int ioctl_sock;
 int dflag = 0, sflag = 0;
 
 u_char *conffile = NULL;
@@ -204,6 +205,9 @@ main(int argc, char *argv[])
        if (conffile == NULL)
                log_init(dflag);
 
+       if ((ioctl_sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
+               fatal("socket");
+
        while (argc--)
                getconfig(*argv++);
 


-- 
I'm not entirely sure you are real.

Reply via email to