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. 59
      src/tuntap_freebsd.c
  2. 76
      src/tuntap_linux.c
  3. 55
      src/tuntap_netbsd.c
  4. 54
      src/tuntap_osx.c

59
src/tuntap_freebsd.c

@ -16,15 +16,19 @@
*
*/
#include "n2n.h"
#ifdef __FreeBSD__
#define N2N_FREEBSD_TAPDEVICE_SIZE 32
void tuntap_close (tuntap_dev *device);
/* ********************************** */
#define N2N_FREEBSD_TAPDEVICE_SIZE 32
int tuntap_open (tuntap_dev *device /* ignored */,
char *dev,
const char *address_mode, /* static or dhcp */
@ -32,6 +36,7 @@ int tuntap_open(tuntap_dev *device /* ignored */,
char *device_mask,
const char * device_mac,
int mtu) {
int i;
char tap_device[N2N_FREEBSD_TAPDEVICE_SIZE];
@ -47,39 +52,34 @@ int tuntap_open(tuntap_dev *device /* ignored */,
if(device->fd < 0) {
traceEvent(TRACE_ERROR, "Unable to open tap device");
return(-1);
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 */
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);
// 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);
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)",
i, device_ip, device_mask);
/* Read MAC address */
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); */
// traceEvent(TRACE_INFO, "%s", buf);
fd = popen(buf, "r");
if(fd < 0) {
tuntap_close(device);
return(-1);
return -1;
} else {
int a, b, c, d, e, f;
@ -102,32 +102,35 @@ int tuntap_open(tuntap_dev *device /* ignored */,
}
/* read_mac(dev, device->mac_addr); */
return(device->fd);
// read_mac(dev, device->mac_addr);
return device->fd;
}
/* ********************************** */
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) {
return(write(tuntap->fd, buf, len));
return write(tuntap->fd, buf, len);
}
/* ********************************** */
void tuntap_close (struct tuntap_dev *tuntap) {
close(tuntap->fd);
}
/* Fill out the ip_addr value from the interface. Called to pick up dynamic
* address changes. */
void tuntap_get_address(struct tuntap_dev *tuntap)
{
// fill out the ip_addr value from the interface, called to pick up dynamic address changes
void tuntap_get_address (struct tuntap_dev *tuntap) {
// no action
}
#endif /* #ifdef __FreeBSD__ */

76
src/tuntap_linux.c

@ -16,14 +16,16 @@
*
*/
#ifdef __linux__
#include "n2n.h"
/* ********************************** */
static int setup_ifname (int fd, const char *ifname, const char *ipaddr,
const char *netmask, uint8_t *mac, int mtu) {
struct ifreq 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) {
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFHWADDR) failed [%d]: %s", errno, strerror(errno));
return(-1);
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);
if(ioctl(fd, SIOCSIFADDR, &ifr) == -1) {
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)) {
inet_pton(AF_INET, netmask, &((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr);
if(ioctl(fd, SIOCSIFNETMASK, &ifr) == -1) {
traceEvent(TRACE_ERROR, "ioctl(SIOCSIFNETMASK, %s) failed [%d]: %s", netmask, errno, strerror(errno));
return(-3);
return -3;
}
}
/* MTU */
// MTU
ifr.ifr_mtu = mtu;
if(ioctl(fd, SIOCSIFMTU, &ifr) == -1) {
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) {
traceEvent(TRACE_ERROR, "ioctl(SIOCGIFFLAGS) failed [%d]: %s", errno, strerror(errno));
return(-5);
return -5;
}
ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
if(ioctl(fd, SIOCSIFFLAGS, &ifr) == -1) {
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.
*
@ -104,6 +105,7 @@ int tuntap_open(tuntap_dev *device,
char *device_mask,
const char * device_mac,
int mtu) {
char *tuntap_device = "/dev/net/tun";
int ioctl_fd;
struct ifreq ifr;
@ -122,7 +124,10 @@ int tuntap_open(tuntap_dev *device,
}
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);
ifr.ifr_name[IFNAMSIZ-1] = '\0';
rc = ioctl(device->fd, TUNSETIFF, (void *)&ifr);
@ -133,26 +138,29 @@ int tuntap_open(tuntap_dev *device,
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));
if(device_mac && device_mac[0]) {
/* Use the user-provided MAC */
// 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. */
// 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++)
device->mac_addr[i] = n2n_rand();
device->mac_addr[0] &= ~0x01; /* Clear multicast bit */
device->mac_addr[0] |= 0x02; /* Set locally-assigned bit */
// clear multicast 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) {
traceEvent(TRACE_ERROR, "netlink socket creation failed [%d]: %s", errno, strerror(errno));
return -1;
@ -172,7 +180,7 @@ int tuntap_open(tuntap_dev *device,
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
/* Subscribe to interface events */
// 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;
@ -193,7 +201,7 @@ int tuntap_open(tuntap_dev *device,
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...");
while(!up_and_running) {
@ -212,7 +220,7 @@ int tuntap_open(tuntap_dev *device,
if(nh->nlmsg_type == NETLINK_GENERIC) {
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)) {
up_and_running = 1;
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->if_idx = if_nametoindex(dev);
return(device->fd);
return device->fd;
}
/* *************************************************** */
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) {
return(write(tuntap->fd, buf, len));
return write(tuntap->fd, buf, len);
}
/* *************************************************** */
void tuntap_close (struct tuntap_dev *tuntap) {
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) {
struct ifreq ifr;
int fd;
@ -272,4 +279,5 @@ void tuntap_get_address(struct tuntap_dev *tuntap) {
close(fd);
}
#endif /* #ifdef __linux__ */

55
src/tuntap_netbsd.c

@ -16,19 +16,24 @@
*
*/
#include "n2n.h"
#ifdef __NetBSD__
#include <errno.h>
#include <string.h>
#include <net/if_tap.h>
#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 */,
char *dev,
const char *address_mode, /* static or dhcp */
@ -36,6 +41,7 @@ int tuntap_open(tuntap_dev *device /* ignored */,
char *device_mask,
const char * device_mac,
int mtu) {
char tap_device[N2N_NETBSD_TAPDEVICE_SIZE];
struct ifreq req;
@ -43,16 +49,14 @@ int tuntap_open(tuntap_dev *device /* ignored */,
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 {
} 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 {
return -1;
} else {
snprintf(tap_device, sizeof(tap_device), req.ifr_name);
}
}
@ -60,7 +64,7 @@ int tuntap_open(tuntap_dev *device /* ignored */,
if(device->fd < 0) {
traceEvent(TRACE_ERROR, "Unable to open tap device (%s)", strerror(errno));
return(-1);
return -1;
} else {
char cmd[256];
FILE *fd;
@ -70,27 +74,24 @@ int tuntap_open(tuntap_dev *device /* ignored */,
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);
// set the hw address before bringing the if up
snprintf(cmd, sizeof(cmd), "ifconfig %s link %s active", tap_device, device_mac);
system(cmd);
}
snprintf(cmd, sizeof(cmd), "ifconfig %s %s netmask %s mtu %d up",
tap_device, device_ip, device_mask, mtu);
snprintf(cmd, sizeof(cmd), "ifconfig %s %s netmask %s mtu %d up", tap_device, device_ip, device_mask, mtu);
system(cmd);
traceEvent(TRACE_NORMAL, "Interface %s up and running (%s/%s)",
tap_device, device_ip, device_mask);
traceEvent(TRACE_NORMAL, "Interface %s up and running (%s/%s)", 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);
/* traceEvent(TRACE_INFO, "%s", cmd); */
// traceEvent(TRACE_INFO, "%s", cmd);
fd = popen(cmd, "r");
if(fd < 0) {
tun_close(device);
return(-1);
return -1;
} else {
int a, b, c, d, e, f;
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);
}
/* ********************************** */
int tuntap_read (struct tuntap_dev *tuntap, unsigned char *buf, int len) {
return(read(tuntap->fd, buf, len));
}
/* ********************************** */
int tuntap_write (struct tuntap_dev *tuntap, unsigned char *buf, int len) {
return(write(tuntap->fd, buf, len));
}
/* ********************************** */
void tuntap_close (struct tuntap_dev *tuntap) {
close(tuntap->fd);
}
/* Fill out the ip_addr value from the interface. Called to pick up dynamic
* address changes. */
void tuntap_get_address(struct tuntap_dev *tuntap)
{
// fill out the ip_addr value from the interface, called to pick up dynamic address changes
void tuntap_get_address (struct tuntap_dev *tuntap) {
// no action
}
#endif /* #ifdef __NetBSD__ */

54
src/tuntap_osx.c

@ -16,15 +16,19 @@
*
*/
#include "n2n.h"
#ifdef __APPLE__
#define N2N_OSX_TAPDEVICE_SIZE 32
void tun_close (tuntap_dev *device);
/* ********************************** */
#define N2N_OSX_TAPDEVICE_SIZE 32
int tuntap_open (tuntap_dev *device /* ignored */,
char *dev,
const char *address_mode, /* static or dhcp */
@ -32,6 +36,7 @@ int tuntap_open(tuntap_dev *device /* ignored */,
char *device_mask,
const char * device_mac,
int mtu) {
int i;
char tap_device[N2N_OSX_TAPDEVICE_SIZE];
@ -47,40 +52,34 @@ int tuntap_open(tuntap_dev *device /* ignored */,
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);
traceEvent(TRACE_ERROR, "Please read https://github.com/ntop/n2n/blob/dev/doc/Building.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);
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);
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)",
i, device_ip, device_mask);
/* Read MAC address */
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); */
// traceEvent(TRACE_INFO, "%s", buf);
fd = popen(buf, "r");
if(fd < 0) {
tuntap_close(device);
return(-1);
return -1;
} else {
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);
}
/* ********************************** */
int tuntap_read (struct tuntap_dev *tuntap, unsigned char *buf, int len) {
return(read(tuntap->fd, buf, len));
}
/* ********************************** */
int tuntap_write (struct tuntap_dev *tuntap, unsigned char *buf, int len) {
return(write(tuntap->fd, buf, len));
}
/* ********************************** */
void tuntap_close (struct tuntap_dev *tuntap) {
close(tuntap->fd);
}
/* Fill out the ip_addr value from the interface. Called to pick up dynamic
* address changes. */
void tuntap_get_address(struct tuntap_dev *tuntap)
{
// fill out the ip_addr value from the interface, called to pick up dynamic address changes
void tuntap_get_address (struct tuntap_dev *tuntap) {
// no action
}
#endif /* __APPLE__ */

Loading…
Cancel
Save