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-2019 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 1.9.6
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 //Keil RTX port?
42 #if defined(USE_RTX) && !defined(RTX_CUSTOM_HEADER)
43 
44 //No support for BSD sockets
45 #undef BSD_SOCKET_SUPPORT
46 #define BSD_SOCKET_SUPPORT DISABLED
47 
48 //Windows port?
49 #elif defined(_WIN32) && !defined(_DONT_USE_WINSOCK)
50 
51 //Undefine conflicting definitions
52 #undef htons
53 #undef htonl
54 #undef ntohs
55 #undef ntohl
56 
57 //Dependencies
58 #include <winsock2.h>
59 
60 #elif (BSD_SOCKET_SUPPORT == ENABLED)
61 
62 //Dependencies
63 #include "os_port.h"
64 
65 //Address families
66 #define AF_INET 2
67 #define AF_INET6 10
68 #define AF_PACKET 17
69 
70 //Socket types
71 #define SOCK_STREAM 1
72 #define SOCK_DGRAM 2
73 #define SOCK_RAW 3
74 
75 //IP protocol identifiers
76 #define IPPROTO_IP 0
77 #define IPPROTO_ICMP 1
78 #define IPPROTO_IGMP 2
79 #define IPPROTO_TCP 6
80 #define IPPROTO_UDP 17
81 #define IPPROTO_ICMPV6 58
82 
83 //Ethernet protocol identifiers
84 #define ETH_P_ALL 0x0000
85 #define ETH_P_IP 0x0800
86 #define ETH_P_ARP 0x0806
87 #define ETH_P_IPV6 0x86DD
88 
89 //Option levels
90 #define SOL_SOCKET 0xFFFF
91 
92 //Common addresses
93 #define INADDR_ANY 0x00000000
94 #define INADDR_LOOPBACK 0x7F000001
95 #define INADDR_BROADCAST 0xFFFFFFFF
96 
97 //Flags used by I/O functions
98 #define MSG_PEEK 0x02
99 #define MSG_DONTROUTE 0x04
100 #define MSG_WAITALL 0x08
101 #define MSG_DONTWAIT 0x01
102 
103 //Flags used by shutdown function
104 #define SD_RECEIVE 0
105 #define SD_SEND 1
106 #define SD_BOTH 2
107 
108 //Socket level options
109 #define SO_REUSEADDR 0x0004
110 #define SO_KEEPALIVE 0x0008
111 #define SO_DONTROUTE 0x0010
112 #define SO_BROADCAST 0x0020
113 #define SO_LINGER 0x0080
114 #define SO_SNDBUF 0x1001
115 #define SO_RCVBUF 0x1002
116 #define SO_SNDTIMEO 0x1005
117 #define SO_RCVTIMEO 0x1006
118 #define SO_ERROR 0x1007
119 #define SO_TYPE 0x1008
120 #define SO_MAX_MSG_SIZE 0x2003
121 #define SO_BINDTODEVICE 0x3000
122 
123 //IP level options
124 #define IP_TTL 2
125 #define IP_MULTICAST_TTL 33
126 
127 //TCP level options
128 #define TCP_NODELAY 0x0001
129 
130 //IOCTL commands
131 #define FIONREAD 0x400466FF
132 #define FIONBIO 0x800466FE
133 
134 //FCNTL commands
135 #define F_GETFL 3
136 #define F_SETFL 4
137 
138 //FCNTL flags
139 #define O_NONBLOCK 0x0004
140 
141 //Status codes
142 #define SOCKET_SUCCESS 0
143 #define SOCKET_ERROR (-1)
144 
145 //Error codes
146 #define EAGAIN 11
147 #define EWOULDBLOCK 11
148 #define EFAULT 14
149 #define EINVAL 22
150 #define ENOPROTOOPT 92
151 #define ECONNRESET 104
152 #define EISCONN 106
153 #define ENOTCONN 107
154 #define ESHUTDOWN 108
155 #define ECONNREFUSED 111
156 
157 //Return codes
158 #define INADDR_NONE ((in_addr_t) (-1))
159 
160 //Macros for manipulating and checking descriptor sets
161 #define FD_SETSIZE 8
162 #define FD_ZERO(fds) selectFdZero(fds)
163 #define FD_SET(s, fds) selectFdSet(fds, s)
164 #define FD_CLR(s, fds) selectFdClr(fds, s)
165 #define FD_ISSET(s, fds) selectFdIsSet(fds, s)
166 
167 //C++ guard
168 #ifdef __cplusplus
169 extern "C" {
170 #endif
171 
172 
173 /**
174  * @brief Length type
175  **/
176 
177 typedef int_t socklen_t;
178 
179 
180 /**
181  * @brief IPv4 address
182  **/
183 
184 typedef uint32_t in_addr_t;
185 
186 
187 /**
188  * @brief Socket address
189  **/
190 
191 typedef struct sockaddr
192 {
193  uint16_t sa_family;
194  uint8_t sa_data[14];
195 } sockaddr;
196 
197 
198 /**
199  * @brief Structure that represents an IPv4 address
200  **/
201 
202 typedef struct in_addr
203 {
205 } in_addr;
206 
207 
208 /**
209  * @brief IPv4 address information
210  **/
211 
212 typedef struct sockaddr_in
213 {
214  uint16_t sin_family;
215  uint16_t sin_port;
217  uint8_t sin_zero[8];
218 } sockaddr_in;
219 
220 
221 /**
222  * @brief Structure that represents an IPv6 address
223  **/
224 
225 typedef struct in6_addr
226 {
227  uint8_t s6_addr[16];
228 } in6_addr;
229 
230 
231 /**
232  * @brief IPv6 address information
233  **/
234 
235 typedef struct sockaddr_in6
236 {
237  uint16_t sin6_family;
238  uint16_t sin6_port;
240 } sockaddr_in6;
241 
242 
243 /**
244  * @brief Set of sockets
245  **/
246 
247 typedef struct fd_set
248 {
251 } fd_set;
252 
253 
254 /**
255  * @brief Timeout structure
256  **/
257 
258 typedef struct timeval
259 {
260  int32_t tv_sec;
261  int32_t tv_usec;
262 } timeval;
263 
264 
265 /**
266  * @brief Information about a given host
267  **/
268 
269 typedef struct hostent
270 {
271  uint16_t h_addrtype;
272  uint16_t h_length;
273  uint8_t h_addr[16];
274 } hostent;
275 
276 
277 //BSD socket related constants
278 extern const in6_addr in6addr_any;
279 extern const in6_addr in6addr_loopback;
280 
281 //BSD socket API
283 int_t bind(int_t s, const sockaddr *addr, socklen_t addrlen);
284 int_t connect(int_t s, const sockaddr *addr, socklen_t addrlen);
285 int_t listen(int_t s, int_t backlog);
286 int_t accept(int_t s, sockaddr *addr, socklen_t *addrlen);
287 int_t send(int_t s, const void *data, size_t length, int_t flags);
288 
289 int_t sendto(int_t s, const void *data, size_t length,
290  int_t flags, const sockaddr *addr, socklen_t addrlen);
291 
292 int_t recv(int_t s, void *data, size_t size, int_t flags);
293 
294 int_t recvfrom(int_t s, void *data, size_t size,
295  int_t flags, sockaddr *addr, socklen_t *addrlen);
296 
299 
300 int_t setsockopt(int_t s, int_t level, int_t optname,
301  const void *optval, socklen_t optlen);
302 
303 int_t getsockopt(int_t s, int_t level, int_t optname,
304  void *optval, socklen_t *optlen);
305 
306 int_t ioctlsocket(int_t s, uint32_t cmd, void *arg);
307 int_t fcntl(int_t s, int_t cmd, void *arg);
308 
309 int_t shutdown(int_t s, int_t how);
311 
312 int_t select(int_t nfds, fd_set *readfds, fd_set *writefds,
313  fd_set *exceptfds, const timeval *timeout);
314 
315 void selectFdZero(fd_set *fds);
316 void selectFdSet(fd_set *fds, int_t s);
317 void selectFdClr(fd_set *fds, int_t s);
319 
320 int_t gethostbyname(const char_t *name, hostent *info);
321 
322 in_addr_t inet_addr(const char_t *cp);
323 
324 int_t inet_aton(const char_t *cp, in_addr *inp);
325 const char_t *inet_ntoa(in_addr in, char_t *cp);
326 
327 int_t inet_pton(int_t af, const char_t *src, void *dst);
328 const char_t *inet_ntop(int_t af, const void *src, char_t *dst, socklen_t size);
329 
330 //C++ guard
331 #ifdef __cplusplus
332 }
333 #endif
334 
335 #endif
336 #endif
int_t listen(int_t s, int_t backlog)
Place a socket in the listening state.
Definition: bsd_socket.c:353
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:108
uint8_t length
Definition: dtls_misc.h:149
int_t socklen_t
Length type.
Definition: bsd_socket.h:177
void selectFdZero(fd_set *fds)
Initializes a descriptor set.
Definition: bsd_socket.c:1824
int_t fcntl(int_t s, int_t cmd, void *arg)
Perform specific operation.
Definition: bsd_socket.c:1505
uint32_t in_addr_t
IPv4 address.
Definition: bsd_socket.h:184
uint8_t sin_zero[8]
Definition: bsd_socket.h:217
int_t bind(int_t s, const sockaddr *addr, socklen_t addrlen)
Associate a local address with a socket.
Definition: bsd_socket.c:150
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:483
int_t selectFdIsSet(fd_set *fds, int_t s)
Check whether a descriptor is set.
Definition: bsd_socket.c:1898
signed int int_t
Definition: compiler_port.h:44
const in6_addr in6addr_loopback
Definition: bsd_socket.c:48
uint16_t sin_family
Definition: bsd_socket.h:214
struct timeval timeval
Timeout structure.
int_t closesocket(int_t s)
The closesocket function closes an existing socket.
Definition: bsd_socket.c:1616
Set of sockets.
Definition: bsd_socket.h:247
int_t connect(int_t s, const sockaddr *addr, socklen_t addrlen)
Establish a connection to a specified socket.
Definition: bsd_socket.c:251
int_t sendto(int_t s, const void *data, size_t length, int_t flags, const sockaddr *addr, socklen_t addrlen)
Send a datagram to a specific destination.
Definition: bsd_socket.c:542
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:2144
int_t ioctlsocket(int_t s, uint32_t cmd, void *arg)
Control the I/O mode of a socket.
Definition: bsd_socket.c:1428
char_t name[]
IPv4 address information.
Definition: bsd_socket.h:212
uint8_t h_addr[16]
Definition: bsd_socket.h:273
uint8_t level
Definition: tls.h:1702
struct sockaddr_in sockaddr_in
IPv4 address information.
Structure that represents an IPv6 address.
Definition: bsd_socket.h:225
uint16_t sa_family
Definition: bsd_socket.h:193
int32_t tv_sec
Definition: bsd_socket.h:260
int_t fd_count
Definition: bsd_socket.h:249
Socket address.
Definition: bsd_socket.h:191
char_t type
IPv6 address information.
Definition: bsd_socket.h:235
struct in_addr in_addr
Structure that represents an IPv4 address.
uint8_t protocol
Structure that represents an IPv4 address.
Definition: bsd_socket.h:202
uint16_t h_length
Definition: bsd_socket.h:272
struct fd_set fd_set
Set of sockets.
void selectFdSet(fd_set *fds, int_t s)
Add a descriptor to an existing set.
Definition: bsd_socket.c:1837
void selectFdClr(fd_set *fds, int_t s)
Remove a descriptor from an existing set.
Definition: bsd_socket.c:1866
struct sockaddr_in6 sockaddr_in6
IPv6 address information.
uint8_t sa_data[14]
Definition: bsd_socket.h:194
struct hostent hostent
Information about a given host.
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:1979
const char_t * inet_ntoa(in_addr in, char_t *cp)
Convert a binary IPv4 address to dot-decimal notation.
Definition: bsd_socket.c:2049
int_t recvfrom(int_t s, void *data, size_t size, int_t flags, sockaddr *addr, socklen_t *addrlen)
Receive a datagram.
Definition: bsd_socket.c:696
int_t fd_array[FD_SETSIZE]
Definition: bsd_socket.h:250
uint16_t sin6_family
Definition: bsd_socket.h:237
int32_t tv_usec
Definition: bsd_socket.h:261
struct sockaddr sockaddr
Socket address.
int_t getpeername(int_t s, sockaddr *addr, socklen_t *addrlen)
Retrieves the address of the peer to which a socket is connected.
Definition: bsd_socket.c:883
int_t getsockname(int_t s, sockaddr *addr, socklen_t *addrlen)
Retrieves the local name for a socket.
Definition: bsd_socket.c:791
in_addr_t s_addr
Definition: bsd_socket.h:204
uint8_t flags
Definition: tcp.h:314
char char_t
Definition: compiler_port.h:43
Information about a given host.
Definition: bsd_socket.h:269
Timeout structure.
Definition: bsd_socket.h:258
int_t inet_aton(const char_t *cp, in_addr *inp)
Convert a dot-decimal string into binary form.
Definition: bsd_socket.c:2013
uint16_t sin_port
Definition: bsd_socket.h:215
uint16_t sin6_port
Definition: bsd_socket.h:238
const in6_addr in6addr_any
Definition: bsd_socket.c:45
in_addr sin_addr
Definition: bsd_socket.h:216
int_t accept(int_t s, sockaddr *addr, socklen_t *addrlen)
Permit an incoming connection attempt on a socket.
Definition: bsd_socket.c:391
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:2072
struct in6_addr in6_addr
Structure that represents an IPv6 address.
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:977
Ipv4Addr addr
Definition: nbns_common.h:121
uint8_t data[]
Definition: dtls_misc.h:176
int_t recv(int_t s, void *data, size_t size, int_t flags)
Receive data from a connected socket.
Definition: bsd_socket.c:647
#define FD_SETSIZE
Definition: bsd_socket.h:161
RTOS abstraction layer.
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:1184
int_t gethostbyname(const char_t *name, hostent *info)
Retrieve host address corresponding to a host name.
Definition: bsd_socket.c:1923
in6_addr sin6_addr
Definition: bsd_socket.h:239
uint8_t s6_addr[16]
Definition: bsd_socket.h:227
int_t shutdown(int_t s, int_t how)
The shutdown function disables sends or receives on a socket.
Definition: bsd_socket.c:1580
int_t select(int_t nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const timeval *timeout)
Determine the status of one or more sockets.
Definition: bsd_socket.c:1654
uint16_t h_addrtype
Definition: bsd_socket.h:271