1e736c2be52abd4062e90c607ffa07c452df18e9
[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,      // TODO: Differentiate differnet wire protos and speeds
24         .Flags = 0,     // TODO: IP checksum offloading, MAC checksum offloading etc
25         .SendPacket = NativeNic_SendPacket,
26         .WaitForPacket = NativeNic_WaitForPacket
27         };
28
29 // === CODE ===
30 int NativeNic_AddDev(char *DevDesc)
31 {
32         Uint8   macaddr[6];
33         char *colonpos = strchr(DevDesc, ':');
34         if( !colonpos )
35                 return -1;
36         *colonpos = 0;
37         if( UnHex(macaddr, 6, DevDesc) != 6 )
38                 return -1;
39         void *ptr = NetTest_OpenTap(colonpos+1);
40         if( !ptr )
41                 return 1;
42         IPStack_Adapter_Add(&gNativeNIC_AdapterType, ptr, macaddr);
43         
44         return 0;
45 }
46
47 void NativeNic_int_FreePacket(void *Ptr, size_t pkt_length, size_t Unused, const void *DataPtr)
48 {
49         free( (void*)DataPtr );
50 }
51
52 tIPStackBuffer *NativeNic_WaitForPacket(void *Ptr)
53 {
54         char    *buf = malloc( MTU );
55         size_t  len;
56
57         len = NetTest_ReadPacket(Ptr, MTU, buf);
58
59         tIPStackBuffer  *ret = IPStack_Buffer_CreateBuffer(1);
60         IPStack_Buffer_AppendSubBuffer(ret, len, 0, buf, NativeNic_int_FreePacket, Ptr);
61         return ret;
62 }
63
64 int NativeNic_SendPacket(void *Ptr, tIPStackBuffer *Buffer)
65 {
66         size_t  len = IPStack_Buffer_GetLength(Buffer);
67         
68         // Check against MTU
69         if( len > MTU )
70                 return -1;
71         
72         // Serialise into stack
73         char    buf[len];
74         IPStack_Buffer_GetData(Buffer, buf, len);
75         
76         NetTest_WritePacket(Ptr, len, buf);
77         return 0;
78 }

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