NetTest - Compiles, doesn't yet do much
[tpg/acess2.git] / Tools / NetTest / tap.c
1 /*
2  * Acess2 Networking Test Suite (NetTest)
3  * - By John Hodge (thePowersGang)
4  *
5  * tap.c
6  * - TAP Network driver
7  */
8 #include <nettest.h>
9 #include <sys/ioctl.h>
10 #include <net/if.h>
11 //#include <linux/if.h>
12 #include <linux/if_tun.h>
13 #include <string.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <stdio.h>
17
18 // === CODE ===
19 void *NetTest_OpenTap(const char *Name)
20 {
21          int    rv;
22
23         struct ifreq    ifr;
24         memset(&ifr, 0, sizeof(ifr));
25         ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
26         
27         if( Name[0] != '\0' )
28         {
29                 if( strlen(Name) > IFNAMSIZ )
30                         return NULL;
31                 strncpy(ifr.ifr_name, Name, IFNAMSIZ);
32         }
33         
34          int    fd = open("/dev/net/tun", O_RDWR);
35         if( fd < 0 )
36         {
37                 perror("NetTest_OpenTap - open");
38                 return NULL;
39         }
40         
41         if( (rv = ioctl(fd, TUNSETIFF, &ifr)) )
42         {
43                 perror("NetTest_OpenTap - ioctl(TUNSETIFF)");
44                 fprintf(stderr, "Opening TUN/TAP device '%s'\n", Name);
45                 close(fd);
46                 return NULL;
47         }
48
49         return (void*)(intptr_t)fd;
50 }
51
52 size_t NetTest_WritePacket(void *Handle, size_t Size, const void *Data)
53 {
54         return write( (intptr_t)Handle, Data, Size);
55 }
56
57 size_t NetTest_ReadPacket(void *Handle, size_t MaxSize, void *Data)
58 {
59         return read( (intptr_t)Handle, Data, MaxSize);
60 }

UCC git Repository :: git.ucc.asn.au