6 #include <api_drv_common.h>
9 #define UDP_ALLOC_BASE 0xC000
12 void UDP_Initialise();
13 void UDP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer);
14 void UDP_Unreachable(tInterface *Interface, int Code, void *Address, int Length, void *Buffer);
15 void UDP_SendPacketTo(tUDPChannel *Channel, int AddrType, void *Address, Uint16 Port, void *Data, size_t Length);
16 // --- Client Channels
17 tVFS_Node *UDP_Channel_Init(tInterface *Interface);
18 Uint64 UDP_Channel_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
19 Uint64 UDP_Channel_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
20 int UDP_Channel_IOCtl(tVFS_Node *Node, int ID, void *Data);
21 void UDP_Channel_Close(tVFS_Node *Node);
23 Uint16 UDP_int_AllocatePort();
24 int UDP_int_MarkPortAsUsed(Uint16 Port);
25 void UDP_int_FreePort(Uint16 Port);
28 tMutex glUDP_Channels;
29 tUDPChannel *gpUDP_Channels;
32 Uint32 gUDP_Ports[0x10000/32];
34 tSocketFile gUDP_SocketFile = {NULL, "udp", UDP_Channel_Init};
38 * \fn void TCP_Initialise()
39 * \brief Initialise the TCP Layer
43 IPStack_AddFile(&gUDP_SocketFile);
44 //IPv4_RegisterCallback(IP4PROT_UDP, UDP_GetPacket, UDP_Unreachable);
45 IPv4_RegisterCallback(IP4PROT_UDP, UDP_GetPacket);
49 * \brief Scan a list of tUDPChannels and find process the first match
50 * \return 0 if no match was found, -1 on error and 1 if a match was found
52 int UDP_int_ScanList(tUDPChannel *List, tInterface *Interface, void *Address, int Length, void *Buffer)
54 tUDPHeader *hdr = Buffer;
63 // Match local endpoint
64 if(chan->Interface && chan->Interface != Interface) continue;
65 if(chan->LocalPort != ntohs(hdr->DestPort)) continue;
67 // Check for remote port restriction
68 if(chan->Remote.Port && chan->Remote.Port != ntohs(hdr->SourcePort))
70 // Check for remote address restriction
73 if(chan->Remote.AddrType != Interface->Type)
75 if(!IPStack_CompareAddress(Interface->Type, Address,
76 &chan->Remote.Addr, chan->RemoteMask)
81 Log_Log("UDP", "Recieved packet for %p", chan);
82 // Create the cached packet
83 len = ntohs(hdr->Length);
84 pack = malloc(sizeof(tUDPPacket) + len);
86 memcpy(&pack->Remote.Addr, Address, IPStack_GetAddressSize(Interface->Type));
87 pack->Remote.Port = ntohs(hdr->SourcePort);
89 memcpy(pack->Data, hdr->Data, len);
91 // Add the packet to the channel's queue
92 SHORTLOCK(&chan->lQueue);
94 chan->QueueEnd->Next = pack;
96 chan->QueueEnd = chan->Queue = pack;
97 SHORTREL(&chan->lQueue);
98 VFS_MarkAvaliable(&chan->Node, 1);
99 Mutex_Release(&glUDP_Channels);
106 * \fn void UDP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer)
107 * \brief Handles a packet from the IP Layer
109 void UDP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer)
111 tUDPHeader *hdr = Buffer;
113 Log_Debug("UDP", "hdr->SourcePort = %i", ntohs(hdr->SourcePort));
114 Log_Debug("UDP", "hdr->DestPort = %i", ntohs(hdr->DestPort));
115 Log_Debug("UDP", "hdr->Length = %i", ntohs(hdr->Length));
116 Log_Debug("UDP", "hdr->Checksum = 0x%x", ntohs(hdr->Checksum));
118 // Check registered connections
119 Mutex_Acquire(&glUDP_Channels);
120 UDP_int_ScanList(gpUDP_Channels, Interface, Address, Length, Buffer);
121 Mutex_Release(&glUDP_Channels);
125 * \brief Handle an ICMP Unrechable Error
127 void UDP_Unreachable(tInterface *Interface, int Code, void *Address, int Length, void *Buffer)
133 * \brief Send a packet
134 * \param Channel Channel to send the packet from
135 * \param Data Packet data
136 * \param Length Length in bytes of packet data
138 void UDP_SendPacketTo(tUDPChannel *Channel, int AddrType, void *Address, Uint16 Port, void *Data, size_t Length)
142 if(Channel->Interface && Channel->Interface->Type != AddrType) return ;
148 hdr = malloc(sizeof(tUDPHeader)+Length);
149 hdr->SourcePort = htons( Channel->LocalPort );
150 hdr->DestPort = htons( Port );
151 hdr->Length = htons( sizeof(tUDPHeader) + Length );
152 hdr->Checksum = 0; // Checksum can be zero on IPv4
153 memcpy(hdr->Data, Data, Length);
154 // Pass on the the IPv4 Layer
155 // TODO: What if Channel->Interface is NULL here?
156 IPv4_SendPacket(Channel->Interface, *(tIPv4*)Address, IP4PROT_UDP, 0, sizeof(tUDPHeader)+Length, hdr);
157 // Free allocated packet
163 // --- Client Channels
164 tVFS_Node *UDP_Channel_Init(tInterface *Interface)
167 new = calloc( sizeof(tUDPChannel), 1 );
168 new->Interface = Interface;
169 new->Node.ImplPtr = new;
170 new->Node.NumACLs = 1;
171 new->Node.ACLs = &gVFS_ACL_EveryoneRW;
172 new->Node.Read = UDP_Channel_Read;
173 new->Node.Write = UDP_Channel_Write;
174 new->Node.IOCtl = UDP_Channel_IOCtl;
175 new->Node.Close = UDP_Channel_Close;
177 Mutex_Acquire(&glUDP_Channels);
178 new->Next = gpUDP_Channels;
179 gpUDP_Channels = new;
180 Mutex_Release(&glUDP_Channels);
186 * \brief Read from the channel file (wait for a packet)
188 Uint64 UDP_Channel_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
190 tUDPChannel *chan = Node->ImplPtr;
195 if(chan->LocalPort == 0) {
196 Log_Notice("UDP", "Channel %p sent with no local port", chan);
200 while(chan->Queue == NULL) Threads_Yield();
204 VFS_SelectNode(Node, VFS_SELECT_READ, NULL, "UDP_Channel_Read");
205 SHORTLOCK(&chan->lQueue);
206 if(chan->Queue == NULL) {
207 SHORTREL(&chan->lQueue);
211 chan->Queue = pack->Next;
213 chan->QueueEnd = NULL;
214 VFS_MarkAvaliable(Node, 0); // Nothing left
216 SHORTREL(&chan->lQueue);
220 // Check that the header fits
222 ofs = 4 + IPStack_GetAddressSize(pack->Remote.AddrType);
225 Log_Notice("UDP", "Insuficient space for header in buffer (%i < %i)", (int)Length, ofs);
230 ep->Port = pack->Remote.Port;
231 ep->AddrType = pack->Remote.AddrType;
232 memcpy(&ep->Addr, &pack->Remote.Addr, IPStack_GetAddressSize(pack->Remote.AddrType));
235 if(Length > ofs + pack->Length) Length = ofs + pack->Length;
236 memcpy((char*)Buffer + ofs, pack->Data, Length - ofs);
238 // Free cached packet
245 * \brief Write to the channel file (send a packet)
247 Uint64 UDP_Channel_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
249 tUDPChannel *chan = Node->ImplPtr;
253 if(chan->LocalPort == 0) return 0;
256 ofs = 2 + 2 + IPStack_GetAddressSize( ep->AddrType );
258 data = (char*)Buffer + ofs;
260 UDP_SendPacketTo(chan, ep->AddrType, &ep->Addr, ep->Port, data, (size_t)Length - ofs);
266 * \brief Names for channel IOCtl Calls
268 static const char *casIOCtls_Channel[] = {
277 * \brief Channel IOCtls
279 int UDP_Channel_IOCtl(tVFS_Node *Node, int ID, void *Data)
281 tUDPChannel *chan = Node->ImplPtr;
282 ENTER("pNode iID pData", Node, ID, Data);
285 BASE_IOCTLS(DRV_TYPE_MISC, "UDP Channel", 0x100, casIOCtls_Channel);
287 case 4: // getset_localport (returns bool success)
288 if(!Data) LEAVE_RET('i', chan->LocalPort);
289 if(!CheckMem( Data, sizeof(Uint16) ) ) {
290 LOG("Invalid pointer %p", Data);
294 chan->LocalPort = *(Uint16*)Data;
295 // Permissions check (Ports lower than 1024 are root-only)
296 if(chan->LocalPort != 0 && chan->LocalPort < 1024) {
297 if( Threads_GetUID() != 0 ) {
298 LOG("Attempt by non-superuser to listen on port %i", chan->LocalPort);
303 // Allocate a random port if requested
304 if( chan->LocalPort == 0 )
305 chan->LocalPort = UDP_int_AllocatePort();
308 // Else, mark the requested port as used
309 if( UDP_int_MarkPortAsUsed(chan->LocalPort) == 0 ) {
310 LOG("Port %i us currently in use", chan->LocalPort);
318 case 5: // getset_remoteport (returns bool success)
319 if(!Data) LEAVE_RET('i', chan->Remote.Port);
320 if(!CheckMem( Data, sizeof(Uint16) ) ) {
321 LOG("Invalid pointer %p", Data);
324 chan->Remote.Port = *(Uint16*)Data;
327 case 6: // getset_remotemask (returns bool success)
328 if(!Data) LEAVE_RET('i', chan->RemoteMask);
329 if(!CheckMem(Data, sizeof(int))) LEAVE_RET('i', -1);
330 if( !chan->Interface ) {
331 LOG("Can't set remote mask on NULL interface");
334 if( *(int*)Data > IPStack_GetAddressSize(chan->Interface->Type) )
336 chan->RemoteMask = *(int*)Data;
339 case 7: // set_remoteaddr (returns bool success)
340 if( !chan->Interface ) {
341 LOG("Can't set remote address on NULL interface");
344 if(!CheckMem(Data, IPStack_GetAddressSize(chan->Interface->Type))) {
345 LOG("Invalid pointer");
348 memcpy(&chan->Remote.Addr, Data, IPStack_GetAddressSize(chan->Interface->Type));
355 * \brief Close and destroy an open channel
357 void UDP_Channel_Close(tVFS_Node *Node)
359 tUDPChannel *chan = Node->ImplPtr;
362 // Remove from the main list first
363 Mutex_Acquire(&glUDP_Channels);
364 if(gpUDP_Channels == chan)
365 gpUDP_Channels = gpUDP_Channels->Next;
368 for(prev = gpUDP_Channels;
369 prev->Next && prev->Next != chan;
372 Log_Warning("UDP", "Bookeeping Fail, channel %p is not in main list", chan);
374 prev->Next = prev->Next->Next;
376 Mutex_Release(&glUDP_Channels);
379 SHORTLOCK(&chan->lQueue);
384 chan->Queue = tmp->Next;
387 SHORTREL(&chan->lQueue);
389 // Free channel structure
394 * \return Port Number on success, or zero on failure
396 Uint16 UDP_int_AllocatePort()
399 Mutex_Acquire(&glUDP_Ports);
401 for( i = UDP_ALLOC_BASE; i < 0x10000; i += 32 )
402 if( gUDP_Ports[i/32] != 0xFFFFFFFF )
404 if(i == 0x10000) return 0;
407 if( !(gUDP_Ports[i/32] & (1 << (i%32))) )
410 Mutex_Release(&glUDP_Ports);
414 * \brief Allocate a specific port
415 * \return Boolean Success
417 int UDP_int_MarkPortAsUsed(Uint16 Port)
419 Mutex_Acquire(&glUDP_Ports);
420 if( gUDP_Ports[Port/32] & (1 << (Port%32)) ) {
422 Mutex_Release(&glUDP_Ports);
424 gUDP_Ports[Port/32] |= 1 << (Port%32);
425 Mutex_Release(&glUDP_Ports);
430 * \brief Free an allocated port
432 void UDP_int_FreePort(Uint16 Port)
434 Mutex_Acquire(&glUDP_Ports);
435 gUDP_Ports[Port/32] &= ~(1 << (Port%32));
436 Mutex_Release(&glUDP_Ports);