Home Units Hierarchy Routines All identifiers

Unit unaSockets

DescriptionUsesClasses, Interfaces, Objects and RecordsFunctions and ProceduresTypesConstantsVariables

Description

Contains Windows sockets version 1.1 wrapper classes.

Version 2.5.2008.02 IOCP and WinSock 2.0 extentions

Uses

Overview

Classes, Interfaces, Objects and Records

Name Description
packed record ip_mreq – ip_mreq –
Class unaMulticastSocket interface to join on */

– unaMulticastSocket –

Class unaSocket Base class implementing Windows socket.
Class unaSocketsConnections – unaSocketsConnections –
Class unaSocks This class is used to create server and client sockets and manage the connections.
Class unaSocksConnection This is base class for connection between two sockets.
Class unaSocksThread This thread is used to handle server or client connections.
Class unaSocksThreads – unaSocketsThreads –
Class unaTcpSocket This class encapsulates TCP socket implementation.
Class unaUdpSocket This class encapsulates UDP socket implementation.
packed record unaURICrack URI crack record
Class unaWSA Windows sockets manager class.

Functions and Procedures

function checkError(value: int; fatal: bool = true ): int;
function lookupHost(const host: string; out ip: string; list: unaStringList = nil): int; overload;
function lookupHostH(const host: string): TIPv4H;
function lookupHostN(const host: string): TIPv4N;
function getHostInfoH(const ipH: TIPv4H): string;
function listAddresses(const host: string; list: unaStringList): int;
function lookupPort(const port: string): int; overload;
function lookupPort(const port: string; out port_info: protoent): int; overload;
function startup(version: uint = $0101): int;
function shutdown(): int;
function getWSAObject(): unaWSA;
function select(s: tSocket; r, w, e: pbool; timeout: tTimeout = tTimeout(INFINITE)): int;
function ipH2str(const ipH: TIPv4H): string; overload;
function ipH2str(const ipH: TIPv6H): string; overload;
function ipN2str(const ipN: TIPv4N): string; overload;
function ipN2str(const addr: TSockAddrIn): string; overload;
function ipH2ipN(const ipH: TIPv4H): TIPv4N;
function ipN2ipH(const ipN: TIPv4N): TIPv4H; overload;
function ipN2ipH(addr: PSockAddrIn): TIPv4H; overload;
function portHFromAddr(addr: PSockAddrIn): word;
function str2ipH(const ip: string): TIPv4H;
function str2ipN(const ip: string): TIPv4N;
function addr2str(addr: pSockAddrIn): string;
function str2addr(const ipport: string; var addr: sockaddr_in): bool;
function isMulticastAddr(const addr: string): bool;
function isMulticastAddrH(const ipH: TIPv4H): bool;
function isMulticastAddrN(const ipN: TIPv4N): bool;
function isBroadcastAddrN(const ipN: TIPv4N): bool;
function isLocalNetworkAddrH(const ipH: TIPv4H): bool;
function isThisHostIP_N(const ipN: TIPv4N): bool;
function sameAddr(const addr1, addr2: sockaddr_in; ipOnly: bool = false): bool;
function makeAddr(const host, port: string; var addr: sockaddr_in): bool;
function sameIPN(const ipN1, ipN2: TIPv4N): bool;
function httpQuery(const ip, port, query: string; callback: tIpQueryCallback = nil; timeout: tTimeout = 5000): int;
function ipQuery(const ip, port, query: string; proto: int = IPPROTO_TCP; callback: tIpQueryCallback = nil; timeout: tTimeout = 5000): int;
function crackURI(URI: string; var crack: unaURICrack; flags: DWORD = 0): bool;
function httpGetData(const URI: string; out data: aString; timeout: tTimeout = 5000): HRESULT;

Types

pIPv4N = ˆTIPv4N;
TIPv4N = array[0..3] of byte;
pIPv4H = ˆTIPv4H;
TIPv4H = uint32;
pIPv6H = ˆTIPv6H;
TIPv6H = array[0..15] of byte;
tConID = cardinal;
unaSocketEvent = (...);
unaSocksOnEventEvent = procedure(sender: tObject; event: unaSocketEvent; id, connId: tConID; data: pointer; size: uint) of object;
tIpQueryCallback = procedure(queryId: tConID; const query: string; const response, responseData: aString) of object;

Constants

c_defUdpConnTimeout = 60000 * 3;
c_unaMC_receive = 1;
c_unaMC_send = 2;
IP_ADD_MEMBERSHIP = 12;
IP_DEFAULT_MULTICAST_LOOP = 1;
IP_DEFAULT_MULTICAST_TTL = 1;
IP_DROP_MEMBERSHIP = 13;
IP_MAX_MEMBERSHIPS = 20;
IP_MULTICAST_IF = 9;
IP_MULTICAST_LOOP = 11;
IP_MULTICAST_TTL = 10;
SD_BOTH = 2;
SO_MAX_MSG_SIZE = $2003;

Variables

c_maxThreadPoolSize: unsigned = 256;

Description

Functions and Procedures

function checkError(value: int; fatal: bool = true ): int;

– internal –

function lookupHost(const host: string; out ip: string; list: unaStringList = nil): int; overload;

Lookups host name. If the "list" parameter is not nil, it also lists all addresses assigned to a host. Host name could be in integer ("194.44.186.254") or alpha ("www.microsoft.com") format or "" for local machine. Fills the given ip string with a string representation of the IP address of the host (if resolved).

Returns

0 if successful or specific WSA error otherwise.

function lookupHostH(const host: string): TIPv4H;

Lookups host name.

Parameters
host
Host name. Could be in integer ("194.44.186.254") or alpha ("www.microsoft.com") format or "" for local machine.
defValue
If lookup fails, it uses this default parameter value as the IP address (in host byte order) of the host.
Returns

IP address (in host byte order) of the host or default parameter value if lookup fails.

function lookupHostN(const host: string): TIPv4N;

Lookups host name.

Parameters
host
Host name. Could be in integer ("194.44.186.254") or alpha ("www.microsoft.com") format or "" for local machine.
defValue
If lookup fails, it uses this default parameter value as the IP address (in network byte order) of the host.
Returns

IP address (in network byte order) of the host or default parameter value if lookup fails.

function getHostInfoH(const ipH: TIPv4H): string;

Lookups host info.

Parameters
IP
ip address in host byte order.
Returns

FQDN of the host (if any)

function listAddresses(const host: string; list: unaStringList): int;

List all addresses assigned to a host.

Parameters
host
Host name could be in integer ("194.44.186.254") or alpha ("www.microsoft.com") format or "" for local machine.
list
List to be filled with IP addresses
Returns

0 if successful or specific WSA error otherwise.

function lookupPort(const port: string): int; overload;

Lookups port number.

Parameters
port
Port name in integer ("110") or string ("POP3") format.
Returns

port number if successful, or -1 if port is invalid or unknown.

function lookupPort(const port: string; out port_info: protoent): int; overload;

Lookups port number. Also fills given port_info parameter.

Parameters
port
Port name in integer ("110") or string ("POP3") format.
Returns

0 if successful, -1 if port is "" or specific WSA error otherwise.

function startup(version: uint = $0101): int;

Startups the Windows sockets by creating unaWsa class instance.

function shutdown(): int;

Shutdowns the Windows sockets by deleting the unaWsa class instance created in startup() routine.

function getWSAObject(): unaWSA;

Returns unaWSA class instance created by a call to startup(), or by unaSocks class.

function select(s: tSocket; r, w, e: pbool; timeout: tTimeout = tTimeout(INFINITE)): int;

Issues select() operation on a socket.

Parameters
s
Socket handle.
r
Pointer to a bool value, receiving READ status.
w
Pointer to a bool value, receiving WRITE status.
e
Pointer to a bool value, receiving ERROR status.
timeout
Specifies the amount of time in milliseconds.
Returns

0 if timeout occured, 1 if successfull, or some specific WSA error otherwise.

function ipH2str(const ipH: TIPv4H): string; overload;

Converts host byte order (little endian) unsigned 32 bits integer to string representing IP address (xxx.xxx.xxx.xxx).

function ipH2str(const ipH: TIPv6H): string; overload;

.

function ipN2str(const ipN: TIPv4N): string; overload;

Converts network byte order (big endian) unsigned 32 bits integer to string representing IP address (xxx.xxx.xxx.xxx).

function ipN2str(const addr: TSockAddrIn): string; overload;

Converts IP from address to string representing IP address (xxx.xxx.xxx.xxx).

function ipH2ipN(const ipH: TIPv4H): TIPv4N;

Converts host byte ordered IP to network byte order (big endian) unsigned 32 bits integer.

function ipN2ipH(const ipN: TIPv4N): TIPv4H; overload;

Converts network byte order (big endian) unsigned 32 bits integer to host byte ordered IP.

function ipN2ipH(addr: PSockAddrIn): TIPv4H; overload;

Converts network byte order (big endian) address record to host byte ordered IP.

function portHFromAddr(addr: PSockAddrIn): word;

Returns port (in host order) from address

function str2ipH(const ip: string): TIPv4H;

Converts string representing IP address (xxx.xxx.xxx.xxx) into host byte order (little endian) unsigned 32 bits integer.

function str2ipN(const ip: string): TIPv4N;

Converts string representing IP address (xxx.xxx.xxx.xxx) into network byte order (big endian) unsigned 32 bits integer.

function addr2str(addr: pSockAddrIn): string;

Converts socket address to string.

function str2addr(const ipport: string; var addr: sockaddr_in): bool;

Converts string to socket address. ipport should be in format udp://name-or-ip:port or tcp://name-or-ip:port

function isMulticastAddr(const addr: string): bool;

Return True if specified address is whiting multicast range.

function isMulticastAddrH(const ipH: TIPv4H): bool;

Return True if specified address is whiting multicast range.

Parameters
ip
IP in host order
function isMulticastAddrN(const ipN: TIPv4N): bool;

Return True if specified address is whiting multicast range.

Parameters
ip
IP in network order
function isBroadcastAddrN(const ipN: TIPv4N): bool;

Return True if specified address is whiting broadcast range.

Parameters
ip
IP in network order
function isLocalNetworkAddrH(const ipH: TIPv4H): bool;

Checks if IP is assigned to LAN range

Parameters
ip
IP in network order
Returns

True if specified address is valid for LAN only and cannot "legitimately appear on the public Internet".

function isThisHostIP_N(const ipN: TIPv4N): bool;

Checks if specified IP address is referencing this host.

Parameters
ip
IP address
Returns

True if IP is referencing local host is some way (127.0.0.1, etc)

function sameAddr(const addr1, addr2: sockaddr_in; ipOnly: bool = false): bool;

Returns True if two addresses are same (has same IP and port).

function makeAddr(const host, port: string; var addr: sockaddr_in): bool;

Returns True if function succeeded.

function sameIPN(const ipN1, ipN2: TIPv4N): bool;

Returns true if both IPs are same

function httpQuery(const ip, port, query: string; callback: tIpQueryCallback = nil; timeout: tTimeout = 5000): int;

returns -1 in case of some problem or valid queryId (which is an integer greater than 0) if all seems to be OK

function ipQuery(const ip, port, query: string; proto: int = IPPROTO_TCP; callback: tIpQueryCallback = nil; timeout: tTimeout = 5000): int;
 
function crackURI(URI: string; var crack: unaURICrack; flags: DWORD = 0): bool;

Uses InternetCrackURL(), takes care of unicode/ansi versions.

function httpGetData(const URI: string; out data: aString; timeout: tTimeout = 5000): HRESULT;

Downloads small file from HTTP server.

Parameters
URI
data location
data
remote data
timeout
timeout
Returns

S_OK if no error

Types

pIPv4N = ˆTIPv4N;
 
TIPv4N = array[0..3] of byte;
 
pIPv4H = ˆTIPv4H;

network byte order

TIPv4H = uint32;
 
pIPv6H = ˆTIPv6H;

host byte order

TIPv6H = array[0..15] of byte;
 
tConID = cardinal;
 
unaSocketEvent = (...);

Different events produced by unaSocks class.

unaseServerListen - SERVER: server was started unaseServerStop - SERVER: server was stopped unaseServerConnect - SERVER: new client was connected to server unaseServerData - SERVER: new data was received from client unaseServerDisconnect - SERVER: client was disconnected from server (by timeout or socket error for UDP)

unaseClientConnect - CLIENT: client was connected unaseClientData - CLIENT: new data was received from server unaseClientDisconnect - CLIENT: client was disconnected from server

unaseThreadStartupError - SERVER: server could not start - CLIENT: client could not connect to server

unaseThreadAdjustSocketOptions - SERVER: adjust socket options created by server (connId is tSocket, data <> nil means "main" socket) - CLIENT: adjust socket options created by client (connId is tSocket, data <> nil means "main" socket)

Values
  • unaseServerListen: server
  • unaseServerStop:  
  • unaseServerConnect:  
  • unaseServerData:  
  • unaseServerDisconnect:  
  • unaseClientConnect: client
  • unaseClientData:  
  • unaseClientDisconnect:  
  • unaseThreadStartupError: thread
  • unaseThreadAdjustSocketOptions:  
unaSocksOnEventEvent = procedure(sender: tObject; event: unaSocketEvent; id, connId: tConID; data: pointer; size: uint) of object;

Socks event handler type

tIpQueryCallback = procedure(queryId: tConID; const query: string; const response, responseData: aString) of object;

– IP/HTTP –

Constants

c_defUdpConnTimeout = 60000 * 3;

default timeout for sockets "connected" to UDP server is 3 minutes

c_unaMC_receive = 1;

I/O flags

c_unaMC_send = 2;
 
IP_ADD_MEMBERSHIP = 12;

set/get - IP multicast loopback */

IP_DEFAULT_MULTICAST_LOOP = 1;
 
IP_DEFAULT_MULTICAST_TTL = 1;

set - drop IP group membership */

IP_DROP_MEMBERSHIP = 13;

set - add IP group membership */

IP_MAX_MEMBERSHIPS = 20;
 
IP_MULTICAST_IF = 9;
 
IP_MULTICAST_LOOP = 11;

set/get - IP multicast TTL */

IP_MULTICAST_TTL = 10;

set/get - IP multicast interface */

SD_BOTH = 2;

missing in Delphi 5 or earlier

SO_MAX_MSG_SIZE = $2003;

maximum message size

Variables

c_maxThreadPoolSize: unsigned = 256;

maximum number of threads in pool

Author

(c) 2012 Lake of Soft


Generated by PasDoc 0.12.1 on 2012-10-09