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 Log_Warning("NET", "Attempt to register 0x%x twice", Type);
48 if(gaRegisteredTypes[i].Callback == NULL) break;
53 tmp = realloc(gaRegisteredTypes, (giRegisteredTypes+1)*sizeof(*gaRegisteredTypes));
56 "Out of heap space! (Attempted to allocate %i)",
57 (giRegisteredTypes+1)*sizeof(*gaRegisteredTypes)
61 i = giRegisteredTypes;
63 gaRegisteredTypes = tmp;
66 gaRegisteredTypes[i].Callback = Callback;
67 gaRegisteredTypes[i].Type = Type;
71 * \fn void Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, int Length, void *Buffer)
72 * \brief Formats and sends a packet on the specified interface
74 void Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, int Length, void *Buffer)
76 int bufSize = sizeof(tEthernetHeader) + ((Length+3)&~3) + 4;
77 Uint8 buf[bufSize]; // dynamic stack arrays ftw!
78 tEthernetHeader *hdr = (void*)buf;
80 Log_Log("NET", "Sending %i bytes to %02x:%02x:%02x:%02x:%02x:%02x (Type 0x%x)",
81 Length, To.B[0], To.B[1], To.B[2], To.B[3], To.B[4], To.B[5], Type);
84 hdr->Src = Adapter->MacAddr;
85 hdr->Type = htons(Type);
87 memcpy(hdr->Data, Buffer, Length);
89 *(Uint32*) &hdr->Data[bufSize-4] = 0;
90 *(Uint32*) &hdr->Data[bufSize-4] = htonl( Link_CalculateCRC(buf, bufSize) );
92 VFS_Write(Adapter->DeviceFD, bufSize, buf);
96 * \fn void Link_WatchDevice(tAdapter *Adapter)
97 * \brief Spawns a worker thread to watch the specified adapter
99 void Link_WatchDevice(tAdapter *Adapter)
101 int tid = Proc_SpawnWorker(); // Create a new worker thread
104 Log_Warning("NET", "Unable to create watcher thread for '%s'", Adapter->Device);
109 Log_Log("NET", "Watching '%s' using tid %i", Adapter->Device, tid);
113 if( !gbLink_CRCTableGenerated )
117 while(Adapter->DeviceFD != -1)
119 Uint8 buf[MAX_PACKET_SIZE];
120 tEthernetHeader *hdr = (void*)buf;
124 // Wait for a packet (Read on a network device is blocking)
125 ret = VFS_Read(Adapter->DeviceFD, MAX_PACKET_SIZE, buf);
128 if(ret <= sizeof(tEthernetHeader)) {
129 Log_Log("NET", "Recieved an undersized packet");
133 Log_Log("NET", "Packet from %02x:%02x:%02x:%02x:%02x:%02x",
134 hdr->Src.B[0], hdr->Src.B[1], hdr->Src.B[2],
135 hdr->Src.B[3], hdr->Src.B[4], hdr->Src.B[5]
137 Log_Log("NET", "to %02x:%02x:%02x:%02x:%02x:%02x",
138 hdr->Dest.B[0], hdr->Dest.B[1], hdr->Dest.B[2],
139 hdr->Dest.B[3], hdr->Dest.B[4], hdr->Dest.B[5]
141 checksum = *(Uint32*)&hdr->Data[ret-sizeof(tEthernetHeader)-4];
142 Log_Log("NET", "Checksum 0x%08x", checksum);
144 // Check if there is a registered callback for this packet type
145 for( i = giRegisteredTypes; i--; )
147 if(gaRegisteredTypes[i].Type == hdr->Type) break;
151 Log_Log("NET", "Unregistered type 0x%x", ntohs(hdr->Type));
156 gaRegisteredTypes[i].Callback(
159 ret - sizeof(tEthernetHeader),
164 Log_Log("NET", "Watcher terminated (file closed)");
167 // From http://www.cl.cam.ac.uk/research/srg/bluebook/21/crc/node6.html
168 #define QUOTIENT 0x04c11db7
169 void Link_InitCRC(void)
174 for (i = 0; i < 256; i++)
177 for (j = 0; j < 8; j++)
179 if (crc & 0x80000000)
180 crc = (crc << 1) ^ QUOTIENT;
184 gaiLink_CRCTable[i] = crc;
187 gbLink_CRCTableGenerated = 1;
190 Uint32 Link_CalculateCRC(void *Data, int Length)
192 // x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1
197 if(Length < 4) return 0;
199 result = *data++ << 24;
200 result |= *data++ << 16;
201 result |= *data++ << 8;
206 for( i = 0; i < Length; i++ )
208 result = (result << 8 | *data++) ^ gaiLink_CRCTable[result >> 24];