bsd_socket.h
Go to the documentation of this file.
1 /**
2  * @file bsd_socket.h
3  * @brief BSD socket API
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2022 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.2.0
29  **/
30 
31 #ifndef _BSD_SOCKET_H
32 #define _BSD_SOCKET_H
33 
34 //BSD socket support
35 #ifndef BSD_SOCKET_SUPPORT
36  #define BSD_SOCKET_SUPPORT ENABLED
37 #elif (BSD_SOCKET_SUPPORT != ENABLED && BSD_SOCKET_SUPPORT != DISABLED)
38  #error BSD_SOCKET_SUPPORT parameter is not valid
39 #endif
40 
41 //Maximum number of file descriptors a fd_set object can hold
42 #ifndef FD_SETSIZE
43  #define FD_SETSIZE 8
44 #elif (FD_SETSIZE < 1)
45  #error FD_SETSIZE parameter is not valid
46 #endif
47 
48 //Set errno variable
49 #ifndef BSD_SOCKET_SET_ERRNO
50  #define BSD_SOCKET_SET_ERRNO(e)
51 #endif
52 
53 //Keil RTX port?
54 #if defined(USE_RTX) && !defined(RTX_CUSTOM_HEADER)
55 
56 //No support for BSD sockets
57 #undef BSD_SOCKET_SUPPORT
58 #define BSD_SOCKET_SUPPORT DISABLED
59 
60 //Windows port?
61 #elif defined(_WIN32) && !defined(_DONT_USE_WINSOCK)
62 
63 //Undefine conflicting definitions
64 #undef htons
65 #undef htonl
66 #undef ntohs
67 #undef ntohl
68 
69 //Dependencies
70 #include <winsock2.h>
71 
72 #elif (BSD_SOCKET_SUPPORT == ENABLED)
73 
74 //Dependencies
75 #include "os_port.h"
76 
77 //Address families
78 #define AF_UNSPEC 0
79 #define AF_INET 2
80 #define AF_INET6 10
81 #define AF_PACKET 17
82 
83 //Protocol families
84 #define PF_UNSPEC AF_UNSPEC
85 #define PF_INET AF_INET
86 #define PF_INET6 AF_INET6
87 #define PF_PACKET AF_PACKET
88 
89 //Socket types
90 #define SOCK_STREAM 1
91 #define SOCK_DGRAM 2
92 #define SOCK_RAW 3
93 
94 //IP protocol identifiers
95 #define IPPROTO_IP 0
96 #define IPPROTO_ICMP 1
97 #define IPPROTO_IGMP 2
98 #define IPPROTO_TCP 6
99 #define IPPROTO_UDP 17
100 #define IPPROTO_ICMPV6 58
101 
102 //Ethernet protocol identifiers
103 #define ETH_P_ALL 0x0000
104 #define ETH_P_IP 0x0800
105 #define ETH_P_ARP 0x0806
106 #define ETH_P_IPV6 0x86DD
107 
108 //Option levels
109 #define SOL_SOCKET 0xFFFF
110 
111 //Common addresses
112 #define INADDR_ANY 0x00000000
113 #define INADDR_LOOPBACK 0x7F000001
114 #define INADDR_BROADCAST 0xFFFFFFFF
115 
116 //Flags used by I/O functions
117 #define MSG_PEEK 0x02
118 #define MSG_DONTROUTE 0x04
119 #define MSG_WAITALL 0x08
120 #define MSG_DONTWAIT 0x01
121 
122 //Flags used by shutdown function
123 #define SD_RECEIVE 0
124 #define SD_SEND 1
125 #define SD_BOTH 2
126 
127 //Flags used by shutdown function (alias)
128 #define SHUT_RD SD_RECEIVE
129 #define SHUT_WR SD_SEND
130 #define SHUT_RDWR SD_BOTH
131 
132 //Socket level options
133 #define SO_REUSEADDR 0x0004
134 #define SO_KEEPALIVE 0x0008
135 #define SO_DONTROUTE 0x0010
136 #define SO_BROADCAST 0x0020
137 #define SO_LINGER 0x0080
138 #define SO_SNDBUF 0x1001
139 #define SO_RCVBUF 0x1002
140 #define SO_SNDTIMEO 0x1005
141 #define SO_RCVTIMEO 0x1006
142 #define SO_ERROR 0x1007
143 #define SO_TYPE 0x1008
144 #define SO_MAX_MSG_SIZE 0x2003
145 #define SO_BINDTODEVICE 0x3000
146 
147 //IP level options
148 #define IP_TOS 1
149 #define IP_TTL 2
150 #define IP_MULTICAST_TTL 33
151 
152 //TCP level options
153 #define TCP_NODELAY 0x0001
154 #define TCP_MAXSEG 0x0002
155 #define TCP_KEEPIDLE 0x0004
156 #define TCP_KEEPINTVL 0x0005
157 #define TCP_KEEPCNT 0x0006
158 
159 //IP TOS option
160 #define IPTOS_LOWDELAY 0x10
161 #define IPTOS_THROUGHPUT 0x08
162 #define IPTOS_RELIABILITY 0x04
163 
164 //IOCTL commands
165 #define FIONBIO 126
166 #define FIONREAD 127
167 #define FIONWRITE 121
168 #define FIONSPACE 120
169 
170 //FCNTL commands
171 #define F_GETFL 3
172 #define F_SETFL 4
173 
174 //FCNTL flags
175 #define O_NONBLOCK 0x0004
176 
177 //Flags used by getaddrinfo
178 #define AI_PASSIVE 0x01
179 #define AI_CANONNAME 0x02
180 #define AI_NUMERICHOST 0x04
181 #define AI_NUMERICSERV 0x08
182 #define AI_ALL 0x10
183 #define AI_ADDRCONFIG 0x20
184 #define AI_V4MAPPED 0x40
185 
186 //Flags used by getnameinfo
187 #define NI_NOFQDN 0x01
188 #define NI_NUMERICHOST 0x02
189 #define NI_NAMEREQD 0x04
190 #define NI_NUMERICSERV 0x08
191 #define NI_DGRAM 0x10
192 
193 //Return values
194 #define SOCKET_SUCCESS 0
195 #define SOCKET_ERROR (-1)
196 
197 //Return values used by getaddrinfo and getnameinfo
198 #define EAI_ADDRFAMILY 1
199 #define EAI_AGAIN 2
200 #define EAI_BADFLAGS 3
201 #define EAI_FAIL 4
202 #define EAI_FAMILY 5
203 #define EAI_MEMORY 6
204 #define EAI_NODATA 7
205 #define EAI_NONAME 8
206 #define EAI_SERVICE 9
207 #define EAI_SOCKTYPE 10
208 #define EAI_SYSTEM 11
209 #define EAI_OVERFLOW 12
210 
211 //Error codes
212 #define EINTR 4
213 #define EAGAIN 11
214 #define EWOULDBLOCK 11
215 #define EFAULT 14
216 #define EINVAL 22
217 #define EINPROGRESS 36
218 #define ETIMEDOUT 60
219 #define ENAMETOOLONG 63
220 #define ENOPROTOOPT 92
221 #define ECONNRESET 104
222 #define EISCONN 106
223 #define ENOTCONN 107
224 #define ESHUTDOWN 108
225 #define ECONNREFUSED 111
226 
227 //Error codes used by gethostbyname_r
228 #define NETDB_SUCCESS 0
229 #define HOST_NOT_FOUND 1
230 #define TRY_AGAIN 2
231 #define NO_RECOVERY 3
232 #define NO_ADDRESS 4
233 
234 //Return codes
235 #define INADDR_NONE ((in_addr_t) (-1))
236 
237 //Maximum length for string representation of IPv4 address
238 #define INET_ADDRSTRLEN 16
239 
240 //Maximum length for string representation of IPv6 address
241 #define INET6_ADDRSTRLEN 40
242 
243 //C++ guard
244 #ifdef __cplusplus
245 extern "C" {
246 #endif
247 
248 
249 /**
250  * @brief Length type
251  **/
252 
253 typedef int_t socklen_t;
254 
255 
256 /**
257  * @brief IPv4 address
258  **/
259 
260 typedef uint32_t in_addr_t;
261 
262 
263 /**
264  * @brief Socket address
265  **/
266 
267 typedef struct sockaddr
268 {
269  uint16_t sa_family;
270  uint8_t sa_data[14];
272 
273 
274 /**
275  * @brief Structure that represents an IPv4 address
276  **/
277 
278 typedef struct in_addr
279 {
282 
283 
284 /**
285  * @brief IPv4 address information
286  **/
287 
288 typedef struct sockaddr_in
289 {
290  uint16_t sin_family;
291  uint16_t sin_port;
292  struct in_addr sin_addr;
293  uint8_t sin_zero[8];
295 
296 
297 /**
298  * @brief Structure that represents an IPv6 address
299  **/
300 
301 typedef struct in6_addr
302 {
303  uint8_t s6_addr[16];
305 
306 
307 /**
308  * @brief IPv6 address information
309  **/
310 
311 typedef struct sockaddr_in6
312 {
313  uint16_t sin6_family;
314  uint16_t sin6_port;
315  struct in6_addr sin6_addr;
317 
318 
319 /**
320  * @brief Set of sockets
321  **/
322 
323 typedef struct fd_set
324 {
328 
329 
330 /**
331  * @brief Information about a given host
332  **/
333 
334 typedef struct hostent
335 {
336  uint16_t h_addrtype;
337  uint16_t h_length;
338  uint8_t h_addr[16];
340 
341 
342 /**
343  * @brief Information about address of a service provider
344  **/
345 
346 typedef struct addrinfo
347 {
353  struct sockaddr *ai_addr;
355  struct addrinfo *ai_next;
357 
358 
359 #ifndef _TIMEVAL_DEFINED
360 
361 /**
362  * @brief Timeout structure
363  **/
364 
365 typedef struct timeval
366 {
367  int32_t tv_sec;
368  int32_t tv_usec;
370 
371 #endif
372 
373 
374 //Forward declaration of functions
375 void socketFdZero(fd_set *fds);
376 void socketFdSet(fd_set *fds, int_t s);
377 void socketFdClr(fd_set *fds, int_t s);
379 
380 //Macros for manipulating and checking descriptor sets
381 #define FD_ZERO(fds) socketFdZero(fds)
382 #define FD_SET(s, fds) socketFdSet(fds, s)
383 #define FD_CLR(s, fds) socketFdClr(fds, s)
384 #define FD_ISSET(s, fds) socketFdIsSet(fds, s)
385 
386 //BSD socket related constants
387 extern const struct in6_addr in6addr_any;
388 extern const struct in6_addr in6addr_loopback;
389 
390 //BSD socket related functions
392 int_t bind(int_t s, const struct sockaddr *addr, socklen_t addrlen);
393 int_t connect(int_t s, const struct sockaddr *addr, socklen_t addrlen);
394 int_t listen(int_t s, int_t backlog);
395 int_t accept(int_t s, struct sockaddr *addr, socklen_t *addrlen);
396 int_t send(int_t s, const void *data, size_t length, int_t flags);
397 
398 int_t sendto(int_t s, const void *data, size_t length, int_t flags,
399  const struct sockaddr *addr, socklen_t addrlen);
400 
401 int_t recv(int_t s, void *data, size_t size, int_t flags);
402 
403 int_t recvfrom(int_t s, void *data, size_t size, int_t flags,
404  struct sockaddr *addr, socklen_t *addrlen);
405 
406 int_t getsockname(int_t s, struct sockaddr *addr, socklen_t *addrlen);
407 int_t getpeername(int_t s, struct sockaddr *addr, socklen_t *addrlen);
408 
409 int_t setsockopt(int_t s, int_t level, int_t optname, const void *optval,
410  socklen_t optlen);
411 
412 int_t getsockopt(int_t s, int_t level, int_t optname, void *optval,
413  socklen_t *optlen);
414 
415 int_t ioctlsocket(int_t s, uint32_t cmd, void *arg);
416 int_t fcntl(int_t s, int_t cmd, void *arg);
417 
418 int_t shutdown(int_t s, int_t how);
420 
421 int_t select(int_t nfds, fd_set *readfds, fd_set *writefds,
422  fd_set *exceptfds, const struct timeval *timeout);
423 
424 int_t gethostname(char_t *name, size_t len);
425 struct hostent *gethostbyname(const char_t *name);
426 
427 struct hostent *gethostbyname_r(const char_t *name, struct hostent *result,
428  char_t *buf, size_t buflen, int_t *h_errnop);
429 
430 int_t getaddrinfo(const char_t *node, const char_t *service,
431  const struct addrinfo *hints, struct addrinfo **res);
432 
433 void freeaddrinfo(struct addrinfo *res);
434 
435 int_t getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
436  char_t *host, size_t hostlen, char_t *serv, size_t servlen, int flags);
437 
438 in_addr_t inet_addr(const char_t *cp);
439 
440 int_t inet_aton(const char_t *cp, struct in_addr *inp);
441 const char_t *inet_ntoa(struct in_addr in);
442 const char_t *inet_ntoa_r(struct in_addr in, char_t *buf, socklen_t buflen);
443 
444 int_t inet_pton(int_t af, const char_t *src, void *dst);
445 const char_t *inet_ntop(int_t af, const void *src, char_t *dst, socklen_t size);
446 
447 //C++ guard
448 #ifdef __cplusplus
449 }
450 #endif
451 
452 #endif
453 #endif
int_t listen(int_t s, int_t backlog)
Place a socket in the listening state.
Definition: bsd_socket.c:316
int_t socket(int_t family, int_t type, int_t protocol)
Create a socket that is bound to a specific transport service provider.
Definition: bsd_socket.c:63
uint8_t length
Definition: coap_common.h:193
struct sockaddr * ai_addr
Definition: bsd_socket.h:353
int_t getaddrinfo(const char_t *node, const char_t *service, const struct addrinfo *hints, struct addrinfo **res)
Convert host and service names to socket address.
Definition: bsd_socket.c:2163
struct addrinfo * PADDRINFO
int_t socklen_t
Length type.
Definition: bsd_socket.h:253
int_t fcntl(int_t s, int_t cmd, void *arg)
Perform specific operation.
Definition: bsd_socket.c:1696
uint32_t in_addr_t
IPv4 address.
Definition: bsd_socket.h:260
uint8_t sin_zero[8]
Definition: bsd_socket.h:293
struct hostent * gethostbyname_r(const char_t *name, struct hostent *result, char_t *buf, size_t buflen, int_t *h_errnop)
Host name resolution (reentrant version)
Definition: bsd_socket.c:2089
struct in6_addr IN6_ADDR
Structure that represents an IPv6 address.
int_t send(int_t s, const void *data, size_t length, int_t flags)
Send data to a connected socket.
Definition: bsd_socket.c:451
uint8_t data[]
Definition: ethernet.h:220
struct in_addr IN_ADDR
Structure that represents an IPv4 address.
signed int int_t
Definition: compiler_port.h:44
uint16_t sin_family
Definition: bsd_socket.h:290
int_t closesocket(int_t s)
The closesocket function closes an existing socket.
Definition: bsd_socket.c:1807
const char_t * inet_ntoa_r(struct in_addr in, char_t *buf, socklen_t buflen)
Convert a binary IPv4 address to dot-decimal notation (reentrant version)
Definition: bsd_socket.c:2612
const struct in6_addr in6addr_any
Definition: bsd_socket.c:47
Set of sockets.
Definition: bsd_socket.h:324
struct timeval * PTIMEVAL
struct in6_addr sin6_addr
Definition: bsd_socket.h:315
const char_t * inet_ntop(int_t af, const void *src, char_t *dst, socklen_t size)
Convert an IPv4 or IPv6 address from binary to text.
Definition: bsd_socket.c:2712
struct sockaddr_in6 SOCKADDR_IN6
IPv6 address information.
int_t ioctlsocket(int_t s, uint32_t cmd, void *arg)
Control the I/O mode of a socket.
Definition: bsd_socket.c:1596
char_t name[]
int_t ai_flags
Definition: bsd_socket.h:348
const uint8_t res[]
IPv4 address information.
Definition: bsd_socket.h:289
void socketFdClr(fd_set *fds, int_t s)
Remove a descriptor from an existing set.
uint8_t h_addr[16]
Definition: bsd_socket.h:338
uint8_t level
Definition: tls.h:1785
void socketFdSet(fd_set *fds, int_t s)
Add a descriptor to an existing set.
Structure that represents an IPv6 address.
Definition: bsd_socket.h:302
int_t getsockname(int_t s, struct sockaddr *addr, socklen_t *addrlen)
Retrieves the local name for a socket.
Definition: bsd_socket.c:767
uint16_t sa_family
Definition: bsd_socket.h:269
struct timeval TIMEVAL
Timeout structure.
int32_t tv_sec
Definition: bsd_socket.h:367
int_t fd_count
Definition: bsd_socket.h:325
struct in_addr * PIN_ADDR
Socket address.
Definition: bsd_socket.h:268
char_t type
int_t recvfrom(int_t s, void *data, size_t size, int_t flags, struct sockaddr *addr, socklen_t *addrlen)
Receive a datagram.
Definition: bsd_socket.c:667
int_t ai_protocol
Definition: bsd_socket.h:351
IPv6 address information.
Definition: bsd_socket.h:312
uint8_t protocol
Structure that represents an IPv4 address.
Definition: bsd_socket.h:279
uint16_t h_length
Definition: bsd_socket.h:337
void socketFdZero(fd_set *fds)
Initializes a descriptor set.
struct sockaddr_in SOCKADDR_IN
IPv4 address information.
uint8_t sa_data[14]
Definition: bsd_socket.h:270
struct sockaddr_in * PSOCKADDR_IN
struct hostent HOSTENT
Information about a given host.
int_t bind(int_t s, const struct sockaddr *addr, socklen_t addrlen)
Associate a local address with a socket.
Definition: bsd_socket.c:105
in_addr_t inet_addr(const char_t *cp)
Convert a dot-decimal string into binary data in network byte order.
Definition: bsd_socket.c:2526
struct hostent * PHOSTENT
int_t select(int_t nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout)
Determine the status of one or more sockets.
Definition: bsd_socket.c:1849
int_t fd_array[FD_SETSIZE]
Definition: bsd_socket.h:326
const struct in6_addr in6addr_loopback
Definition: bsd_socket.c:50
uint16_t sin6_family
Definition: bsd_socket.h:313
int32_t tv_usec
Definition: bsd_socket.h:368
socklen_t ai_addrlen
Definition: bsd_socket.h:352
struct addrinfo ADDRINFO
Information about address of a service provider.
int_t gethostname(char_t *name, size_t len)
Get system host name.
Definition: bsd_socket.c:2025
struct sockaddr * PSOCKADDR
in_addr_t s_addr
Definition: bsd_socket.h:280
uint8_t flags
Definition: tcp.h:349
int_t connect(int_t s, const struct sockaddr *addr, socklen_t addrlen)
Establish a connection to a specified socket.
Definition: bsd_socket.c:200
char char_t
Definition: compiler_port.h:43
struct in6_addr * PIN6_ADDR
Information about a given host.
Definition: bsd_socket.h:335
int_t socketFdIsSet(fd_set *fds, int_t s)
Check whether a descriptor is set.
int_t getnameinfo(const struct sockaddr *addr, socklen_t addrlen, char_t *host, size_t hostlen, char_t *serv, size_t servlen, int flags)
Convert a socket address to a corresponding host and service.
Definition: bsd_socket.c:2430
const char_t * inet_ntoa(struct in_addr in)
Convert a binary IPv4 address to dot-decimal notation.
Definition: bsd_socket.c:2595
struct sockaddr SOCKADDR
Socket address.
void freeaddrinfo(struct addrinfo *res)
Free socket address structures.
Definition: bsd_socket.c:2401
Timeout structure.
Definition: bsd_socket.h:366
uint16_t sin_port
Definition: bsd_socket.h:291
int_t accept(int_t s, struct sockaddr *addr, socklen_t *addrlen)
Permit an incoming connection attempt on a socket.
Definition: bsd_socket.c:354
uint16_t sin6_port
Definition: bsd_socket.h:314
struct in_addr sin_addr
Definition: bsd_socket.h:292
#define FD_SET(s, fds)
Definition: bsd_socket.h:382
int_t ai_socktype
Definition: bsd_socket.h:350
uint8_t s
int_t inet_pton(int_t af, const char_t *src, void *dst)
Convert an IPv4 or IPv6 address from text to binary form.
Definition: bsd_socket.c:2640
int_t setsockopt(int_t s, int_t level, int_t optname, const void *optval, socklen_t optlen)
The setsockopt function sets a socket option.
Definition: bsd_socket.c:957
Ipv4Addr addr
Definition: nbns_common.h:121
int_t inet_aton(const char_t *cp, struct in_addr *inp)
Convert a dot-decimal string into binary form.
Definition: bsd_socket.c:2560
struct hostent * gethostbyname(const char_t *name)
Host name resolution.
Definition: bsd_socket.c:2066
int_t sendto(int_t s, const void *data, size_t length, int_t flags, const struct sockaddr *addr, socklen_t addrlen)
Send a datagram to a specific destination.
Definition: bsd_socket.c:510
int_t ai_family
Definition: bsd_socket.h:349
int_t recv(int_t s, void *data, size_t size, int_t flags)
Receive data from a connected socket.
Definition: bsd_socket.c:618
#define FD_SETSIZE
Definition: bsd_socket.h:43
RTOS abstraction layer.
struct fd_set * PFD_SET
int_t getsockopt(int_t s, int_t level, int_t optname, void *optval, socklen_t *optlen)
The getsockopt function retrieves a socket option.
Definition: bsd_socket.c:1262
int_t getpeername(int_t s, struct sockaddr *addr, socklen_t *addrlen)
Retrieves the address of the peer to which a socket is connected.
Definition: bsd_socket.c:861
struct sockaddr_in6 * PSOCKADDR_IN6
Information about address of a service provider.
Definition: bsd_socket.h:347
uint8_t s6_addr[16]
Definition: bsd_socket.h:303
struct fd_set fd_set
Set of sockets.
int_t shutdown(int_t s, int_t how)
The shutdown function disables sends or receives on a socket.
Definition: bsd_socket.c:1771
uint16_t h_addrtype
Definition: bsd_socket.h:336
struct addrinfo * ai_next
Definition: bsd_socket.h:355
char_t * ai_canonname
Definition: bsd_socket.h:354