From 8e005d0f13767db538e3d939188d73370db9042d Mon Sep 17 00:00:00 2001 From: Miguel Date: Thu, 27 Sep 2018 14:30:33 +0200 Subject: added linux tap support for fool's network stack --- net/main.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 net/main.c (limited to 'net/main.c') diff --git a/net/main.c b/net/main.c new file mode 100644 index 0000000..339f037 --- /dev/null +++ b/net/main.c @@ -0,0 +1,90 @@ +//http://www.saminiir.com/lets-code-tcp-ip-stack-1-ethernet-arp/ +//https://openvpn.net/tuntap + +#ifdef PLATFORM_LINUX + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "netdev.h" + +int fd; + +int tap_transmit(const void * p_data, uint16_t p_len) +{ + write(fd,p_data,p_len); + return 0; +} + +int main(int argc, char **argv) +{ + int err; + char dev[]="tap0"; + struct ifreq ifr; + + if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) { + printf("Cannot open TUN/TAP dev\n"); + exit(1); + } + + + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_flags = IFF_TAP | IFF_NO_PI; + strncpy(ifr.ifr_name, dev,IFNAMSIZ); + + if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){ + printf("ioctl failed for TAP\n"); + close(fd); + return err; + } + + + struct netdev net_dev; + + + uint8_t mac[6]; + mac[0]=0x7a; + mac[1]=0xa4; + mac[2]=0x7b; + mac[3]=0x72; + mac[4]=0x56; + mac[5]=0xa2; + + memcpy(net_dev.hwaddr,mac,6); // use mac obtained from device + net_dev.ip=(192<<0)+(168<<8)+(0<<16)+(20<<24); // 192.168.0.20 // TODO: not hardcode! + net_dev.transmit=tap_transmit; + + uint8_t buf[1024]; + + while(1) + { + uint32_t len=read(fd,buf,1024); + + if(len==-1) + { + printf("read error: %s \n",strerror(errno)); + } + + printf("read %i bytes\n",len); + net_incoming(&net_dev,buf); + } + + return 0; +} + + +#endif + + -- cgit v1.2.3