#ifndef _NETWORKINTERFACE_H_ #define _NETWORKINTERFACE_H_ // todo: figure out how to create a network interface device, and copy data to it. // // easiest is probably to use a 'tap' device ( provided by the kext already loaded by openvpn/tunnelblick ) // #include #include #include "err/posix.h" #include "stringutils.h" class network_interface { int _tap; int _tapnum; public: network_interface() { for (int i=0 ; i<16 ; i++) { _tap= open(stringformat("/dev/tap%d", i).c_str(), O_RDWR); if (_tap!=-1) { _tapnum= i; break; } } if (_tap==-1) throw posixerror("open /dev/tap"); // device = 169.254.2.1 // pc = 169.254.2.2 system(stringformat("ifconfig tap%d up 169.254.2.2 netmask 255.255.255.0", _tapnum).c_str()); } ~network_interface() { close(_tap); } void setlladdr(const ByteVector& addr) { if (addr.size()!=6) throw "setlladdr"; system(stringformat("ifconfig tap%d lladdr %02x:%02x:%02x:%02x:%02x:%02x", _tapnum, addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]).c_str()); } size_t readpacket(uint8_t *p, size_t n) { return read(_tap, p, n); } bool sendpacket(const uint8_t *p, size_t n) { if (-1==write(_tap, p, n)) return false; return true; } }; #endif