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, const void *Address, Uint16 Port, const void *Data, size_t Length);
16 // --- Client Channels
17 tVFS_Node *UDP_Channel_Init(tInterface *Interface);
18 size_t UDP_Channel_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags);
19 size_t UDP_Channel_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags);
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 tVFS_NodeType gUDP_NodeType = {
29 .Read = UDP_Channel_Read,
30 .Write = UDP_Channel_Write,
31 .IOCtl = UDP_Channel_IOCtl,
32 .Close = UDP_Channel_Close
34 tMutex glUDP_Channels;
35 tUDPChannel *gpUDP_Channels;
38 Uint32 gUDP_Ports[0x10000/32];
40 tSocketFile gUDP_SocketFile = {NULL, "udp", UDP_Channel_Init};
44 * \fn void TCP_Initialise()
45 * \brief Initialise the TCP Layer
49 IPStack_AddFile(&gUDP_SocketFile);
50 //IPv4_RegisterCallback(IP4PROT_UDP, UDP_GetPacket, UDP_Unreachable);
51 IPv4_RegisterCallback(IP4PROT_UDP, UDP_GetPacket);
55 * \brief Scan a list of tUDPChannels and find process the first match
56 * \return 0 if no match was found, -1 on error and 1 if a match was found
58 int UDP_int_ScanList(tUDPChannel *List, tInterface *Interface, void *Address, int Length, void *Buffer)
60 tUDPHeader *hdr = Buffer;
69 // Match local endpoint
70 if(chan->Interface && chan->Interface != Interface) continue;
71 if(chan->LocalPort != ntohs(hdr->DestPort)) continue;
73 // Check for remote port restriction
74 if(chan->Remote.Port && chan->Remote.Port != ntohs(hdr->SourcePort))
76 // Check for remote address restriction
79 if(chan->Remote.AddrType != Interface->Type)
81 if(!IPStack_CompareAddress(Interface->Type, Address,
82 &chan->Remote.Addr, chan->RemoteMask)
87 Log_Log("UDP", "Recieved packet for %p", chan);
88 // Create the cached packet
89 len = ntohs(hdr->Length);
90 pack = malloc(sizeof(tUDPPacket) + len);
92 memcpy(&pack->Remote.Addr, Address, IPStack_GetAddressSize(Interface->Type));
93 pack->Remote.Port = ntohs(hdr->SourcePort);
94 pack->Remote.AddrType = Interface->Type;
96 memcpy(pack->Data, hdr->Data, len);
98 // Add the packet to the channel's queue
99 SHORTLOCK(&chan->lQueue);
101 chan->QueueEnd->Next = pack;
103 chan->QueueEnd = chan->Queue = pack;
104 SHORTREL(&chan->lQueue);
105 VFS_MarkAvaliable(&chan->Node, 1);
106 Mutex_Release(&glUDP_Channels);
113 * \fn void UDP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer)
114 * \brief Handles a packet from the IP Layer
116 void UDP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer)
118 tUDPHeader *hdr = Buffer;
120 Log_Debug("UDP", "%i bytes :%i->:%i (Cksum 0x%04x)",
121 ntohs(hdr->Length), ntohs(hdr->SourcePort), ntohs(hdr->Length), ntohs(hdr->Checksum));
123 // Check registered connections
124 Mutex_Acquire(&glUDP_Channels);
125 UDP_int_ScanList(gpUDP_Channels, Interface, Address, Length, Buffer);
126 Mutex_Release(&glUDP_Channels);
130 * \brief Handle an ICMP Unrechable Error
132 void UDP_Unreachable(tInterface *Interface, int Code, void *Address, int Length, void *Buffer)
138 * \brief Send a packet
139 * \param Channel Channel to send the packet from
140 * \param Data Packet data
141 * \param Length Length in bytes of packet data
143 void UDP_SendPacketTo(tUDPChannel *Channel, int AddrType, const void *Address, Uint16 Port, const void *Data, size_t Length)
147 if(Channel->Interface && Channel->Interface->Type != AddrType) return ;
153 hdr.SourcePort = htons( Channel->LocalPort );
154 hdr.DestPort = htons( Port );
155 hdr.Length = htons( sizeof(tUDPHeader) + Length );
156 hdr.Checksum = 0; // Checksum can be zero on IPv4
157 // Pass on the the IPv4 Layer
158 tIPStackBuffer *buffer = IPStack_Buffer_CreateBuffer(2 + IPV4_BUFFERS);
159 IPStack_Buffer_AppendSubBuffer(buffer, Length, 0, Data, NULL, NULL);
160 IPStack_Buffer_AppendSubBuffer(buffer, sizeof(hdr), 0, &hdr, NULL, NULL);
161 // TODO: What if Channel->Interface is NULL here?
162 IPv4_SendPacket(Channel->Interface, *(tIPv4*)Address, IP4PROT_UDP, 0, buffer);
163 IPStack_Buffer_DestroyBuffer(buffer);
168 // --- Client Channels
169 tVFS_Node *UDP_Channel_Init(tInterface *Interface)
172 new = calloc( sizeof(tUDPChannel), 1 );
173 new->Interface = Interface;
174 new->Node.ImplPtr = new;
175 new->Node.NumACLs = 1;
176 new->Node.ACLs = &gVFS_ACL_EveryoneRW;
177 new->Node.Type = &gUDP_NodeType;
179 Mutex_Acquire(&glUDP_Channels);
180 new->Next = gpUDP_Channels;
181 gpUDP_Channels = new;
182 Mutex_Release(&glUDP_Channels);
188 * \brief Read from the channel file (wait for a packet)
190 size_t UDP_Channel_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags)
192 tUDPChannel *chan = Node->ImplPtr;
197 if(chan->LocalPort == 0) {
198 Log_Notice("UDP", "Channel %p sent with no local port", chan);
202 while(chan->Queue == NULL) Threads_Yield();
206 tTime timeout_z = 0, *timeout = (Flags & VFS_IOFLAG_NOBLOCK) ? &timeout_z : NULL;
207 int rv = VFS_SelectNode(Node, VFS_SELECT_READ, timeout, "UDP_Channel_Read");
209 errno = (Flags & VFS_IOFLAG_NOBLOCK) ? EWOULDBLOCK : EINTR;
211 SHORTLOCK(&chan->lQueue);
212 if(chan->Queue == NULL) {
213 SHORTREL(&chan->lQueue);
217 chan->Queue = pack->Next;
219 chan->QueueEnd = NULL;
220 VFS_MarkAvaliable(Node, 0); // Nothing left
222 SHORTREL(&chan->lQueue);
226 // Check that the header fits
227 addrlen = IPStack_GetAddressSize(pack->Remote.AddrType);
232 Log_Notice("UDP", "Insuficient space for header in buffer (%i < %i)", (int)Length, ofs);
237 ep->Port = pack->Remote.Port;
238 ep->AddrType = pack->Remote.AddrType;
239 memcpy(&ep->Addr, &pack->Remote.Addr, addrlen);
242 if(Length > ofs + pack->Length) Length = ofs + pack->Length;
243 memcpy((char*)Buffer + ofs, pack->Data, Length - ofs);
245 // Free cached packet
252 * \brief Write to the channel file (send a packet)
254 size_t UDP_Channel_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags)
256 tUDPChannel *chan = Node->ImplPtr;
257 const tUDPEndpoint *ep;
261 if(chan->LocalPort == 0) {
262 Log_Notice("UDP", "Write to channel %p with zero local port", chan);
267 ofs = 2 + 2 + IPStack_GetAddressSize( ep->AddrType );
269 data = (const char *)Buffer + ofs;
271 UDP_SendPacketTo(chan, ep->AddrType, &ep->Addr, ep->Port, data, (size_t)Length - ofs);
277 * \brief Names for channel IOCtl Calls
279 static const char *casIOCtls_Channel[] = {
288 * \brief Channel IOCtls
290 int UDP_Channel_IOCtl(tVFS_Node *Node, int ID, void *Data)
292 tUDPChannel *chan = Node->ImplPtr;
293 ENTER("pNode iID pData", Node, ID, Data);
296 BASE_IOCTLS(DRV_TYPE_MISC, "UDP Channel", 0x100, casIOCtls_Channel);
298 case 4: // getset_localport (returns bool success)
299 if(!Data) LEAVE_RET('i', chan->LocalPort);
300 if(!CheckMem( Data, sizeof(Uint16) ) ) {
301 LOG("Invalid pointer %p", Data);
305 chan->LocalPort = *(Uint16*)Data;
306 // Permissions check (Ports lower than 1024 are root-only)
307 if(chan->LocalPort != 0 && chan->LocalPort < 1024) {
308 if( Threads_GetUID() != 0 ) {
309 LOG("Attempt by non-superuser to listen on port %i", chan->LocalPort);
314 // Allocate a random port if requested
315 if( chan->LocalPort == 0 )
316 chan->LocalPort = UDP_int_AllocatePort();
319 // Else, mark the requested port as used
320 if( UDP_int_MarkPortAsUsed(chan->LocalPort) == 0 ) {
321 LOG("Port %i us currently in use", chan->LocalPort);
329 case 5: // getset_remoteport (returns bool success)
330 if(!Data) LEAVE_RET('i', chan->Remote.Port);
331 if(!CheckMem( Data, sizeof(Uint16) ) ) {
332 LOG("Invalid pointer %p", Data);
335 chan->Remote.Port = *(Uint16*)Data;
338 case 6: // getset_remotemask (returns bool success)
339 if(!Data) LEAVE_RET('i', chan->RemoteMask);
340 if(!CheckMem(Data, sizeof(int))) LEAVE_RET('i', -1);
341 if( !chan->Interface ) {
342 LOG("Can't set remote mask on NULL interface");
345 if( *(int*)Data > IPStack_GetAddressSize(chan->Interface->Type) )
347 chan->RemoteMask = *(int*)Data;
350 case 7: // set_remoteaddr (returns bool success)
351 if( !chan->Interface ) {
352 LOG("Can't set remote address on NULL interface");
355 if(!CheckMem(Data, IPStack_GetAddressSize(chan->Interface->Type))) {
356 LOG("Invalid pointer");
359 memcpy(&chan->Remote.Addr, Data, IPStack_GetAddressSize(chan->Interface->Type));
366 * \brief Close and destroy an open channel
368 void UDP_Channel_Close(tVFS_Node *Node)
370 tUDPChannel *chan = Node->ImplPtr;
373 // Remove from the main list first
374 Mutex_Acquire(&glUDP_Channels);
375 if(gpUDP_Channels == chan)
376 gpUDP_Channels = gpUDP_Channels->Next;
379 for(prev = gpUDP_Channels;
380 prev->Next && prev->Next != chan;
383 Log_Warning("UDP", "Bookeeping Fail, channel %p is not in main list", chan);
385 prev->Next = prev->Next->Next;
387 Mutex_Release(&glUDP_Channels);
390 SHORTLOCK(&chan->lQueue);
395 chan->Queue = tmp->Next;
398 SHORTREL(&chan->lQueue);
400 // Free channel structure
405 * \return Port Number on success, or zero on failure
407 Uint16 UDP_int_AllocatePort()
410 Mutex_Acquire(&glUDP_Ports);
412 for( i = UDP_ALLOC_BASE; i < 0x10000; i += 32 )
413 if( gUDP_Ports[i/32] != 0xFFFFFFFF )
415 if(i == 0x10000) return 0;
418 if( !(gUDP_Ports[i/32] & (1 << (i%32))) )
421 Mutex_Release(&glUDP_Ports);
425 * \brief Allocate a specific port
426 * \return Boolean Success
428 int UDP_int_MarkPortAsUsed(Uint16 Port)
430 Mutex_Acquire(&glUDP_Ports);
431 if( gUDP_Ports[Port/32] & (1 << (Port%32)) ) {
433 Mutex_Release(&glUDP_Ports);
435 gUDP_Ports[Port/32] |= 1 << (Port%32);
436 Mutex_Release(&glUDP_Ports);
441 * \brief Free an allocated port
443 void UDP_int_FreePort(Uint16 Port)
445 Mutex_Acquire(&glUDP_Ports);
446 gUDP_Ports[Port/32] &= ~(1 << (Port%32));
447 Mutex_Release(&glUDP_Ports);