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  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _BSD_SOCKET_H
30 #define _BSD_SOCKET_H
31 
32 //BSD socket support
33 #ifndef BSD_SOCKET_SUPPORT
34  #define BSD_SOCKET_SUPPORT ENABLED
35 #elif (BSD_SOCKET_SUPPORT != ENABLED && BSD_SOCKET_SUPPORT != DISABLED)
36  #error BSD_SOCKET_SUPPORT parameter is not valid
37 #endif
38 
39 //Keil RTX port?
40 #if defined(USE_RTX) && !defined(RTX_CUSTOM_HEADER)
41 
42 //No support for BSD sockets
43 #undef BSD_SOCKET_SUPPORT
44 #define BSD_SOCKET_SUPPORT DISABLED
45 
46 //Windows port?
47 #elif defined(_WIN32) && !defined(_DONT_USE_WINSOCK)
48 
49 //Undefine conflicting definitions
50 #undef htons
51 #undef htonl
52 #undef ntohs
53 #undef ntohl
54 
55 //Dependencies
56 #include <winsock2.h>
57 
58 #elif (BSD_SOCKET_SUPPORT == ENABLED)
59 
60 //Dependencies
61 #include "os_port.h"
62 
63 //Address families
64 #define AF_INET 2
65 #define AF_INET6 10
66 #define AF_PACKET 17
67 
68 //Socket types
69 #define SOCK_STREAM 1
70 #define SOCK_DGRAM 2
71 #define SOCK_RAW 3
72 
73 //IP protocol identifiers
74 #define IPPROTO_ICMP 1
75 #define IPPROTO_IGMP 2
76 #define IPPROTO_TCP 6
77 #define IPPROTO_UDP 17
78 #define IPPROTO_ICMPV6 58
79 
80 //Ethernet protocol identifiers
81 #define ETH_P_ALL 0x0000
82 #define ETH_P_IP 0x0800
83 #define ETH_P_ARP 0x0806
84 #define ETH_P_IPV6 0x86DD
85 
86 //Option levels
87 #define SOL_SOCKET 0xFFFF
88 
89 //Common addresses
90 #define INADDR_ANY 0x00000000
91 #define INADDR_LOOPBACK 0x7F000001
92 #define INADDR_BROADCAST 0xFFFFFFFF
93 
94 //Flags used by I/O functions
95 #define MSG_PEEK 0x02
96 #define MSG_DONTROUTE 0x04
97 #define MSG_WAITALL 0x08
98 #define MSG_DONTWAIT 0x01
99 
100 //Flags used by shutdown function
101 #define SD_RECEIVE 0
102 #define SD_SEND 1
103 #define SD_BOTH 2
104 
105 //Socket level options
106 #define SO_REUSEADDR 0x0004
107 #define SO_KEEPALIVE 0x0008
108 #define SO_DONTROUTE 0x0010
109 #define SO_BROADCAST 0x0020
110 #define SO_LINGER 0x0080
111 #define SO_SNDBUF 0x1001
112 #define SO_RCVBUF 0x1002
113 #define SO_SNDTIMEO 0x1005
114 #define SO_RCVTIMEO 0x1006
115 #define SO_ERROR 0x1007
116 #define SO_TYPE 0x1008
117 #define SO_MAX_MSG_SIZE 0x2003
118 #define SO_BINDTODEVICE 0x3000
119 
120 //TCP level options
121 #define TCP_NODELAY 0x0001
122 
123 //IOCTL commands
124 #define FIONREAD 0x400466FF
125 #define FIONBIO 0x800466FE
126 
127 //FCNTL commands
128 #define F_GETFL 3
129 #define F_SETFL 4
130 
131 //FCNTL flags
132 #define O_NONBLOCK 0x0004
133 
134 //Status codes
135 #define SOCKET_SUCCESS 0
136 #define SOCKET_ERROR (-1)
137 
138 //Error codes
139 #define EAGAIN 11
140 #define EWOULDBLOCK 11
141 #define EFAULT 14
142 #define EINVAL 22
143 #define ENOPROTOOPT 92
144 #define ECONNRESET 104
145 #define EISCONN 106
146 #define ENOTCONN 107
147 #define ESHUTDOWN 108
148 #define ECONNREFUSED 111
149 
150 //Return codes
151 #define INADDR_NONE ((in_addr_t) (-1))
152 
153 //Macros for manipulating and checking descriptor sets
154 #define FD_SETSIZE 8
155 #define FD_ZERO(fds) selectFdZero(fds)
156 #define FD_SET(s, fds) selectFdSet(fds, s)
157 #define FD_CLR(s, fds) selectFdClr(fds, s)
158 #define FD_ISSET(s, fds) selectFdIsSet(fds, s)
159 
160 //C++ guard
161 #ifdef __cplusplus
162  extern "C" {
163 #endif
164 
165 
166 /**
167  * @brief Length type
168  **/
169 
170 typedef int_t socklen_t;
171 
172 
173 /**
174  * @brief IPv4 address
175  **/
176 
177 typedef uint32_t in_addr_t;
178 
179 
180 /**
181  * @brief Socket address
182  **/
183 
184 typedef struct sockaddr
185 {
186  uint16_t sa_family;
187  uint8_t sa_data[14];
188 } sockaddr;
189 
190 
191 /**
192  * @brief Structure that represents an IPv4 address
193  **/
194 
195 typedef struct in_addr
196 {
198 } in_addr;
199 
200 
201 /**
202  * @brief IPv4 address information
203  **/
204 
205 typedef struct sockaddr_in
206 {
207  uint16_t sin_family;
208  uint16_t sin_port;
210  uint8_t sin_zero[8];
211 } sockaddr_in;
212 
213 
214 /**
215  * @brief Structure that represents an IPv6 address
216  **/
217 
218 typedef struct in6_addr
219 {
220  uint8_t s6_addr[16];
221 } in6_addr;
222 
223 
224 /**
225  * @brief IPv6 address information
226  **/
227 
228 typedef struct sockaddr_in6
229 {
230  uint16_t sin6_family;
231  uint16_t sin6_port;
233 } sockaddr_in6;
234 
235 
236 /**
237  * @brief Set of sockets
238  **/
239 
240 typedef struct fd_set
241 {
244 } fd_set;
245 
246 
247 /**
248  * @brief Timeout structure
249  **/
250 
251 typedef struct timeval
252 {
253  int32_t tv_sec;
254  int32_t tv_usec;
255 } timeval;
256 
257 
258 /**
259  * @brief Information about a given host
260  **/
261 
262 typedef struct hostent
263 {
264  uint16_t h_addrtype;
265  uint16_t h_length;
266  uint8_t h_addr[16];
267 } hostent;
268 
269 
270 //BSD socket related constants
271 extern const in6_addr in6addr_any;
272 extern const in6_addr in6addr_loopback;
273 
274 //BSD socket API
276 int_t bind(int_t s, const sockaddr *addr, socklen_t addrlen);
277 int_t connect(int_t s, const sockaddr *addr, socklen_t addrlen);
278 int_t listen(int_t s, int_t backlog);
279 int_t accept(int_t s, sockaddr *addr, socklen_t *addrlen);
280 int_t send(int_t s, const void *data, size_t length, int_t flags);
281 
282 int_t sendto(int_t s, const void *data, size_t length,
283  int_t flags, const sockaddr *addr, socklen_t addrlen);
284 
285 int_t recv(int_t s, void *data, size_t size, int_t flags);
286 
287 int_t recvfrom(int_t s, void *data, size_t size,
288  int_t flags, sockaddr *addr, socklen_t *addrlen);
289 
292 
293 int_t setsockopt(int_t s, int_t level, int_t optname,
294  const void *optval, socklen_t optlen);
295 
296 int_t getsockopt(int_t s, int_t level, int_t optname,
297  void *optval, socklen_t *optlen);
298 
299 int_t ioctlsocket(int_t s, uint32_t cmd, void *arg);
300 int_t fcntl(int_t s, int_t cmd, void *arg);
301 
302 int_t shutdown(int_t s, int_t how);
304 
305 int_t select(int_t nfds, fd_set *readfds, fd_set *writefds,
306  fd_set *exceptfds, const timeval *timeout);
307 
308 void selectFdZero(fd_set *fds);
309 void selectFdSet(fd_set *fds, int_t s);
310 void selectFdClr(fd_set *fds, int_t s);
312 
313 int_t gethostbyname(const char_t *name, hostent *info);
314 
315 in_addr_t inet_addr(const char_t *cp);
316 
317 int_t inet_aton(const char_t *cp, in_addr *inp);
318 const char_t *inet_ntoa(in_addr in, char_t *cp);
319 
320 int_t inet_pton(int_t af, const char_t *src, void *dst);
321 const char_t *inet_ntop(int_t af, const void *src, char_t *dst, socklen_t size);
322 
323 //C++ guard
324 #ifdef __cplusplus
325  }
326 #endif
327 
328 #endif
329 #endif
struct sockaddr sockaddr
Socket address.
Set of sockets.
Definition: bsd_socket.h:240
int_t closesocket(int_t s)
The closesocket function closes an existing socket.
Definition: bsd_socket.c:1494
struct in_addr in_addr
Structure that represents an IPv4 address.
char char_t
Definition: compiler_port.h:41
uint8_t flags
Definition: tcp.h:312
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:540
int_t selectFdIsSet(fd_set *fds, int_t s)
Check whether a descriptor is set.
Definition: bsd_socket.c:1776
int_t listen(int_t s, int_t backlog)
Place a socket in the listening state.
Definition: bsd_socket.c:351
uint16_t sin_family
Definition: bsd_socket.h:207
uint16_t sin6_port
Definition: bsd_socket.h:231
uint16_t h_addrtype
Definition: bsd_socket.h:264
Information about a given host.
Definition: bsd_socket.h:262
uint16_t h_length
Definition: bsd_socket.h:265
uint8_t h_addr[16]
Definition: bsd_socket.h:266
IPv4 address information.
Definition: bsd_socket.h:205
uint8_t sin_zero[8]
Definition: bsd_socket.h:210
char_t type
int_t shutdown(int_t s, int_t how)
The shutdown function disables sends or receives on a socket.
Definition: bsd_socket.c:1458
int_t ioctlsocket(int_t s, uint32_t cmd, void *arg)
Control the I/O mode of a socket.
Definition: bsd_socket.c:1306
uint8_t level
Definition: tls.h:1696
int_t getsockname(int_t s, sockaddr *addr, socklen_t *addrlen)
Retrieves the local name for a socket.
Definition: bsd_socket.c:789
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:694
struct timeval timeval
Timeout structure.
const in6_addr in6addr_any
Definition: bsd_socket.c:43
uint16_t sin_port
Definition: bsd_socket.h:208
void selectFdClr(fd_set *fds, int_t s)
Remove a descriptor from an existing set.
Definition: bsd_socket.c:1744
int_t bind(int_t s, const sockaddr *addr, socklen_t addrlen)
Associate a local address with a socket.
Definition: bsd_socket.c:148
int_t socklen_t
Length type.
Definition: bsd_socket.h:170
const in6_addr in6addr_loopback
Definition: bsd_socket.c:46
uint16_t sa_family
Definition: bsd_socket.h:186
struct hostent hostent
Information about a given host.
uint8_t s6_addr[16]
Definition: bsd_socket.h:220
#define FD_SETSIZE
Definition: bsd_socket.h:154
signed int int_t
Definition: compiler_port.h:42
char_t name[]
Structure that represents an IPv6 address.
Definition: bsd_socket.h:218
uint32_t in_addr_t
IPv4 address.
Definition: bsd_socket.h:177
Socket address.
Definition: bsd_socket.h:184
RTOS abstraction layer.
in6_addr sin6_addr
Definition: bsd_socket.h:232
in_addr sin_addr
Definition: bsd_socket.h:209
uint8_t sa_data[14]
Definition: bsd_socket.h:187
Structure that represents an IPv4 address.
Definition: bsd_socket.h:195
Timeout structure.
Definition: bsd_socket.h:251
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:881
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:1125
in_addr_t s_addr
Definition: bsd_socket.h:197
int32_t tv_sec
Definition: bsd_socket.h:253
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:1857
int_t accept(int_t s, sockaddr *addr, socklen_t *addrlen)
Permit an incoming connection attempt on a socket.
Definition: bsd_socket.c:389
void selectFdSet(fd_set *fds, int_t s)
Add a descriptor to an existing set.
Definition: bsd_socket.c:1715
uint8_t data[]
Definition: dtls_misc.h:167
int_t connect(int_t s, const sockaddr *addr, socklen_t addrlen)
Establish a connection to a specified socket.
Definition: bsd_socket.c:249
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:2022
struct fd_set fd_set
Set of sockets.
int_t fd_count
Definition: bsd_socket.h:242
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:1950
int_t inet_aton(const char_t *cp, in_addr *inp)
Convert a dot-decimal string into binary form.
Definition: bsd_socket.c:1891
IPv6 address information.
Definition: bsd_socket.h:228
struct sockaddr_in sockaddr_in
IPv4 address information.
const char_t * inet_ntoa(in_addr in, char_t *cp)
Convert a binary IPv4 address to dot-decimal notation.
Definition: bsd_socket.c:1927
void selectFdZero(fd_set *fds)
Initializes a descriptor set.
Definition: bsd_socket.c:1702
uint8_t protocol
uint8_t length
Definition: dtls_misc.h:140
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:1532
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:975
struct sockaddr_in6 sockaddr_in6
IPv6 address information.
int_t fcntl(int_t s, int_t cmd, void *arg)
Perform specific operation.
Definition: bsd_socket.c:1383
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:106
uint16_t sin6_family
Definition: bsd_socket.h:230
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:481
Ipv4Addr addr
Definition: nbns_common.h:119
int32_t tv_usec
Definition: bsd_socket.h:254
struct in6_addr in6_addr
Structure that represents an IPv6 address.
int_t recv(int_t s, void *data, size_t size, int_t flags)
Receive data from a connected socket.
Definition: bsd_socket.c:645
int_t gethostbyname(const char_t *name, hostent *info)
Retrieve host address corresponding to a host name.
Definition: bsd_socket.c:1801
int_t fd_array[FD_SETSIZE]
Definition: bsd_socket.h:243