Second night's commit.
We're a bit closer to something sane, now. We can wrap, but not unwrap, packets. Asymmetric encryption is *big*. encrypted text with a 4096-bit RSA public key is 512 bytes. We can't fragment yet. Fortunately, this isn't an infinite regress once we *can* fragment. Performance is still a big question mark, of course. There may still be endianness issues hanging around. The eid<->rloc map is almost certainly far, far too slow to be of any use in the real world.
This commit is contained in:
123
pass-1/util.c
Normal file
123
pass-1/util.c
Normal file
@@ -0,0 +1,123 @@
|
||||
#include "util.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;
|
||||
}
|
||||
|
||||
|
||||
// shamelessly copied from:
|
||||
// http://www.roman10.net/how-to-calculate-iptcpudp-checksumpart-2-implementation/
|
||||
unsigned short compute_checksum(unsigned short *addr, unsigned int count) {
|
||||
|
||||
unsigned long sum = 0;
|
||||
|
||||
while (count > 1) {
|
||||
sum += * addr++;
|
||||
count -= 2;
|
||||
}
|
||||
|
||||
//if any bytes left, pad the bytes and add
|
||||
if(count > 0) {
|
||||
sum += ((*addr)&htons(0xFF00));
|
||||
}
|
||||
|
||||
//Fold sum to 16 bits: add carrier to result
|
||||
while (sum>>16) {
|
||||
sum = (sum & 0xffff) + (sum >> 16);
|
||||
}
|
||||
|
||||
//one's complement
|
||||
sum = ~sum;
|
||||
|
||||
return ((unsigned short)sum);
|
||||
|
||||
}
|
||||
|
||||
void compute_ip_checksum(struct iphdr* pkt)
|
||||
{
|
||||
pkt->check = 0x0000;
|
||||
pkt->check = compute_checksum( (unsigned short*) pkt, pkt->ihl * 4 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
// ioctl(fd, SIOCGIFFLAGS, {ifr_name="wrapper", ifr_flags=IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST}) = 0
|
||||
// ioctl(fd, SIOCSIFFLAGS, {ifr_name="wrapper", ifr_flags=IFF_UP|IFF_POINTOPOINT|IFF_RUNNING|IFF_NOARP|IFF_MULTICAST}) = 0
|
||||
|
||||
return 1;
|
||||
|
||||
}
|
Reference in New Issue
Block a user