Files
hide-eid/pass-1/util.c

143 lines
3.5 KiB
C
Raw Normal View History

#include "util.h"
#include "rlocs.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
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;
}
int session_setup( struct session *session, char *config_file, char *listen_if, char *output_if )
{
memset( &session, 0, sizeof( struct session ) );
session->listen_if = -1;
session->output_if = -1;
session->rlocs = rlocs_new( config_file );
if ( session->rlocs == NULL ) {
warn( "Failed to get config from %s", config_file );
return 0;
}
rlocs_debug_output( session->rlocs );
// TODO: We can scale the tun architecture by using multiqueue and having
// a bunch of workers, rather than this noddy scheme. If we don't jump
// directly to something saner, anyway...
session->listen_if = create_tun( listen_if );
if ( session->listen_if == -1 ) {
warn( "Error opening %s for listening", listen_if );
rlocs_free( session->rlocs );
return 0;
}
link_set_up( listen_if, 1 );
if ( strcmp( listen_if, output_if ) == 0 ) {
session->same_if = 1;
session->output_if = session->listen_if;
} else {
session->same_if = 0;
session->output_if = create_tun( output_if );
if ( session->output_if == -1 ) {
warn( "Error opening %s for outputting", output_if );
rlocs_free( session->rlocs );
close( session->listen_if );
return 0;
}
link_set_up( output_if, 1 );
}
return 1;
}
void session_teardown( struct session *session )
{
rlocs_free( session->rlocs );
if ( session->listen_if >= 0 ) {
close( session->listen_if );
}
if ( session->output_if >= 0 && !session->same_if ) {
close( session->output_if );
}
}