Embedded TCP/IP stack: fnet_socket.h Source File

FNET

fnet_socket.h
1 /**************************************************************************
2 *
3 * Copyright 2011-2016 by Andrey Butok. FNET Community.
4 * Copyright 2008-2010 by Andrey Butok. Freescale Semiconductor, Inc.
5 * Copyright 2003 by Andrey Butok. Motorola SPS.
6 *
7 ***************************************************************************
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 ***************************************************************************
22 *
23 * Socket API.
24 *
25 ***************************************************************************/
26 
27 #ifndef _FNET_SOCKET_H_
28 
29 #define _FNET_SOCKET_H_
30 
31 #include "fnet_ip4.h"
32 #include "fnet_ip6.h"
33 
122 /* Special addresses */
123 
124 /**************************************************************************/
127 #define INADDR_ANY (fnet_ip4_addr_t)(0x00000000U)
128 
129 /**************************************************************************/
133 #define INADDR_BROADCAST (fnet_ip4_addr_t)(0xffffffffU)
134 
135 /**************************************************************************/
139 typedef fnet_uint16_t fnet_address_family_t;
140 
141 /**************************************************************************/
144 typedef fnet_uint32_t fnet_scope_id_t;
145 
146 /**************************************************************************/
149 #define AF_UNSPEC (0U)
150 /**************************************************************************/
153 #define AF_INET (1U)
154 /**************************************************************************/
157 #define AF_INET6 (2U)
158 /**************************************************************************/
162 #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)))
163 
164 /**************************************************************************/
169 #if FNET_CFG_IP6
170  #define FNET_SA_DATA_SIZE (sizeof(struct fnet_in6_addr))
171 #else /* IPv4 */
172  #define FNET_SA_DATA_SIZE (sizeof(struct fnet_in_addr))
173 #endif
174 
175 /**************************************************************************/
183 #if FNET_CFG_IP6
184  #define FNET_IP_ADDR_STR_SIZE FNET_IP6_ADDR_STR_SIZE
185 #else /* IPv4 */
186  #define FNET_IP_ADDR_STR_SIZE FNET_IP4_ADDR_STR_SIZE
187 #endif
188 
189 #define FNET_IP_ADDR_STR_SIZE_MAX FNET_IP6_ADDR_STR_SIZE
190 
191 /**************************************************************************/
199 {
201 };
202 
203 /**************************************************************************/
213 {
216  fnet_uint16_t sin_port;
220  struct fnet_in_addr sin_addr;
221 };
222 
223 
224 /************************************************************************
225 * IPv6
226 *************************************************************************/
227 
228 
229 /**************************************************************************/
237 {
239 };
240 
241 /**************************************************************************/
251 {
254  fnet_uint16_t sin6_port;
258  struct fnet_in6_addr sin6_addr;
259 };
260 
261 /**************************************************************************/
271 {
275  fnet_uint16_t sa_port;
279  fnet_uint8_t sa_data[FNET_SA_DATA_SIZE];
282 };
283 
284 /**************************************************************************/
294 {
295  struct fnet_in_addr imr_multiaddr;
298 };
299 
300 /**************************************************************************/
310 {
311  struct fnet_in6_addr ipv6imr_multiaddr;
314 };
315 
316 /**************************************************************************/
319 typedef enum
320 {
321  SOCK_UNSPEC = (0U),
323  SOCK_STREAM = (1U),
327  SOCK_DGRAM = (2U),
331  SOCK_RAW = (3U)
340 
341 /**************************************************************************/
344 typedef enum
345 {
348  SS_CONNECTED = (2),
351 
352 /**************************************************************************/
356 typedef enum
357 {
358  IPPROTO_IP = (0),
360  IPPROTO_ICMP = (1),
361  IPPROTO_IGMP = (2),
362  IPPROTO_TCP = (6),
364  IPPROTO_UDP = (17),
365  IPPROTO_IPV6 = (41),
368  SOL_SOCKET = (255255)
371 
372 /**************************************************************************/
473 typedef enum
474 {
516  /* TCP level (IPPROTO_TCP) options */
517 
591  /* IPv4 level (IPPROTO_IP) options */
592 
609  /* IPv6 level (IPPROTO_IPV6) options */
610 
623 
624 /**************************************************************************/
628 {
632  fnet_uint16_t l_linger;
638 };
639 
640 /**************************************************************************/
643 typedef void *fnet_socket_t;
644 
645 /**************************************************************************/
652 typedef enum
653 {
654  MSG_OOB = (0x1U),
659  MSG_PEEK = (0x2U),
662  MSG_DONTROUTE = (0x4U)
666 
667 /**************************************************************************/
672 typedef enum
673 {
674  SD_READ = (0x1U),
676  SD_WRITE = (0x2U),
682 
683 #if defined(__cplusplus)
684 extern "C" {
685 #endif
686 
687 /***************************************************************************/
735 fnet_socket_t fnet_socket( fnet_address_family_t family, fnet_socket_type_t type, fnet_uint32_t protocol );
736 
737 /***************************************************************************/
771 fnet_return_t fnet_socket_bind( fnet_socket_t s, const struct fnet_sockaddr *name, fnet_size_t namelen );
772 
773 /***************************************************************************/
806 fnet_return_t fnet_socket_listen( fnet_socket_t s, fnet_size_t backlog );
807 
808 /***************************************************************************/
849 fnet_socket_t fnet_socket_accept( fnet_socket_t s, struct fnet_sockaddr *addr, fnet_size_t *addrlen );
850 
851 /***************************************************************************/
903 fnet_return_t fnet_socket_connect( fnet_socket_t s, struct fnet_sockaddr *name, fnet_size_t namelen );
904 
905 /***************************************************************************/
947 fnet_ssize_t fnet_socket_recv( fnet_socket_t s, void *buf, fnet_size_t len, fnet_flag_t flags );
948 
949 /***************************************************************************/
1004 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 );
1005 
1006 /***************************************************************************/
1051 fnet_ssize_t fnet_socket_send( fnet_socket_t s, const void *buf, fnet_size_t len, fnet_flag_t flags );
1052 
1053 /***************************************************************************/
1110 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 );
1111 
1112 /***************************************************************************/
1143 fnet_return_t fnet_socket_shutdown( fnet_socket_t s, fnet_sd_flags_t how );
1144 
1145 /***************************************************************************/
1183 fnet_return_t fnet_socket_close( fnet_socket_t s );
1184 
1185 /***************************************************************************/
1218 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 );
1219 
1220 /***************************************************************************/
1253 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 );
1254 
1255 /***************************************************************************/
1284 fnet_return_t fnet_socket_getpeername( fnet_socket_t s, struct fnet_sockaddr *name, fnet_size_t *namelen );
1285 
1286 /***************************************************************************/
1313 fnet_return_t fnet_socket_getname( fnet_socket_t s, struct fnet_sockaddr *name, fnet_size_t *namelen );
1314 
1315 /***************************************************************************/
1333 fnet_bool_t fnet_socket_addr_are_equal(const struct fnet_sockaddr *addr1, const struct fnet_sockaddr *addr2);
1334 
1335 /***************************************************************************/
1352 
1353 /***************************************************************************/
1369 
1370 #if FNET_CFG_SOCKET_CALLBACK_ON_RX || defined(__DOXYGEN__)
1371 /***************************************************************************/
1390 void fnet_socket_set_callback_on_rx( void(*callback)(void));
1391 #endif
1392 
1393 /* BSD Socket API names */
1394 #if FNET_CFG_SOCKET_BSD_NAMES
1395 #define socket fnet_socket
1396 #define bind fnet_socket_bind
1397 #define listen fnet_socket_listen
1398 #define accept fnet_socket_accept
1399 #define connect fnet_socket_connect
1400 #define recv fnet_socket_recv
1401 #define recvfrom fnet_socket_recvfrom
1402 #define send fnet_socket_send
1403 #define sendto fnet_socket_sendto
1404 #define shutdown fnet_socket_shutdown
1405 #define closesocket fnet_socket_close
1406 #define setsockopt fnet_socket_setopt
1407 #define getsockopt fnet_socket_getopt
1408 #define getpeername fnet_socket_getpeername
1409 #define getsockname fnet_socket_getname
1410 #endif
1411 
1412 #if defined(__cplusplus)
1413 }
1414 #endif
1415 
1418 #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:250
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:368
This option defines the IPv4 TTL (time-to-live) vlaue for outgoing datagrams.
Definition: fnet_socket.h:595
When the SO_KEEPALIVE option is enabled, TCP probes a connection that has been idle for some amount o...
Definition: fnet_socket.h:571
fnet_uint16_t sin6_port
16-bit port number used to demultiplex the transport-level messages (in network byte order)...
Definition: fnet_socket.h:254
Join the socket to the IPv4 multicast group on the specified interface. It tells the system to receiv...
Definition: fnet_socket.h:602
IPv6 address structure.
Definition: fnet_socket.h:236
fnet_uint32_t fnet_scope_id_t
cope zone index type, defining network interface.
Definition: fnet_socket.h:144
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:296
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:272
IPv4 multicast group information.
Definition: fnet_socket.h:293
fnet_sd_flags_t
The flags used by fnet_socket_shutdown().
Definition: fnet_socket.h:672
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:510
This option defines hop limit used for outgoing unicast IPv6 packets. Its value can be from 0 till ...
Definition: fnet_socket.h:611
unsigned int fnet_flag_t
Unsigned integer type representing the bit flag.
Definition: fnet_stdlib.h:66
Set the hop limit to use for outgoing multicast IPv6 packets. If IPV6_MULTICAST_HOPS is not set...
Definition: fnet_socket.h:616
Stream socket. Provides reliable, two-way, connection-based byte stream. It corresponds to the TCP p...
Definition: fnet_socket.h:323
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.
Drops membership to a IPv4 multicast group and interface. This option is available only if FNET_CFG_...
Definition: fnet_socket.h:607
If this option is set to 1, the Nagle algorithm is disabled (and vice versa). The Nagle algorithm i...
Definition: fnet_socket.h:553
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:475
fnet_socket_options_t
Socket options for the fnet_socket_setopt() and the fnet_socket_getopt().
Definition: fnet_socket.h:473
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:565
Data sending is disabled.
Definition: fnet_socket.h:676
fnet_scope_id_t sa_scope_id
Scope zone index, defining network interface.
Definition: fnet_socket.h:278
fnet_return_t
General return codes, used by most of API functions.
Definition: fnet_stdlib.h:90
Process out-of-band data instead of regular data. This option is avalable only if FNET_CFG_TCP_URGE...
Definition: fnet_socket.h:654
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:485
fnet_scope_id_t sin6_scope_id
Scope zone index, defining network interface.
Definition: fnet_socket.h:257
Receive a copy of the data without consuming it.
Definition: fnet_socket.h:659
IPv4 address structure.
Definition: fnet_socket.h:198
This option is used to determine the amount of data in the socket output buffer. This is a read-onl...
Definition: fnet_socket.h:513
ICMPv4 protocol number.
Definition: fnet_socket.h:360
This option enables keep-alive probes for a socket connection. These probes are used to maintain a TC...
Definition: fnet_socket.h:479
This option defines the maximum size of the input segments (MSS). The TCP Maximum Segment Size (MSS...
Definition: fnet_socket.h:518
Not connected to any socket.
Definition: fnet_socket.h:346
This option is set when the final (FIN) segment arrives. This option indicates that another side wi...
Definition: fnet_socket.h:561
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:312
IGMP protocol number.
Definition: fnet_socket.h:361
fnet_ip6_addr_t s6_addr
128-bit IPv6 address.
Definition: fnet_socket.h:238
IPv6 options level number for fnet_socket_getopt() and fnet_socket_setopt().
Definition: fnet_socket.h:365
fnet_uint16_t sa_port
16-bit port number used to demultiplex the transport-level messages (in network byte order)...
Definition: fnet_socket.h:275
(RFC3493) Join a multicast group on a specified local interface. It is valid only for the SOCK_DGRAM...
Definition: fnet_socket.h:618
fnet_bool_t l_onoff
Determines, whether the option will be turned on FNET_TRUE, or off FNET_FALSE.
Definition: fnet_socket.h:629
In listening state.
Definition: fnet_socket.h:349
void * fnet_socket_t
Socket descriptor.
Definition: fnet_socket.h:643
unsigned long fnet_size_t
Unsigned integer type representing the size in bytes.
Definition: fnet_stdlib.h:56
128-bit IPv6 address type.
Definition: fnet_ip6.h:38
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_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.
IPv4 options level number for fnet_socket_getopt() and fnet_socket_setopt().
Definition: fnet_socket.h:358
Data receiving is disabled.
Definition: fnet_socket.h:674
Unspecified socket type.
Definition: fnet_socket.h:321
fnet_protocol_t
Protocol numbers and Level numbers for the fnet_socket_setopt() and the fnet_socket_getopt().
Definition: fnet_socket.h:356
This structure is used for the SO_LINGER option.
Definition: fnet_socket.h:627
fnet_socket_state_t
Socket state.
Definition: fnet_socket.h:344
This option allows to change IPv4 "time to live" (TTL) value for outgoing multicast datagrams...
Definition: fnet_socket.h:597
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:497
#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:172
This option returns a per-socket-based error code. The error code is defined by the fnet_error_t typ...
Definition: fnet_socket.h:503
IPv6 multicast group information.
Definition: fnet_socket.h:309
Send without using routing tables.
Definition: fnet_socket.h:662
ICMPv6 protocol number.
Definition: fnet_socket.h:367
Both receiving and sending are disabled.
Definition: fnet_socket.h:678
fnet_ip4_addr_t s_addr
32-bit IPv4 address (in network byte order).
Definition: fnet_socket.h:200
fnet_address_family_t sin_family
Specifies the address family. It must ne set to AF_INET.
Definition: fnet_socket.h:214
fnet_bool_t
Boolean type.
Definition: fnet_stdlib.h:81
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:362
Datagram socket. Provides unreliable, connectionless datagrams. It corresponds to the UDP protocol...
Definition: fnet_socket.h:327
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:508
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:652
When the SO_KEEPALIVE option is enabled, TCP probes a connection that has been idle for some amount o...
Definition: fnet_socket.h:583
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:212
UDP protocol number.
Definition: fnet_socket.h:364
This option defines the current state of the socket. This is the read-only option and it is defined ...
Definition: fnet_socket.h:501
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:499
This option specifies that out-of-band (OOB) data will be received in line with regular data...
Definition: fnet_socket.h:492
fnet_return_t fnet_socket_close(fnet_socket_t s)
Closes an existing socket.
fnet_socket_type_t
Socket types.
Definition: fnet_socket.h:319
fnet_bool_t fnet_socket_addr_is_multicast(const struct fnet_sockaddr *addr)
Determines, if socket address is multicast.
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:541
This option controls the action taken when unsent data is present, and fnet_socket_close() is called...
Definition: fnet_socket.h:488
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:36
This option defines the IPv4 TOS (type-of-service) field for outgoing datagrams.
Definition: fnet_socket.h:593
fnet_address_family_t sin6_family
Specifies the address family. It must ne set to AF_INET6.
Definition: fnet_socket.h:252
When the SO_KEEPALIVE option is enabled, TCP probes a connection that has been idle for some amount o...
Definition: fnet_socket.h:576
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:632
Raw socket. Raw sockets allow an application to have direct access to lower-level communication prot...
Definition: fnet_socket.h:331
In process of connecting.
Definition: fnet_socket.h:347
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:61
fnet_uint16_t fnet_address_family_t
Address family type.
Definition: fnet_socket.h:139
Socket address structure.
Definition: fnet_socket.h:270
Connected to a socket.
Definition: fnet_socket.h:348
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:620
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:219
fnet_uint16_t sin_port
16-bit port number used to demultiplex the transport-level messages (in network byte order)...
Definition: fnet_socket.h:216

© 2005-2018 by Andrey Butok. http://fnet.sourceforge.net