Network Sockets
Platform-independent implementation for TCP/UDP network sockets.
Used by agent code and be useful for network clients loader agents.
Constants
Socket types
VALUES
-
NSK_STREAM - TCP socket
-
NSK_DRAM - UDP socket
#define NSK_STREAM #define NSK_DRAM
Return values
Each constant represents failed operation
#define NSK_OK 0 #define NSK_ERR_RESOLVE -1 #define NSK_ERR_SOCKET -2 #define NSK_ERR_CONNECT -3 #define NSK_ERR_BIND -4 #define NSK_ERR_LISTEN -5 #define NSK_ERR_ACCEPT -6 #define NSK_ERR_SETOPT -7 #define NSK_ERR_SETNB -8
nsk_poll() return values
VALUES
-
NSK_POLL_OK - poll successful, no incoming data
-
NSK_POLL_NEW_DATA - data was received
-
NSK_POLL_DISCONNECTED - local or remote node issued disconnect
-
NSK_POLL_FAILURE - poll call was failed
#define NSK_POLL_OK 0 #define NSK_POLL_NEW_DATA 1 #define NSK_POLL_DISCONNECT 2 #define NSK_POLL_FAILURE 3
nsk_recv() return values
VALUES
-
NSK_RECV_DISCONNECT - poll successful, no incoming data
-
NSK_RECV_NO_DATA - no data was received (only for non-blocking sockets)
-
NSK_RECV_ERROR - recv call was failed
#define NSK_RECV_DISCONNECT -1 #define NSK_RECV_NO_DATA -2 #define NSK_RECV_ERROR -3
nsk_addr_to_string() flags
VALUES
-
NSK_ADDR_FLAG_DEFAULT - no flags
-
NSK_ADDR_FLAG_SERVICES - resolve flags using /etc/services (not implemented)
#define NSK_ADDR_FLAG_DEFAULT 0x00 #define NSK_ADDR_FLAG_SERVICES 0x01
Functions
nsk_resolve
publicplat
Resolve hostname and write result into he
ARGUMENTS
-
host - hostname or string representation of IP
-
he - destination buffer
NOTES
On some platforms may lock mutexes, because implementation is not re-enterable
LIBEXPORT PLATAPI int nsk_resolve(const char* host, nsk_host_entry* he)
nsk_setaddr
publicplat
Set up socket address object
ARGUMENTS
-
sa - socket address object
-
he - host entry resolved in nsk_resolve()
-
port - port number
LIBEXPORT PLATAPI int nsk_setaddr(nsk_addr* sa, nsk_host_entry* he, unsigned short port)
nsk_addr_to_string
publicplat
Converts socket address into string <IP>:<port>
ARGUMENTS
-
sa - socket address object
-
buf - destination buffer
-
buflen - length of destination buffer
-
default_port - if port inside sa equals this value, port is not printed to buffer
-
flags - see below
LIBEXPORT PLATAPI int nsk_addr_to_string(nsk_addr* sa, char* buf, size_t buflen,int default_port, int flags)
nsk_connect
publicplat
Set up socket and connect to remote node
ARGUMENTS
-
clnt_socket - socket object
-
sa - socket address of remote server
-
type - socket type
LIBEXPORT PLATAPI int nsk_connect(nsk_socket* clnt_socket, nsk_addr* sa, int type)
nsk_listen
publicplat
Listen on server socket
ARGUMENTS
-
srv_socket - socket object
-
sa - socket address of local server
-
type - socket type
LIBEXPORT PLATAPI int nsk_listen(nsk_socket* srv_socket, nsk_addr* sa, int type)
nsk_accept
publicplat
Accept clients connection
ARGUMENTS
-
srv_socket - server socket (earlier initialized with nsk_listen())
-
clnt_socket - socket object (new object)
-
sa - socket address of client
LIBEXPORT PLATAPI int nsk_accept(nsk_socket* srv_socket, nsk_socket* clnt_socket, nsk_addr* clnt_sa)
nsk_disconnect
publicplat
Shutdown and close connection - both for server and client sockets
ARGUMENTS
-
socket - socket object
LIBEXPORT PLATAPI int nsk_disconnect(nsk_socket* socket)
nsk_setopt
publicplat
Set socket option. Interface for platform setsockopt() or equal function.
LIBEXPORT PLATAPI int nsk_setopt(nsk_socket* socket, int level, int optname,const void* optval, size_t optlen)
nsk_setnb
publicplat
Enable or disable nonblocking operations on socket
ARGUMENTS
-
socket - socket object
-
nonblocking - if set to B_TRUE, enables non-blocking operations, if set to B_FALSE - disables them
LIBEXPORT PLATAPI int nsk_setnb(nsk_socket* socket, boolean_t nonblocking)
nsk_poll
publicplat
Wait on incoming data or disconnect of socket.
Polling of multiple sockets is not supported
RETURN VALUES
One of NSK_POLL_ results
LIBEXPORT PLATAPI int nsk_poll(nsk_socket* socket, ts_time_t timeout)
nsk_send
publicplat
Send data over socket
RETURN VALUES
0 in case of disconnect, -1 on error, or number of bytes sent
LIBEXPORT PLATAPI int nsk_send(nsk_socket* socket, void* data, size_t len)
nsk_recv
publicplat
Receive data from socket
RETURN VALUES
one of NSK_RECV_ value
LIBEXPORT PLATAPI int nsk_recv(nsk_socket* socket, void* data, size_t len)
nsk_init, nsk_fini
publicplat
LIBEXPORT PLATAPI int nsk_init(void) LIBEXPORT PLATAPI void nsk_fini(void)