3 * - Link/Media Layer Interface
9 #define MAX_PACKET_SIZE 2048
12 int giRegisteredTypes = 0;
15 tPacketCallback Callback;
20 * \fn void Link_RegisterType(Uint16 Type, tPacketCallback Callback)
21 * \brief Registers a callback for a specific packet type
23 void Link_RegisterType(Uint16 Type, tPacketCallback Callback)
27 for( i = giRegisteredTypes; i -- ; )
29 if(gaRegisteredTypes[i].Type == Type) {
30 Warning("[NET ] Attempt to register 0x%x twice", Type);
34 if(gaRegisteredTypes[i].Callback == NULL) break;
39 tmp = realloc(gaRegisteredTypes, (giRegisteredTypes+1)*sizeof(*gaRegisteredTypes));
40 if(!tmp) Panic("[NET ] Out of heap space!");
41 i = giRegisteredTypes;
43 gaRegisteredTypes = tmp;
46 gaRegisteredTypes[i].Callback = Callback;
47 gaRegisteredTypes[i].Type = Type;
51 * \fn void Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, int Length, void *Buffer)
52 * \brief Formats and sends a packet on the specified interface
54 void Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, int Length, void *Buffer)
56 int bufSize = sizeof(tEthernetHeader) + Length;
58 tEthernetHeader *hdr = (void*)buf;
61 hdr->Src = Adapter->MacAddr;
62 hdr->Type = htons(Type);
64 memcpy(hdr->Data, Buffer, Length);
66 VFS_Write(Adapter->DeviceFD, bufSize, buf);
70 * \fn void Link_WatchDevice(tAdapter *Adapter)
71 * \brief Spawns a worker thread to watch the specified adapter
73 void Link_WatchDevice(tAdapter *Adapter)
75 int tid = Proc_SpawnWorker(); // Create a new worker thread
78 Panic("[NET ] Unable to create watcher thread for '%s'", Adapter->Device);
82 Log("[NET ] Watching '%s' using tid %i", Adapter->Device, tid);
87 while(Adapter->DeviceFD != -1)
89 Uint8 buf[MAX_PACKET_SIZE];
90 tEthernetHeader *hdr = (void*)buf;
94 ret = VFS_Read(Adapter->DeviceFD, MAX_PACKET_SIZE, buf);
97 if(ret <= sizeof(tEthernetHeader)) {
98 Log("[NET ] Recieved an undersized packet");
102 // Check if there is a registered callback for this packet type
103 for( i = giRegisteredTypes; i--; )
105 if(gaRegisteredTypes[i].Type == hdr->Type) continue;
108 if( i + 1 == 0 ) continue;
111 gaRegisteredTypes[i].Callback(
114 ret - sizeof(tEthernetHeader),