Imported Upstream version 0.3.2
[debian/uanytun.git] / src / uanytun.c
index c9c6c44..0e36781 100644 (file)
@@ -20,8 +20,9 @@
  *  This file is part of uAnytun.
  *
  *  uAnytun is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License version 3 as
- *  published by the Free Software Foundation.
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  any later version.
  *
  *  uAnytun is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -249,7 +250,7 @@ int main_loop(tun_device_t* dev, udp_socket_t* sock, options_t* opt)
   encrypted_packet_t encrypted_packet;
   encrypted_packet_init(&encrypted_packet, opt->auth_tag_length_);
   seq_nr_t seq_nr = 0;
-  fd_set readfds;
+  fd_set readfds, readyfds;
 
   cipher_t c;
   auth_algo_t aa;
@@ -260,36 +261,45 @@ int main_loop(tun_device_t* dev, udp_socket_t* sock, options_t* opt)
   if(ret)
     return ret;
 
-  signal_init();
+  FD_ZERO(&readfds);
+  FD_SET(dev->fd_, &readfds);
+  FD_SET(sock->fd_, &readfds);
+  int nfds = dev->fd_ > sock->fd_ ? dev->fd_ : sock->fd_;
+
   int return_value = 0;
-  while(!return_value) {
-    FD_ZERO(&readfds);
-    FD_SET(dev->fd_, &readfds);
-    FD_SET(sock->fd_, &readfds);
-    int nfds = dev->fd_ > sock->fd_ ? dev->fd_+1 : sock->fd_+1;
+  int sig_fd = signal_init();
+  if(sig_fd < 0)
+    return_value -1;
 
-    int ret = select(nfds, &readfds, NULL, NULL, NULL);
+  FD_SET(sig_fd, &readfds);
+  nfds = (nfds < sig_fd) ? sig_fd : nfds;
+
+  while(!return_value) {
+    memcpy(&readyfds, &readfds, sizeof(readyfds));
+    int ret = select(nfds + 1, &readyfds, NULL, NULL, NULL);
     if(ret == -1 && errno != EINTR) {
       log_printf(ERROR, "select returned with error: %s", strerror(errno));
       return_value = -1;
       break;
     }
-    if(!ret)
+    if(!ret || ret == -1)
       continue;
 
-    if(signal_exit) {
-      return_value = 1;
-      break;
+    if(FD_ISSET(sig_fd, &readyfds)) {
+      if(signal_handle()) {
+        return_value = 1;
+        break;
+      }
     }
 
-    if(FD_ISSET(dev->fd_, &readfds)) {
+    if(FD_ISSET(dev->fd_, &readyfds)) {
       return_value = process_tun_data(dev, sock, opt, &plain_packet, &encrypted_packet, &c, &aa, &kd, seq_nr);
       seq_nr++;
       if(return_value)
         break;
     }
 
-    if(FD_ISSET(sock->fd_, &readfds)) {
+    if(FD_ISSET(sock->fd_, &readyfds)) {
       return_value = process_sock_data(dev, sock, opt, &plain_packet, &encrypted_packet, &c, &aa, &kd, &seq_win); 
       if(return_value)
         break;
@@ -302,6 +312,7 @@ int main_loop(tun_device_t* dev, udp_socket_t* sock, options_t* opt)
   key_derivation_close(&kd);
 #endif
   seq_win_clear(&seq_win);
+  signal_stop();
 
   return return_value;
 }
@@ -391,7 +402,9 @@ int main(int argc, char* argv[])
 
   if(opt.post_up_script_) {
     log_printf(NOTICE, "executing post-up script '%s'", opt.post_up_script_);
-    int ret = exec_script(opt.post_up_script_, dev.actual_name_);
+    char* const argv[] = { opt.post_up_script_, dev.actual_name_, NULL };
+    char* const evp[] = { NULL };
+    int ret = uanytun_exec(opt.post_up_script_, argv, evp);
   }