Nets v3.0.0
socket.h File Reference

Network socket functions. (TCP, UDP). More...

#include "nets/defines.h"
#include <limits.h>
#include <stdlib.h>
#include <stdbool.h>

Macros

#define ANY_IP_ADDRESS_V4   "0.0.0.0"
#define ANY_IP_ADDRESS_V6   "::"
#define LOOPBACK_IP_ADDRESS_V4   "127.0.0.1"
#define LOOPBACK_IP_ADDRESS_V6   "::1"
#define LOCALHOST_HOSTNAME   "localhost"
#define ANY_IP_ADDRESS_SERVICE   "0"
#define MAX_NUMERIC_HOST_LENGTH   47
#define MAX_NUMERIC_SERVICE_LENGTH   6
#define IP_V4_SIZE   4
#define IP_V6_SIZE   16

Typedefs

typedef struct Socket_T Socket_T
typedef Socket_TSocket
typedef struct SocketAddress_T SocketAddress_T
typedef SocketAddress_TSocketAddress
typedef struct SslContext_T SslContext_T
typedef SslContext_TSslContext
typedef uint8_t SocketFamily
typedef uint8_t SocketType
typedef uint8_t SocketShutdown

Enumerations

enum  SocketFamily_T { IP_V4_SOCKET_FAMILY = 0 , IP_V6_SOCKET_FAMILY = 1 , SOCKET_FAMILY_COUNT = 2 }
 Socket internet protocol (IP) address family type. More...
enum  SocketType_T { STREAM_SOCKET_TYPE = 0 , DATAGRAM_SOCKET_TYPE = 1 , SOCKET_TYPE_COUNT = 2 }
 Socket communication protocol type. More...
enum  SocketShutdown_T { RECEIVE_ONLY_SOCKET_SHUTDOWN = 0 , SEND_ONLY_SOCKET_SHUTDOWN = 1 , RECEIVE_SEND_SOCKET_SHUTDOWN = 2 , SOCKET_SHUTDOWN_COUNT = 3 }
 Socket connection shutdown mode. More...

Functions

bool initializeNetwork ()
 Initializes network subsystems.
void terminateNetwork ()
 Terminates network subsystems.
bool isNetworkInitialized ()
 Returns true if network subsystems are initialized.
void disableSigpipe ()
 Disablea SIGPIPE signals on Linux for a current thread.
NetsResult createSocket (SocketType type, SocketFamily family, SocketAddress localAddress, bool isBlocking, bool isOnlyIPv6, SslContext sslContext, Socket *socket)
 Creates a new network socket instance.
void destroySocket (Socket socket)
 Destroys network socket instance.
SocketType getSocketType (Socket socket)
 Returns socket communication protocol type.
SocketFamily getSocketFamily (Socket socket)
 Returns socket internet protocol (IP) address family type.
bool isSocketBlocking (Socket socket)
 Returns true if socket is in blocking mode.
bool isSocketOnlyIPv6 (Socket socket)
 Returns true if IPv6 socket is not accepting IPv4 connections.
bool getSocketLocalAddress (Socket socket, SocketAddress socketAddress)
 Gets local socket IP address.
bool getSocketRemoteAddress (Socket socket, SocketAddress socketAddress)
 Gets remote socket IP address.
void * getSocketHandle (Socket socket)
 Returns socket internal handle.
SslContext getSocketSslContext (Socket socket)
 Returns socket SSL context instance.
bool isSocketNoDelay (Socket socket)
 Returns true if stream socket sends without caching.
void setSocketNoDelay (Socket socket, bool value)
 Sets socket no delay mode.
bool isSocketListening (Socket socket)
 Returns true if socket is in the listening state.
size_t getMaxSocketQueueSize ()
 Returns maximum number of queued connections.
size_t getSocketQueueSize (Socket socket)
 Returns socket pending connections queue size.
NetsResult listenSocket (Socket socket, size_t queueSize)
 Puts socket in a listening state.
NetsResult acceptSocket (Socket socket, Socket *accepted)
 Accepts a new socket connection.
NetsResult acceptSslSocket (Socket socket)
 Accepts socket SSL connection.
NetsResult connectSocket (Socket socket, SocketAddress remoteAddress)
 Connects socket to the specified remote address.
NetsResult connectSslSocket (Socket socket, const char *hostname)
 Establishes socket SSL connection.
NetsResult shutdownSocket (Socket socket, SocketShutdown shutdown)
 Shutdowns part of the full-duplex connection.
NetsResult socketReceive (Socket socket, void *receiveBuffer, size_t bufferSize, size_t *byteCount)
 Receives pending socket data.
NetsResult socketSend (Socket socket, const void *data, size_t byteCount)
 Sends specified data to the remote socket.
NetsResult socketReceiveFrom (Socket socket, SocketAddress remoteAddress, void *receiveBuffer, size_t bufferSize, size_t *byteCount)
 Receives pending data from the remote socket.
NetsResult socketSendTo (Socket socket, const void *data, size_t byteCount, SocketAddress remoteAddress)
 Sends specified data to the remote socket.
NetsResult createSocketAddress (const char *host, const char *service, SocketAddress *socketAddress)
 Creates a new socket IP address instance.
NetsResult createAnySocketAddress (SocketFamily family, SocketAddress *socketAddress)
 Creates a new any socket IP address instance.
SocketAddress createSocketAddressCopy (SocketAddress socketAddress)
 Creates a new socket IP address copy instance.
void destroySocketAddress (SocketAddress socketAddress)
 Destroys socket IP address instance.
NetsResult resolveSocketAddresses (const char *host, const char *service, SocketFamily family, SocketType type, SocketAddress **socketAddresses, size_t *addressCount)
 Resolves a new socket IP address array. (Blocking call).
void destroySocketAddresses (SocketAddress *socketAddresses, size_t addressCount)
 Destroys resolved socket IP address array.
void getUrlParts (const char *url, size_t urlLength, size_t *hostOffset, size_t *hostLength, size_t *serviceOffset, size_t *serviceLength, size_t *pathOffset)
 Returns URL link parts location.
void copySocketAddress (SocketAddress sourceAddress, SocketAddress destinationAddress)
 Copies source socket IP address to the destination.
int compareSocketAddress (SocketAddress a, SocketAddress b)
 Compares two socket IP addresses.
SocketFamily getSocketAddressFamily (SocketAddress socketAddress)
 Returns socket IP address family type.
size_t getSocketFamilyIpSize (SocketFamily family)
 Returns socket IP address family byte array size.
size_t getSocketAddressIpSize (SocketAddress socketAddress)
 Returns socket IP address byte array size.
bool isSocketAddressAny (SocketAddress socketAddress)
 Returns true if socket address is any address.
bool isSocketAddressLoopback (SocketAddress socketAddress)
 Returns true if socket address is loopback address.
bool isSocketAddressMappedV4 (SocketAddress socketAddress)
 Returns true if socket address is IPv4 mapped IPv6.
const uint8_t * getSocketAddressIP (SocketAddress socketAddress)
 Returns socket IP address byte array.
void setSocketAddressIP (SocketAddress socketAddress, const uint8_t *ip)
 Sets socket IP address byte array.
uint16_t getSocketAddressPort (SocketAddress socketAddress)
 Returns socket IP address port number.
void setSocketAddressPort (SocketAddress socketAddress, uint16_t port)
 Sets socket IP address port number.
void getSocketAddressHost (SocketAddress socketAddress, char *host, size_t length)
 Returns socket IP address numeric host name.
void getSocketAddressService (SocketAddress socketAddress, char *service, size_t length)
 Returns socket IP address numeric service name.
void getSocketAddressHostService (SocketAddress socketAddress, char *host, size_t hostLength, char *service, size_t serviceLength)
 Returns socket IP address numeric host and service name.
NetsResult createPublicSslContext (const char *certificateFilePath, const char *certificatesDirectory, SslContext *sslContext)
 Creates a new public socket SSL context.
NetsResult createPrivateSslContext (const char *certificateFilePath, const char *privateKeyFilePath, bool certificateChain, SslContext *sslContext)
 Creates a new private socket SSL context.
void destroySslContext (SslContext sslContext)
 Destroys socket SSL context instance.

Detailed Description

Network socket functions. (TCP, UDP).

A network socket is a a software abstraction that represents one endpoint of a two-way communication link between programs over a network. Typically identified by an IP address, a transport protocol (TCP, UDP), and a port number.

Macro Definition Documentation

◆ ANY_IP_ADDRESS_V4

#define ANY_IP_ADDRESS_V4   "0.0.0.0"

Internet Protocol v4 any address.

◆ ANY_IP_ADDRESS_V6

#define ANY_IP_ADDRESS_V6   "::"

Internet Protocol v6 any address.

◆ LOOPBACK_IP_ADDRESS_V4

#define LOOPBACK_IP_ADDRESS_V4   "127.0.0.1"

Internet protocol v4 loopback address.

◆ LOOPBACK_IP_ADDRESS_V6

#define LOOPBACK_IP_ADDRESS_V6   "::1"

Internet protocol v6 loopback address.

◆ LOCALHOST_HOSTNAME

#define LOCALHOST_HOSTNAME   "localhost"

Current computer IP address.

◆ ANY_IP_ADDRESS_SERVICE

#define ANY_IP_ADDRESS_SERVICE   "0"

System-allocated, dynamic port.

◆ MAX_NUMERIC_HOST_LENGTH

#define MAX_NUMERIC_HOST_LENGTH   47

Maximum numeric host string length. (Including null terminator)

◆ MAX_NUMERIC_SERVICE_LENGTH

#define MAX_NUMERIC_SERVICE_LENGTH   6

Maximum numeric service string length. (Including null terminator)

◆ IP_V4_SIZE

#define IP_V4_SIZE   4

Internet Protocol v4 address size in bytes.

◆ IP_V6_SIZE

#define IP_V6_SIZE   16

Internet Protocol v6 address size in bytes.

Typedef Documentation

◆ Socket_T

typedef struct Socket_T Socket_T

Socket structure.

◆ Socket

typedef Socket_T* Socket

Socket instance.

◆ SocketAddress_T

Socket IP address structure.

◆ SocketAddress

Socket IP address instance.

◆ SslContext_T

typedef struct SslContext_T SslContext_T

Secure socket layer (SSL) context structure.

◆ SslContext

Secure socket layer (SSL) context instance.

◆ SocketFamily

typedef uint8_t SocketFamily

Socket internet protocol (IP) address family type.

◆ SocketType

typedef uint8_t SocketType

Socket communication protocol type.

◆ SocketShutdown

typedef uint8_t SocketShutdown

Socket connection shutdown mode.

Enumeration Type Documentation

◆ SocketFamily_T

Socket internet protocol (IP) address family type.

Enumerator
IP_V4_SOCKET_FAMILY 

IPv4 socket IP address family. (32-bit addresses)

IP_V6_SOCKET_FAMILY 

IPv6 socket IP address family. (128-bit addresses)

SOCKET_FAMILY_COUNT 

Socket internet protocol (IP) address family count.

◆ SocketType_T

Socket communication protocol type.

Enumerator
STREAM_SOCKET_TYPE 

Connection oriented, reliable, ordered byte stream. (TCP)

DATAGRAM_SOCKET_TYPE 

Connectionless, unreliable, message oriented packets. (UDP)

SOCKET_TYPE_COUNT 

Socket communication protocol type count.

◆ SocketShutdown_T

Socket connection shutdown mode.

Enumerator
RECEIVE_ONLY_SOCKET_SHUTDOWN 

Shutdowns only receive side of the socket.

SEND_ONLY_SOCKET_SHUTDOWN 

Shutdowns only send side of the socket.

RECEIVE_SEND_SOCKET_SHUTDOWN 

Shutdowns both receive and send sides of the socket.

SOCKET_SHUTDOWN_COUNT 

Socket connection shutdown mode count.

Function Documentation

◆ initializeNetwork()

bool initializeNetwork ( )

Initializes network subsystems.

Warning
You should call this function before using sockets!
Returns
True on success, otherwise false.

◆ isNetworkInitialized()

bool isNetworkInitialized ( )

Returns true if network subsystems are initialized.

Note
You should initialize network before using sockets!

◆ createSocket()

NetsResult createSocket ( SocketType type,
SocketFamily family,
SocketAddress localAddress,
bool isBlocking,
bool isOnlyIPv6,
SslContext sslContext,
Socket * socket )

Creates a new network socket instance.

Creates, initializes, and binds a new endpoint for network communication.

Note
You should destroy created socket instance manually.
Parameters
typesocket communication protocol type
familyinternet protocol address family
localAddresssocket local bind address instance
isBlockingcreate socket in blocking mode
isOnlyIPv6create socket in IPv6 only mode
sslContextsocket SSL context instance or NULL
[out]socketpointer to the socket instance
Returns
The NetsResult code and writes socket instance on success.

◆ destroySocket()

void destroySocket ( Socket socket)

Destroys network socket instance.

Parameters
sockettarget socket instance or NULL

◆ getSocketType()

SocketType getSocketType ( Socket socket)

Returns socket communication protocol type.

Parameters
sockettarget socket instance

◆ getSocketFamily()

SocketFamily getSocketFamily ( Socket socket)

Returns socket internet protocol (IP) address family type.

Parameters
sockettarget socket instance

◆ isSocketBlocking()

bool isSocketBlocking ( Socket socket)

Returns true if socket is in blocking mode.

Parameters
sockettarget socket instance

◆ isSocketOnlyIPv6()

bool isSocketOnlyIPv6 ( Socket socket)

Returns true if IPv6 socket is not accepting IPv4 connections.

Parameters
sockettarget socket instance

◆ getSocketLocalAddress()

bool getSocketLocalAddress ( Socket socket,
SocketAddress socketAddress )

Gets local socket IP address.

Returns
True on success, otherwise false.
Parameters
sockettarget socket instance
[out]socketAddresssocket IP address instance

◆ getSocketRemoteAddress()

bool getSocketRemoteAddress ( Socket socket,
SocketAddress socketAddress )

Gets remote socket IP address.

Returns
True on success, otherwise false.
Parameters
sockettarget socket instance
[out]socketAddresssocket IP address instance

◆ getSocketHandle()

void * getSocketHandle ( Socket socket)

Returns socket internal handle.

Parameters
sockettarget socket instance

◆ getSocketSslContext()

SslContext getSocketSslContext ( Socket socket)

Returns socket SSL context instance.

Parameters
sockettarget socket instance

◆ isSocketNoDelay()

bool isSocketNoDelay ( Socket socket)

Returns true if stream socket sends without caching.

Parameters
sockettarget socket instance

◆ setSocketNoDelay()

void setSocketNoDelay ( Socket socket,
bool value )

Sets socket no delay mode.

Does stream socket sends without caching.

Parameters
sockettarget socket instance
valueno delay mode value

◆ isSocketListening()

bool isSocketListening ( Socket socket)

Returns true if socket is in the listening state.

Parameters
sockettarget socket instance

◆ getSocketQueueSize()

size_t getSocketQueueSize ( Socket socket)

Returns socket pending connections queue size.

Parameters
sockettarget socket instance

◆ listenSocket()

NetsResult listenSocket ( Socket socket,
size_t queueSize )

Puts socket in a listening state.

Returns
The operation NetsResult code.
Parameters
sockettarget socket instance
queueSizepending connections queue size

◆ acceptSocket()

NetsResult acceptSocket ( Socket socket,
Socket * accepted )

Accepts a new socket connection.

Note
You should destroy accepted socket instance manually!
Returns
The operation NetsResult code.
Parameters
sockettarget socket instance
[out]acceptedpointer to the accepted socket

◆ acceptSslSocket()

NetsResult acceptSslSocket ( Socket socket)

Accepts socket SSL connection.

Returns
The operation NetsResult code.
Parameters
sockettarget socket instance

◆ connectSocket()

NetsResult connectSocket ( Socket socket,
SocketAddress remoteAddress )

Connects socket to the specified remote address.

Returns
The operation NetsResult code.
Parameters
sockettarget socket instance
remoteAddressremote socket IP address instance

◆ connectSslSocket()

NetsResult connectSslSocket ( Socket socket,
const char * hostname )

Establishes socket SSL connection.

Returns
The operation NetsResult code.
Parameters
sockettarget socket instance
[in]hostnameremote socket SNI hostname or NULL

◆ shutdownSocket()

NetsResult shutdownSocket ( Socket socket,
SocketShutdown shutdown )

Shutdowns part of the full-duplex connection.

Returns
The operation NetsResult code.
Parameters
sockettarget socket instance
shutdownsocket connection shutdown mode

◆ socketReceive()

NetsResult socketReceive ( Socket socket,
void * receiveBuffer,
size_t bufferSize,
size_t * byteCount )

Receives pending socket data.

Returns
The operation NetsResult code.
Parameters
sockettarget socket instance
[out]receiveBufferdata receive buffer
bufferSizedata receive buffer size in bytes
[out]byteCountpointer to the received byte count

◆ socketSend()

NetsResult socketSend ( Socket socket,
const void * data,
size_t byteCount )

Sends specified data to the remote socket.

Returns
The operation NetsResult code.
Parameters
sockettarget socket instance
[in]datasend data buffer
byteCountdata byte count to send

◆ socketReceiveFrom()

NetsResult socketReceiveFrom ( Socket socket,
SocketAddress remoteAddress,
void * receiveBuffer,
size_t bufferSize,
size_t * byteCount )

Receives pending data from the remote socket.

Returns
The operation NetsResult code.
Parameters
sockettarget socket instance
[out]remoteAddressremote socket IP address
[out]receiveBufferdata receive buffer
bufferSizedata receive buffer size in bytes
[out]byteCountpointer to the received byte count

◆ socketSendTo()

NetsResult socketSendTo ( Socket socket,
const void * data,
size_t byteCount,
SocketAddress remoteAddress )

Sends specified data to the remote socket.

Returns
The operation NetsResult code.
Parameters
sockettarget socket instance
[in]datasend data buffer
byteCountdata byte count to send
remoteAddressdestination remote socket IP address

◆ createSocketAddress()

NetsResult createSocketAddress ( const char * host,
const char * service,
SocketAddress * socketAddress )

Creates a new socket IP address instance.

Returns
The operation NetsResult code.
Parameters
[in]hostsocket IP address host name string
[in]servicesocket IP address service name string (port)
[out]socketAddresspointer to the socket address instance

◆ createAnySocketAddress()

NetsResult createAnySocketAddress ( SocketFamily family,
SocketAddress * socketAddress )

Creates a new any socket IP address instance.

Returns
The operation NetsResult code.
Parameters
familysocket IP address family type
[out]socketAddresspointer to the socket address instance

◆ createSocketAddressCopy()

SocketAddress createSocketAddressCopy ( SocketAddress socketAddress)

Creates a new socket IP address copy instance.

Returns
A new socket address instance on success, otherwise NULL.
Parameters
socketAddresstarget socket address instance to copy

◆ destroySocketAddress()

void destroySocketAddress ( SocketAddress socketAddress)

Destroys socket IP address instance.

Parameters
socketAddresstarget socket address instance or NULL

◆ resolveSocketAddresses()

NetsResult resolveSocketAddresses ( const char * host,
const char * service,
SocketFamily family,
SocketType type,
SocketAddress ** socketAddresses,
size_t * addressCount )

Resolves a new socket IP address array. (Blocking call).

Returns
The operation NetsResult code.
Parameters
[in]hostsocket IP address host name string
[in]servicesocket IP address service name string (port)
familysocket IP address family type
typesocket communication protocol type
[out]socketAddressespointer to the socket address array
[out]addressCountpointer to the socket address count

◆ destroySocketAddresses()

void destroySocketAddresses ( SocketAddress * socketAddresses,
size_t addressCount )

Destroys resolved socket IP address array.

Parameters
[in]socketAddressessocket IP address array
addressCountsocket address count

◆ getUrlParts()

void getUrlParts ( const char * url,
size_t urlLength,
size_t * hostOffset,
size_t * hostLength,
size_t * serviceOffset,
size_t * serviceLength,
size_t * pathOffset )

Returns URL link parts location.

Parameters
[in]urluniform resource locator string
urlLengthURL string length
[out]hostOffsetpointer to the host part offset or NULL
[out]hostLengthpointer to the host part length or NULL
[out]serviceOffsetpointer to the service part offset or NULL
[out]serviceLengthpointer to the service part length or NULL
[out]pathOffsetpointer to the path part offset or NULL

◆ copySocketAddress()

void copySocketAddress ( SocketAddress sourceAddress,
SocketAddress destinationAddress )

Copies source socket IP address to the destination.

Parameters
sourceAddresssource socket address instance
destinationAddressdestination socket address instance

◆ compareSocketAddress()

int compareSocketAddress ( SocketAddress a,
SocketAddress b )

Compares two socket IP addresses.

Parameters
afirst socket address instance
bsecond socket address instance

◆ getSocketAddressFamily()

SocketFamily getSocketAddressFamily ( SocketAddress socketAddress)

Returns socket IP address family type.

Parameters
socketAddresstarget socket address instance

◆ getSocketFamilyIpSize()

size_t getSocketFamilyIpSize ( SocketFamily family)

Returns socket IP address family byte array size.

Parameters
familysocket IP address family type

◆ getSocketAddressIpSize()

size_t getSocketAddressIpSize ( SocketAddress socketAddress)

Returns socket IP address byte array size.

Parameters
socketAddresstarget socket address instance

◆ isSocketAddressAny()

bool isSocketAddressAny ( SocketAddress socketAddress)

Returns true if socket address is any address.

Parameters
socketAddresstarget socket address instance

◆ isSocketAddressLoopback()

bool isSocketAddressLoopback ( SocketAddress socketAddress)

Returns true if socket address is loopback address.

Parameters
socketAddresstarget socket address instance

◆ isSocketAddressMappedV4()

bool isSocketAddressMappedV4 ( SocketAddress socketAddress)

Returns true if socket address is IPv4 mapped IPv6.

Parameters
socketAddresstarget socket address instance

◆ getSocketAddressIP()

const uint8_t * getSocketAddressIP ( SocketAddress socketAddress)

Returns socket IP address byte array.

Parameters
socketAddresstarget socket address instance

◆ setSocketAddressIP()

void setSocketAddressIP ( SocketAddress socketAddress,
const uint8_t * ip )

Sets socket IP address byte array.

Parameters
socketAddresstarget socket address instance
[in]ipsocket IP address byte array

◆ getSocketAddressPort()

uint16_t getSocketAddressPort ( SocketAddress socketAddress)

Returns socket IP address port number.

Parameters
socketAddresstarget socket address instance

◆ setSocketAddressPort()

void setSocketAddressPort ( SocketAddress socketAddress,
uint16_t port )

Sets socket IP address port number.

Parameters
socketAddresstarget socket address instance
portsocket IP address port number

◆ getSocketAddressHost()

void getSocketAddressHost ( SocketAddress socketAddress,
char * host,
size_t length )

Returns socket IP address numeric host name.

Returns numeric host name on success, otherwise empty string.

Parameters
socketAddresstarget socket address instance
[out]hostpointer to the host name string
lengthhost name string length (including null terminator)

◆ getSocketAddressService()

void getSocketAddressService ( SocketAddress socketAddress,
char * service,
size_t length )

Returns socket IP address numeric service name.

Returns numeric service name on success, otherwise empty string.

Parameters
socketAddresstarget socket address instance
[out]servicepointer to the service name string
lengthservice name string length (including null terminator)

◆ getSocketAddressHostService()

void getSocketAddressHostService ( SocketAddress socketAddress,
char * host,
size_t hostLength,
char * service,
size_t serviceLength )

Returns socket IP address numeric host and service name.

Returns numeric host and service names on success, otherwise empty strings.

Parameters
socketAddresstarget socket address instance
[out]hostpointer to the host name string
hostLengthhost name string length (including null terminator)
[out]servicepointer to the service name string
serviceLengthservice name string length (including null terminator)

◆ createPublicSslContext()

NetsResult createPublicSslContext ( const char * certificateFilePath,
const char * certificatesDirectory,
SslContext * sslContext )

Creates a new public socket SSL context.

Returns
The operation NetsResult code.
Parameters
[in]certificateFilePathcertificate file path string or NULL
[in]certificatesDirectorycertificates directory path string or NULL
[out]sslContextpointer to the SSL context instance

◆ createPrivateSslContext()

NetsResult createPrivateSslContext ( const char * certificateFilePath,
const char * privateKeyFilePath,
bool certificateChain,
SslContext * sslContext )

Creates a new private socket SSL context.

Returns
The operation NetsResult code.
Parameters
[in]certificateFilePathcertificates file path string
[in]privateKeyFilePathprivate key file path string
certificateChainfile path is certificate chain
[out]sslContextpointer to the SSL context instance

◆ destroySslContext()

void destroySslContext ( SslContext sslContext)

Destroys socket SSL context instance.

Parameters
sslContexttarget SSL context instance or NULL