Browse Source

readability code clean-up (#551)

pull/554/head
Logan oos Even 4 years ago
committed by GitHub
parent
commit
df14d54a29
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 175
      src/tuntap_freebsd.c
  2. 362
      src/tuntap_linux.c
  3. 185
      src/tuntap_netbsd.c
  4. 174
      src/tuntap_osx.c

175
src/tuntap_freebsd.c

@ -16,118 +16,121 @@
* *
*/ */
#include "n2n.h" #include "n2n.h"
#ifdef __FreeBSD__
void tuntap_close(tuntap_dev *device); #ifdef __FreeBSD__
/* ********************************** */
#define N2N_FREEBSD_TAPDEVICE_SIZE 32 #define N2N_FREEBSD_TAPDEVICE_SIZE 32
int tuntap_open(tuntap_dev *device /* ignored */,
char *dev,
const char *address_mode, /* static or dhcp */
char *device_ip,
char *device_mask,
const char * device_mac,
int mtu) {
int i;
char tap_device[N2N_FREEBSD_TAPDEVICE_SIZE];
for (i = 0; i < 255; i++) {
snprintf(tap_device, sizeof(tap_device), "/dev/tap%d", i);
device->fd = open(tap_device, O_RDWR);
if(device->fd > 0) {
traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device);
break;
}
}
if(device->fd < 0) {
traceEvent(TRACE_ERROR, "Unable to open tap device");
return(-1);
} else {
char buf[256];
FILE *fd;
device->ip_addr = inet_addr(device_ip);
if ( device_mac && device_mac[0] != '\0' )
{
/* FIXME - This is not tested. Might be wrong syntax for OS X */
/* Set the hw address before bringing the if up. */
snprintf(buf, sizeof(buf), "ifconfig tap%d ether %s",
i, device_mac);
system(buf);
}
snprintf(buf, sizeof(buf), "ifconfig tap%d %s netmask %s mtu %d up",
i, device_ip, device_mask, mtu);
system(buf);
traceEvent(TRACE_NORMAL, "Interface tap%d up and running (%s/%s)", void tuntap_close (tuntap_dev *device);
i, device_ip, device_mask);
/* Read MAC address */
snprintf(buf, sizeof(buf), "ifconfig tap%d |grep ether|cut -c 8-24", i); int tuntap_open (tuntap_dev *device /* ignored */,
/* traceEvent(TRACE_INFO, "%s", buf); */ char *dev,
const char *address_mode, /* static or dhcp */
char *device_ip,
char *device_mask,
const char * device_mac,
int mtu) {
fd = popen(buf, "r"); int i;
if(fd < 0) { char tap_device[N2N_FREEBSD_TAPDEVICE_SIZE];
tuntap_close(device);
return(-1); for(i = 0; i < 255; i++) {
snprintf(tap_device, sizeof(tap_device), "/dev/tap%d", i);
device->fd = open(tap_device, O_RDWR);
if(device->fd > 0) {
traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device);
break;
}
}
if(device->fd < 0) {
traceEvent(TRACE_ERROR, "Unable to open tap device");
return -1;
} else { } else {
int a, b, c, d, e, f; char buf[256];
FILE *fd;
buf[0] = 0;
fgets(buf, sizeof(buf), fd); device->ip_addr = inet_addr(device_ip);
pclose(fd);
if(device_mac && device_mac[0] != '\0') {
if(buf[0] == '\0') { // FIXME - this is not tested, might be wrong syntax for OS X
traceEvent(TRACE_ERROR, "Unable to read tap%d interface MAC address");
exit(0); // set the hw address before bringing the if up
} snprintf(buf, sizeof(buf), "ifconfig tap%d ether %s", i, device_mac);
system(buf);
traceEvent(TRACE_NORMAL, "Interface tap%d mac %s", i, 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; snprintf(buf, sizeof(buf), "ifconfig tap%d %s netmask %s mtu %d up", i, device_ip, device_mask, mtu);
device->mac_addr[2] = c, device->mac_addr[3] = d; system(buf);
device->mac_addr[4] = e, device->mac_addr[5] = f;
} traceEvent(TRACE_NORMAL, "Interface tap%d up and running (%s/%s)", i, device_ip, device_mask);
// read MAC address
snprintf(buf, sizeof(buf), "ifconfig tap%d |grep ether|cut -c 8-24", i);
// traceEvent(TRACE_INFO, "%s", buf);
fd = popen(buf, "r");
if(fd < 0) {
tuntap_close(device);
return -1;
} else {
int a, b, c, d, e, f;
buf[0] = 0;
fgets(buf, sizeof(buf), fd);
pclose(fd);
if(buf[0] == '\0') {
traceEvent(TRACE_ERROR, "Unable to read tap%d interface MAC address");
exit(0);
}
traceEvent(TRACE_NORMAL, "Interface tap%d mac %s", i, 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
* address changes. */ // fill out the ip_addr value from the interface, called to pick up dynamic address changes
void tuntap_get_address(struct tuntap_dev *tuntap) void tuntap_get_address (struct tuntap_dev *tuntap) {
{
// no action
} }
#endif /* #ifdef __FreeBSD__ */ #endif /* #ifdef __FreeBSD__ */

362
src/tuntap_linux.c

@ -16,71 +16,72 @@
* *
*/ */
#ifdef __linux__ #ifdef __linux__
#include "n2n.h" #include "n2n.h"
/* ********************************** */
static int setup_ifname(int fd, const char *ifname, const char *ipaddr, static int setup_ifname (int fd, const char *ifname, const char *ipaddr,
const char *netmask, uint8_t *mac, int mtu) { const char *netmask, uint8_t *mac, int mtu) {
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr)); struct ifreq ifr;
strncpy(ifr.ifr_name, ifname, IFNAMSIZ); memset(&ifr, 0, sizeof(ifr));
ifr.ifr_name[IFNAMSIZ-1] = '\0';
ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
memcpy(ifr.ifr_hwaddr.sa_data, mac, 6); ifr.ifr_name[IFNAMSIZ-1] = '\0';
if(ioctl(fd, SIOCSIFHWADDR, &ifr) == -1) { ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFHWADDR) failed [%d]: %s", errno, strerror(errno)); memcpy(ifr.ifr_hwaddr.sa_data, mac, 6);
return(-1);
}
ifr.ifr_addr.sa_family = AF_INET; if(ioctl(fd, SIOCSIFHWADDR, &ifr) == -1) {
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFHWADDR) failed [%d]: %s", errno, strerror(errno));
return -1;
}
ifr.ifr_addr.sa_family = AF_INET;
/* Interface Address */ // interface address
inet_pton(AF_INET, ipaddr, &((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr); inet_pton(AF_INET, ipaddr, &((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr);
if(ioctl(fd, SIOCSIFADDR, &ifr) == -1) { if(ioctl(fd, SIOCSIFADDR, &ifr) == -1) {
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFADDR) failed [%d]: %s", errno, strerror(errno)); traceEvent(TRACE_ERROR, "ioctl(SIOCSIFADDR) failed [%d]: %s", errno, strerror(errno));
return(-2); return -2;
} }
/* Netmask */ // netmask
if(netmask && (((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr.s_addr != 0)) { if(netmask && (((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr.s_addr != 0)) {
inet_pton(AF_INET, netmask, &((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr); inet_pton(AF_INET, netmask, &((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr);
if(ioctl(fd, SIOCSIFNETMASK, &ifr) == -1) { if(ioctl(fd, SIOCSIFNETMASK, &ifr) == -1) {
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFNETMASK, %s) failed [%d]: %s", netmask, errno, strerror(errno)); traceEvent(TRACE_ERROR, "ioctl(SIOCSIFNETMASK, %s) failed [%d]: %s", netmask, errno, strerror(errno));
return(-3); return -3;
}
} }
}
/* MTU */ // MTU
ifr.ifr_mtu = mtu; ifr.ifr_mtu = mtu;
if(ioctl(fd, SIOCSIFMTU, &ifr) == -1) { if(ioctl(fd, SIOCSIFMTU, &ifr) == -1) {
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFMTU) failed [%d]: %s", errno, strerror(errno)); traceEvent(TRACE_ERROR, "ioctl(SIOCSIFMTU) failed [%d]: %s", errno, strerror(errno));
return(-4); return -4;
} }
/* Set up and running */ // set up and running
if(ioctl(fd, SIOCGIFFLAGS, &ifr) == -1) { if(ioctl(fd, SIOCGIFFLAGS, &ifr) == -1) {
traceEvent(TRACE_ERROR, "ioctl(SIOCGIFFLAGS) failed [%d]: %s", errno, strerror(errno)); traceEvent(TRACE_ERROR, "ioctl(SIOCGIFFLAGS) failed [%d]: %s", errno, strerror(errno));
return(-5); return -5;
} }
ifr.ifr_flags |= (IFF_UP | IFF_RUNNING); ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
if(ioctl(fd, SIOCSIFFLAGS, &ifr) == -1) { if(ioctl(fd, SIOCSIFFLAGS, &ifr) == -1) {
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFFLAGS) failed [%d]: %s", errno, strerror(errno)); traceEvent(TRACE_ERROR, "ioctl(SIOCSIFFLAGS) failed [%d]: %s", errno, strerror(errno));
return(-6); return -6;
} }
return(0); return 0;
} }
/* ********************************** */
/** @brief Open and configure the TAP device for packet read/write. /** @brief Open and configure the TAP device for packet read/write.
* *
@ -88,7 +89,7 @@ static int setup_ifname(int fd, const char *ifname, const char *ipaddr,
* configures it. * configures it.
* *
* @param device - [inout] a device info holder object * @param device - [inout] a device info holder object
* @param dev - user-defined name for the new iface, * @param dev - user-defined name for the new iface,
* if NULL system will assign a name * if NULL system will assign a name
* @param device_ip - address of iface * @param device_ip - address of iface
* @param device_mask - netmask for device_ip * @param device_mask - netmask for device_ip
@ -97,179 +98,186 @@ static int setup_ifname(int fd, const char *ifname, const char *ipaddr,
* @return - negative value on error * @return - negative value on error
* - non-negative file-descriptor on success * - non-negative file-descriptor on success
*/ */
int tuntap_open(tuntap_dev *device, int tuntap_open (tuntap_dev *device,
char *dev, /* user-definable interface name, eg. edge0 */ char *dev, /* user-definable interface name, eg. edge0 */
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 *tuntap_device = "/dev/net/tun";
int ioctl_fd; char *tuntap_device = "/dev/net/tun";
struct ifreq ifr; int ioctl_fd;
int rc; struct ifreq ifr;
int nl_fd; int rc;
char nl_buf[8192]; /* >= 8192 to avoid truncation, see "man 7 netlink" */ int nl_fd;
struct iovec iov; char nl_buf[8192]; /* >= 8192 to avoid truncation, see "man 7 netlink" */
struct sockaddr_nl sa; struct iovec iov;
int up_and_running = 0; struct sockaddr_nl sa;
struct msghdr msg; int up_and_running = 0;
struct msghdr msg;
device->fd = open(tuntap_device, O_RDWR);
if(device->fd < 0) { device->fd = open(tuntap_device, O_RDWR);
traceEvent(TRACE_ERROR, "tuntap open() error: %s[%d]. Is the tun kernel module loaded?\n", strerror(errno), errno); if(device->fd < 0) {
return -1; traceEvent(TRACE_ERROR, "tuntap open() error: %s[%d]. Is the tun kernel module loaded?\n", strerror(errno), errno);
} return -1;
}
memset(&ifr, 0, sizeof(ifr)); memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP|IFF_NO_PI; /* Want a TAP device for layer 2 frames. */
strncpy(ifr.ifr_name, dev, IFNAMSIZ-1);
ifr.ifr_name[IFNAMSIZ-1] = '\0';
rc = ioctl(device->fd, TUNSETIFF, (void *)&ifr);
if(rc < 0) { // want a TAP device for layer 2 frames
traceEvent(TRACE_ERROR, "tuntap ioctl(TUNSETIFF, IFF_TAP) error: %s[%d]\n", strerror(errno), rc); ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
close(device->fd);
return -1;
}
/* Store the device name for later reuse */ strncpy(ifr.ifr_name, dev, IFNAMSIZ-1);
strncpy(device->dev_name, ifr.ifr_name, MIN(IFNAMSIZ, N2N_IFNAMSIZ) ); ifr.ifr_name[IFNAMSIZ-1] = '\0';
rc = ioctl(device->fd, TUNSETIFF, (void *)&ifr);
if(device_mac && device_mac[0]) { if(rc < 0) {
/* Use the user-provided MAC */ traceEvent(TRACE_ERROR, "tuntap ioctl(TUNSETIFF, IFF_TAP) error: %s[%d]\n", strerror(errno), rc);
str2mac(device->mac_addr, device_mac); close(device->fd);
} else { return -1;
/* Set an explicit random MAC to know the exact MAC in use. Manually }
* reading the MAC address is not safe as it may change internally
* also after the TAP interface UP status has been notified. */
int i;
for(i = 0; i < 6; i++) // store the device name for later reuse
device->mac_addr[i] = n2n_rand(); strncpy(device->dev_name, ifr.ifr_name, MIN(IFNAMSIZ, N2N_IFNAMSIZ));
device->mac_addr[0] &= ~0x01; /* Clear multicast bit */ if(device_mac && device_mac[0]) {
device->mac_addr[0] |= 0x02; /* Set locally-assigned bit */ // use the user-provided MAC
} str2mac(device->mac_addr, device_mac);
} else {
// set an explicit random MAC to know the exact MAC in use, manually
// reading the MAC address is not safe as it may change internally
// also after the TAP interface UP status has been notified
int i;
/* Initialize Netlink socket */ for(i = 0; i < 6; i++)
if((nl_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1) { device->mac_addr[i] = n2n_rand();
traceEvent(TRACE_ERROR, "netlink socket creation failed [%d]: %s", errno, strerror(errno));
return -1;
}
iov.iov_base = nl_buf; // clear multicast bit
iov.iov_len = sizeof(nl_buf); device->mac_addr[0] &= ~0x01;
memset(&sa, 0, sizeof(sa)); // set locally-assigned bit
sa.nl_family = PF_NETLINK; device->mac_addr[0] |= 0x02;
sa.nl_groups = RTMGRP_LINK; }
sa.nl_pid = getpid();
memset(&msg, 0, sizeof(msg)); // initialize netlink socket
msg.msg_name = &sa; if((nl_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1) {
msg.msg_namelen = sizeof(sa); traceEvent(TRACE_ERROR, "netlink socket creation failed [%d]: %s", errno, strerror(errno));
msg.msg_iov = &iov; return -1;
msg.msg_iovlen = 1; }
/* Subscribe to interface events */ iov.iov_base = nl_buf;
if(bind(nl_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1) { iov.iov_len = sizeof(nl_buf);
traceEvent(TRACE_ERROR, "netlink socket bind failed [%d]: %s", errno, strerror(errno));
return -1;
}
if((ioctl_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) { memset(&sa, 0, sizeof(sa));
traceEvent(TRACE_ERROR, "socket creation failed [%d]: %s", errno, strerror(errno)); sa.nl_family = PF_NETLINK;
close(nl_fd); sa.nl_groups = RTMGRP_LINK;
return -1; sa.nl_pid = getpid();
}
if(setup_ifname(ioctl_fd, device->dev_name, device_ip, device_mask, device->mac_addr, mtu) < 0) { memset(&msg, 0, sizeof(msg));
close(nl_fd); msg.msg_name = &sa;
close(ioctl_fd); msg.msg_namelen = sizeof(sa);
close(device->fd); msg.msg_iov = &iov;
return -1; msg.msg_iovlen = 1;
}
close(ioctl_fd); // subscribe to interface events
if(bind(nl_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
traceEvent(TRACE_ERROR, "netlink socket bind failed [%d]: %s", errno, strerror(errno));
return -1;
}
/* Wait for the up and running notification */ if((ioctl_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
traceEvent(TRACE_INFO, "Waiting for TAP interface to be up and running..."); traceEvent(TRACE_ERROR, "socket creation failed [%d]: %s", errno, strerror(errno));
close(nl_fd);
return -1;
}
if(setup_ifname(ioctl_fd, device->dev_name, device_ip, device_mask, device->mac_addr, mtu) < 0) {
close(nl_fd);
close(ioctl_fd);
close(device->fd);
return -1;
}
close(ioctl_fd);
while(!up_and_running) { // wait for the up and running notification
ssize_t len = recvmsg(nl_fd, &msg, 0); traceEvent(TRACE_INFO, "Waiting for TAP interface to be up and running...");
struct nlmsghdr *nh;
for(nh = (struct nlmsghdr *)nl_buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len)) { while(!up_and_running) {
if(nh->nlmsg_type == NLMSG_ERROR) { ssize_t len = recvmsg(nl_fd, &msg, 0);
traceEvent(TRACE_DEBUG, "nh->nlmsg_type == NLMSG_ERROR"); struct nlmsghdr *nh;
break;
}
if(nh->nlmsg_type == NLMSG_DONE) for(nh = (struct nlmsghdr *)nl_buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len)) {
break; if(nh->nlmsg_type == NLMSG_ERROR) {
traceEvent(TRACE_DEBUG, "nh->nlmsg_type == NLMSG_ERROR");
break;
}
if(nh->nlmsg_type == NETLINK_GENERIC) { if(nh->nlmsg_type == NLMSG_DONE)
struct ifinfomsg *ifi = NLMSG_DATA(nh); break;
/* NOTE: skipping interface name check, assuming it's our TAP */ if(nh->nlmsg_type == NETLINK_GENERIC) {
if((ifi->ifi_flags & IFF_UP) && (ifi->ifi_flags & IFF_RUNNING)) { struct ifinfomsg *ifi = NLMSG_DATA(nh);
up_and_running = 1;
traceEvent(TRACE_INFO, "Interface is up and running"); // NOTE: skipping interface name check, assuming it's our TAP
break; if((ifi->ifi_flags & IFF_UP) && (ifi->ifi_flags & IFF_RUNNING)) {
up_and_running = 1;
traceEvent(TRACE_INFO, "Interface is up and running");
break;
}
}
} }
}
} }
}
close(nl_fd); close(nl_fd);
device->ip_addr = inet_addr(device_ip); device->ip_addr = inet_addr(device_ip);
device->device_mask = inet_addr(device_mask); device->device_mask = inet_addr(device_mask);
device->if_idx = if_nametoindex(dev); device->if_idx = if_nametoindex(dev);
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 address changes
* address changes. */ void tuntap_get_address (struct tuntap_dev *tuntap) {
void tuntap_get_address(struct tuntap_dev *tuntap) {
struct ifreq ifr;
int fd;
if((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) { struct ifreq ifr;
traceEvent(TRACE_ERROR, "socket creation failed [%d]: %s", errno, strerror(errno)); int fd;
return;
}
ifr.ifr_addr.sa_family = AF_INET; if((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
strncpy(ifr.ifr_name, tuntap->dev_name, IFNAMSIZ); traceEvent(TRACE_ERROR, "socket creation failed [%d]: %s", errno, strerror(errno));
ifr.ifr_name[IFNAMSIZ-1] = '\0'; return;
}
if(ioctl(fd, SIOCGIFADDR, &ifr) != -1) ifr.ifr_addr.sa_family = AF_INET;
tuntap->ip_addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr; strncpy(ifr.ifr_name, tuntap->dev_name, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ-1] = '\0';
close(fd); if(ioctl(fd, SIOCGIFADDR, &ifr) != -1)
tuntap->ip_addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
close(fd);
} }
#endif /* #ifdef __linux__ */ #endif /* #ifdef __linux__ */

185
src/tuntap_netbsd.c

@ -16,130 +16,133 @@
* *
*/ */
#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 #define N2N_NETBSD_TAPDEVICE_SIZE 32
int tuntap_open(tuntap_dev *device /* ignored */,
char *dev,
const char *address_mode, /* static or dhcp */
char *device_ip,
char *device_mask,
const char * device_mac,
int mtu) {
char tap_device[N2N_NETBSD_TAPDEVICE_SIZE];
struct ifreq req;
if(dev) {
snprintf(tap_device, sizeof(tap_device), "/dev/%s", dev);
device->fd = open(tap_device, O_RDWR);
snprintf(tap_device, sizeof(tap_device), "%s", dev);
}
else {
device->fd = open("/dev/tap", O_RDWR);
if(device->fd >= 0) {
if(ioctl(device->fd, TAPGIFNAME, &req) == -1) {
traceEvent(TRACE_ERROR, "Unable to obtain name of tap device (%s)", strerror(errno));
close(device->fd);
return(-1);
}
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); void tun_close (tuntap_dev *device);
device->ip_addr = inet_addr(device_ip);
if( device_mac && device_mac[0] != '\0') { int tuntap_open (tuntap_dev *device /* ignored */,
/* Set the hw address before bringing the if up. */ char *dev,
snprintf(cmd, sizeof(cmd), "ifconfig %s link %s active", const char *address_mode, /* static or dhcp */
tap_device, device_mac); char *device_ip,
system(cmd); char *device_mask,
const char * device_mac,
int mtu) {
char tap_device[N2N_NETBSD_TAPDEVICE_SIZE];
struct ifreq req;
if(dev) {
snprintf(tap_device, sizeof(tap_device), "/dev/%s", dev);
device->fd = open(tap_device, O_RDWR);
snprintf(tap_device, sizeof(tap_device), "%s", dev);
} else {
device->fd = open("/dev/tap", O_RDWR);
if(device->fd >= 0) {
if(ioctl(device->fd, TAPGIFNAME, &req) == -1) {
traceEvent(TRACE_ERROR, "Unable to obtain name of tap device (%s)", strerror(errno));
close(device->fd);
return -1;
} else {
snprintf(tap_device, sizeof(tap_device), req.ifr_name);
}
}
} }
snprintf(cmd, sizeof(cmd), "ifconfig %s %s netmask %s mtu %d up", if(device->fd < 0) {
tap_device, device_ip, device_mask, mtu); traceEvent(TRACE_ERROR, "Unable to open tap device (%s)", strerror(errno));
system(cmd); return -1;
} else {
char cmd[256];
FILE *fd;
traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device);
traceEvent(TRACE_NORMAL, "Interface %s up and running (%s/%s)", device->ip_addr = inet_addr(device_ip);
tap_device, device_ip, device_mask);
/* Read MAC address */ if(device_mac && device_mac[0] != '\0') {
snprintf(cmd, sizeof(cmd), "ifconfig %s |grep address|cut -c 11-28", tap_device); // set the hw address before bringing the if up
/* traceEvent(TRACE_INFO, "%s", cmd); */ snprintf(cmd, sizeof(cmd), "ifconfig %s link %s active", tap_device, device_mac);
system(cmd);
}
fd = popen(cmd, "r"); snprintf(cmd, sizeof(cmd), "ifconfig %s %s netmask %s mtu %d up", tap_device, device_ip, device_mask, mtu);
if(fd < 0) { system(cmd);
tun_close(device);
return(-1); traceEvent(TRACE_NORMAL, "Interface %s up and running (%s/%s)", tap_device, device_ip, device_mask);
} else {
int a, b, c, d, e, f; // read MAC address
char buf[256]; snprintf(cmd, sizeof(cmd), "ifconfig %s |grep address|cut -c 11-28", tap_device);
// traceEvent(TRACE_INFO, "%s", cmd);
buf[0] = 0;
fgets(buf, sizeof(buf), fd); fd = popen(cmd, "r");
pclose(fd); if(fd < 0) {
tun_close(device);
if(buf[0] == '\0') { return -1;
traceEvent(TRACE_ERROR, "Unable to read %s interface MAC address [%s]", tap_device, cmd); } else {
exit(0); int a, b, c, d, e, f;
} char buf[256];
traceEvent(TRACE_NORMAL, "Interface %s mac %s", tap_device, buf); buf[0] = 0;
if(sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", &a, &b, &c, &d, &e, &f) == 6) { fgets(buf, sizeof(buf), fd);
device->mac_addr[0] = a, device->mac_addr[1] = b; pclose(fd);
device->mac_addr[2] = c, device->mac_addr[3] = d;
device->mac_addr[4] = e, device->mac_addr[5] = f; 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
* address changes. */ // fill out the ip_addr value from the interface, called to pick up dynamic address 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__ */

174
src/tuntap_osx.c

@ -16,119 +16,119 @@
* *
*/ */
#include "n2n.h" #include "n2n.h"
#ifdef __APPLE__
void tun_close(tuntap_dev *device); #ifdef __APPLE__
/* ********************************** */
#define N2N_OSX_TAPDEVICE_SIZE 32 #define N2N_OSX_TAPDEVICE_SIZE 32
int tuntap_open(tuntap_dev *device /* ignored */,
char *dev,
const char *address_mode, /* static or dhcp */
char *device_ip,
char *device_mask,
const char * device_mac,
int mtu) {
int i;
char tap_device[N2N_OSX_TAPDEVICE_SIZE];
for (i = 0; i < 255; i++) {
snprintf(tap_device, sizeof(tap_device), "/dev/tap%d", i);
device->fd = open(tap_device, O_RDWR);
if(device->fd > 0) {
traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device);
break;
}
}
if(device->fd < 0) {
traceEvent(TRACE_ERROR, "Unable to open any tap devices /dev/tap0 through /dev/tap254. Is this user properly authorized to access those descriptors?");
traceEvent(TRACE_ERROR, "Please read https://github.com/ntop/n2n/blob/dev/doc/macOS.md");
return(-1);
} else {
char buf[256];
FILE *fd;
device->ip_addr = inet_addr(device_ip);
if ( device_mac && device_mac[0] != '\0' )
{
/* FIXME - This is not tested. Might be wrong syntax for OS X */
/* Set the hw address before bringing the if up. */
snprintf(buf, sizeof(buf), "ifconfig tap%d ether %s",
i, device_mac);
system(buf);
}
snprintf(buf, sizeof(buf), "ifconfig tap%d %s netmask %s mtu %d up",
i, device_ip, device_mask, mtu);
system(buf);
traceEvent(TRACE_NORMAL, "Interface tap%d up and running (%s/%s)", void tun_close (tuntap_dev *device);
i, device_ip, device_mask);
int tuntap_open (tuntap_dev *device /* ignored */,
char *dev,
const char *address_mode, /* static or dhcp */
char *device_ip,
char *device_mask,
const char * device_mac,
int mtu) {
/* Read MAC address */ int i;
char tap_device[N2N_OSX_TAPDEVICE_SIZE];
snprintf(buf, sizeof(buf), "ifconfig tap%d |grep ether|cut -c 8-24", i); for(i = 0; i < 255; i++) {
/* traceEvent(TRACE_INFO, "%s", buf); */ snprintf(tap_device, sizeof(tap_device), "/dev/tap%d", i);
device->fd = open(tap_device, O_RDWR);
if(device->fd > 0) {
traceEvent(TRACE_NORMAL, "Succesfully open %s", tap_device);
break;
}
}
fd = popen(buf, "r"); if(device->fd < 0) {
if(fd < 0) { traceEvent(TRACE_ERROR, "Unable to open any tap devices /dev/tap0 through /dev/tap254. Is this user properly authorized to access those descriptors?");
tuntap_close(device); traceEvent(TRACE_ERROR, "Please read https://github.com/ntop/n2n/blob/dev/doc/Building.md");
return(-1); return -1;
} else { } else {
int a, b, c, d, e, f; char buf[256];
FILE *fd;
buf[0] = 0;
fgets(buf, sizeof(buf), fd); device->ip_addr = inet_addr(device_ip);
pclose(fd);
if(device_mac && device_mac[0] != '\0') {
if(buf[0] == '\0') { // FIXME - this is not tested. might be wrong syntax for OS X
traceEvent(TRACE_ERROR, "Unable to read tap%d interface MAC address"); // set the hw address before bringing the if up
exit(0); snprintf(buf, sizeof(buf), "ifconfig tap%d ether %s", i, device_mac);
} system(buf);
}
traceEvent(TRACE_NORMAL, "Interface tap%d [MTU %d] mac %s", i, mtu, buf);
if(sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", &a, &b, &c, &d, &e, &f) == 6) { snprintf(buf, sizeof(buf), "ifconfig tap%d %s netmask %s mtu %d up", i, device_ip, device_mask, mtu);
device->mac_addr[0] = a, device->mac_addr[1] = b; system(buf);
device->mac_addr[2] = c, device->mac_addr[3] = d;
device->mac_addr[4] = e, device->mac_addr[5] = f; traceEvent(TRACE_NORMAL, "Interface tap%d up and running (%s/%s)", i, device_ip, device_mask);
}
// read MAC address
snprintf(buf, sizeof(buf), "ifconfig tap%d |grep ether|cut -c 8-24", i);
// traceEvent(TRACE_INFO, "%s", buf);
fd = popen(buf, "r");
if(fd < 0) {
tuntap_close(device);
return -1;
} else {
int a, b, c, d, e, f;
buf[0] = 0;
fgets(buf, sizeof(buf), fd);
pclose(fd);
if(buf[0] == '\0') {
traceEvent(TRACE_ERROR, "Unable to read tap%d interface MAC address");
exit(0);
}
traceEvent(TRACE_NORMAL, "Interface tap%d [MTU %d] mac %s", i, mtu, 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 address changes
* address changes. */ void tuntap_get_address (struct tuntap_dev *tuntap) {
void tuntap_get_address(struct tuntap_dev *tuntap)
{ // no action
} }
#endif /* __APPLE__ */ #endif /* __APPLE__ */

Loading…
Cancel
Save