Tools/nativelib - Many features implimented
[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         if( UnHex(macaddr, 6, DevDesc) != 6 )
37                 return -1;
38         void *ptr = NetTest_OpenTap(colonpos+1);
39         if( !ptr )
40                 return 1;
41         IPStack_Adapter_Add(&gNativeNIC_AdapterType, ptr, macaddr);
42         
43         return 0;
44 }
45
46 void NativeNic_int_FreePacket(void *Ptr, size_t pkt_length, size_t Unused, const void *DataPtr)
47 {
48         free( (void*)DataPtr );
49 }
50
51 tIPStackBuffer *NativeNic_WaitForPacket(void *Ptr)
52 {
53         char    *buf = malloc( MTU );
54         size_t  len;
55
56         len = NetTest_ReadPacket(Ptr, MTU, buf);
57
58         tIPStackBuffer  *ret = IPStack_Buffer_CreateBuffer(1);
59         IPStack_Buffer_AppendSubBuffer(ret, len, 0, buf, NativeNic_int_FreePacket, Ptr);
60         return ret;
61 }
62
63 int NativeNic_SendPacket(void *Ptr, tIPStackBuffer *Buffer)
64 {
65         size_t  len = IPStack_Buffer_GetLength(Buffer);
66         
67         // Check against MTU
68         if( len > MTU )
69                 return -1;
70         
71         // Serialise into stack
72         char    buf[len];
73         IPStack_Buffer_GetData(Buffer, buf, len);
74         
75         NetTest_WritePacket(Ptr, len, buf);
76         return 0;
77 }

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