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

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