3 * - Link/Media Layer Interface
9 #define MAX_PACKET_SIZE 2048
12 void Link_RegisterType(Uint16 Type, tPacketCallback Callback);
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);
19 int giRegisteredTypes = 0;
22 tPacketCallback Callback;
24 int gbLink_CRCTableGenerated = 0;
25 Uint32 gaiLink_CRCTable[256];
29 * \fn void Link_RegisterType(Uint16 Type, tPacketCallback Callback)
30 * \brief Registers a callback for a specific packet type
32 * \todo Make thread safe (place a mutex on the list)
34 void Link_RegisterType(Uint16 Type, tPacketCallback Callback)
39 Type = htons(Type); // Set to network order
41 for( i = giRegisteredTypes; i -- ; )
43 if(gaRegisteredTypes[i].Type == Type) {
44 Warning("[NET ] Attempt to register 0x%x twice", Type);
48 if(gaRegisteredTypes[i].Callback == NULL) break;
53 tmp = realloc(gaRegisteredTypes, (giRegisteredTypes+1)*sizeof(*gaRegisteredTypes));
55 Warning("[NET ] Out of heap space!");
58 i = giRegisteredTypes;
60 gaRegisteredTypes = tmp;
63 gaRegisteredTypes[i].Callback = Callback;
64 gaRegisteredTypes[i].Type = Type;
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
71 void Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, int Length, void *Buffer)
73 int bufSize = sizeof(tEthernetHeader) + Length + 4;
74 Uint8 buf[bufSize]; // dynamic stack arrays ftw!
75 tEthernetHeader *hdr = (void*)buf;
77 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);
81 hdr->Src = Adapter->MacAddr;
82 hdr->Type = htons(Type);
84 memcpy(hdr->Data, Buffer, Length);
86 *(Uint32*) &hdr->Data[Length] = 0;
87 *(Uint32*) &hdr->Data[Length] = htonl( Link_CalculateCRC(buf, bufSize) );
90 VFS_Write(Adapter->DeviceFD, bufSize, buf);
94 * \fn void Link_WatchDevice(tAdapter *Adapter)
95 * \brief Spawns a worker thread to watch the specified adapter
97 void Link_WatchDevice(tAdapter *Adapter)
99 int tid = Proc_SpawnWorker(); // Create a new worker thread
102 Warning("[NET ] Unable to create watcher thread for '%s'", Adapter->Device);
107 Log("[NET ] Watching '%s' using tid %i", Adapter->Device, tid);
111 if( !gbLink_CRCTableGenerated )
115 while(Adapter->DeviceFD != -1)
117 Uint8 buf[MAX_PACKET_SIZE];
118 tEthernetHeader *hdr = (void*)buf;
122 // Wait for a packet (Read on a network device is blocking)
123 ret = VFS_Read(Adapter->DeviceFD, MAX_PACKET_SIZE, buf);
126 if(ret <= sizeof(tEthernetHeader)) {
127 Log("[NET ] Recieved an undersized packet");
131 Log("[NET ] Packet from %02x:%02x:%02x:%02x:%02x:%02x",
132 hdr->Src.B[0], hdr->Src.B[1], hdr->Src.B[2],
133 hdr->Src.B[3], hdr->Src.B[4], hdr->Src.B[5]
135 Log("[NET ] to %02x:%02x:%02x:%02x:%02x:%02x",
136 hdr->Dest.B[0], hdr->Dest.B[1], hdr->Dest.B[2],
137 hdr->Dest.B[3], hdr->Dest.B[4], hdr->Dest.B[5]
139 checksum = *(Uint32*)&hdr->Data[ret-sizeof(tEthernetHeader)-4];
140 Log("[NET ] Checksum 0x%08x", checksum);
142 // Check if there is a registered callback for this packet type
143 for( i = giRegisteredTypes; i--; )
145 if(gaRegisteredTypes[i].Type == hdr->Type) break;
149 Log("[NET ] Unregistered type 0x%x", ntohs(hdr->Type));
154 gaRegisteredTypes[i].Callback(
157 ret - sizeof(tEthernetHeader),
162 Log("[NET ] Watcher terminated (file closed)");
165 // From http://www.cl.cam.ac.uk/research/srg/bluebook/21/crc/node6.html
166 #define QUOTIENT 0x04c11db7
167 void Link_InitCRC(void)
172 for (i = 0; i < 256; i++)
175 for (j = 0; j < 8; j++)
177 if (crc & 0x80000000)
178 crc = (crc << 1) ^ QUOTIENT;
182 gaiLink_CRCTable[i] = crc;
185 gbLink_CRCTableGenerated = 1;
188 Uint32 Link_CalculateCRC(void *Data, int Length)
190 // x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1
195 if(Length < 4) return 0;
197 result = *data++ << 24;
198 result |= *data++ << 16;
199 result |= *data++ << 8;
204 for( i = 0; i < Length; i++ )
206 result = (result << 8 | *data++) ^ gaiLink_CRCTable[result >> 24];