Usermode/libc - Fix strchr and strrchr behavior
[tpg/acess2.git] / Tools / NetTest / nic.c
1 /*
2  * Acess2 Networking Test Suite (NetTest)
3  * - By John Hodge (thePowersGang)
4  *
5  * nic.c
6  * - Acess -> TAP Wrapper
7  */
8 #include <acess.h>
9 #include <IPStack/include/adapters_api.h>
10 #include <nettest.h>
11
12 // === CONSTANTS ===
13 static const int MTU = 1520;
14
15 // === PROTOTYPES ===
16 void    NativeNic_int_FreePacket(void *Ptr, size_t pkt_length, size_t Unused, const void *DataPtr);
17 tIPStackBuffer  *NativeNic_WaitForPacket(void *Ptr);
18  int    NativeNic_SendPacket(void *Ptr, tIPStackBuffer *Buffer);
19
20 // === GLOBALS ===
21 tIPStack_AdapterType    gNativeNIC_AdapterType = {
22         .Name = "NativeNIC",
23         .Type = 0,
24         .Flags = 0,
25         .SendPacket = NativeNic_SendPacket,
26         .WaitForPacket = NativeNic_WaitForPacket
27         };
28
29 // === CODE ===
30 int NativeNic_AddDev(char *DevDesc)
31 {
32         Uint8   macaddr[6];
33         // Parse descriptor into mac address and path
34         // 123456789ABC:tun:ifname
35         // 123456789ABC:unix:/path
36         // 123456789ABC:file:/path
37         // 123456789ABC:tcp:host_desc
38         char *colonpos = strchr(DevDesc, ':');
39         if( !colonpos )
40                 return -1;
41         if( UnHex(macaddr, 6, DevDesc) != 6 )
42                 return -1;
43         
44         char *class = colonpos + 1;
45         colonpos = strchr(class, ':');
46         void *ptr;
47         if( strncmp(class, "tun:", 4) == 0 ) {
48                 ptr = NetTest_OpenTap(colonpos+1);
49         }
50         else if( strncmp(class, "unix:", 5) == 0 ) {
51                 ptr = NetTest_OpenUnix(colonpos+1);
52         }
53         else {
54                 Log_Warning("NIC", "Unknown opener '%.*s'", colonpos-class, class);
55                 ptr = NULL;
56         }
57         if( !ptr )
58                 return 1;
59         IPStack_Adapter_Add(&gNativeNIC_AdapterType, ptr, macaddr);
60         
61         return 0;
62 }
63
64 void NativeNic_int_FreePacket(void *Ptr, size_t pkt_length, size_t Unused, const void *DataPtr)
65 {
66         free( (void*)DataPtr );
67 }
68
69 tIPStackBuffer *NativeNic_WaitForPacket(void *Ptr)
70 {
71         char    *buf = malloc( MTU );
72         size_t  len;
73
74         len = NetTest_ReadPacket(Ptr, MTU, buf);
75
76         tIPStackBuffer  *ret = IPStack_Buffer_CreateBuffer(1);
77         IPStack_Buffer_AppendSubBuffer(ret, len, 0, buf, NativeNic_int_FreePacket, Ptr);
78         
79         Debug_HexDump("NativeNic: RX", buf, len);
80         
81         return ret;
82 }
83
84 int NativeNic_SendPacket(void *Ptr, tIPStackBuffer *Buffer)
85 {
86         size_t  len = IPStack_Buffer_GetLength(Buffer);
87         
88         // Check against MTU
89         if( len > MTU )
90                 return -1;
91         
92         // Serialise into stack
93         char    buf[len];
94         IPStack_Buffer_GetData(Buffer, buf, len);
95
96         Debug_HexDump("NativeNic: TX", buf, len);
97         
98         NetTest_WritePacket(Ptr, len, buf);
99         return 0;
100 }

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