mirror of
https://github.com/ntop/n2n.git
synced 2024-09-20 00:51:10 +02:00
readability code clean-up (#551)
This commit is contained in:
parent
71065278fa
commit
df14d54a29
|
@ -16,15 +16,19 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include "n2n.h"
|
#include "n2n.h"
|
||||||
|
|
||||||
|
|
||||||
#ifdef __FreeBSD__
|
#ifdef __FreeBSD__
|
||||||
|
|
||||||
void tuntap_close(tuntap_dev *device);
|
|
||||||
|
|
||||||
/* ********************************** */
|
|
||||||
|
|
||||||
#define N2N_FREEBSD_TAPDEVICE_SIZE 32
|
#define N2N_FREEBSD_TAPDEVICE_SIZE 32
|
||||||
|
|
||||||
|
|
||||||
|
void tuntap_close (tuntap_dev *device);
|
||||||
|
|
||||||
|
|
||||||
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 */
|
||||||
|
@ -32,6 +36,7 @@ int tuntap_open(tuntap_dev *device /* ignored */,
|
||||||
char *device_mask,
|
char *device_mask,
|
||||||
const char * device_mac,
|
const char * device_mac,
|
||||||
int mtu) {
|
int mtu) {
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
char tap_device[N2N_FREEBSD_TAPDEVICE_SIZE];
|
char tap_device[N2N_FREEBSD_TAPDEVICE_SIZE];
|
||||||
|
|
||||||
|
@ -47,39 +52,34 @@ int tuntap_open(tuntap_dev *device /* ignored */,
|
||||||
|
|
||||||
if(device->fd < 0) {
|
if(device->fd < 0) {
|
||||||
traceEvent(TRACE_ERROR, "Unable to open tap device");
|
traceEvent(TRACE_ERROR, "Unable to open tap device");
|
||||||
return(-1);
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
char buf[256];
|
char buf[256];
|
||||||
FILE *fd;
|
FILE *fd;
|
||||||
|
|
||||||
device->ip_addr = inet_addr(device_ip);
|
device->ip_addr = inet_addr(device_ip);
|
||||||
|
|
||||||
if ( device_mac && device_mac[0] != '\0' )
|
if(device_mac && device_mac[0] != '\0') {
|
||||||
{
|
// FIXME - this is not tested, might be wrong syntax for OS X
|
||||||
/* FIXME - This is not tested. Might be wrong syntax for OS X */
|
|
||||||
|
|
||||||
/* Set the hw address before bringing the if up. */
|
// set the hw address before bringing the if up
|
||||||
snprintf(buf, sizeof(buf), "ifconfig tap%d ether %s",
|
snprintf(buf, sizeof(buf), "ifconfig tap%d ether %s", i, device_mac);
|
||||||
i, device_mac);
|
|
||||||
system(buf);
|
system(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(buf, sizeof(buf), "ifconfig tap%d %s netmask %s mtu %d up",
|
snprintf(buf, sizeof(buf), "ifconfig tap%d %s netmask %s mtu %d up", i, device_ip, device_mask, mtu);
|
||||||
i, device_ip, device_mask, mtu);
|
|
||||||
system(buf);
|
system(buf);
|
||||||
|
|
||||||
traceEvent(TRACE_NORMAL, "Interface tap%d up and running (%s/%s)",
|
traceEvent(TRACE_NORMAL, "Interface tap%d up and running (%s/%s)", i, device_ip, device_mask);
|
||||||
i, device_ip, device_mask);
|
|
||||||
|
|
||||||
/* Read MAC address */
|
|
||||||
|
|
||||||
|
// read MAC address
|
||||||
snprintf(buf, sizeof(buf), "ifconfig tap%d |grep ether|cut -c 8-24", i);
|
snprintf(buf, sizeof(buf), "ifconfig tap%d |grep ether|cut -c 8-24", i);
|
||||||
/* traceEvent(TRACE_INFO, "%s", buf); */
|
// traceEvent(TRACE_INFO, "%s", buf);
|
||||||
|
|
||||||
fd = popen(buf, "r");
|
fd = popen(buf, "r");
|
||||||
if(fd < 0) {
|
if(fd < 0) {
|
||||||
tuntap_close(device);
|
tuntap_close(device);
|
||||||
return(-1);
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
int a, b, c, d, e, f;
|
int a, b, c, d, e, f;
|
||||||
|
|
||||||
|
@ -102,32 +102,35 @@ int tuntap_open(tuntap_dev *device /* ignored */,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* 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__ */
|
||||||
|
|
|
@ -16,14 +16,16 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#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;
|
struct ifreq ifr;
|
||||||
|
|
||||||
memset(&ifr, 0, sizeof(ifr));
|
memset(&ifr, 0, sizeof(ifr));
|
||||||
|
@ -36,51 +38,50 @@ static int setup_ifname(int fd, const char *ifname, const char *ipaddr,
|
||||||
|
|
||||||
if(ioctl(fd, SIOCSIFHWADDR, &ifr) == -1) {
|
if(ioctl(fd, SIOCSIFHWADDR, &ifr) == -1) {
|
||||||
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFHWADDR) failed [%d]: %s", errno, strerror(errno));
|
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFHWADDR) failed [%d]: %s", errno, strerror(errno));
|
||||||
return(-1);
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ifr.ifr_addr.sa_family = AF_INET;
|
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.
|
||||||
*
|
*
|
||||||
|
@ -104,6 +105,7 @@ int tuntap_open(tuntap_dev *device,
|
||||||
char *device_mask,
|
char *device_mask,
|
||||||
const char * device_mac,
|
const char * device_mac,
|
||||||
int mtu) {
|
int mtu) {
|
||||||
|
|
||||||
char *tuntap_device = "/dev/net/tun";
|
char *tuntap_device = "/dev/net/tun";
|
||||||
int ioctl_fd;
|
int ioctl_fd;
|
||||||
struct ifreq ifr;
|
struct ifreq ifr;
|
||||||
|
@ -122,7 +124,10 @@ int tuntap_open(tuntap_dev *device,
|
||||||
}
|
}
|
||||||
|
|
||||||
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. */
|
|
||||||
|
// want a TAP device for layer 2 frames
|
||||||
|
ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
|
||||||
|
|
||||||
strncpy(ifr.ifr_name, dev, IFNAMSIZ-1);
|
strncpy(ifr.ifr_name, dev, IFNAMSIZ-1);
|
||||||
ifr.ifr_name[IFNAMSIZ-1] = '\0';
|
ifr.ifr_name[IFNAMSIZ-1] = '\0';
|
||||||
rc = ioctl(device->fd, TUNSETIFF, (void *)&ifr);
|
rc = ioctl(device->fd, TUNSETIFF, (void *)&ifr);
|
||||||
|
@ -133,26 +138,29 @@ int tuntap_open(tuntap_dev *device,
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Store the device name for later reuse */
|
// store the device name for later reuse
|
||||||
strncpy(device->dev_name, ifr.ifr_name, MIN(IFNAMSIZ, N2N_IFNAMSIZ));
|
strncpy(device->dev_name, ifr.ifr_name, MIN(IFNAMSIZ, N2N_IFNAMSIZ));
|
||||||
|
|
||||||
if(device_mac && device_mac[0]) {
|
if(device_mac && device_mac[0]) {
|
||||||
/* Use the user-provided MAC */
|
// use the user-provided MAC
|
||||||
str2mac(device->mac_addr, device_mac);
|
str2mac(device->mac_addr, device_mac);
|
||||||
} else {
|
} else {
|
||||||
/* Set an explicit random MAC to know the exact MAC in use. Manually
|
// 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
|
// reading the MAC address is not safe as it may change internally
|
||||||
* also after the TAP interface UP status has been notified. */
|
// also after the TAP interface UP status has been notified
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for(i = 0; i < 6; i++)
|
for(i = 0; i < 6; i++)
|
||||||
device->mac_addr[i] = n2n_rand();
|
device->mac_addr[i] = n2n_rand();
|
||||||
|
|
||||||
device->mac_addr[0] &= ~0x01; /* Clear multicast bit */
|
// clear multicast bit
|
||||||
device->mac_addr[0] |= 0x02; /* Set locally-assigned bit */
|
device->mac_addr[0] &= ~0x01;
|
||||||
|
|
||||||
|
// set locally-assigned bit
|
||||||
|
device->mac_addr[0] |= 0x02;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize Netlink socket */
|
// initialize netlink socket
|
||||||
if((nl_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1) {
|
if((nl_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1) {
|
||||||
traceEvent(TRACE_ERROR, "netlink socket creation failed [%d]: %s", errno, strerror(errno));
|
traceEvent(TRACE_ERROR, "netlink socket creation failed [%d]: %s", errno, strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -172,7 +180,7 @@ int tuntap_open(tuntap_dev *device,
|
||||||
msg.msg_iov = &iov;
|
msg.msg_iov = &iov;
|
||||||
msg.msg_iovlen = 1;
|
msg.msg_iovlen = 1;
|
||||||
|
|
||||||
/* Subscribe to interface events */
|
// subscribe to interface events
|
||||||
if(bind(nl_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
|
if(bind(nl_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
|
||||||
traceEvent(TRACE_ERROR, "netlink socket bind failed [%d]: %s", errno, strerror(errno));
|
traceEvent(TRACE_ERROR, "netlink socket bind failed [%d]: %s", errno, strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -193,7 +201,7 @@ int tuntap_open(tuntap_dev *device,
|
||||||
|
|
||||||
close(ioctl_fd);
|
close(ioctl_fd);
|
||||||
|
|
||||||
/* Wait for the up and running notification */
|
// wait for the up and running notification
|
||||||
traceEvent(TRACE_INFO, "Waiting for TAP interface to be up and running...");
|
traceEvent(TRACE_INFO, "Waiting for TAP interface to be up and running...");
|
||||||
|
|
||||||
while(!up_and_running) {
|
while(!up_and_running) {
|
||||||
|
@ -212,7 +220,7 @@ int tuntap_open(tuntap_dev *device,
|
||||||
if(nh->nlmsg_type == NETLINK_GENERIC) {
|
if(nh->nlmsg_type == NETLINK_GENERIC) {
|
||||||
struct ifinfomsg *ifi = NLMSG_DATA(nh);
|
struct ifinfomsg *ifi = NLMSG_DATA(nh);
|
||||||
|
|
||||||
/* NOTE: skipping interface name check, assuming it's our TAP */
|
// NOTE: skipping interface name check, assuming it's our TAP
|
||||||
if((ifi->ifi_flags & IFF_UP) && (ifi->ifi_flags & IFF_RUNNING)) {
|
if((ifi->ifi_flags & IFF_UP) && (ifi->ifi_flags & IFF_RUNNING)) {
|
||||||
up_and_running = 1;
|
up_and_running = 1;
|
||||||
traceEvent(TRACE_INFO, "Interface is up and running");
|
traceEvent(TRACE_INFO, "Interface is up and running");
|
||||||
|
@ -228,32 +236,31 @@ int tuntap_open(tuntap_dev *device,
|
||||||
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;
|
struct ifreq ifr;
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
|
@ -272,4 +279,5 @@ void tuntap_get_address(struct tuntap_dev *tuntap) {
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif /* #ifdef __linux__ */
|
#endif /* #ifdef __linux__ */
|
||||||
|
|
|
@ -16,19 +16,24 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#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
|
||||||
|
|
||||||
|
|
||||||
|
void tun_close (tuntap_dev *device);
|
||||||
|
|
||||||
|
|
||||||
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 */
|
||||||
|
@ -36,6 +41,7 @@ int tuntap_open(tuntap_dev *device /* ignored */,
|
||||||
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;
|
||||||
|
|
||||||
|
@ -43,16 +49,14 @@ int tuntap_open(tuntap_dev *device /* ignored */,
|
||||||
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)", strerror(errno));
|
traceEvent(TRACE_ERROR, "Unable to obtain name of tap device (%s)", strerror(errno));
|
||||||
close(device->fd);
|
close(device->fd);
|
||||||
return(-1);
|
return -1;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
snprintf(tap_device, sizeof(tap_device), req.ifr_name);
|
snprintf(tap_device, sizeof(tap_device), req.ifr_name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,7 +64,7 @@ int tuntap_open(tuntap_dev *device /* ignored */,
|
||||||
|
|
||||||
if(device->fd < 0) {
|
if(device->fd < 0) {
|
||||||
traceEvent(TRACE_ERROR, "Unable to open tap device (%s)", strerror(errno));
|
traceEvent(TRACE_ERROR, "Unable to open tap device (%s)", strerror(errno));
|
||||||
return(-1);
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
char cmd[256];
|
char cmd[256];
|
||||||
FILE *fd;
|
FILE *fd;
|
||||||
|
@ -70,27 +74,24 @@ int tuntap_open(tuntap_dev *device /* ignored */,
|
||||||
device->ip_addr = inet_addr(device_ip);
|
device->ip_addr = inet_addr(device_ip);
|
||||||
|
|
||||||
if(device_mac && device_mac[0] != '\0') {
|
if(device_mac && device_mac[0] != '\0') {
|
||||||
/* Set the hw address before bringing the if up. */
|
// set the hw address before bringing the if up
|
||||||
snprintf(cmd, sizeof(cmd), "ifconfig %s link %s active",
|
snprintf(cmd, sizeof(cmd), "ifconfig %s link %s active", tap_device, device_mac);
|
||||||
tap_device, device_mac);
|
|
||||||
system(cmd);
|
system(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(cmd, sizeof(cmd), "ifconfig %s %s netmask %s mtu %d up",
|
snprintf(cmd, sizeof(cmd), "ifconfig %s %s netmask %s mtu %d up", tap_device, device_ip, device_mask, mtu);
|
||||||
tap_device, device_ip, device_mask, mtu);
|
|
||||||
system(cmd);
|
system(cmd);
|
||||||
|
|
||||||
traceEvent(TRACE_NORMAL, "Interface %s up and running (%s/%s)",
|
traceEvent(TRACE_NORMAL, "Interface %s up and running (%s/%s)", tap_device, device_ip, device_mask);
|
||||||
tap_device, device_ip, device_mask);
|
|
||||||
|
|
||||||
/* Read MAC address */
|
// read MAC address
|
||||||
snprintf(cmd, sizeof(cmd), "ifconfig %s |grep address|cut -c 11-28", tap_device);
|
snprintf(cmd, sizeof(cmd), "ifconfig %s |grep address|cut -c 11-28", tap_device);
|
||||||
/* traceEvent(TRACE_INFO, "%s", cmd); */
|
// traceEvent(TRACE_INFO, "%s", cmd);
|
||||||
|
|
||||||
fd = popen(cmd, "r");
|
fd = popen(cmd, "r");
|
||||||
if(fd < 0) {
|
if(fd < 0) {
|
||||||
tun_close(device);
|
tun_close(device);
|
||||||
return(-1);
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
int a, b, c, d, e, f;
|
int a, b, c, d, e, f;
|
||||||
char buf[256];
|
char buf[256];
|
||||||
|
@ -113,33 +114,35 @@ int tuntap_open(tuntap_dev *device /* ignored */,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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__ */
|
||||||
|
|
|
@ -16,15 +16,19 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include "n2n.h"
|
#include "n2n.h"
|
||||||
|
|
||||||
|
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
|
|
||||||
void tun_close(tuntap_dev *device);
|
|
||||||
|
|
||||||
/* ********************************** */
|
|
||||||
|
|
||||||
#define N2N_OSX_TAPDEVICE_SIZE 32
|
#define N2N_OSX_TAPDEVICE_SIZE 32
|
||||||
|
|
||||||
|
|
||||||
|
void tun_close (tuntap_dev *device);
|
||||||
|
|
||||||
|
|
||||||
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 */
|
||||||
|
@ -32,6 +36,7 @@ int tuntap_open(tuntap_dev *device /* ignored */,
|
||||||
char *device_mask,
|
char *device_mask,
|
||||||
const char * device_mac,
|
const char * device_mac,
|
||||||
int mtu) {
|
int mtu) {
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
char tap_device[N2N_OSX_TAPDEVICE_SIZE];
|
char tap_device[N2N_OSX_TAPDEVICE_SIZE];
|
||||||
|
|
||||||
|
@ -47,40 +52,34 @@ int tuntap_open(tuntap_dev *device /* ignored */,
|
||||||
|
|
||||||
if(device->fd < 0) {
|
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, "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");
|
traceEvent(TRACE_ERROR, "Please read https://github.com/ntop/n2n/blob/dev/doc/Building.md");
|
||||||
return(-1);
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
char buf[256];
|
char buf[256];
|
||||||
FILE *fd;
|
FILE *fd;
|
||||||
|
|
||||||
device->ip_addr = inet_addr(device_ip);
|
device->ip_addr = inet_addr(device_ip);
|
||||||
|
|
||||||
if ( device_mac && device_mac[0] != '\0' )
|
if(device_mac && device_mac[0] != '\0') {
|
||||||
{
|
// FIXME - this is not tested. might be wrong syntax for OS X
|
||||||
/* 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);
|
||||||
/* Set the hw address before bringing the if up. */
|
|
||||||
snprintf(buf, sizeof(buf), "ifconfig tap%d ether %s",
|
|
||||||
i, device_mac);
|
|
||||||
system(buf);
|
system(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(buf, sizeof(buf), "ifconfig tap%d %s netmask %s mtu %d up",
|
snprintf(buf, sizeof(buf), "ifconfig tap%d %s netmask %s mtu %d up", i, device_ip, device_mask, mtu);
|
||||||
i, device_ip, device_mask, mtu);
|
|
||||||
system(buf);
|
system(buf);
|
||||||
|
|
||||||
traceEvent(TRACE_NORMAL, "Interface tap%d up and running (%s/%s)",
|
traceEvent(TRACE_NORMAL, "Interface tap%d up and running (%s/%s)", i, device_ip, device_mask);
|
||||||
i, device_ip, device_mask);
|
|
||||||
|
|
||||||
/* Read MAC address */
|
|
||||||
|
|
||||||
|
// read MAC address
|
||||||
snprintf(buf, sizeof(buf), "ifconfig tap%d |grep ether|cut -c 8-24", i);
|
snprintf(buf, sizeof(buf), "ifconfig tap%d |grep ether|cut -c 8-24", i);
|
||||||
/* traceEvent(TRACE_INFO, "%s", buf); */
|
// traceEvent(TRACE_INFO, "%s", buf);
|
||||||
|
|
||||||
fd = popen(buf, "r");
|
fd = popen(buf, "r");
|
||||||
if(fd < 0) {
|
if(fd < 0) {
|
||||||
tuntap_close(device);
|
tuntap_close(device);
|
||||||
return(-1);
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
int a, b, c, d, e, f;
|
int a, b, c, d, e, f;
|
||||||
|
|
||||||
|
@ -102,33 +101,34 @@ int tuntap_open(tuntap_dev *device /* ignored */,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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…
Reference in New Issue
Block a user