regularly scan for lost interface
authorKonrad Rosenbaum <konrad@silmor.de>
Sat, 13 Feb 2010 18:26:40 +0000 (18:26 +0000)
committerKonrad Rosenbaum <konrad@silmor.de>
Sat, 13 Feb 2010 18:26:40 +0000 (18:26 +0000)
git-svn-id: https://silmor.de/svn/misc/tdhcp@176 752dcaaa-03e5-0310-b894-97e397d490cd

server.c
sock.c
sock.h

index 04374be..0e72a9b 100644 (file)
--- a/server.c
+++ b/server.c
@@ -391,17 +391,19 @@ int main(int argc,char**argv)
        while(1){
                fd_set rfd,xfd;
                int sret;
+               struct timeval tv;
                //wait for event
                FD_ZERO(&rfd);
                FD_ZERO(&xfd);
                FD_SET(sockfd,&rfd);
                FD_SET(sockfd,&xfd);
-               sret=select(sockfd+1,&rfd,0,&xfd,0);
+               tv.tv_sec=1;tv.tv_usec=0;
+               sret=select(sockfd+1,&rfd,0,&xfd,&tv);
                //check for errors
                if(sret<0){
                        int e=errno;
                        if(e==EAGAIN)continue;
-                       td_log(LOGERROR,"Error caught: %s\n",strerror(e));
+                       td_log(LOGERROR,"Error caught: %s",strerror(e));
                        return 1;
                }
                //check for event
@@ -413,10 +415,15 @@ int main(int argc,char**argv)
                                        handlemessage(msg2);
                        }
                        if(FD_ISSET(sockfd,&xfd)){
-                               td_log(LOGERROR,"Exception on socket caught.\n");
+                               td_log(LOGERROR,"Exception on socket caught.");
                                return 1;
                        }
                }
+               //check that the interface still exists
+               if(!checkiface()){
+                       td_log(LOGERROR,"Interface lost, exiting.");
+                       return 1;
+               }
        }
        /*should not be reachable*/
        td_log(LOGDEBUG,"hmm, Konrad needs better coffee - this line should not be reachable");
diff --git a/sock.c b/sock.c
index 6c5ac52..a1b204d 100644 (file)
--- a/sock.c
+++ b/sock.c
@@ -58,6 +58,20 @@ static void get_if_lladdr(const char*dev,struct sockaddr_in6*sa)
        freeifaddrs(ifa);
 }
 
+int checkiface()
+{
+       struct ifreq ifr;
+       //sanity check
+       if(ifindex<0 || sockfd<0)return 0;
+       //try to find the iface
+       Memzero(&ifr,sizeof(ifr));
+       ifr.ifr_ifindex=ifindex;
+       if(ioctl(sockfd,SIOCGIFNAME,&ifr)<0){
+               return 0;
+       }
+       return 1;
+}
+
 /*initializes the socket on port*/
 void initsocket(short port,const char*dev)
 {
diff --git a/sock.h b/sock.h
index fad3663..d30a6b4 100644 (file)
--- a/sock.h
+++ b/sock.h
@@ -25,6 +25,9 @@ void initsocket(short,const char*);
 /*joins DHCP multicast group*/
 void joindhcp();
 
+/*checks that the interface still exists; returns true if found*/
+int checkiface();
+
 /*set the server multicast as target*/
 struct sockaddr_in6;
 void settargetserver(struct sockaddr_in6*);