Fiddling with IPStack
[tpg/acess2.git] / Modules / IPStack / link.c
1 /*
2  * Acess2 IP Stack
3  * - Link/Media Layer Interface
4  */
5 #include "ipstack.h"
6 #include "link.h"
7
8 // === CONSTANTS ===
9 #define MAX_PACKET_SIZE 2048
10
11 // === PROTOTYPES ===
12 void    Link_RegisterType(Uint16 Type, tPacketCallback Callback);
13 void    Link_InitCRC();
14 Uint32  Link_CalculateCRC(void *Data, int Length);
15 void    Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, int Length, void *Buffer);
16 void    Link_WatchDevice(tAdapter *Adapter);
17
18 // === GLOBALS ===
19  int    giRegisteredTypes = 0;
20  int    giRegisteredTypeSpace = 0;
21 struct {
22         Uint16  Type;
23         tPacketCallback Callback;
24 }       *gaRegisteredTypes;
25  int    gbLink_CRCTableGenerated = 0;
26 Uint32  gaiLink_CRCTable[256];
27
28 // === CODE ===
29 /**
30  * \fn void Link_RegisterType(Uint16 Type, tPacketCallback Callback)
31  * \brief Registers a callback for a specific packet type
32  * 
33  * \todo Make thread safe (place a mutex on the list)
34  */
35 void Link_RegisterType(Uint16 Type, tPacketCallback Callback)
36 {
37          int    i;
38         void    *tmp;
39         
40         Type = htons(Type);     // Set to network order
41         
42         for( i = giRegisteredTypes; i -- ; )
43         {
44                 if(gaRegisteredTypes[i].Type == Type) {
45                         Log_Warning("NET", "Attempt to register 0x%x twice", Type);
46                         return ;
47                 }
48                 // Ooh! Free slot!
49                 if(gaRegisteredTypes[i].Callback == NULL)       break;
50         }
51         
52         if(i == -1)
53         {
54                 giRegisteredTypeSpace += 5;
55                 tmp = realloc(gaRegisteredTypes, giRegisteredTypeSpace*sizeof(*gaRegisteredTypes));
56                 if(!tmp) {
57                         Log_Warning("NET",
58                                 "Out of heap space! (Attempted to allocate %i)",
59                                 giRegisteredTypeSpace*sizeof(*gaRegisteredTypes)
60                                 );
61                         return ;
62                 }
63                 gaRegisteredTypes = tmp;
64                 i = giRegisteredTypes;
65                 giRegisteredTypes ++;
66         }
67         
68         gaRegisteredTypes[i].Callback = Callback;
69         gaRegisteredTypes[i].Type = Type;
70 }
71
72 /**
73  * \fn void Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, int Length, void *Buffer)
74  * \brief Formats and sends a packet on the specified interface
75  */
76 void Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, int Length, void *Buffer)
77 {
78          int    bufSize = sizeof(tEthernetHeader) + ((Length+3)&~3) + 4;
79         Uint8   buf[bufSize];   // dynamic stack arrays ftw!
80         tEthernetHeader *hdr = (void*)buf;
81         
82         Log_Log("NET", "Sending %i bytes to %02x:%02x:%02x:%02x:%02x:%02x (Type 0x%x)",
83                 Length, To.B[0], To.B[1], To.B[2], To.B[3], To.B[4], To.B[5], Type);
84         
85         hdr->Dest = To;
86         hdr->Src = Adapter->MacAddr;
87         hdr->Type = htons(Type);
88         
89         memcpy(hdr->Data, Buffer, Length);
90         
91         *(Uint32*) &hdr->Data[bufSize-4] = 0;
92         *(Uint32*) &hdr->Data[bufSize-4] = htonl( Link_CalculateCRC(buf, bufSize) );
93         
94         VFS_Write(Adapter->DeviceFD, bufSize, buf);
95 }
96
97 /**
98  * \fn void Link_WatchDevice(tAdapter *Adapter)
99  * \brief Spawns a worker thread to watch the specified adapter
100  */
101 void Link_WatchDevice(tAdapter *Adapter)
102 {
103          int    tid = Proc_SpawnWorker();       // Create a new worker thread
104         
105         if(tid < 0) {
106                 Log_Warning("NET", "Unable to create watcher thread for '%s'", Adapter->Device);
107                 return ;
108         }
109         
110         if(tid > 0) {
111                 Log_Log("NET", "Watching '%s' using tid %i", Adapter->Device, tid);
112                 return ;
113         }
114         
115         if( !gbLink_CRCTableGenerated )
116                 Link_InitCRC();
117         
118         // Child Thread
119         while(Adapter->DeviceFD != -1)
120         {
121                 Uint8   buf[MAX_PACKET_SIZE];
122                 tEthernetHeader *hdr = (void*)buf;
123                  int    ret, i;
124                 Uint32  checksum;
125                 
126                 // Wait for a packet (Read on a network device is blocking)
127                 Log_Debug("NET", "Waiting on adapter FD#0x%x", Adapter->DeviceFD);
128                 ret = VFS_Read(Adapter->DeviceFD, MAX_PACKET_SIZE, buf);
129                 if(ret == -1)   break;
130                 
131                 if(ret <= (int)sizeof(tEthernetHeader)) {
132                         Log_Log("NET", "Recieved an undersized packet");
133                         continue;
134                 }
135                 
136                 Log_Log("NET", "Packet from %02x:%02x:%02x:%02x:%02x:%02x",
137                         hdr->Src.B[0], hdr->Src.B[1], hdr->Src.B[2],
138                         hdr->Src.B[3], hdr->Src.B[4], hdr->Src.B[5]
139                         );
140                 Log_Log("NET", "to %02x:%02x:%02x:%02x:%02x:%02x",
141                         hdr->Dest.B[0], hdr->Dest.B[1], hdr->Dest.B[2],
142                         hdr->Dest.B[3], hdr->Dest.B[4], hdr->Dest.B[5]
143                         );
144                 checksum = *(Uint32*)&hdr->Data[ret-sizeof(tEthernetHeader)-4];
145                 Log_Log("NET", "Checksum 0x%08x", checksum);
146                 
147                 // Check if there is a registered callback for this packet type
148                 for( i = giRegisteredTypes; i--; )
149                 {
150                         if(gaRegisteredTypes[i].Type == hdr->Type)      break;
151                 }
152                 // No? Ignore it
153                 if( i == -1 ) {
154                         Log_Log("NET", "Unregistered type 0x%x", ntohs(hdr->Type));
155                         continue;
156                 }
157                 
158                 // Call the callback
159                 gaRegisteredTypes[i].Callback(
160                         Adapter,
161                         hdr->Src,
162                         ret - sizeof(tEthernetHeader),
163                         hdr->Data
164                         );
165         }
166         
167         Log_Log("NET", "Watcher terminated (file closed)");
168         
169         Threads_Exit(0, 0);
170 }
171
172 // From http://www.cl.cam.ac.uk/research/srg/bluebook/21/crc/node6.html
173 #define QUOTIENT        0x04c11db7
174 void Link_InitCRC(void)
175 {
176      int        i, j;
177     Uint32      crc;
178
179     for (i = 0; i < 256; i++)
180     {
181         crc = i << 24;
182         for (j = 0; j < 8; j++)
183         {
184             if (crc & 0x80000000)
185                 crc = (crc << 1) ^ QUOTIENT;
186             else
187                 crc = crc << 1;
188         }
189         gaiLink_CRCTable[i] = crc;
190     }
191         
192         gbLink_CRCTableGenerated = 1;
193 }
194
195 Uint32 Link_CalculateCRC(void *Data, int Length)
196 {
197         // x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1
198         Uint32  result;
199      int        i;
200         Uint32  *data = Data;
201     
202     if(Length < 4)      return 0;
203
204     result = *data++ << 24;
205     result |= *data++ << 16;
206     result |= *data++ << 8;
207     result |= *data++;
208     result = ~ result;
209     Length -= 4;
210     
211     for( i = 0; i < Length; i++ )
212     {
213         result = (result << 8 | *data++) ^ gaiLink_CRCTable[result >> 24];
214     }
215     
216     return ~result;
217 }

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