Embed: (wiki syntax)

« Back to documentation index

Fnet_socket

Data Structures

union  fnet_ip6_addr_t
 128-bit IPv6 address type. More...
struct  in_addr
 IPv4 address structure. More...
struct  sockaddr_in
 IPv4 Socket address structure. More...
struct  in6_addr
 IPv6 address structure. More...
struct  sockaddr_in6
 IPv6 Socket address structure. More...
struct  sockaddr
 Socket address structure. More...
struct  ip_mreq
 IPv4 multicast group information. More...
struct  ipv6_mreq
 IPv6 multicast group information. More...
struct  linger
 This structure is used for the SO_LINGER option. More...

Typedefs

typedef fnet_uint32_t fnet_ip4_addr_t
 32-bit IPv4 address type.
typedef fnet_uint16_t fnet_address_family_t
 Address family type.
typedef fnet_uint32_t fnet_scope_id_t
 cope zone index type, defining network interface.
typedef void * fnet_socket_t
 Socket descriptor.

Enumerations

enum  fnet_socket_type_t { SOCK_UNSPEC = (0U), SOCK_STREAM = (1U), SOCK_DGRAM = (2U), SOCK_RAW = (3U) }
 

Socket types.

More...
enum  fnet_socket_state_t { SS_UNCONNECTED = (0), SS_CONNECTING = (1), SS_CONNECTED = (2), SS_LISTENING = (3) }
 

Socket state.

More...
enum  fnet_protocol_t {
  IPPROTO_IP = (0), IPPROTO_ICMP = (1), IPPROTO_IGMP = (2), IPPROTO_TCP = (6),
  IPPROTO_UDP = (17), IPPROTO_IPV6 = (41), IPPROTO_ICMPV6 = (58), SOL_SOCKET = (255255)
}
 

Protocol numbers and Level numbers for the fnet_socket_setopt() and the fnet_socket_getopt().

More...
enum  fnet_socket_options_t {
  SO_ACCEPTCONN, SO_KEEPALIVE, SO_DONTROUTE, SO_LINGER,
  SO_OOBINLINE, SO_SNDBUF, SO_RCVBUF, SO_STATE,
  SO_ERROR, SO_TYPE, SO_RCVNUM, SO_SNDNUM,
  TCP_MSS, TCP_BSD, TCP_NODELAY, TCP_FINRCVD,
  TCP_URGRCVD, TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_KEEPCNT,
  IP_TOS, IP_TTL, IP_MULTICAST_TTL, IP_ADD_MEMBERSHIP,
  IP_DROP_MEMBERSHIP, IPV6_UNICAST_HOPS, IPV6_MULTICAST_HOPS, IPV6_JOIN_GROUP,
  IPV6_LEAVE_GROUP
}
 

Socket options for the fnet_socket_setopt() and the fnet_socket_getopt().

More...
enum  fnet_msg_flags_t { MSG_OOB = (0x1U), MSG_PEEK = (0x2U), MSG_DONTROUTE = (0x4U) }
 

The flags parameters for receiving and sending functions fnet_socket_recv(), fnet_socket_recvfrom(), fnet_socket_send(), and fnet_socket_sendto().

More...
enum  fnet_sd_flags_t { SD_READ = (0x1U), SD_WRITE = (0x2U), SD_BOTH = (SD_READ | SD_WRITE) }
 

The flags used by fnet_socket_shutdown().

More...

Functions

fnet_uint16_t fnet_htons (fnet_uint16_t short_var)
 Converts 16-bit value from host to network byte order.
fnet_uint16_t fnet_ntohs (fnet_uint16_t short_var)
 Converts 16-bit value from network to host byte order.
fnet_uint32_t fnet_htonl (fnet_uint32_t long_var)
 Converts 32-bit value from host to network byte order.
fnet_uint32_t fnet_ntohl (fnet_uint32_t long_var)
 Converts 32-bit value from network to host byte order.
fnet_char_tfnet_inet_ntoa (struct in_addr addr, fnet_char_t *res_str)
 Converts an IPv4 address into a string in Internet standard dotted-decimal format.
fnet_return_t fnet_inet_aton (fnet_char_t *cp, struct in_addr *addr)
 Converts the string in the standard dotted-decimal notation to an integer value, suitable for use as an IPv4 address.
fnet_char_tfnet_inet_ntop (fnet_address_family_t family, const void *addr, fnet_char_t *str, fnet_size_t str_len)
 Converts IPv4 or IPv6 address from binary to text form.
fnet_return_t fnet_inet_pton (fnet_address_family_t family, const fnet_char_t *str, void *addr, fnet_size_t addr_len)
 Converts IPv4 and IPv6 addresses from text to binary form.
fnet_return_t fnet_inet_ptos (const fnet_char_t *str, struct sockaddr *addr)
 Converts IPv4 and IPv6 addresses from text to socket-address structure.
fnet_socket_t fnet_socket (fnet_address_family_t family, fnet_socket_type_t type, fnet_uint32_t protocol)
 Creates a socket.
fnet_return_t fnet_socket_bind (fnet_socket_t s, const struct sockaddr *name, fnet_size_t namelen)
 Assigns a local address to a socket.
fnet_return_t fnet_socket_listen (fnet_socket_t s, fnet_size_t backlog)
 Places the socket into a state, where it is listening for an incoming connection.
fnet_socket_t fnet_socket_accept (fnet_socket_t s, struct sockaddr *addr, fnet_size_t *addrlen)
 Accepts a connection on the specified socket.
fnet_return_t fnet_socket_connect (fnet_socket_t s, struct sockaddr *name, fnet_size_t namelen)
 Establishes a connection with the specified socket.
fnet_int32_t fnet_socket_recv (fnet_socket_t s, fnet_uint8_t *buf, fnet_size_t len, fnet_flag_t flags)
 Receives the data from a connected socket.
fnet_int32_t fnet_socket_recvfrom (fnet_socket_t s, fnet_uint8_t *buf, fnet_size_t len, fnet_flag_t flags, struct sockaddr *from, fnet_size_t *fromlen)
 Receives the data and captures the address, from which the data was sent.
fnet_int32_t fnet_socket_send (fnet_socket_t s, fnet_uint8_t *buf, fnet_size_t len, fnet_flag_t flags)
 Sends the data on a connected socket.
fnet_int32_t fnet_socket_sendto (fnet_socket_t s, fnet_uint8_t *buf, fnet_size_t len, fnet_flag_t flags, const struct sockaddr *to, fnet_size_t tolen)
 Sends the data to a specific destination.
fnet_return_t fnet_socket_shutdown (fnet_socket_t s, fnet_sd_flags_t how)
 Terminates the connection in one or both directions.
fnet_return_t fnet_socket_close (fnet_socket_t s)
 Closes an existing socket.
fnet_return_t fnet_socket_setopt (fnet_socket_t s, fnet_protocol_t level, fnet_socket_options_t optname, const void *optval, fnet_size_t optvallen)
 Sets a socket option.
fnet_return_t fnet_socket_getopt (fnet_socket_t s, fnet_protocol_t level, fnet_socket_options_t optname, void *optval, fnet_size_t *optvallen)
 Gets a socket option.
fnet_return_t fnet_socket_getpeername (fnet_socket_t s, struct sockaddr *name, fnet_size_t *namelen)
 Retrieves the name of a peer connected to a socket.
fnet_return_t fnet_socket_getname (fnet_socket_t s, struct sockaddr *name, fnet_size_t *namelen)
 Retrieves the current name for the specified socket.
fnet_bool_t fnet_socket_addr_are_equal (const struct sockaddr *addr1, const struct sockaddr *addr2)
 Compares socket addresses.
fnet_bool_t fnet_socket_addr_is_unspecified (const struct sockaddr *addr)
 Determines, if socket address is unspecified.
fnet_bool_t fnet_socket_addr_is_multicast (const struct sockaddr *addr)
 Determines, if socket address is multicast.
void fnet_socket_set_callback_on_rx (void(*callback)(void))
 Registers the "Socket Rx" event handler callback.

Detailed Description

The Socket Application Program Interface (API) defines the way, in which the application program interacts with the TCP/IP stack. It has the BSD-like, non-blocking socket interface that makes porting of existing network applications to the FNET more convenient.

The following table summarizes the supported socket API functions:

Socket functions
CategoryFNET RoutineBSD RoutineMeaningServer ClientSOCK_STREAM SOCK_DGRAM
setupfnet_socket()socket()Creates a new unnamed socket within a specified communication domain family.XXX X
setupfnet_socket_bind()bind()Assigns a local address to a socket.XXXX
serverfnet_socket_listen()listen()Prepares a socket to accept the incoming connections.X
X
serverfnet_socket_accept()accept()Accepts the connections. X
X
clientfnet_socket_connect()connect()Establishes a connection to a foreign socket.
XXX
inputfnet_socket_recv()recv()Receives the data.X XXX
inputfnet_socket_recvfrom()recvfrom()Receives the data and address of the sender.XX
X
outputfnet_socket_send()send()Sends the data.X XXX
outputfnet_socket_sendto()sendto()Sends the data to a specified address.XX
X
terminationfnet_socket_shutdown()shutdown()Terminates a connection in one or both directions.XXXX
terminationfnet_socket_close()closesocket()Terminates a connection and releases the socket.XXXX
administrationfnet_socket_setopt()fnet_socket_setopt()Sets socket or protocol options.XXXX
administrationfnet_socket_getopt()getsockopt()Gets socket or protocol options.XXXX
administrationfnet_socket_getname()getsockname()Gets a local address assigned to a socket.XXXX
administrationfnet_socket_getpeername()getpeername()Gets a foreign address assigned to a socket.XXXX

Configuration parameters:

  • FNET_CFG_SOCKET_MAX
  • FNET_CFG_SOCKET_TCP_TX_BUF_SIZE
  • FNET_CFG_SOCKET_TCP_RX_BUF_SIZE
  • FNET_CFG_SOCKET_UDP_TX_BUF_SIZE
  • FNET_CFG_SOCKET_UDP_RX_BUF_SIZE
  • FNET_CFG_SOCKET_TCP_MSS
  • FNET_CFG_RAW
  • FNET_CFG_SOCKET_BSD_NAMES
  • FNET_CFG_SOCKET_CALLBACK_ON_RX

Typedef Documentation

typedef fnet_uint16_t fnet_address_family_t

Address family type.

See also:
AF_UNSPEC, AF_INET, AF_INET6, AF_SUPPORTED

Definition at line 139 of file fnet_socket.h.

typedef fnet_uint32_t fnet_ip4_addr_t

32-bit IPv4 address type.

Definition at line 36 of file fnet_ip.h.

typedef fnet_uint32_t fnet_scope_id_t

cope zone index type, defining network interface.

Definition at line 144 of file fnet_socket.h.

typedef void* fnet_socket_t

Socket descriptor.

Definition at line 685 of file fnet_socket.h.


Enumeration Type Documentation

The flags parameters for receiving and sending functions fnet_socket_recv(), fnet_socket_recvfrom(), fnet_socket_send(), and fnet_socket_sendto().

The optional flag specifies the way, in which the call is made. It can be constructed by using the bitwise OR.

Enumerator:
MSG_OOB 

Process out-of-band data instead of regular data.


This option is avalable only if FNET_CFG_TCP_URGENT is set to 1.

MSG_PEEK 

Receive a copy of the data without consuming it.

MSG_DONTROUTE 

Send without using routing tables.

Definition at line 694 of file fnet_socket.h.

Protocol numbers and Level numbers for the fnet_socket_setopt() and the fnet_socket_getopt().

Enumerator:
IPPROTO_IP 

IPv4 options level number for fnet_socket_getopt() and fnet_socket_setopt().

IPPROTO_ICMP 

ICMPv4 protocol number.

IPPROTO_IGMP 

IGMP protocol number.

IPPROTO_TCP 

TCP protocol number; TCP options level number for fnet_socket_getopt() and fnet_socket_setopt().

IPPROTO_UDP 

UDP protocol number.

IPPROTO_IPV6 

IPv6 options level number for fnet_socket_getopt() and fnet_socket_setopt().

IPPROTO_ICMPV6 

ICMPv6 protocol number.

SOL_SOCKET 

Socket options level number for fnet_socket_getopt() and fnet_socket_setopt().

Definition at line 361 of file fnet_socket.h.

The flags used by fnet_socket_shutdown().

They describe what types of socket operation will no longer be allowed.

Enumerator:
SD_READ 

Data receiving is disabled.

SD_WRITE 

Data sending is disabled.

SD_BOTH 

Both receiving and sending are disabled.

Definition at line 714 of file fnet_socket.h.

Socket options for the fnet_socket_setopt() and the fnet_socket_getopt().

Socket SOL_SOCKET, IPPROTO_TCP, IPPROTO_IP and IPPROTO_IPV6 level options
OptionOption TypeDefault ValueRead/Write
SO_ACCEPTCONNfnet_uint32_t0R
SO_KEEPALIVEfnet_uint32_t1RW
SO_DONTROUTEfnet_uint32_t0RW
SO_LINGERstruct linger{0,0}RW
SO_OOBINLINEfnet_uint32_t0RW
SO_SNDBUFfnet_uint32_t FNET_CFG_SOCKET_UDP_TX_BUF_SIZE for UDP
FNET_CFG_SOCKET_TCP_TX_BUF_SIZE for TCP
RW
SO_RCVBUFfnet_uint32_t FNET_CFG_SOCKET_UDP_RX_BUF_SIZE for UDP
FNET_CFG_SOCKET_TCP_RX_BUF_SIZE for TCP
RW
SO_STATEfnet_socket_state_tSS_UNCONNECTEDR
SO_ERRORfnet_error_tFNET_ERR_OKR
SO_TYPEfnet_socket_type_tSOCK_DGRAM for UDP
SOCK_STREAM for TCP
R
SO_RCVNUMfnet_uint32_t0R
SO_SNDNUMfnet_uint32_t0R
TCP_MSSfnet_uint32_t536RW
TCP_BSDfnet_uint32_t1RW
TCP_NODELAYfnet_uint32_t1RW
TCP_FINRCVDfnet_uint32_t0R
TCP_URGRCVDfnet_uint32_t0R
TCP_KEEPIDLEfnet_uint32_t7200RW
TCP_KEEPINTVLfnet_uint32_t75RW
TCP_KEEPCNTfnet_uint32_t8RW
IP_TOSfnet_uint32_t0RW
IP_TTLfnet_uint32_t64RW
IP_MULTICAST_TTLfnet_uint32_t1RW
IP_ADD_MEMBERSHIPip_mreqN/AW
IP_DROP_MEMBERSHIPip_mreqN/AW
IPV6_UNICAST_HOPSfnet_uint32_t0 (64)RW
IPV6_MULTICAST_HOPSfnet_uint32_t1RW
IPV6_JOIN_GROUPipv6_mreqN/AW
IPV6_LEAVE_GROUPipv6_mreqN/AW
Enumerator:
SO_ACCEPTCONN 

Returns 1 if a socket is in listening mode and returns 0 when vice versa.

This is the read-only option and it is valid only for connection-oriented protocols (TCP).

SO_KEEPALIVE 

This option enables keep-alive probes for a socket connection.

These probes are used to maintain a TCP connection and regularly test the connection to ensure that it's still available. It is only valid for connection-oriented protocols (TCP).

SO_DONTROUTE 

This option enables bypassing of a routing algorithm.

It means that the network interface tries to send a datagram without a gateway.

SO_LINGER 

This option controls the action taken when unsent data is present, and fnet_socket_close() is called.

This option is defined by the linger structure.

SO_OOBINLINE 

This option specifies that out-of-band (OOB) data will be received in line with regular data.

It is valid only for the TCP protocol.
This option is avalable only if FNET_CFG_TCP_URGENT is set to 1.

SO_SNDBUF 

This option defines the maximum per-socket buffer size for output data.

SO_RCVBUF 

This option defines the maximum per-socket buffer size for input data.

SO_STATE 

This option defines the current state of the socket.


This is the read-only option and it is defined by the fnet_socket_state_t type.

SO_ERROR 

This option returns a per-socket-based error code.


The error code is defined by the fnet_error_t type.
After the error value has been read in the fnet_socket_getopt function, it is cleared. But a successful call using the socket routines does not clear the socket-based error.

SO_TYPE 

This option defines the type of the socket.

This is a read-only option and it is defined by the fnet_socket_type_t type.

SO_RCVNUM 

This option is used to determine the amount of data pending in the socket-input buffer.


This is a read-only option.

SO_SNDNUM 

This option is used to determine the amount of data in the socket output buffer.


This is a read-only option.

TCP_MSS 

This option defines the maximum size of the input segments (MSS).


The TCP Maximum Segment Size (MSS) defines the maximum amount of data that a host is willing to accept in a single TCP segment.
This Maximum Segment Size (MSS) announcement is sent from the data receiver to the data sender and says "I can accept TCP segments up to size X". The size (X) may be larger or smaller than the default.
The MSS counts only data octets in the segment, it does not count the TCP header or the IP header.
The default value is defined by the FNET_CFG_SOCKET_TCP_MSS user-configuration option.
This option can be set to:

  • 0 = The selection of the MSS is automatic and is based on the MTU of the outgoing interface minus 40 (does not include the 20 byte IP header and the 20 byte TCP header).
    It is done to assist in avoiding of IP fragmentation at the endpoints of the TCP connection.
  • Non-zero value (up to 64K) = The TCP segment could be as large as 64K (the maximum IP datagram size), but it could be fragmented at the IP layer in order to be transmitted across the network to the receiving host.
TCP_BSD 

If this option is set to 1, the BSD interpretation of the urgent pointer is used.

In this case the urgent pointer of the TCP segment points to the next byte following after the urgent byte. Most of the TCP implementations use this interpretation by default.
If this option is set to 0, the interpretation of the TCP specification is used. In this case the urgent pointer of the TCP segment points to the urgent byte.
This option is avalable only if FNET_CFG_TCP_URGENT is set to 1.

TCP_NODELAY 

If this option is set to 1, the Nagle algorithm is disabled (and vice versa).


The Nagle algorithm is effective in reducing the number of small packets sent by the host by essentially buffering send data, if there is unacknowledged data already "in flight", or until a full-size packet can be sent.
But for some applications this algorithm can impede performance, especially for a bulky data transfer.

TCP_FINRCVD 

This option is set when the final (FIN) segment arrives.


This option indicates that another side will not send any data in the current connection.
This is the read-only option.

TCP_URGRCVD 

This option is set when the urgent byte arrives, and reset when this byte is read.


This option can be set only if the SO_OOBINLINE option is set to 0.
This is the read-only option.
This option is avalable only if FNET_CFG_TCP_URGENT is set to 1.

TCP_KEEPIDLE 

When the SO_KEEPALIVE option is enabled, TCP probes a connection that has been idle for some amount of time.

The default value for this idle period is 7200 seconds (2 hours). The TCP_KEEPIDLE option can be used to affect this value for a given socket, and specifies the number of seconds of idle time between keepalive probes.

TCP_KEEPINTVL 

When the SO_KEEPALIVE option is enabled, TCP probes a connection that has been idle for some amount of time.

If the remote system does not respond to a keepalive probe, TCP retransmits the probe after some amount of time. The default value for this retransmit interval is 75 seconds. The TCP_KEEPINTVL option can be used to affect this value for a given socket, and specifies the number of seconds to wait before retransmitting a keepalive probe.

TCP_KEEPCNT 

When the SO_KEEPALIVE option is enabled, TCP probes a connection that has been idle for some amount of time.

If the remote system does not respond to a keepalive probe, TCP retransmits the probe a certain number of times before a connection is considered to be broken. The default value for this keepalive probe retransmit limit is 8. The TCP_KEEPCNT option can be used to affect this value for a given socket, and specifies the maximum number of keepalive probes to be sent.

IP_TOS 

This option defines the IPv4 TOS (type-of-service) field for outgoing datagrams.

IP_TTL 

This option defines the IPv4 TTL (time-to-live) vlaue for outgoing datagrams.

IP_MULTICAST_TTL 

This option allows to change IPv4 "time to live" (TTL) value for outgoing multicast datagrams.

Otherwise, multicast datagrams are sent with a default value of 1, to prevent them to be forwarded beyond the local network.
This option is available only if FNET_CFG_MULTICAST is set to 1.

IP_ADD_MEMBERSHIP 

Join the socket to the IPv4 multicast group on the specified interface.

It tells the system to receive packets on the network whose destination is the group address (but not its own). It is valid only for the SOCK_DGRAM (UDP) sockets.
This option is available only if FNET_CFG_MULTICAST is set to 1.

IP_DROP_MEMBERSHIP 

Drops membership to a IPv4 multicast group and interface.


This option is available only if FNET_CFG_MULTICAST is set to 1.

IPV6_UNICAST_HOPS 

This option defines hop limit used for outgoing unicast IPv6 packets.


Its value can be from 0 till 255.
By default the option is set to 0. It means that the hop limit is defined by local IPv6 router, otherwise it equals to 64.

IPV6_MULTICAST_HOPS 

Set the hop limit to use for outgoing multicast IPv6 packets.


If IPV6_MULTICAST_HOPS is not set, the default is 1.

IPV6_JOIN_GROUP 

(RFC3493) Join a multicast group on a specified local interface.


It is valid only for the SOCK_DGRAM (UDP) sockets.

IPV6_LEAVE_GROUP 

(RFC3493) Leave a multicast group on a specified interface.


It is valid only for the SOCK_DGRAM (UDP) sockets.

Definition at line 486 of file fnet_socket.h.

Socket state.

Enumerator:
SS_UNCONNECTED 

Not connected to any socket.

SS_CONNECTING 

In process of connecting.

SS_CONNECTED 

Connected to a socket.

SS_LISTENING 

In listening state.

Definition at line 345 of file fnet_socket.h.

Socket types.

Enumerator:
SOCK_UNSPEC 

Unspecified socket type.

SOCK_STREAM 

Stream socket.


Provides reliable, two-way, connection-based byte stream. It corresponds to the TCP protocol

SOCK_DGRAM 

Datagram socket.


Provides unreliable, connectionless datagrams. It corresponds to the UDP protocol.

SOCK_RAW 

Raw socket.


Raw sockets allow an application to have direct access to lower-level communication protocols. Raw sockets are intended to take advantage of some protocol feature that is not directly accessible through a normal interface, or to build new protocols on top of existing low-level protocols.
It can be enabled by the FNET_CFG_RAW option.

Definition at line 320 of file fnet_socket.h.


Function Documentation

fnet_uint32_t fnet_htonl ( fnet_uint32_t  long_var )

Converts 32-bit value from host to network byte order.

Parameters:
long_varA 32-bit number in host byte order.
Returns:
This function returns the network byte-ordered long_var.
See also:
FNET_HTONS(), FNET_NTOHS(), FNET_HTONL(), FNET_NTOHL(), fnet_ntohs(), fnet_htons(), fnet_ntohl()

The function converts the unsigned long integer from host byte order to network byte order.

fnet_uint16_t fnet_htons ( fnet_uint16_t  short_var )

Converts 16-bit value from host to network byte order.

Parameters:
short_varA 16-bit number in host byte order.
Returns:
This function returns the network byte-ordered short_var.
See also:
FNET_HTONS(), FNET_NTOHS(), FNET_HTONL(), FNET_NTOHL(), fnet_ntohs(), fnet_htonl(), fnet_ntohl()

The function converts the unsigned 16-bit integer from host byte order to network byte order.

fnet_return_t fnet_inet_aton ( fnet_char_t cp,
struct in_addr addr 
)

Converts the string in the standard dotted-decimal notation to an integer value, suitable for use as an IPv4 address.

Parameters:
cpNull-terminated character string representing a number expressed in the Internet standard "." (dotted) notation.
addrPointer to an integer will contain a suitable binary representation of the Internet address cp.
Returns:
This function returns:
  • FNET_OK if no error occurs.
  • FNET_ERR if the string in the cp parameter does not contain a legitimate Internet address.
See also:
fnet_inet_aton(), fnet_inet_ntop(), fnet_inet_pton()

This function interprets the character string specified by the cp parameter. This string represents a numeric Internet address expressed in the Internet standard "." notation. The value returned, pointed to by the addr, is a number suitable for use as an Internet address.

Note:
fnet_inet_pton() extends the fnet_inet_aton() function to support multiple address families.
fnet_inet_aton() is now considered to be deprecated.
fnet_char_t* fnet_inet_ntoa ( struct in_addr  addr,
fnet_char_t res_str 
)

Converts an IPv4 address into a string in Internet standard dotted-decimal format.

Parameters:
addrStructure that represents an Internet address.
res_strPointer to a character buffer will contain the resulting text address in standard "." notation.
The res_str buffer must be at least 16 bytes long (FNET_IP4_ADDR_STR_SIZE).
Returns:
This function always returns the res_str.
See also:
fnet_inet_aton(), fnet_inet_ntop(), fnet_inet_pton()

This function takes an Internet address structure, specified by the addr parameter, and returns a null-terminated ASCII string, representing the address in "." (dot) notation as in "a.b.c.d" into buffer pointed to by the res_str.

Note:
fnet_inet_ntop() extends the fnet_inet_ntoa() function to support multiple address families.
fnet_inet_ntoa() is now considered to be deprecated.
fnet_char_t* fnet_inet_ntop ( fnet_address_family_t  family,
const void *  addr,
fnet_char_t str,
fnet_size_t  str_len 
)

Converts IPv4 or IPv6 address from binary to text form.

Parameters:
familyThe address family (AF_INET or AF_INET6).
addrPointer to the IP address in network-byte order.
strPointer to a buffer in which to store the NULL-terminated string representation of the IP address.
For an IPv4 address, the str buffer must be at least 16 bytes long (FNET_IP4_ADDR_STR_SIZE).
For an IPv6 address, the str buffer must be at least 46 bytes long (FNET_IP6_ADDR_STR_SIZE).
str_lenLength of the str buffer.
Returns:
This function returns:
  • pointer to a buffer containing the string representation of IP address (the str), if no error occurs,
  • FNET_NULL if an error occurs.
See also:
fnet_inet_pton()

This function converts the network address structure, specified by the addr parameter, in the addr_family address family into a character string. The resulting string is copied to the buffer pointed to by str.

Note:
fnet_inet_ntop() extends the fnet_inet_ntoa() function to support multiple address families.
fnet_inet_ntoa() is now considered to be deprecated.
fnet_return_t fnet_inet_pton ( fnet_address_family_t  family,
const fnet_char_t str,
void *  addr,
fnet_size_t  addr_len 
)

Converts IPv4 and IPv6 addresses from text to binary form.

Parameters:
familyThe address family (AF_INET or AF_INET6).
strNull-terminated character string that contains the text representation of the IP address to convert to numeric binary form.
addrPointer to a buffer in which to store the numeric binary representation of the IP address str.
addr_lenLength of the addr buffer.
Returns:
This function returns:
  • FNET_OK if no error occurs.
  • FNET_ERR if the string in the str parameter does not contain a legitimate Internet address.
See also:
fnet_inet_ntop()

This function converts the character string src into a network address structure in the addr_family address family, then copies the network address structure to the addr buffer.

Note:
fnet_inet_pton() extends the fnet_inet_aton() function to support multiple address families.
fnet_inet_aton() is now considered to be deprecated.
fnet_return_t fnet_inet_ptos ( const fnet_char_t str,
struct sockaddr addr 
)

Converts IPv4 and IPv6 addresses from text to socket-address structure.

Parameters:
strNull-terminated character string that contains the text representation of the IP address to convert to socket-address structure.
addrPointer to a socket-address structure to be filled.
Returns:
This function returns:
  • FNET_OK if no error occurs.
  • FNET_ERR if the string in the str parameter does not contain a legitimate Internet address.
See also:
fnet_inet_pton()

This function converts the character string src into a socket-address structure.

fnet_uint32_t fnet_ntohl ( fnet_uint32_t  long_var )

Converts 32-bit value from network to host byte order.

Parameters:
long_varA 32-bit number in network byte order.
Returns:
This function returns the host byte-ordered long_var.
See also:
FNET_HTONS(), FNET_NTOHS(), FNET_HTONL(), FNET_NTOHL(), fnet_htons(), fnet_ntohs(), fnet_htonl()

The function converts the unsigned long integer from network byte order to host byte order.

fnet_uint16_t fnet_ntohs ( fnet_uint16_t  short_var )

Converts 16-bit value from network to host byte order.

Parameters:
short_varA 16-bit number in network byte order.
Returns:
This function returns the host byte-ordered short_var.
See also:
FNET_HTONS(), FNET_NTOHS(), FNET_HTONL(), FNET_NTOHL(), fnet_htons(), fnet_htonl(), fnet_ntohl()

The function converts the unsigned 16-bit integer from network byte order to host byte order.

fnet_socket_t fnet_socket ( fnet_address_family_t  family,
fnet_socket_type_t  type,
fnet_uint32_t  protocol 
)

Creates a socket.

Parameters:
familyAddress family that the socket will use, defined by the fnet_address_family_t.
typeType specification for the new socket, defined by fnet_socket_type_t. It can be SOCK_STREAM (TCP) or SOCK_DGRAM (UDP).
protocolProtocol to be used with the socket that is specific to the indicated address family. This stack supports IPPROTO_TCP and IPPROTO_UDP.
This parameter is optional, and can be set to zero, as the type already defines the proper protocol.
Returns:
This function returns:
  • Socket descriptor referencing the new socket, if no error occurs.
  • FNET_NULL if an error occurs.
    The specific error code can be retrieved using the fnet_error_get().
See also:
fnet_socket_close()

This function creates a socket and returns its descriptor.
The fnet_socket() function causes a socket descriptor and any related resources to be allocated and bound to a specific transport-service provider that supports the requested combination of address family, socket type, and protocol parameters.

After a socket is created:

Definition at line 109 of file ns_fnet_port.c.

fnet_socket_t fnet_socket_accept ( fnet_socket_t  s,
struct sockaddr addr,
fnet_size_t addrlen 
)

Accepts a connection on the specified socket.

Parameters:
sDescriptor, identifying an unconnected socket.
addrOptional pointer to a buffer that receives the address of the remote host at the other end of the connection.
addrlenOptional pointer to an integer that contains the length of the addr parameter.
Returns:
This function returns:
  • Socket descriptor referencing the new socket, if no error occurs.
  • FNET_NULL if an error occurs.
    The specific error code can be retrieved using fnet_error_get().
See also:
fnet_socket(), fnet_socket_bind()

The function extracts the first connection in the queue of pending connections on the listening socket s, and returns the new socket descriptor.
The newly-created socket is the socket that will handle the actual connection and has the same properties as the socket s. The original socket remains open and listens for new connection requests. If no pending connections are present in the queue of the socket, the fnet_socket_accept() returns FNET_NULL and the specific error code is set to FNET_ERR_AGAIN.
The parameter addr is a result parameter that is filled in with the address of the remote host. The addrlen should initially contain the amount of space pointed to by the addr; on return it will contain the actual length of the address returned (in bytes).
The fnet_socket_accept() function is used only with connection-oriented socket types (SOCK_STREAM).
If addr and/or addrlen are equal to 0, then no information about the remote address of the accepted socket is returned.

fnet_bool_t fnet_socket_addr_are_equal ( const struct sockaddr addr1,
const struct sockaddr addr2 
)

Compares socket addresses.

Parameters:
addr1First socket address to be compared.
addr2Second socket address to be compared.
Returns:
This function returns:

This function compares specified socket addresses, depending on address family.
Port number and Scope Id do not take part in process of comparison.

fnet_bool_t fnet_socket_addr_is_multicast ( const struct sockaddr addr )

Determines, if socket address is multicast.

Parameters:
addrSocket address.
Returns:
This function returns:
  • FNET_TRUE if the address is multicast.
  • FNET_FALSE if the address is not multicast.

This function determines if the socket address is multicast or not.

fnet_bool_t fnet_socket_addr_is_unspecified ( const struct sockaddr addr )

Determines, if socket address is unspecified.

Parameters:
addrSocket address.
Returns:
This function returns:
  • FNET_TRUE if the address is unspecified.
  • FNET_FALSE if the address is specified.

This function determines if the socket address is unspecified (IP address is set to all zeros) or not.

fnet_return_t fnet_socket_bind ( fnet_socket_t  s,
const struct sockaddr name,
fnet_size_t  namelen 
)

Assigns a local address to a socket.

Parameters:
sDescriptor, identifying a socket to bind.
nameThe address to be assigned to the socket, from the sockaddr structure.
namelenThe length of the name parameter. Normally namelen is set to sizeof(name).
Returns:
This function returns:
See also:
fnet_socket()

This function associates a local address with the socket.
The fnet_socket_bind() function is used on an unconnected socket before subsequent calls to the fnet_socket_connect() or the fnet_socket_listen() functions. It is used to bind to either connection-oriented (stream) or connectionless (datagram) sockets.
Within the sockaddr structure, the address may be the address assigned to a network interface on the host or the INADDR_ANY. Using the INADDR_ANY causes the stack to use the default network interface address. Using a port number of 0 causes the service provider to assign a unique port to the socket with a value between 1024 and 5000.

Definition at line 131 of file ns_fnet_port.c.

fnet_return_t fnet_socket_close ( fnet_socket_t  s )

Closes an existing socket.

Parameters:
sDescriptor, identifying a socket to close.
Returns:
This function returns:
See also:
fnet_socket()

This function releases the socket descriptor s, so that further references to s fail with the error FNET_ERR_BAD_DESC.
An application should always have a matching call to the fnet_socket_close() for each successful call to the fnet_socket() to return any socket resources to the system.

For the SOCK_STREAM sockets, the SO_LINGER option controls the action of the closing, when unsent data is present on the socket and the fnet_socket_close() function is performed. This option has effect on TCP sockets only. To enable or disable the SO_LINGER option, the application should use the linger structure. This structure includes the l_onoff and the l_linger fields. If the l_onoff has a non-zero value, then the SO_LINGER is enabled. Otherwise, the SO_LINGER option is disabled. The l_linger field includes the timeout in seconds. If the SO_LINGER option is not used , the remaining data of the output buffer can be sent after the execution of the fnet_socket_close() in the background.
If a timeout expires and unsent data is present, the hard reset occurs and unsent data will be deleted.
If the SO_LINGER option is used with a zero timeout interval, the hard reset occurs immediately and unsent data will be deleted.

Definition at line 235 of file ns_fnet_port.c.

fnet_return_t fnet_socket_connect ( fnet_socket_t  s,
struct sockaddr name,
fnet_size_t  namelen 
)

Establishes a connection with the specified socket.

Parameters:
sDescriptor identifying an unconnected socket.
nameAddress (name) of the socket, with which the connection should be established.
namelenLength of the name.
Returns:
This function returns:
See also:
fnet_socket(), fnet_socket_bind()

For connection-oriented sockets (SOCK_STREAM), this function activates an active connection with the foreign host.

When the socket call completes successfully, the socket is ready to send and receive data. Any attempt to reconnect the active connection will fail with the error code FNET_ERR_ISCONN. For connection-oriented sockets, it is often not possible to complete the connection immediately. Until the connection attempt completes on a socket, all subsequent calls to fnet_socket_connect() on the same socket will fail with the error code FNET_ERR_INPROGRESS, or succeed with FNET_ERR_ISCONN when the connection completes successfully. Use the fnet_socket_getopt() function to determine the completion of the connection request by checking the SO_STATE to see if the socket is connected (SS_CONNECTED), is still connecting (SS_CONNECTING) or the connection has failed (SS_UNCONNECTED).

For a connectionless socket (SOCK_DGRAM), the operation performed by fnet_socket_connect() is merely to establish a default destination address that can be used on subsequent fnet_socket_send() and fnet_socket_recv() calls. Any datagrams received from an address other than the destination address specified will be discarded. The default destination can be changed by simply calling the fnet_socket_connect() again, even if the socket is connected already. Any datagrams queued for receipt are discarded from the previous fnet_socket_connect(). For connectionless sockets, the name can indicate any valid address, including a broadcast address.

If the socket s is unbound, unique values are assigned to the local association by the stack, and the socket is marked as bound.

fnet_return_t fnet_socket_getname ( fnet_socket_t  s,
struct sockaddr name,
fnet_size_t namelen 
)

Retrieves the current name for the specified socket.

Parameters:
sDescriptor identifying a socket.
nameStructure that receives the name (address) of the socket s.
namelenPointer to the size of the name structure.
Returns:
This function returns:
See also:
fnet_socket_getpeername()

This function retrieves the current name for the specified socket s and stores it in the name.
It is used on a bound or connected socket.

fnet_return_t fnet_socket_getopt ( fnet_socket_t  s,
fnet_protocol_t  level,
fnet_socket_options_t  optname,
void *  optval,
fnet_size_t optvallen 
)

Gets a socket option.

Parameters:
sDescriptor identifying a socket.
levelLevel, at which the option is defined. The supported levels are the SOL_SOCKET, IPPROTO_IP, IPPROTO_IPV6 or IPPROTO_TCP.
optnameSocket option, for which the value is to be retrievied.
optvalPointer to a buffer in which the value for the requested option has to be returned.
optvallenPointer to the size of the optval buffer.
Returns:
This function returns:
See also:
fnet_socket_setopt()

This function retrieves the current value for a socket option associated with the socket s, and stores the result in the optval.
The various socket options are described by the fnet_socket_options_t.

fnet_return_t fnet_socket_getpeername ( fnet_socket_t  s,
struct sockaddr name,
fnet_size_t namelen 
)

Retrieves the name of a peer connected to a socket.

Parameters:
sDescriptor identifying a connected socket.
nameStructure that receives the name (address) of the peer.
namelenPointer to the size of the name structure.
Returns:
This function returns:
See also:
fnet_socket_getname()

This function retrieves the name of the peer connected to the socket s and stores it in the sockaddr structure identified by name.
The fnet_socket_getpeername() function can be used only with a connected socket. For datagram sockets, only the name of a peer specified in a previous fnet_socket_connect() call will be returned - any name specified by a previous fnet_socket_sendto() call will not be returned by the fnet_socket_getpeername().

fnet_return_t fnet_socket_listen ( fnet_socket_t  s,
fnet_size_t  backlog 
)

Places the socket into a state, where it is listening for an incoming connection.

Parameters:
sDescriptor identifying a socket that will be used for listening.
backlogMaximum length of the queue of pending connections.
Returns:
This function returns:
See also:
fnet_socket(), fnet_socket_bind(), fnet_socket_accept()

Only connection-oriented sockets (SOCK_STREAM) are used with the fnet_socket_listen().
The socket is put into passive mode, where the incoming connection requests are acknowledged and queued pending acceptance by the listening socket. The fnet_socket_listen() function is typically used by servers that can have more than one connection request at a time.
An application can call fnet_socket_listen() more than once on the same socket. If there are more pending connections than the new backlog value, the excess pending connections will be reset and dropped.

fnet_int32_t fnet_socket_recv ( fnet_socket_t  s,
fnet_uint8_t *  buf,
fnet_size_t  len,
fnet_flag_t  flags 
)

Receives the data from a connected socket.

Parameters:
sDescriptor identifying a connected socket.
bufBuffer for the incoming data.
lenLength of the buf.
flagsOptional flag specifying the way, in which the call is made. It can be constructed by using the bitwise OR operator with any of the values defined by the fnet_msg_flags_t.
Returns:
This function returns:
  • The number of bytes received, if no error occurs. The return value is set to zero, if there is no input data.
  • FNET_ERR if an error occurs.
    The specific error code can be retrieved using the fnet_error_get().
See also:
fnet_socket_connect(), fnet_socket_recvfrom()

For connection-oriented sockets (SOCK_STREAM), calling the fnet_socket_recv() will return as much information as is currently available up to the size of the buffer supplied.

For connectionless sockets (SOCK_DGRAM), the data is extracted from the first enqueued datagram (message), whose destination address is specified by the fnet_socket_connect() function. If the datagram is larger than the supplied buffer, the buffer is filled with the first part of the datagram, and the fnet_socket_recv() generates the error FNET_ERR_MSGSIZE and the excess data is lost.

The flags parameter can be used to influence the behavior of the function invocation beyond the options specified for the associated socket.

fnet_int32_t fnet_socket_recvfrom ( fnet_socket_t  s,
fnet_uint8_t *  buf,
fnet_size_t  len,
fnet_flag_t  flags,
struct sockaddr from,
fnet_size_t fromlen 
)

Receives the data and captures the address, from which the data was sent.

Parameters:
sDescriptor, identifying a bound socket.
bufBuffer for the incoming data.
lenLength of the buf.
flagsOptional flag specifying the way in which the call is made. It can be constructed by using the bitwise OR operator with any of the values defined by the fnet_msg_flags_t.
fromOptional pointer to a buffer that will hold the source address upon return.
fromlenOptional pointer to the size of the from buffer.
Returns:
This function returns:
  • The number of bytes received, if no error occurs. The return value is set to zero, if the socket is nonblocking and there is no input data.
  • FNET_ERR if an error occurs.
    The specific error code can be retrieved using the fnet_error_get().
See also:
fnet_socket_bind(), fnet_socket_recv()

For connection-oriented sockets (SOCK_STREAM), calling the fnet_socket_recvfrom() will return as much information as is currently available up to the size of the buffer supplied. The fnet_socket_recvfrom() call is rarely used for stream sockets, because the routine provides the peer-socket information, and stream sockets are tied to a specific peer.

For connectionless sockets (SOCK_DGRAM), the data is extracted from the first enqueued datagram. If the datagram is larger than the buffer supplied, the buffer is filled with the first part of the datagram, and the fnet_socket_recvfrom() generates the error FNET_ERR_MSGSIZE and the excess data is lost.

If the from parameter is non-zero, the network address of the peer that sent the data is copied to the corresponding sockaddr structure. The value pointed to by fromlen is initialized to the size of this structure and is modified on return, to indicate the actual size of the address stored in the sockaddr structure.

The flags parameter can be used to influence the behavior of the function invocation beyond the options specified for the associated socket.

Definition at line 208 of file ns_fnet_port.c.

fnet_int32_t fnet_socket_send ( fnet_socket_t  s,
fnet_uint8_t *  buf,
fnet_size_t  len,
fnet_flag_t  flags 
)

Sends the data on a connected socket.

Parameters:
sDescriptor, identifying a connected socket.
bufBuffer containing the data to be transmitted.
lenLength of the data in buf.
flagsOptional flag specifying the way in which the call is made. It can be constructed by using the bitwise OR operator with any of the values defined by the fnet_msg_flags_t.
Returns:
This function returns:
  • The total number of bytes sent, if no error occurs. It can be less than the number indicated by len.
  • FNET_ERR if an error occurs.
    The specific error code can be retrieved using the fnet_error_get().
See also:
fnet_socket_connect(), fnet_socket_sendto()

For stream-oriented sockets (SOCK_STREAM), the number of actually sent bytes can be between 0 and the requested length, depending on buffer availability on both client and server machines.

For message-oriented sockets (SOCK_DGRAM), care must be taken not to exceed the maximum datagram size, which can be obtained by using the fnet_socket_getopt() to retrieve the value of the socket option SO_SNDBUF. If the data is too long to pass atomically through the underlying datagram protocol, the error FNET_ERR_MSGSIZE is returned and no data is transmitted.
Calling the fnet_socket_send() with a zero len parameter is permissible. In such cases, the fnet_socket_send() will return zero as a valid value, and for message-oriented sockets (SOCK_DGRAM) a zero-length transport datagram is sent.

The flags parameter can be used to influence the behavior of the function invocation beyond the options specified for the associated socket.

fnet_int32_t fnet_socket_sendto ( fnet_socket_t  s,
fnet_uint8_t *  buf,
fnet_size_t  len,
fnet_flag_t  flags,
const struct sockaddr to,
fnet_size_t  tolen 
)

Sends the data to a specific destination.

Parameters:
sDescriptor, identifying a socket.
bufBuffer containing the data to be transmitted.
lenLength of the data in buf.
flagsOptional flag specifying the way, in which the call is made. It can be constructed by using the bitwise OR operator with any of the values defined by the fnet_msg_flags_t.
toPointer to the address of the target socket.
tolenSize of the address in to.
Returns:
This function returns:
  • The total number of bytes sent, if no error occurs. It can be lesser than the number indicated by len.
  • FNET_ERR if an error occurs.
    The specific error code can be retrieved using the fnet_error_get().
See also:
fnet_socket_connect(), fnet_socket_send()

For nonb-locking stream-oriented sockets (SOCK_STREAM), the number of actually sent bytes can be between 1 and the requested length, depending on buffer availability on both client and server machines.

For message-oriented sockets (SOCK_DGRAM), care must be taken not not exceed the maximum datagram size, which can be obtained by using the fnet_socket_getopt() to retrieve the value of the socket option SO_SNDBUF. If the data is too long to pass atomically through the underlying datagram protocol, the error FNET_ERR_MSGSIZE is returned and no data is transmitted.
Calling fnet_socket_sendto() with a zero len parameter is permissible. In such cases, the fnet_socket_sendto() will return zero as a valid value, and for message-oriented sockets (SOCK_DGRAM), a zero-length transport datagram is sent.

The fnet_socket_sendto() function is normally used on a connectionless socket (SOCK_DGRAM) to send a datagram to a specific peer socket identified by the to parameter. Even if the connectionless socket has been previously connected to a specific address, the to parameter overrides the destination address for that particular datagram only.
On a connection-oriented socket (SOCK_STREAM), the to and tolen parameters are ignored, making the fnet_socket_sendto() equivalent to fnet_socket_send().

The flags parameter can be used to influence the behavior of the function invocation beyond the options specified for the associated socket.

Definition at line 193 of file ns_fnet_port.c.

void fnet_socket_set_callback_on_rx ( void(*)(void)  callback )

Registers the "Socket Rx" event handler callback.

Parameters:
callbackPointer to the event-handler callback function.

This function registers the handler callback function for the "Socket Rx" event.
This event occurs when the socket layer receives any packet for any existing socket. It can be a packet with or without data-payload including ACK, FIN and SYN.
For example, it can be used to wake-up a sleeping RTOS application waiting for a network activity. To stop the event handling, set callback parameter to zero value.

fnet_return_t fnet_socket_setopt ( fnet_socket_t  s,
fnet_protocol_t  level,
fnet_socket_options_t  optname,
const void *  optval,
fnet_size_t  optvallen 
)

Sets a socket option.

Parameters:
sDescriptor identifying a socket.
levelLevel, at which the option is defined. The supported levels are SOL_SOCKET, IPPROTO_IP, IPPROTO_IPV6 or IPPROTO_TCP.
optnameSocket option for which the value is to be set.
optvalPointer to a buffer, in which the value for the requested option is supplied.
optvallenSize of the optval buffer.
Returns:
This function returns:
See also:
fnet_socket_getopt()

This function sets the current value for a socket option associated with the specified socket.
The various socket options are described by the fnet_socket_options_t.

Definition at line 152 of file ns_fnet_port.c.

fnet_return_t fnet_socket_shutdown ( fnet_socket_t  s,
fnet_sd_flags_t  how 
)

Terminates the connection in one or both directions.

Parameters:
sDescriptor, identifying a socket.
howFlag that describes what types of operation will no longer be allowed, it's defined by the fnet_sd_flags_t.
Returns:
This function returns:
See also:
fnet_socket_close()

This function is used on all types of sockets to disable reception, transmission, or both.
If the how parameter is SD_READ, subsequent calls to the fnet_socket_recv() function on the socket will be unallowed. If the how parameter is SD_WRITE, subsequent calls to the fnet_socket_send() function are unallowed. Setting the how to the SD_BOTH disables both sends and receives as described above.
The fnet_socket_shutdown() function does not close the socket. Any resources attached to the socket will not be freed until the fnet_socket_close() is invoked.