-/*
- open the local ethertap device
-*/
-int setup_device(void)
-{
- struct ifreq ifr;
-
-cp
- if(!get_config_string(lookup_config(config_tree, "Device"), &device_fname)))
- device_fname = DEFAULT_DEVICE;
-
-cp
- if((device_fd = open(device_fname, O_RDWR | O_NONBLOCK)) < 0)
- {
- syslog(LOG_ERR, _("Could not open %s: %m"), device_fname);
- return -1;
- }
-cp
- device_fd = device_fd;
-
- /* Set default MAC address for ethertap devices */
-
- mymac.type = SUBNET_MAC;
- mymac.net.mac.address.x[0] = 0xfe;
- mymac.net.mac.address.x[1] = 0xfd;
- mymac.net.mac.address.x[2] = 0x00;
- mymac.net.mac.address.x[3] = 0x00;
- mymac.net.mac.address.x[4] = 0x00;
- mymac.net.mac.address.x[5] = 0x00;
-
-#ifdef HAVE_TUNTAP
- /* Ok now check if this is an old ethertap or a new tun/tap thingie */
-
- memset(&ifr, 0, sizeof(ifr));
-cp
- ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
- if (netname)
- strncpy(ifr.ifr_name, netname, IFNAMSIZ);
-cp
- if (!ioctl(device_fd, TUNSETIFF, (void *) &ifr))
- {
- device_info = _("Linux tun/tap device");
- device_type = DEVICE_TYPE_TUNTAP;
- }
- else
- if (!ioctl(device_fd, (('T'<< 8) | 202), (void *) &ifr))
- {
- syslog(LOG_WARNING, _("Old ioctl() request was needed for %s"), device_fname);
- device_type = TAP_TYPE_TUNTAP;
- device_info = _("Linux tun/tap device");
- }
- else
+int device_fd = -1;
+static device_type_t device_type;
+char *device = NULL;
+char *iface = NULL;
+static char *type = NULL;
+static char ifrname[IFNAMSIZ];
+static char *device_info;
+
+static bool setup_device(void) {
+ if(!get_config_string(lookup_config(config_tree, "Device"), &device))
+ device = xstrdup(DEFAULT_DEVICE);
+
+ if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
+ if(netname)
+ iface = xstrdup(netname);
+
+ device_fd = open(device, O_RDWR | O_NONBLOCK);
+
+ if(device_fd < 0) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", device, strerror(errno));
+ return false;
+ }
+
+#ifdef FD_CLOEXEC
+ fcntl(device_fd, F_SETFD, FD_CLOEXEC);