Unit UDP

From Ultibo.org
Revision as of 04:44, 27 March 2017 by Ultibo (Talk | contribs)

Jump to: navigation, search

Return to Unit Reference


Description


Ultibo UDP (User Datagram Protocol) unit

To be documented

Constants



UDP specific constants UDP_*
Note: Some UDP definitions are in the Protocol or IP modules
 
UDP_PROTOCOL_NAME = 'UDP';  
 
MIN_UDP_PACKET = 8; Not Counting Adapter and Transport Header
MAX_UDP_PACKET = 8192; Not Counting Adapter and Transport Header
 
UDP_TIMEOUT = 0; Wait forever on a UDP Read
UDP_BUFFER_SIZE = 65536; UDP Receive Buffer Size
 
UDP_MAX_PORT = 65536;  
 
UDP_HEADER_SIZE = 8; SizeOf(TUDPHeader);
 
UDP_DATAGRAM_SIZE = 8; SizeOf(TUDPDatagram)


UDP socket option
See Sockets.pas


UDP port UDP_PORT_*
UDP_PORT_START = 49152; First dynamic port (Previously 1024) As per IANA assignment
UDP_PORT_STOP = 65534; Last dynamic port (Previously 5000) Short of IANA assignment to allow for rollover


Type definitions



UDP header

PUDPHeader = ^TUDPHeader;

TUDPHeader = packed record

Note: Some UDP definitions are in the Protocol or IP modules
Note: 8 Bytes
SourcePort:Word; Network Order
DestPort:Word; Network Order
Length:Word; Network Order
Checksum:Word;  

UDP datagram

PUDPDatagram = ^TUDPDatagram;

TUDPDatagram = record

Note: 8 Bytes (Used by UDPBuffer)
Size:Word; Word to keep size even
RemotePort:Word;  
Next:PUDPDatagram; Followed by RemoteAddress (4 or 16 Bytes)


Class definitions



UDP protocol transport

TUDPProtocolTransport = class(TProtocolTransport)

private
 
public
 

UDP protocol

TUDPProtocol = class(TNetworkProtocol)

constructor Create(AManager:TProtocolManager; const AName:String);  
destructor Destroy; override;  
private
FNextPort:Word;  
 
function PacketHandler(AHandle:THandle; ASource,ADest,APacket:Pointer; ASize:Integer; ABroadcast:Boolean):Boolean;  
protected
function AddTransport(ATransport:TNetworkTransport):Boolean; override;  
function RemoveTransport(ATransport:TNetworkTransport):Boolean; override;  
 
function OpenPort(ASocket:TProtocolSocket; APort:Word):Boolean; override;  
function ClosePort(ASocket:TProtocolSocket):Boolean; override;  
function FindPort(APort:Word; AWrite,ALock:Boolean):TProtocolPort; override;  
 
function FindSocket(AFamily,AStruct,AProtocol:Word; ALocalAddress,ARemoteAddress:Pointer; ALocalPort,ARemotePort:Word; ABroadcast,AListen,ALock:Boolean; AState:LongWord):TProtocolSocket; override;  
procedure FlushSockets(All:Boolean); override;  
 
function SelectCheck(ASource,ADest:PFDSet; ACode:Integer):Integer; override;  
 
function SendPacket(ASocket:TProtocolSocket; ASource,ADest:Pointer; ASourcePort,ADestPort:Word; APacket:PPacketFragment; ASize,AFlags:Integer):Integer; override;  
public
function Accept(ASocket:TProtocolSocket; ASockAddr:PSockAddr; AAddrLength:PInteger):TProtocolSocket; override;  
function Bind(ASocket:TProtocolSocket; var ASockAddr:TSockAddr; AAddrLength:Integer):Integer; override;  
function CloseSocket(ASocket:TProtocolSocket):Integer; override;  
function Connect(ASocket:TProtocolSocket; var ASockAddr:TSockAddr; AAddrLength:Integer):Integer; override;  
function IoctlSocket(ASocket:TProtocolSocket; ACmd:DWORD; var AArg:u_long):Integer; override;  
function GetPeerName(ASocket:TProtocolSocket; var ASockAddr:TSockAddr; var AAddrLength:Integer):Integer; override;  
function GetSockName(ASocket:TProtocolSocket; var ASockAddr:TSockAddr; var AAddrLength:Integer):Integer; override;  
function GetSockOpt(ASocket:TProtocolSocket; ALevel,AOptName:Integer; AOptValue:PChar; var AOptLength:Integer):Integer; override;  
function Listen(ASocket:TProtocolSocket; ABacklog:Integer):Integer; override;  
function Recv(ASocket:TProtocolSocket; var ABuffer; ALength,AFlags:Integer):Integer; override;  
function RecvFrom(ASocket:TProtocolSocket; var ABuffer; ALength,AFlags:Integer; var AFromAddr:TSockAddr; var AFromLength:Integer):Integer; override;  
function Select(ANfds:Integer; AReadfds,AWritefds,AExceptfds:PFDSet; ATimeout:PTimeVal):LongInt; override;  
function Send(ASocket:TProtocolSocket; var ABuffer; ALength,AFlags:Integer):Integer; override;  
function SendTo(ASocket:TProtocolSocket; var ABuffer; ALength,AFlags:Integer; var AToAddr:TSockAddr; AToLength:Integer):Integer; override;  
function SetSockOpt(ASocket:TProtocolSocket; ALevel,AOptName:Integer; AOptValue:PChar; AOptLength:Integer):Integer; override;  
function Shutdown(ASocket:TProtocolSocket; AHow:Integer):Integer; override;  
function Socket(AFamily,AStruct,AProtocol:Integer):TProtocolSocket; override;  
 
function StartProtocol:Boolean; override;  
function StopProtocol:Boolean; override;  
function ProcessProtocol:Boolean; override;  

UDP socket

TUDPSocket = class(TProtocolSocket)

Note: SOCK_DGRAM
 
constructor Create(AProtocol:TNetworkProtocol; ATransport:TNetworkTransport);  
destructor Destroy; override;  
private
FRecvData:TUDPBuffer;  
public
property RecvData:TUDPBuffer read FRecvData;  
 
function GetOption(ALevel,AOptName:Integer; AOptValue:PChar; var AOptLength:Integer):Integer; override;  
function SetOption(ALevel,AOptName:Integer; AOptValue:PChar; AOptLength:Integer):Integer; override;  
function IoCtl(ACommand:DWORD; var AArgument:u_long):Integer; override;  
 
function IsConnected(ALocalAddress,ARemoteAddress:Pointer; ALocalPort,ARemotePort:Word; ABroadcast:Boolean):Boolean; override;  
function IsListening(ALocalAddress,ARemoteAddress:Pointer; ALocalPort,ARemotePort:Word; ABroadcast:Boolean):Boolean; override;  

UDP state

TUDPState = class(TProtocolState)

constructor Create;  
private
 
public
 

UDP buffer

TUDPBuffer = class(TSocketBuffer)

constructor Create(ASocket:TTransportSocket);  
destructor Destroy; override;  
private
FOffset:Word; Offset to RemoteAddress
FLength:Word; Length of RemoteAddress
 
FRead:Pointer; Pointer to Next Read from Buffer
FWrite:Pointer; Pointer to Next Write to Buffer
 
FCount:LongWord; Number of Datagrams in Buffer
 
FFirst:PUDPDatagram; Pointer to First Datagram
FLast:PUDPDatagram; Pointer to Last Datagram
 
function AddDatagram(ASize:Integer):Boolean;  
function RemoveDatagram:Boolean;  
procedure FlushDatagrams;  
protected
procedure SetSize(ASize:LongWord); override;  
public
function GetNext:Integer;  
function GetCount:LongWord;  
 
function ReadBuffer(var ABuffer; var ASize:Integer; ARemoteAddress:Pointer; ARemotePort:PWORD; AFlags:Integer):Boolean;  
function WriteBuffer(var ABuffer; ASize:Integer; ARemoteAddress:Pointer; ARemotePort:PWORD):Boolean;  

UDP options

TUDPOptions = class(TProtocolOptions)

Note: For Get/Set Options Level = IPPROTO_UDP Option = ????
 
constructor Create;  
private
FOptions:LongWord;  
 
function GetNoChecksum:Boolean;  
procedure SetNoChecksum(ANoChecksum:Boolean);  
public
property NoChecksum:Boolean read GetNoChecksum write SetNoChecksum;  


Public variables


None defined

Function declarations



Initialization Functions

procedure UDPInit;
Description: To be documented
Note None documented


UDP functions

function CheckUDP(AFamily:Word; ABuffer:Pointer):Boolean;
Description: Verify that the packet is a valid UDP packet
Buffer The complete packet including Transport header
Note If checksum is zero then no checksum was added, return True


function GetUDPHeaderOffset(AFamily:Word; ABuffer:Pointer):Word;
Description: To be documented
Buffer The complete packet including Transport header


function GetUDPHeaderLength(AFamily:Word; ABuffer:Pointer):Word;
Description: To be documented
Buffer The complete packet including Transport header


function GetUDPDataOffset(AFamily:Word; ABuffer:Pointer):Word;
Description: To be documented
Buffer The complete packet including Transport header


function GetUDPDataLength(AFamily:Word; ABuffer:Pointer):Word;
Description: To be documented
Buffer The complete packet including Transport header


function ChecksumUDPRecv(AFamily:Word; APseudo:PIPPseudo; ABuffer:Pointer; AOffset,ALength:Word):Word;
Description: Validate the Checksum of UDP Pseudo, Header and Data on Receive
Note None documented


function ChecksumUDPSend(AFamily:Word; APseudo:PIPPseudo; AHeader:PUDPHeader; AData:Pointer; ALength:Word):Word;
Description: Checksum the UDP Pseudo, Header and Data on Send
Note None documented


Return to Unit Reference