Embedded TCP/IP stack  4.7.0
fnet_socket.h
1 /**************************************************************************
2 *
3 * Copyright 2008-2018 by Andrey Butok. FNET Community
4 *
5 ***************************************************************************
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 ***************************************************************************
20 *
21 * Socket API.
22 *
23 ***************************************************************************/
24 
25 #ifndef _FNET_SOCKET_H_
26 
27 #define _FNET_SOCKET_H_
28 
29 #include "fnet_ip4.h"
30 #include "fnet_ip6.h"
31 
120 /* Special addresses */
121 
122 /**************************************************************************/
125 #define INADDR_ANY (fnet_ip4_addr_t)(0x00000000U)
126 
127 /**************************************************************************/
131 #define INADDR_BROADCAST (fnet_ip4_addr_t)(0xffffffffU)
132 
133 /**************************************************************************/
137 typedef fnet_uint16_t fnet_address_family_t;
138 
139 /**************************************************************************/
142 typedef fnet_uint32_t fnet_scope_id_t;
143 
144 /**************************************************************************/
147 #define AF_UNSPEC (0U)
148 /**************************************************************************/
151 #define AF_INET (1U)
152 /**************************************************************************/
155 #define AF_INET6 (2U)
156 /**************************************************************************/
160 #define AF_SUPPORTED ((fnet_address_family_t)((fnet_address_family_t)(AF_INET6*(fnet_address_family_t)FNET_CFG_IP6) | (fnet_address_family_t)(AF_INET*(fnet_address_family_t)FNET_CFG_IP4)))
161 
162 /**************************************************************************/
167 #if FNET_CFG_IP6
168  #define FNET_SA_DATA_SIZE (sizeof(struct fnet_in6_addr))
169 #else /* IPv4 */
170  #define FNET_SA_DATA_SIZE (sizeof(struct fnet_in_addr))
171 #endif
172 
173 /**************************************************************************/
181 #if FNET_CFG_IP6
182  #define FNET_IP_ADDR_STR_SIZE FNET_IP6_ADDR_STR_SIZE
183 #else /* IPv4 */
184  #define FNET_IP_ADDR_STR_SIZE FNET_IP4_ADDR_STR_SIZE
185 #endif
186 
187 #define FNET_IP_ADDR_STR_SIZE_MAX FNET_IP6_ADDR_STR_SIZE
188 
189 /**************************************************************************/
197 {
199 };
200 
201 /**************************************************************************/
211 {
214  fnet_uint16_t sin_port;
219 };
220 
221 
222 /************************************************************************
223 * IPv6
224 *************************************************************************/
225 
226 
227 /**************************************************************************/
235 {
237 };
238 
239 /**************************************************************************/
249 {
252  fnet_uint16_t sin6_port;
257 };
258 
259 /**************************************************************************/
269 {
273  fnet_uint16_t sa_port;
277  fnet_uint8_t sa_data[FNET_SA_DATA_SIZE];
280 };
281 
282 /**************************************************************************/
292 {
296 };
297 
298 /**************************************************************************/
308 {
312 };
313 
314 /**************************************************************************/
317 typedef enum
318 {
319  SOCK_UNSPEC = (0U),
321  SOCK_STREAM = (1U),
325  SOCK_DGRAM = (2U),
329  SOCK_RAW = (3U)
338 
339 /**************************************************************************/
342 typedef enum
343 {
344  SS_CLOSED = (0),
350 
351 /**************************************************************************/
355 typedef enum
356 {
357  IPPROTO_IP = (0),
359  IPPROTO_ICMP = (1),
360  IPPROTO_IGMP = (2),
361  IPPROTO_TCP = (6),
363  IPPROTO_UDP = (17),
364  IPPROTO_IPV6 = (41),
367  SOL_SOCKET = (255255)
370 
371 /**************************************************************************/
478 typedef enum
479 {
525  /* TCP level (IPPROTO_TCP) options */
526 
600  /* IPv4 level (IPPROTO_IP) options */
601 
618  /* IPv6 level (IPPROTO_IPV6) options */
619 
632 
633 /**************************************************************************/
637 {
641  fnet_uint16_t l_linger;
647 };
648 
649 /**************************************************************************/
652 typedef void *fnet_socket_t;
653 
654 /**************************************************************************/
661 typedef enum
662 {
663  MSG_OOB = (0x1U),
668  MSG_PEEK = (0x2U),
671  MSG_DONTROUTE = (0x4U)
675 
676 /**************************************************************************/
681 typedef enum
682 {
683  SD_READ = (0x1U),
685  SD_WRITE = (0x2U),
691 
692 /**************************************************************************/
696 typedef enum
697 {
706 
707 /**************************************************************************/
710 typedef struct
711 {
717 
718 
719 #if defined(__cplusplus)
720 extern "C" {
721 #endif
722 
723 /***************************************************************************/
771 fnet_socket_t fnet_socket( fnet_address_family_t family, fnet_socket_type_t type, fnet_uint32_t protocol );
772 
773 /***************************************************************************/
807 fnet_return_t fnet_socket_bind( fnet_socket_t s, const struct fnet_sockaddr *name, fnet_size_t namelen );
808 
809 /***************************************************************************/
843 
844 /***************************************************************************/
886 
887 /***************************************************************************/
940 
941 /***************************************************************************/
984 
985 /***************************************************************************/
1040 fnet_ssize_t fnet_socket_recvfrom( fnet_socket_t s, void *buf, fnet_size_t len, fnet_flag_t flags, struct fnet_sockaddr *from, fnet_size_t *fromlen );
1041 
1042 /***************************************************************************/
1087 fnet_ssize_t fnet_socket_send( fnet_socket_t s, const void *buf, fnet_size_t len, fnet_flag_t flags );
1088 
1089 /***************************************************************************/
1146 fnet_ssize_t fnet_socket_sendto( fnet_socket_t s, const void *buf, fnet_size_t len, fnet_flag_t flags, const struct fnet_sockaddr *to, fnet_size_t tolen );
1147 
1148 /***************************************************************************/
1180 
1181 /***************************************************************************/
1220 
1221 /***************************************************************************/
1254 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 );
1255 
1256 /***************************************************************************/
1290 
1291 /***************************************************************************/
1321 
1322 /***************************************************************************/
1350 
1351 /***************************************************************************/
1369 fnet_bool_t fnet_socket_addr_are_equal(const struct fnet_sockaddr *addr1, const struct fnet_sockaddr *addr2);
1370 
1371 /***************************************************************************/
1388 
1389 /***************************************************************************/
1405 
1406 /***************************************************************************/
1422 fnet_size_t fnet_socket_poll(fnet_socket_poll_t *socket_poll, fnet_size_t socket_poll_size);
1423 
1424 #if FNET_CFG_SOCKET_CALLBACK_ON_RX || defined(__DOXYGEN__)
1425 /***************************************************************************/
1444 void fnet_socket_set_callback_on_rx( void(*callback)(void));
1445 #endif
1446 
1447 /* BSD Socket API names */
1448 #if FNET_CFG_SOCKET_BSD_NAMES
1449 #define socket fnet_socket
1450 #define bind fnet_socket_bind
1451 #define listen fnet_socket_listen
1452 #define accept fnet_socket_accept
1453 #define connect fnet_socket_connect
1454 #define recv fnet_socket_recv
1455 #define recvfrom fnet_socket_recvfrom
1456 #define send fnet_socket_send
1457 #define sendto fnet_socket_sendto
1458 #define shutdown fnet_socket_shutdown
1459 #define closesocket fnet_socket_close
1460 #define setsockopt fnet_socket_setopt
1461 #define getsockopt fnet_socket_getopt
1462 #define getpeername fnet_socket_getpeername
1463 #define getsockname fnet_socket_getname
1464 #endif
1465 
1466 #if defined(__cplusplus)
1467 }
1468 #endif
1469 
1472 #endif /* _FNET_SOCKET_H_ */
fnet_return_t fnet_socket_getname(fnet_socket_t s, struct fnet_sockaddr *name, fnet_size_t *namelen)
Retrieves the current name for the specified socket.
IPv6 Socket address structure.
Definition: fnet_socket.h:248
fnet_socket_event_t events
A bit mask specifying the events the application is interested in.
Definition: fnet_socket.h:713
There is no registered event.
Definition: fnet_socket.h:698
fnet_ssize_t fnet_socket_sendto(fnet_socket_t s, const void *buf, fnet_size_t len, fnet_flag_t flags, const struct fnet_sockaddr *to, fnet_size_t tolen)
Sends the data to a specific destination.
Socket options level number for fnet_socket_getopt() and fnet_socket_setopt().
Definition: fnet_socket.h:367
This option defines the IPv4 TTL (time-to-live) vlaue for outgoing datagrams.
Definition: fnet_socket.h:604
When the SO_KEEPALIVE option is enabled, TCP probes a connection that has been idle for some amount o...
Definition: fnet_socket.h:580
fnet_uint16_t sin6_port
16-bit port number used to demultiplex the transport-level messages (in network byte order)...
Definition: fnet_socket.h:252
Complete queue length of the socket. If it has the nonzeo value, unaccepted complete connections are ...
Definition: fnet_socket.h:523
Join the socket to the IPv4 multicast group on the specified interface. It tells the system to receiv...
Definition: fnet_socket.h:611
IPv6 address structure.
Definition: fnet_socket.h:234
fnet_uint32_t fnet_scope_id_t
cope zone index type, defining network interface.
Definition: fnet_socket.h:142
fnet_socket_t s
Socket descriptor. If its value is zero, it is ignored.
Definition: fnet_socket.h:712
fnet_scope_id_t imr_interface
Interface index. It equals to the scope zone index, defining network interface. If this member is zer...
Definition: fnet_socket.h:294
fnet_address_family_t sa_family
Address family. Specifies the address family, to which the address belongs. It is defined by fnet_ad...
Definition: fnet_socket.h:270
IPv4 multicast group information.
Definition: fnet_socket.h:291
fnet_sd_flags_t
The flags used by fnet_socket_shutdown().
Definition: fnet_socket.h:681
fnet_return_t fnet_socket_shutdown(fnet_socket_t s, fnet_sd_flags_t how)
Terminates the connection in one or both directions.
fnet_bool_t fnet_socket_addr_are_equal(const struct fnet_sockaddr *addr1, const struct fnet_sockaddr *addr2)
Compares socket addresses.
This option is used to determine the amount of data pending in the socket-input buffer. This is a read-only option.
Definition: fnet_socket.h:515
There is a free buffer space and the socket may accept data for writing. Also, it may be used as the ...
Definition: fnet_socket.h:700
This option defines hop limit used for outgoing unicast IPv6 packets. Its value can be from 0 till ...
Definition: fnet_socket.h:620
unsigned int fnet_flag_t
Unsigned integer type representing the bit flag.
Definition: fnet_stdlib.h:65
Set the hop limit to use for outgoing multicast IPv6 packets. If IPV6_MULTICAST_HOPS is not set...
Definition: fnet_socket.h:625
Stream socket. Provides reliable, two-way, connection-based byte stream. It corresponds to the TCP p...
Definition: fnet_socket.h:321
Bitmask of all event types.
Definition: fnet_socket.h:704
fnet_return_t fnet_socket_connect(fnet_socket_t s, struct fnet_sockaddr *name, fnet_size_t namelen)
Establishes a connection with the specified socket.
struct fnet_in_addr imr_multiaddr
IPv4 multicast address of group.
Definition: fnet_socket.h:293
Drops membership to a IPv4 multicast group and interface. This option is available only if FNET_CFG_...
Definition: fnet_socket.h:616
struct fnet_in6_addr sin6_addr
128-bit IPv6 internet address.
Definition: fnet_socket.h:256
If this option is set to 1, the Nagle algorithm is disabled (and vice versa). The Nagle algorithm i...
Definition: fnet_socket.h:562
Returns 1 if a socket is in listening mode and returns 0 when vice versa. This is the read-only optio...
Definition: fnet_socket.h:480
fnet_socket_options_t
Socket options for the fnet_socket_setopt() and the fnet_socket_getopt().
Definition: fnet_socket.h:478
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.
Definition: fnet_socket.h:574
Data sending is disabled.
Definition: fnet_socket.h:685
fnet_scope_id_t sa_scope_id
Scope zone index, defining network interface.
Definition: fnet_socket.h:276
fnet_return_t
General return codes, used by most of API functions.
Definition: fnet_stdlib.h:89
Process out-of-band data instead of regular data. This option is avalable only if FNET_CFG_TCP_URGE...
Definition: fnet_socket.h:663
fnet_ssize_t fnet_socket_recv(fnet_socket_t s, void *buf, fnet_size_t len, fnet_flag_t flags)
Receives the data from a connected socket.
This option enables bypassing of a routing algorithm. It means that the network interface tries to se...
Definition: fnet_socket.h:490
fnet_scope_id_t sin6_scope_id
Scope zone index, defining network interface.
Definition: fnet_socket.h:255
Receive a copy of the data without consuming it.
Definition: fnet_socket.h:668
IPv4 address structure.
Definition: fnet_socket.h:196
This option is used to determine the amount of data in the socket output buffer. This is a read-onl...
Definition: fnet_socket.h:518
ICMPv4 protocol number.
Definition: fnet_socket.h:359
This option enables keep-alive probes for a socket connection. These probes are used to maintain a TC...
Definition: fnet_socket.h:484
This option defines the maximum size of the input segments (MSS). The TCP Maximum Segment Size (MSS...
Definition: fnet_socket.h:527
This option is set when the final (FIN) segment arrives. This option indicates that another side wi...
Definition: fnet_socket.h:570
fnet_scope_id_t ipv6imr_interface
Interface index. It equals to the scope zone index, defining network interface. If this member is zer...
Definition: fnet_socket.h:310
IGMP protocol number.
Definition: fnet_socket.h:360
fnet_ip6_addr_t s6_addr
128-bit IPv6 address.
Definition: fnet_socket.h:236
IPv6 options level number for fnet_socket_getopt() and fnet_socket_setopt().
Definition: fnet_socket.h:364
fnet_uint16_t sa_port
16-bit port number used to demultiplex the transport-level messages (in network byte order)...
Definition: fnet_socket.h:273
(RFC3493) Join a multicast group on a specified local interface. It is valid only for the SOCK_DGRAM...
Definition: fnet_socket.h:627
fnet_bool_t l_onoff
Determines, whether the option will be turned on FNET_TRUE, or off FNET_FALSE.
Definition: fnet_socket.h:638
In listening state.
Definition: fnet_socket.h:348
void * fnet_socket_t
Socket descriptor.
Definition: fnet_socket.h:652
unsigned long fnet_size_t
Unsigned integer type representing the size in bytes.
Definition: fnet_stdlib.h:55
128-bit IPv6 address type.
Definition: fnet_ip6.h:37
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_socket_event_t events_occurred
A bit mask specifying the events that actually occurred. It is filled by by the fnet_socket_poll() an...
Definition: fnet_socket.h:714
fnet_ssize_t fnet_socket_send(fnet_socket_t s, const void *buf, fnet_size_t len, fnet_flag_t flags)
Sends the data on a connected socket.
fnet_size_t fnet_socket_poll(fnet_socket_poll_t *socket_poll, fnet_size_t socket_poll_size)
Polls socket event state.
IPv4 options level number for fnet_socket_getopt() and fnet_socket_setopt().
Definition: fnet_socket.h:357
Data receiving is disabled.
Definition: fnet_socket.h:683
Unspecified socket type.
Definition: fnet_socket.h:319
fnet_protocol_t
Protocol numbers and Level numbers for the fnet_socket_setopt() and the fnet_socket_getopt().
Definition: fnet_socket.h:355
This structure is used for the SO_LINGER option.
Definition: fnet_socket.h:636
fnet_socket_state_t
Socket state.
Definition: fnet_socket.h:342
This option allows to change IPv4 "time to live" (TTL) value for outgoing multicast datagrams...
Definition: fnet_socket.h:606
fnet_return_t fnet_socket_bind(fnet_socket_t s, const struct fnet_sockaddr *name, fnet_size_t namelen)
Assigns a local address to a socket.
This option defines the maximum per-socket buffer size for output data.
Definition: fnet_socket.h:502
#define FNET_SA_DATA_SIZE
Size of sa_data[] field of fnet_sockaddr structure. It used to cover fnet_sockaddr_in and fnet_socka...
Definition: fnet_socket.h:170
This option returns a per-socket-based error code. The error code is defined by the fnet_error_t typ...
Definition: fnet_socket.h:508
IPv6 multicast group information.
Definition: fnet_socket.h:307
Send without using routing tables.
Definition: fnet_socket.h:671
ICMPv6 protocol number.
Definition: fnet_socket.h:366
In process of closing connection. For TCP, after FIN receive.
Definition: fnet_socket.h:345
Both receiving and sending are disabled.
Definition: fnet_socket.h:687
struct fnet_in6_addr ipv6imr_multiaddr
IPv6 multicast address of group.
Definition: fnet_socket.h:309
fnet_ip4_addr_t s_addr
32-bit IPv4 address (in network byte order).
Definition: fnet_socket.h:198
fnet_address_family_t sin_family
Specifies the address family. It must ne set to AF_INET.
Definition: fnet_socket.h:212
fnet_bool_t
Boolean type.
Definition: fnet_stdlib.h:80
There is a socket error. It may happen when a connect attempt is failed, or connection is closed by r...
Definition: fnet_socket.h:702
fnet_uint8_t sa_data[FNET_SA_DATA_SIZE]
Address value. For the TCP/IP stack, it contains the destination address and port number for a socket...
Definition: fnet_socket.h:277
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_socket_t fnet_socket(fnet_address_family_t family, fnet_socket_type_t type, fnet_uint32_t protocol)
Creates a socket.
TCP protocol number; TCP options level number for fnet_socket_getopt() and fnet_socket_setopt().
Definition: fnet_socket.h:361
struct fnet_in_addr sin_addr
32-bit internet address.
Definition: fnet_socket.h:218
Datagram socket. Provides unreliable, connectionless datagrams. It corresponds to the UDP protocol...
Definition: fnet_socket.h:325
This option defines the type of the socket. This is a read-only option and it is defined by the fnet_...
Definition: fnet_socket.h:513
fnet_msg_flags_t
The flags parameters for receiving and sending functions fnet_socket_recv(), fnet_socket_recvfrom(), fnet_socket_send(), and fnet_socket_sendto().
Definition: fnet_socket.h:661
When the SO_KEEPALIVE option is enabled, TCP probes a connection that has been idle for some amount o...
Definition: fnet_socket.h:592
There is data for reading or a new pending connection for accepting.
Definition: fnet_socket.h:699
fnet_ssize_t fnet_socket_recvfrom(fnet_socket_t s, void *buf, fnet_size_t len, fnet_flag_t flags, struct fnet_sockaddr *from, fnet_size_t *fromlen)
Receives the data and captures the address, from which the data was sent.
IPv4 Socket address structure.
Definition: fnet_socket.h:210
UDP protocol number.
Definition: fnet_socket.h:363
Backlog limit of the socket or the maximal number of queued connections, which is set by fnet_socket_...
Definition: fnet_socket.h:521
This option defines the current state of the socket. This is the read-only option and it is defined ...
Definition: fnet_socket.h:506
fnet_bool_t fnet_socket_addr_is_unspecified(const struct fnet_sockaddr *addr)
Determines, if socket address is unspecified.
This option defines the maximum per-socket buffer size for input data.
Definition: fnet_socket.h:504
This option specifies that out-of-band (OOB) data will be received in line with regular data...
Definition: fnet_socket.h:497
fnet_return_t fnet_socket_close(fnet_socket_t s)
Closes an existing socket.
Not connected to any socket.
Definition: fnet_socket.h:344
fnet_socket_type_t
Socket types.
Definition: fnet_socket.h:317
fnet_bool_t fnet_socket_addr_is_multicast(const struct fnet_sockaddr *addr)
Determines, if socket address is multicast.
fnet_socket_event_t
Socket event type, used by fnet_socket_poll_t.
Definition: fnet_socket.h:696
If this option is set to 1, the BSD interpretation of the urgent pointer is used. In this case the ur...
Definition: fnet_socket.h:550
This option controls the action taken when unsent data is present, and fnet_socket_close() is called...
Definition: fnet_socket.h:493
void fnet_socket_set_callback_on_rx(void(*callback)(void))
Registers the "Socket Rx" event handler callback.
fnet_uint32_t fnet_ip4_addr_t
32-bit IPv4 address type.
Definition: fnet_ip4.h:35
This option defines the IPv4 TOS (type-of-service) field for outgoing datagrams.
Definition: fnet_socket.h:602
fnet_address_family_t sin6_family
Specifies the address family. It must ne set to AF_INET6.
Definition: fnet_socket.h:250
When the SO_KEEPALIVE option is enabled, TCP probes a connection that has been idle for some amount o...
Definition: fnet_socket.h:585
Socket poll structure used by fnet_socket_poll().
Definition: fnet_socket.h:710
fnet_uint16_t l_linger
Specifies the amount of time (in seconds) to wait when the connection is closed and unsent data is di...
Definition: fnet_socket.h:641
Raw socket. Raw sockets allow an application to have direct access to lower-level communication prot...
Definition: fnet_socket.h:329
In process of connecting.
Definition: fnet_socket.h:346
fnet_return_t fnet_socket_getpeername(fnet_socket_t s, struct fnet_sockaddr *name, fnet_size_t *namelen)
Retrieves the name of a peer connected to a socket.
long fnet_ssize_t
Signed integer type representing the size in bytes.
Definition: fnet_stdlib.h:60
fnet_uint16_t fnet_address_family_t
Address family type.
Definition: fnet_socket.h:137
Socket address structure.
Definition: fnet_socket.h:268
Connected to a socket.
Definition: fnet_socket.h:347
fnet_socket_t fnet_socket_accept(fnet_socket_t s, struct fnet_sockaddr *addr, fnet_size_t *addrlen)
Accepts a connection on the specified socket.
(RFC3493) Leave a multicast group on a specified interface. It is valid only for the SOCK_DGRAM (UDP...
Definition: fnet_socket.h:629
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_scope_id_t sin_scope_id
Scope zone index, defining network interface.
Definition: fnet_socket.h:217
fnet_uint16_t sin_port
16-bit port number used to demultiplex the transport-level messages (in network byte order)...
Definition: fnet_socket.h:214

© 2005-2020 by Andrej Butok. http://fnet.sourceforge.net