A.12 IP "Sockets" Networking (TCP/IP, UDP/IP)

As the IP networking functions (with a few exceptions) are exact clones of identically-named WinSock functions, many references and tutorials are available on their use. Perhaps the best function reference for WinSock is online at http://www.sockets.com/winsock.htm.

Also, as WinSock is based on the BSD socket design, almost all UNIX systems have man pages describing these functions and their behavior. However, as this library is based on WinSock, there may be minor differences in operation between the UNIX descriptions and the operation of these functions.

The functions not reflected in WinSock or BSD sockets are InitSocket(), ExitSocket(), Socket_SetCallback(), and Socket_AddCallback(). InitSocket() and ExitSocket() function similarly to the initialization and shutdown functions in other modules of PModeLib. Socket_AddCallback() is essentially a mapping of WinSock's WSAAsyncSelect() function into the DOS assembly environment.

A.12.1 Data Structures

A.12.1.1 SOCKADDR

STRUC   SOCKADDR
.Port           resw 1  ; Port number
.Address        resd 1  ; 32-bit IP address
ENDSTRUC

A.12.1.2 HOSTENT

STRUC   HOSTENT
.Name           resd 1  ; Pointer to official name of host
                        ;  (0-terminated string)
.Aliases        resd 1  ; Pointer to 0-terminated array of pointers to
                        ;  0-terminated alias name strings
.AddrList       resd 1  ; Pointer to 0-terminated array of pointers to
                        ;  32-bit IP addresses
ENDSTRUC

A.12.2 Constants

A.12.2.1 Addresses

INADDR_ANY

The "any" address (0.0.0.0). Use this address when it doesn't matter what address a socket has.

INADDR_LOOPBACK

The "loopback" address (127.0.0.1). Also called "localhost", this address refers to the local machine.

INADDR_BROADCAST

The "broadcast" address (255.255.255.255). This address refers to all reachable hosts on the local network.

A.12.2.2 Socket Types

SOCK_STREAM

A stream (TCP/IP) socket.

SOCK_DGRAM

A datagram (UDP/IP) socket.

A.12.2.3 Events

SOCKEVENT_READ

Socket is ready for reading.

SOCKEVENT_WRITE

Socket is ready for writing.

SOCKEVENT_OOB

Socket received out-of-band data.

SOCKEVENT_ACCEPT

Socket is ready to accept a new incoming connection.

SOCKEVENT_CONNECT

Socket completed connection process.

SOCKEVENT_CLOSE

Socket closed (possibly by remote end).

A.12.2.4 Protocols

IPPROTO_TCP

TCP protocol/layer.

SOL_SOCKET

Specifies Sockets layer for Socket_getsockopt() and Socket_setsockopt() (not really a protocol).

A.12.2.5 Socket Options

Socket options may be set using the Socket_getsockopt() and Socket_setsockopt() functions. Some options may be read-only in some cases (e.g. changes may be ignored or have no effect when trying to set them with Socket_setsockopt()).

SOCKOPT_ACCEPTCONN

Boolean indicating if the socket is in the listen state. False (0) unless a Socket_listen() has been performed.

SOCKOPT_BROADCAST

Boolean indicating if the socket is configured for the transmission of broadcast messages. Defaults to false (0).

SOCKOPT_DEBUG

Read-only boolean indicating if debugging is enabled for the socket. Defaults to false (0).

SOCKOPT_DONTLINGER

Boolean indicating if the SOCKOPT_LINGER option is disabled. Defaults to true (1).

SOCKOPT_DONTROUTE

Read-only boolean indicating if routing is disabled for the socket. Defaults to false (0).

SOCKOPT_ERROR

Integer error status for the socket. When read, cleared to 0. Default value is 0.

SOCKOPT_KEEPALIVE

Boolean indicating if keepalives are being sent for the socket. Defaults to false (0).

SOCKOPT_LINGER

The current linger options. Not available in current implementation.

SOCKOPT_OOBINLINE

Boolean indicating if out-of-band data is being received in the normal data stream. Defaults to false (0).

SOCKOPT_RCVBUF

Read-only integer buffer size for receives on the socket.

SOCKOPT_REUSEADDR

Boolean indicating if the address to which the socket is bound can be used by other sockets. Defaults to false (0).

SOCKOPT_SNDBUF

Read-only integer buffer size for sends on the socket.

SOCKOPT_TYPE

Integer indicating the type of the socket (e.g. SOCK_STREAM). Defaults to the type specified when the socket was created (via Socket_create()).

TCP_NODELAY

Boolean indicating if the Nagle algorithm for send coalescing is disabled.

A.12.3 InitSocket()

Usage

bool InitSocket(void);

Purpose

Initializes socket driver.

Inputs

None

Outputs

Returns 1 on error, 0 on success.

Notes

Important: Call this function before calling any other socket routines!

A.12.4 ExitSocket()

Usage

void ExitSocket(void);

Purpose

Shuts down socket driver.

Inputs

None

Outputs

None

Notes

Assumes InitSocket() has been called.

A.12.5 Socket_SetCallback()

Usage

bool Socket_SetCallback(unsigned int HandlerAddress);

Purpose

Sets the callback function used for socket event notification.

Inputs

HandlerAddress, address of the callback procedure.

Outputs

Returns 1 on error, 0 on success.

Notes

Callback procedure should use the C calling convention, compatible with the following C declaration:

void Callback(unsigned int Socket, unsigned int Event);

Socket is the socket that triggered the event(s).

Event is the bitmask of the event(s) triggering the callback. The bitmask is an OR'ed combination of SOCKEVENT_ constants, listed in Section A.12.2.3.

A.12.6 Socket_AddCallback()

Usage

bool Socket_AddCallback(unsigned int Socket, unsigned int EventMask);

Purpose

Requests event notification for a socket.

Inputs

Socket, the socket to enable notification events for.

EventMask, bitmask designating which events to call the callback for. This should be an OR'ed combination of SOCKEVENT_ constants, listed in Section A.12.2.3.

Outputs

Returns 1 on error, 0 on success.

Notes

Assumes Socket_SetCallback() has been called to set a socket callback handler.

If called more than once for a particular socket, only the last call's EventMask is active. To disable callbacks for a particular socket, call with EventMask=0.

A.12.7 Socket_accept()

Usage

unsigned int Socket_accept(unsigned int Socket, SOCKADDR *Name);

Purpose

Accepts a connection on a socket.

Inputs

Socket, a socket which is listening for connections after a Socket_listen().

Name, an optional (may be 0) pointer to a SOCKADDR structure which receives the network address of the connecting entity.

Outputs

Returns -1 on error, otherwise returns the socket for the accepted connection and fills the SOCKADDR structure pointed to by Name (if Name is not 0).

A.12.8 Socket_bind()

Usage

bool Socket_bind(unsigned int Socket, SOCKADDR *Name);

Purpose

Associates a local address with a socket.

Inputs

Socket, an unbound socket.

Name, a pointer to a SOCKADDR structure containing the network address to assign to the socket.

Outputs

Returns 1 on error, 0 on success.

A.12.9 Socket_close()

Usage

bool Socket_bind(unsigned int Socket);

Purpose

Closes a socket.

Inputs

Socket, the socket to close.

Outputs

Returns 1 on error, 0 on success.

A.12.10 Socket_connect()

Usage

bool Socket_connect(unsigned int Socket, SOCKADDR *Name);

Purpose

Establishes a connection to a peer.

Inputs

Socket, an unconnected socket.

Name, a pointer to a SOCKADDR structure containing the network address of the peer to which the socket is to be connected.

Outputs

Returns 1 on error, 0 on success.

A.12.11 Socket_create()

Usage

unsigned int Socket_create(int Type);

Purpose

Creates a socket.

Inputs

Type, the type of socket to create, must be one of the types listed in Section A.12.2.2.

Outputs

Returns -1 on error, or the created socket on success.

A.12.12 Socket_getpeername()

Usage

bool Socket_getpeername(unsigned int Socket, SOCKADDR *Name);

Purpose

Gets the address of the peer to which a socket is connected.

Inputs

Socket, a connected socket.

Name, a pointer to a SOCKADDR structure which will receive the network address of the remote peer.

Outputs

Returns 1 on error, otherwise returns 0 and fills the SOCKADDR structure pointed to by Name.

A.12.13 Socket_getsockname()

Usage

bool Socket_getsockname(unsigned int Socket, SOCKADDR *Name);

Purpose

Gets the local address of a socket.

Inputs

Socket, a connected socket.

Name, a pointer to a SOCKADDR structure which will receive the network address of the socket.

Outputs

Returns 1 on error, otherwise returns 0 and fills the SOCKADDR structure pointed to by Name.

A.12.14 Socket_getsockopt()

Usage

bool Socket_getsockopt(unsigned int Socket, int Level, int OptName, char *OptVal, int *OptLen);

Purpose

Retrieves a socket option.

Inputs

Socket, a socket.

Level, the level at which the option is defined. Supported levels are SOL_SOCKET (socket level) and IPPROTO_TCP (TCP level).

OptName, the option for which the value is to be retrieved. See Section A.12.2.5 for a complete list of options.

OptVal, the buffer in which the value for the requested option is to be returned.

OptLen, (pointer to) the size of the buffer pointed to by OptVal.

Outputs

Returns 1 on error, otherwise returns 0 and fills the buffer pointed to by OptVal.

A.12.15 Socket_htonl()

Usage

unsigned int Socket_htonl(unsigned int HostVal);

Purpose

Converts an unsigned int from host to network byte order.

Inputs

HostVal, a 32-bit number in host byte order.

Outputs

Returns the HostVal in network byte order.

A.12.16 Socket_ntohl()

Usage

unsigned int Socket_ntohl(unsigned int NetVal);

Purpose

Converts an unsigned int from network to host byte order.

Inputs

NetVal, a 32-bit number in network byte order.

Outputs

Returns the NetVal in host byte order.

A.12.17 Socket_htons()

Usage

unsigned short Socket_htons(unsigned short HostVal);

Purpose

Converts an unsigned short from host to network byte order.

Inputs

HostVal, a 16-bit number in host byte order.

Outputs

Returns the HostVal in network byte order.

A.12.18 Socket_ntohs()

Usage

unsigned short Socket_ntohs(unsigned short NetVal);

Purpose

Converts an unsigned short from network to host byte order.

Inputs

NetVal, a 16-bit number in network byte order.

Outputs

Returns the NetVal in host byte order.

A.12.19 Socket_inet_addr()

Usage

unsigned int Socket_inet_addr(char *DottedAddress);

Purpose

Converts a string containing a dotted address into a 32-bit address.

Inputs

DottedAddress, pointer to 0-terminated string representing a number expressed in the Internet standard "." notation.

Outputs

Returns the Internet address corresponding to DottedAddress in network byte order, or 0 if DottedAddress is invalid.

A.12.20 Socket_inet_ntoa()

Usage

char *Socket_inet_ntoa(unsigned int Address);

Purpose

Converts a 32-bit network address into a string in dotted decimal format.

Inputs

Address, Internet address, in network byte order, to convert.

Outputs

Returns pointer to a 0-terminated static string containing the address in standard "." notation. This buffer is overwritten on subsequent calls to this function.

A.12.21 Socket_listen()

Usage

bool Socket_listen(unsigned int Socket, int BackLog);

Purpose

Enables a socket to listen for incoming connections.

Inputs

Socket, a bound (using Socket_bind()), unconnected socket.

BackLog, the maximum length to which the queue of pending connections may grow.

Outputs

Returns 1 on error, 0 on success.

Notes

BackLog is silently limited to between 1 and 5, inclusive.

A.12.22 Socket_recv()

Usage

int Socket_recv(unsigned int Socket, void *Buffer, int MaxLen, unsigned int Flags);

Purpose

Receives data from a connected socket.

Inputs

Socket, a connected socket.

Buffer, the starting address of the buffer to be filled with the incoming data.

MaxLen, the maximum number of bytes to receive.

Flags, bitmask specifying special operation for the function:

  • Bit 0 = PEEK: peek at the incoming data. The data is copied into the buffer but is not removed from the input queue.

  • Bit 1 = OOB: get out-of-band data instead of normal data.

Outputs

Returns the number of bytes received, or 0 if the connection has been closed, or -1 on error.

A.12.23 Socket_recvfrom()

Usage

int Socket_recvfrom(unsigned int Socket, void *Buffer, int MaxLen, unsigned int Flags, SOCKADDR *From);

Purpose

Receives a datagram and stores its source address.

Inputs

Socket, a bound socket.

Buffer, the starting address of the buffer to be filled with the incoming data.

MaxLen, the maximum number of bytes to receive.

Flags, bitmask specifying special operation for the function:

  • Bit 0 = PEEK: peek at the incoming data. The data is copied into the buffer but is not removed from the input queue.

  • Bit 1 = OOB: get out-of-band data instead of normal data.

From, an optional (may be 0) pointer to the SOCKADDR structure which is to receive the network address of the source.

Outputs

Returns -1 on error, otherwise returns the number of bytes received and fills the SOCKADDR structure pointed to by From (if From is not 0).

A.12.24 Socket_send()

Usage

int Socket_send(unsigned int Socket, void *Buffer, int Len, unsigned int Flags);

Purpose

Transmits data on a connected socket.

Inputs

Socket, a connected socket.

Buffer, the starting address of the buffer containing the data to be transmitted.

Len, the maximum number of bytes to transmit.

Flags, bitmask specifying special operation for the function:

  • Bit 0 = OOB: send out-of-band data. This is only valid for stream (TCP) sockets.

Outputs

Returns the number of bytes actually transmitted, or -1 on error.

A.12.25 Socket_sendto()

Usage

int Socket_sendto(unsigned int Socket, void *Buffer, int Len, unsigned int Flags, SOCKADDR *To);

Purpose

Sends a datagram to a specific destination address.

Inputs

Socket, a socket.

Buffer, the starting address of the buffer containing the data to be transmitted.

Len, the maximum number of bytes to transmit.

Flags, bitmask specifying special operation for the function:

  • Bit 0 = OOB: send out-of-band data. This is only valid for stream (TCP) sockets.

To, a pointer to a SOCKADDR structure which contains the network address of the destination.

Outputs

Returns the number of bytes actually transmitted, or -1 on error.

A.12.26 Socket_setsockopt()

Usage

bool Socket_setsockopt(unsigned int Socket, int Level, int OptName, char *OptVal, int OptLen);

Purpose

Sets a socket option.

Inputs

Socket, a socket.

Level, the level at which the option is defined. Supported levels are SOL_SOCKET (socket level) and IPPROTO_TCP (TCP level).

OptName, the option for which the value is to be set. See Section A.12.2.5 for a complete list of options.

OptVal, the buffer in which the value for the requested option is supplied.

OptLen, the size of the buffer pointed to by OptVal.

Outputs

Returns 1 on error, otherwise returns 0.

A.12.27 Socket_shutdown()

Usage

bool Socket_shutdown(unsigned int Socket, unsigned int Flags);

Purpose

Disables sends and/or receives on a socket.

Inputs

Socket, a socket.

Flags, a bitmask specifying what to disable:

  • Bit 0 = subsequent receives on the socket will be disallowed.

  • Bit 1 = subsequent sends on the socket will be disallowed. A FIN is sent for TCP stream sockets.

Outputs

Returns 1 on error, 0 on success.

Notes

Flags=0 has no effect. Flags=3 (both bits set) disables both sends and receives; however, the socket will not be closed and resources used by the socket will not be freed until Socket_close() is called.

A.12.28 Socket_gethostbyaddr()

Usage

HOSTENT *Socket_gethostbyaddr(unsigned int Address);

Purpose

Gets host information corresponding to an address.

Inputs

Address, the network address to retrieve information about, in network byte order.

Outputs

Returns a pointer to a static HOSTENT structure, or 0 on error. This buffer is overwritten on subsequent calls to this function.

A.12.29 Socket_gethostbyname()

Usage

HOSTENT *Socket_gethostbyname(char * Name);

Purpose

Gets host information corresponding to a hostname.

Inputs

Name, pointer to a 0-terminated string containing the name of the host.

Outputs

Returns a pointer to a static HOSTENT structure, or 0 on error. This buffer is overwritten on subsequent calls to this function.

A.12.30 Socket_gethostname()

Usage

bool Socket_gethostname(char * Name, int NameLen);

Purpose

Gets the "standard" host name for the local machine.

Inputs

Name, pointer to a buffer that will receive the name of the host.

NameLen, the length of the buffer in bytes.

Outputs

Returns 1 on error, otherwise returns 0 and fills the buffer pointed to by Name with a 0-terminated string containing the name of the local machine.

A.12.31 Socket_GetLastError()

Usage

int Socket_GetLastError(void);

Purpose

Get the error status for the last operation which failed.

Inputs

None

Outputs

Returns the error code.