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 * \todo Make thread safe (place a mutex on the list)
25 void Link_RegisterType(Uint16 Type, tPacketCallback Callback)
29 for( i = giRegisteredTypes; i -- ; )
31 if(gaRegisteredTypes[i].Type == Type) {
32 Warning("[NET ] Attempt to register 0x%x twice", Type);
36 if(gaRegisteredTypes[i].Callback == NULL) break;
41 tmp = realloc(gaRegisteredTypes, (giRegisteredTypes+1)*sizeof(*gaRegisteredTypes));
43 Warning("[NET ] Out of heap space!");
46 i = giRegisteredTypes;
48 gaRegisteredTypes = tmp;
51 gaRegisteredTypes[i].Callback = Callback;
52 gaRegisteredTypes[i].Type = Type;
56 * \fn void Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, int Length, void *Buffer)
57 * \brief Formats and sends a packet on the specified interface
59 void Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, int Length, void *Buffer)
61 int bufSize = sizeof(tEthernetHeader) + Length;
63 tEthernetHeader *hdr = (void*)buf;
66 hdr->Src = Adapter->MacAddr;
67 hdr->Type = htons(Type);
69 memcpy(hdr->Data, Buffer, Length);
71 VFS_Write(Adapter->DeviceFD, bufSize, buf);
75 * \fn void Link_WatchDevice(tAdapter *Adapter)
76 * \brief Spawns a worker thread to watch the specified adapter
78 void Link_WatchDevice(tAdapter *Adapter)
80 int tid = Proc_SpawnWorker(); // Create a new worker thread
83 Warning("[NET ] Unable to create watcher thread for '%s'", Adapter->Device);
88 Log("[NET ] Watching '%s' using tid %i", Adapter->Device, tid);
93 while(Adapter->DeviceFD != -1)
95 Uint8 buf[MAX_PACKET_SIZE];
96 tEthernetHeader *hdr = (void*)buf;
99 // Wait for a packet (Read on a network device is blocking)
100 ret = VFS_Read(Adapter->DeviceFD, MAX_PACKET_SIZE, buf);
103 if(ret <= sizeof(tEthernetHeader)) {
104 Log("[NET ] Recieved an undersized packet");
108 // Check if there is a registered callback for this packet type
109 for( i = giRegisteredTypes; i--; )
111 if(gaRegisteredTypes[i].Type == hdr->Type) continue;
114 if( i == -1 ) continue;
117 gaRegisteredTypes[i].Callback(
120 ret - sizeof(tEthernetHeader),
125 Log("[NET ] Watcher terminated (file closed)");