#include "util.h" #include #include #include #include #include #include #include #include #include #include #include void* xmalloc( size_t bytes ) { void* result = malloc( bytes ); if ( bytes > 0 && result == NULL ) { warn( "Couldn't allocate memory, exiting!" ); exit(2); } memset( result, 0, bytes ); return result; } int create_tun( const char* name ) { int fd, err; struct ifreq ifr; if ( ( fd = open( "/dev/net/tun", O_RDWR ) ) < 0 ) { warn( "Error %s opening tun to create %s", strerror(errno), name ); return -1; } memset( &ifr, 0, sizeof( struct ifreq ) ); ifr.ifr_flags = IFF_TUN | IFF_NO_PI | IFF_UP; strncpy( ifr.ifr_name, name, IFNAMSIZ ); if ( (err = ioctl( fd, TUNSETIFF, (void*) &ifr ) ) < 0 ) { warn( "Error creating tun device %s: %s", name, strerror(errno) ); close( fd ); return -1; } return fd; } int link_set_up( char *link_name, int state ) { int fd = socket( PF_INET, SOCK_DGRAM, IPPROTO_IP ); struct ifreq ifr; memset( &ifr, 0, sizeof( struct ifreq ) ); strncpy( ifr.ifr_name, link_name, IFNAMSIZ ); if ( ioctl( fd, SIOCGIFFLAGS, (void*)&ifr ) < 0 ) { warn( "Failed to read interface flags for %s: %s", link_name, strerror(errno) ); return 0; } if ( state ) { ifr.ifr_flags |= IFF_UP; } else { ifr.ifr_flags = ( ifr.ifr_flags & ~IFF_UP ); } if ( ioctl( fd, SIOCSIFFLAGS, (void*)&ifr ) < 0 ) { warn( "Failed to set link state for interface %s to %i: %s", link_name, state, strerror(errno) ); return 0; } return 1; }