Tools/NetTest - Add TCP retransmit test
[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 #include <sys/socket.h>
18 #include <sys/un.h>
19
20 // === CODE ===
21 void *NetTest_OpenTap(const char *Name)
22 {
23          int    rv;
24
25         struct ifreq    ifr;
26         memset(&ifr, 0, sizeof(ifr));
27         ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
28         
29         if( Name[0] != '\0' )
30         {
31                 if( strlen(Name) > IFNAMSIZ )
32                         return NULL;
33                 strncpy(ifr.ifr_name, Name, IFNAMSIZ);
34         }
35         
36          int    fd = open("/dev/net/tun", O_RDWR);
37         if( fd < 0 )
38         {
39                 perror("NetTest_OpenTap - open");
40                 return NULL;
41         }
42         
43         if( (rv = ioctl(fd, TUNSETIFF, &ifr)) )
44         {
45                 perror("NetTest_OpenTap - ioctl(TUNSETIFF)");
46                 fprintf(stderr, "Opening TUN/TAP device '%s'\n", Name);
47                 close(fd);
48                 return NULL;
49         }
50
51         return (void*)(intptr_t)fd;
52 }
53
54 void *NetTest_OpenUnix(const char *Path)
55 {
56         int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
57         struct sockaddr_un      sa = {AF_UNIX, ""};
58         struct sockaddr_un      sa_local = {AF_UNIX, ""};
59         strcpy(sa.sun_path, Path);
60         if( connect(fd, (struct sockaddr*)&sa, sizeof(sa)) ) {
61                 perror("NetTest_OpenUnix - connect");
62                 close(fd);
63                 return NULL;
64         }
65         if( bind(fd, (struct sockaddr*)&sa_local, sizeof(sa)) ) {
66                 perror("NetTest_OpenUnix - bind");
67                 close(fd);
68                 return NULL;
69         }
70         
71         {
72                 char somenulls[] = { 0,0,0,0,0,0, 0,0,0,0,0, 0,0};
73                 write(fd, somenulls, sizeof(somenulls));
74         }
75         
76         return (void*)(intptr_t)fd;
77 }
78
79 size_t NetTest_WritePacket(void *Handle, size_t Size, const void *Data)
80 {
81         int ret = write( (intptr_t)Handle, Data, Size);
82         if( ret < 0 ) {
83                 perror("NetTest_WritePacket - write");
84                 return 0;
85         }
86         return ret;
87 }
88
89 size_t NetTest_ReadPacket(void *Handle, size_t MaxSize, void *Data)
90 {
91         return read( (intptr_t)Handle, Data, MaxSize);
92 }

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