UDP layer

equ >UdpDataPos \ -- offset
The UDP data offset in an outgoing UDP packet.

: UDPcksum      \ *hdr len ipsrc ipdest -- cksum
A generic UDP checksum function. The parameters supplied are used to form a 'pseudoheader' from which the acual checksum is generated.

: UDPSend       { hs *pb | *ip *sk -- len|err }
Send data on a connected UDP socket. HS is a handle to a UDP socket handle. *PB is the address of a PBuff pointer already filled with the data to send (as UDP data). Returns the number of bytes sent if OK else SOCKET_ERROR.

: SendDatagram  { *pb ipaddr | *ip *udp -- }
Send data as a UDP packet without using a socket. The parameters are *pb, a pointer to a PBuff containing the UDP data, and ipaddr, the IP address to send to.

: GetUDPAddress \ *pbuf -- *udp
Extract start of UDP data from IP packet.

: GetUDPLen     \ *udp -- len
Extract length of UDP packet.

: >UDPData      \ *udp -- *udpdata
Extract pointer to UDP packet data.

variable UDPServiceChain        \ -- addr
This variable anchors the chain that will be walked to find out what to do with a UDP packet that has arrived.

struct /UDPService      \ -- len
Describes the UDP service and how to run the service.

  int us.next           \ Link to next service in chain
  int us.port           \ UDP Port that this service handles
  int us.xtReceive      \ Word that will process this packet
  int us.xtIdle         \ Word that will run idle action
end-struct

: UDPprocessRx  \ *pb *udp --
Walk the UDP service chain and execute the first receive action for the port. If no association is found, try to route the packet.

: RxUDPPacket   { *pb | *udp hs *q bf -- }
Process a received UDP packet.