tuntap_netbsd.c (n2n-2.8) | : | tuntap_netbsd.c (n2n-3.0) | ||
---|---|---|---|---|
/** | /** | |||
* (C) 2007-20 - ntop.org and contributors | * (C) 2007-21 - ntop.org and contributors | |||
* | * | |||
* This program is free software; you can redistribute it and/or modify | * This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 3 of the License, or | * the Free Software Foundation; either version 3 of the License, or | |||
* (at your option) any later version. | * (at your option) any later version. | |||
* | * | |||
* This program is distributed in the hope that it will be useful, | * This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | * GNU General Public License for more details. | |||
skipping to change at line 27 | skipping to change at line 27 | |||
*/ | */ | |||
#include "n2n.h" | #include "n2n.h" | |||
#ifdef __NetBSD__ | #ifdef __NetBSD__ | |||
#include <errno.h> | #include <errno.h> | |||
#include <string.h> | #include <string.h> | |||
#include <net/if_tap.h> | #include <net/if_tap.h> | |||
void tun_close(tuntap_dev *device); | #define N2N_NETBSD_TAPDEVICE_SIZE 32 | |||
/* ********************************** */ | void tun_close (tuntap_dev *device); | |||
#define N2N_NETBSD_TAPDEVICE_SIZE 32 | int tuntap_open (tuntap_dev *device /* ignored */, | |||
int tuntap_open(tuntap_dev *device /* ignored */, | char *dev, | |||
char *dev, | const char *address_mode, /* static or dhcp */ | |||
const char *address_mode, /* static or dhcp */ | char *device_ip, | |||
char *device_ip, | char *device_mask, | |||
char *device_mask, | const char * device_mac, | |||
const char * device_mac, | int mtu) { | |||
int mtu) { | ||||
char tap_device[N2N_NETBSD_TAPDEVICE_SIZE]; | char tap_device[N2N_NETBSD_TAPDEVICE_SIZE]; | |||
struct ifreq req; | struct ifreq req; | |||
if(dev) { | if(dev) { | |||
snprintf(tap_device, sizeof(tap_device), "/dev/%s", dev); | snprintf(tap_device, sizeof(tap_device), "/dev/%s", dev); | |||
device->fd = open(tap_device, O_RDWR); | device->fd = open(tap_device, O_RDWR); | |||
snprintf(tap_device, sizeof(tap_device), "%s", dev); | snprintf(tap_device, sizeof(tap_device), "%s", dev); | |||
} | } else { | |||
else { | device->fd = open("/dev/tap", O_RDWR); | |||
device->fd = open("/dev/tap", O_RDWR); | if(device->fd >= 0) { | |||
if(device->fd >= 0) { | if(ioctl(device->fd, TAPGIFNAME, &req) == -1) { | |||
if(ioctl(device->fd, TAPGIFNAME, &req) == -1) { | traceEvent(TRACE_ERROR, "Unable to obtain name of tap device (%s | |||
traceEvent(TRACE_ERROR, "Unable to obtain name of tap device (%s)", s | )", strerror(errno)); | |||
trerror(errno)); | close(device->fd); | |||
close(device->fd); | return -1; | |||
return(-1); | } else { | |||
snprintf(tap_device, sizeof(tap_device), req.ifr_name); | ||||
} | ||||
} | } | |||
else { | } | |||
snprintf(tap_device, sizeof(tap_device), req.ifr_name); | ||||
if(device->fd < 0) { | ||||
traceEvent(TRACE_ERROR, "Unable to open tap device (%s)", strerror(errno | ||||
)); | ||||
return -1; | ||||
} else { | ||||
char cmd[256]; | ||||
FILE *fd; | ||||
traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device); | ||||
device->ip_addr = inet_addr(device_ip); | ||||
if(device_mac && device_mac[0] != '\0') { | ||||
// set the hw address before bringing the if up | ||||
snprintf(cmd, sizeof(cmd), "ifconfig %s link %s active", tap_device, | ||||
device_mac); | ||||
system(cmd); | ||||
} | } | |||
} | ||||
} | ||||
if(device->fd < 0) { | snprintf(cmd, sizeof(cmd), "ifconfig %s %s netmask %s mtu %d up", tap_de | |||
traceEvent(TRACE_ERROR, "Unable to open tap device (%s)", strerror(errno)); | vice, device_ip, device_mask, mtu); | |||
return(-1); | ||||
} else { | ||||
char cmd[256]; | ||||
FILE *fd; | ||||
traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device); | ||||
device->ip_addr = inet_addr(device_ip); | ||||
if( device_mac && device_mac[0] != '\0') { | ||||
/* Set the hw address before bringing the if up. */ | ||||
snprintf(cmd, sizeof(cmd), "ifconfig %s link %s active", | ||||
tap_device, device_mac); | ||||
system(cmd); | system(cmd); | |||
} | ||||
snprintf(cmd, sizeof(cmd), "ifconfig %s %s netmask %s mtu %d up", | traceEvent(TRACE_NORMAL, "Interface %s up and running (%s/%s)", tap_devi | |||
tap_device, device_ip, device_mask, mtu); | ce, device_ip, device_mask); | |||
system(cmd); | ||||
traceEvent(TRACE_NORMAL, "Interface %s up and running (%s/%s)", | ||||
tap_device, device_ip, device_mask); | ||||
/* Read MAC address */ | ||||
snprintf(cmd, sizeof(cmd), "ifconfig %s |grep address|cut -c 11-28", tap_dev | ||||
ice); | ||||
/* traceEvent(TRACE_INFO, "%s", cmd); */ | ||||
fd = popen(cmd, "r"); | ||||
if(fd < 0) { | ||||
tun_close(device); | ||||
return(-1); | ||||
} else { | ||||
int a, b, c, d, e, f; | ||||
char buf[256]; | ||||
buf[0] = 0; | // read MAC address | |||
fgets(buf, sizeof(buf), fd); | snprintf(cmd, sizeof(cmd), "ifconfig %s |grep address|cut -c 11-28", tap | |||
pclose(fd); | _device); | |||
// traceEvent(TRACE_INFO, "%s", cmd); | ||||
if(buf[0] == '\0') { | ||||
traceEvent(TRACE_ERROR, "Unable to read %s interface MAC address [%s]", t | fd = popen(cmd, "r"); | |||
ap_device, cmd); | if(fd < 0) { | |||
exit(0); | tun_close(device); | |||
} | return -1; | |||
} else { | ||||
traceEvent(TRACE_NORMAL, "Interface %s mac %s", tap_device, buf); | int a, b, c, d, e, f; | |||
if(sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", &a, &b, &c, &d, &e, &f) == | char buf[256]; | |||
6) { | ||||
device->mac_addr[0] = a, device->mac_addr[1] = b; | buf[0] = 0; | |||
device->mac_addr[2] = c, device->mac_addr[3] = d; | fgets(buf, sizeof(buf), fd); | |||
device->mac_addr[4] = e, device->mac_addr[5] = f; | pclose(fd); | |||
} | ||||
if(buf[0] == '\0') { | ||||
traceEvent(TRACE_ERROR, "Unable to read %s interface MAC address | ||||
[%s]", tap_device, cmd); | ||||
exit(0); | ||||
} | ||||
traceEvent(TRACE_NORMAL, "Interface %s mac %s", tap_device, buf); | ||||
if(sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", &a, &b, &c, &d, &e, | ||||
&f) == 6) { | ||||
device->mac_addr[0] = a, device->mac_addr[1] = b; | ||||
device->mac_addr[2] = c, device->mac_addr[3] = d; | ||||
device->mac_addr[4] = e, device->mac_addr[5] = f; | ||||
} | ||||
} | ||||
} | } | |||
} | ||||
/* read_mac(dev, device->mac_addr); */ | // read_mac(dev, device->mac_addr); | |||
return(device->fd); | ||||
return(device->fd); | ||||
} | } | |||
/* ********************************** */ | int tuntap_read (struct tuntap_dev *tuntap, unsigned char *buf, int len) { | |||
int tuntap_read(struct tuntap_dev *tuntap, unsigned char *buf, int len) { | return(read(tuntap->fd, buf, len)); | |||
return(read(tuntap->fd, buf, len)); | ||||
} | } | |||
/* ********************************** */ | int tuntap_write (struct tuntap_dev *tuntap, unsigned char *buf, int len) { | |||
int tuntap_write(struct tuntap_dev *tuntap, unsigned char *buf, int len) { | return(write(tuntap->fd, buf, len)); | |||
return(write(tuntap->fd, buf, len)); | ||||
} | } | |||
/* ********************************** */ | void tuntap_close (struct tuntap_dev *tuntap) { | |||
void tuntap_close(struct tuntap_dev *tuntap) { | close(tuntap->fd); | |||
close(tuntap->fd); | ||||
} | } | |||
/* Fill out the ip_addr value from the interface. Called to pick up dynamic | // fill out the ip_addr value from the interface, called to pick up dynamic addr | |||
* address changes. */ | ess changes | |||
void tuntap_get_address(struct tuntap_dev *tuntap) | void tuntap_get_address (struct tuntap_dev *tuntap) { | |||
{ | ||||
// no action | ||||
} | } | |||
#endif /* #ifdef __NetBSD__ */ | #endif /* #ifdef __NetBSD__ */ | |||
End of changes. 19 change blocks. | ||||
99 lines changed or deleted | 97 lines changed or added |