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_T * | Socket |
| typedef struct SocketAddress_T | SocketAddress_T |
| typedef SocketAddress_T * | SocketAddress |
| typedef struct SslContext_T | SslContext_T |
| typedef SslContext_T * | SslContext |
| 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. | |
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.
| #define ANY_IP_ADDRESS_V4 "0.0.0.0" |
Internet Protocol v4 any address.
| #define ANY_IP_ADDRESS_V6 "::" |
Internet Protocol v6 any address.
| #define LOOPBACK_IP_ADDRESS_V4 "127.0.0.1" |
Internet protocol v4 loopback address.
| #define LOOPBACK_IP_ADDRESS_V6 "::1" |
Internet protocol v6 loopback address.
| #define LOCALHOST_HOSTNAME "localhost" |
Current computer IP address.
| #define ANY_IP_ADDRESS_SERVICE "0" |
System-allocated, dynamic port.
| #define MAX_NUMERIC_HOST_LENGTH 47 |
Maximum numeric host string length. (Including null terminator)
| #define MAX_NUMERIC_SERVICE_LENGTH 6 |
Maximum numeric service string length. (Including null terminator)
| #define IP_V4_SIZE 4 |
Internet Protocol v4 address size in bytes.
| #define IP_V6_SIZE 16 |
Internet Protocol v6 address size in bytes.
| typedef struct SocketAddress_T SocketAddress_T |
Socket IP address structure.
| typedef SocketAddress_T* SocketAddress |
Socket IP address instance.
| typedef struct SslContext_T SslContext_T |
Secure socket layer (SSL) context structure.
| typedef SslContext_T* SslContext |
Secure socket layer (SSL) context instance.
| typedef uint8_t SocketFamily |
Socket internet protocol (IP) address family type.
| typedef uint8_t SocketType |
Socket communication protocol type.
| typedef uint8_t SocketShutdown |
Socket connection shutdown mode.
| enum SocketFamily_T |
| enum SocketType_T |
| enum 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. |
| bool initializeNetwork | ( | ) |
Initializes network subsystems.
| bool isNetworkInitialized | ( | ) |
Returns true if network subsystems are initialized.
| 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.
| type | socket communication protocol type | |
| family | internet protocol address family | |
| localAddress | socket local bind address instance | |
| isBlocking | create socket in blocking mode | |
| isOnlyIPv6 | create socket in IPv6 only mode | |
| sslContext | socket SSL context instance or NULL | |
| [out] | socket | pointer to the socket instance |
| void destroySocket | ( | Socket | socket | ) |
Destroys network socket instance.
| socket | target socket instance or NULL |
| SocketType getSocketType | ( | Socket | socket | ) |
Returns socket communication protocol type.
| socket | target socket instance |
| SocketFamily getSocketFamily | ( | Socket | socket | ) |
Returns socket internet protocol (IP) address family type.
| socket | target socket instance |
| bool isSocketBlocking | ( | Socket | socket | ) |
Returns true if socket is in blocking mode.
| socket | target socket instance |
| bool isSocketOnlyIPv6 | ( | Socket | socket | ) |
Returns true if IPv6 socket is not accepting IPv4 connections.
| socket | target socket instance |
| bool getSocketLocalAddress | ( | Socket | socket, |
| SocketAddress | socketAddress ) |
Gets local socket IP address.
| socket | target socket instance | |
| [out] | socketAddress | socket IP address instance |
| bool getSocketRemoteAddress | ( | Socket | socket, |
| SocketAddress | socketAddress ) |
Gets remote socket IP address.
| socket | target socket instance | |
| [out] | socketAddress | socket IP address instance |
| void * getSocketHandle | ( | Socket | socket | ) |
Returns socket internal handle.
| socket | target socket instance |
| SslContext getSocketSslContext | ( | Socket | socket | ) |
Returns socket SSL context instance.
| socket | target socket instance |
| bool isSocketNoDelay | ( | Socket | socket | ) |
Returns true if stream socket sends without caching.
| socket | target socket instance |
| void setSocketNoDelay | ( | Socket | socket, |
| bool | value ) |
Sets socket no delay mode.
Does stream socket sends without caching.
| socket | target socket instance |
| value | no delay mode value |
| bool isSocketListening | ( | Socket | socket | ) |
Returns true if socket is in the listening state.
| socket | target socket instance |
| size_t getSocketQueueSize | ( | Socket | socket | ) |
Returns socket pending connections queue size.
| socket | target socket instance |
| NetsResult listenSocket | ( | Socket | socket, |
| size_t | queueSize ) |
Puts socket in a listening state.
| socket | target socket instance |
| queueSize | pending connections queue size |
Accepts a new socket connection.
| socket | target socket instance | |
| [out] | accepted | pointer to the accepted socket |
| NetsResult acceptSslSocket | ( | Socket | socket | ) |
Accepts socket SSL connection.
| socket | target socket instance |
| NetsResult connectSocket | ( | Socket | socket, |
| SocketAddress | remoteAddress ) |
Connects socket to the specified remote address.
| socket | target socket instance |
| remoteAddress | remote socket IP address instance |
| NetsResult connectSslSocket | ( | Socket | socket, |
| const char * | hostname ) |
Establishes socket SSL connection.
| socket | target socket instance | |
| [in] | hostname | remote socket SNI hostname or NULL |
| NetsResult shutdownSocket | ( | Socket | socket, |
| SocketShutdown | shutdown ) |
Shutdowns part of the full-duplex connection.
| socket | target socket instance |
| shutdown | socket connection shutdown mode |
| NetsResult socketReceive | ( | Socket | socket, |
| void * | receiveBuffer, | ||
| size_t | bufferSize, | ||
| size_t * | byteCount ) |
Receives pending socket data.
| socket | target socket instance | |
| [out] | receiveBuffer | data receive buffer |
| bufferSize | data receive buffer size in bytes | |
| [out] | byteCount | pointer to the received byte count |
| NetsResult socketSend | ( | Socket | socket, |
| const void * | data, | ||
| size_t | byteCount ) |
Sends specified data to the remote socket.
| socket | target socket instance | |
| [in] | data | send data buffer |
| byteCount | data byte count to send |
| NetsResult socketReceiveFrom | ( | Socket | socket, |
| SocketAddress | remoteAddress, | ||
| void * | receiveBuffer, | ||
| size_t | bufferSize, | ||
| size_t * | byteCount ) |
Receives pending data from the remote socket.
| socket | target socket instance | |
| [out] | remoteAddress | remote socket IP address |
| [out] | receiveBuffer | data receive buffer |
| bufferSize | data receive buffer size in bytes | |
| [out] | byteCount | pointer to the received byte count |
| NetsResult socketSendTo | ( | Socket | socket, |
| const void * | data, | ||
| size_t | byteCount, | ||
| SocketAddress | remoteAddress ) |
Sends specified data to the remote socket.
| socket | target socket instance | |
| [in] | data | send data buffer |
| byteCount | data byte count to send | |
| remoteAddress | destination remote socket IP address |
| NetsResult createSocketAddress | ( | const char * | host, |
| const char * | service, | ||
| SocketAddress * | socketAddress ) |
Creates a new socket IP address instance.
| [in] | host | socket IP address host name string |
| [in] | service | socket IP address service name string (port) |
| [out] | socketAddress | pointer to the socket address instance |
| NetsResult createAnySocketAddress | ( | SocketFamily | family, |
| SocketAddress * | socketAddress ) |
Creates a new any socket IP address instance.
| family | socket IP address family type | |
| [out] | socketAddress | pointer to the socket address instance |
| SocketAddress createSocketAddressCopy | ( | SocketAddress | socketAddress | ) |
Creates a new socket IP address copy instance.
| socketAddress | target socket address instance to copy |
| void destroySocketAddress | ( | SocketAddress | socketAddress | ) |
Destroys socket IP address instance.
| socketAddress | target socket address instance or NULL |
| 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).
| [in] | host | socket IP address host name string |
| [in] | service | socket IP address service name string (port) |
| family | socket IP address family type | |
| type | socket communication protocol type | |
| [out] | socketAddresses | pointer to the socket address array |
| [out] | addressCount | pointer to the socket address count |
| void destroySocketAddresses | ( | SocketAddress * | socketAddresses, |
| size_t | addressCount ) |
Destroys resolved socket IP address array.
| [in] | socketAddresses | socket IP address array |
| addressCount | socket address count |
| 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.
| [in] | url | uniform resource locator string |
| urlLength | URL string length | |
| [out] | hostOffset | pointer to the host part offset or NULL |
| [out] | hostLength | pointer to the host part length or NULL |
| [out] | serviceOffset | pointer to the service part offset or NULL |
| [out] | serviceLength | pointer to the service part length or NULL |
| [out] | pathOffset | pointer to the path part offset or NULL |
| void copySocketAddress | ( | SocketAddress | sourceAddress, |
| SocketAddress | destinationAddress ) |
Copies source socket IP address to the destination.
| sourceAddress | source socket address instance |
| destinationAddress | destination socket address instance |
| int compareSocketAddress | ( | SocketAddress | a, |
| SocketAddress | b ) |
Compares two socket IP addresses.
| a | first socket address instance |
| b | second socket address instance |
| SocketFamily getSocketAddressFamily | ( | SocketAddress | socketAddress | ) |
Returns socket IP address family type.
| socketAddress | target socket address instance |
| size_t getSocketFamilyIpSize | ( | SocketFamily | family | ) |
Returns socket IP address family byte array size.
| family | socket IP address family type |
| size_t getSocketAddressIpSize | ( | SocketAddress | socketAddress | ) |
Returns socket IP address byte array size.
| socketAddress | target socket address instance |
| bool isSocketAddressAny | ( | SocketAddress | socketAddress | ) |
Returns true if socket address is any address.
| socketAddress | target socket address instance |
| bool isSocketAddressLoopback | ( | SocketAddress | socketAddress | ) |
Returns true if socket address is loopback address.
| socketAddress | target socket address instance |
| bool isSocketAddressMappedV4 | ( | SocketAddress | socketAddress | ) |
Returns true if socket address is IPv4 mapped IPv6.
| socketAddress | target socket address instance |
| const uint8_t * getSocketAddressIP | ( | SocketAddress | socketAddress | ) |
Returns socket IP address byte array.
| socketAddress | target socket address instance |
| void setSocketAddressIP | ( | SocketAddress | socketAddress, |
| const uint8_t * | ip ) |
Sets socket IP address byte array.
| socketAddress | target socket address instance | |
| [in] | ip | socket IP address byte array |
| uint16_t getSocketAddressPort | ( | SocketAddress | socketAddress | ) |
Returns socket IP address port number.
| socketAddress | target socket address instance |
| void setSocketAddressPort | ( | SocketAddress | socketAddress, |
| uint16_t | port ) |
Sets socket IP address port number.
| socketAddress | target socket address instance |
| port | socket IP address port number |
| 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.
| socketAddress | target socket address instance | |
| [out] | host | pointer to the host name string |
| length | host name string length (including null terminator) |
| 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.
| socketAddress | target socket address instance | |
| [out] | service | pointer to the service name string |
| length | service name string length (including null terminator) |
| 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.
| socketAddress | target socket address instance | |
| [out] | host | pointer to the host name string |
| hostLength | host name string length (including null terminator) | |
| [out] | service | pointer to the service name string |
| serviceLength | service name string length (including null terminator) |
| NetsResult createPublicSslContext | ( | const char * | certificateFilePath, |
| const char * | certificatesDirectory, | ||
| SslContext * | sslContext ) |
Creates a new public socket SSL context.
| [in] | certificateFilePath | certificate file path string or NULL |
| [in] | certificatesDirectory | certificates directory path string or NULL |
| [out] | sslContext | pointer to the SSL context instance |
| NetsResult createPrivateSslContext | ( | const char * | certificateFilePath, |
| const char * | privateKeyFilePath, | ||
| bool | certificateChain, | ||
| SslContext * | sslContext ) |
Creates a new private socket SSL context.
| [in] | certificateFilePath | certificates file path string |
| [in] | privateKeyFilePath | private key file path string |
| certificateChain | file path is certificate chain | |
| [out] | sslContext | pointer to the SSL context instance |
| void destroySslContext | ( | SslContext | sslContext | ) |
Destroys socket SSL context instance.
| sslContext | target SSL context instance or NULL |