mikroSDK Reference Manual
bsd_socket.h
Go to the documentation of this file.
1
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_IPV6 41
101#define IPPROTO_ESP 50
102#define IPPROTO_AH 51
103#define IPPROTO_ICMPV6 58
104
105//Ethernet protocol identifiers
106#define ETH_P_ALL 0x0000
107#define ETH_P_IP 0x0800
108#define ETH_P_ARP 0x0806
109#define ETH_P_IPV6 0x86DD
110
111//Option levels
112#define SOL_SOCKET 0xFFFF
113
114//Common addresses
115#define INADDR_ANY 0x00000000
116#define INADDR_LOOPBACK 0x7F000001
117#define INADDR_BROADCAST 0xFFFFFFFF
118
119//Flags used by I/O functions
120#define MSG_PEEK 0x0002
121#define MSG_DONTROUTE 0x0004
122#define MSG_CTRUNC 0x0008
123#define MSG_DONTWAIT 0x0040
124#define MSG_WAITALL 0x0100
125
126//Flags used by shutdown function
127#define SD_RECEIVE 0
128#define SD_SEND 1
129#define SD_BOTH 2
130
131//Flags used by shutdown function (alias)
132#define SHUT_RD SD_RECEIVE
133#define SHUT_WR SD_SEND
134#define SHUT_RDWR SD_BOTH
135
136//Socket level options
137#define SO_REUSEADDR 0x0004
138#define SO_KEEPALIVE 0x0008
139#define SO_DONTROUTE 0x0010
140#define SO_BROADCAST 0x0020
141#define SO_LINGER 0x0080
142#define SO_SNDBUF 0x1001
143#define SO_RCVBUF 0x1002
144#define SO_SNDTIMEO 0x1005
145#define SO_RCVTIMEO 0x1006
146#define SO_ERROR 0x1007
147#define SO_TYPE 0x1008
148#define SO_MAX_MSG_SIZE 0x2003
149#define SO_BINDTODEVICE 0x3000
150
151//IP level options
152#define IP_TOS 1
153#define IP_TTL 2
154#define IP_ROUTER_ALERT 5
155#define IP_PKTINFO 8
156#define IP_RECVTTL 12
157#define IP_RECVTOS 13
158#define IP_MULTICAST_IF 32
159#define IP_MULTICAST_TTL 33
160#define IP_MULTICAST_LOOP 34
161#define IP_ADD_MEMBERSHIP 35
162#define IP_DROP_MEMBERSHIP 36
163
164//IPv6 level options
165#define IPV6_UNICAST_HOPS 16
166#define IPV6_MULTICAST_IF 17
167#define IPV6_MULTICAST_HOPS 18
168#define IPV6_MULTICAST_LOOP 19
169#define IPV6_ADD_MEMBERSHIP 20
170#define IPV6_DROP_MEMBERSHIP 21
171#define IPV6_V6ONLY 26
172#define IPV6_PKTINFO 50
173#define IPV6_RECVHOPLIMIT 51
174#define IPV6_HOPLIMIT 52
175#define IPV6_RECVTCLASS 66
176#define IPV6_TCLASS 67
177
178//TCP level options
179#define TCP_NODELAY 0x0001
180#define TCP_MAXSEG 0x0002
181#define TCP_KEEPIDLE 0x0004
182#define TCP_KEEPINTVL 0x0005
183#define TCP_KEEPCNT 0x0006
184
185//IP TOS option
186#define IPTOS_LOWDELAY 0x10
187#define IPTOS_THROUGHPUT 0x08
188#define IPTOS_RELIABILITY 0x04
189
190//IOCTL commands
191#define FIONBIO 126
192#define FIONREAD 127
193#define FIONWRITE 121
194#define FIONSPACE 120
195
196//FCNTL commands
197#define F_GETFL 3
198#define F_SETFL 4
199
200//FCNTL flags
201#define O_NONBLOCK 0x0004
202
203//Flags used by getaddrinfo
204#define AI_PASSIVE 0x01
205#define AI_CANONNAME 0x02
206#define AI_NUMERICHOST 0x04
207#define AI_NUMERICSERV 0x08
208#define AI_ALL 0x10
209#define AI_ADDRCONFIG 0x20
210#define AI_V4MAPPED 0x40
211
212//Flags used by getnameinfo
213#define NI_NOFQDN 0x01
214#define NI_NUMERICHOST 0x02
215#define NI_NAMEREQD 0x04
216#define NI_NUMERICSERV 0x08
217#define NI_DGRAM 0x10
218
219//Return values
220#define SOCKET_SUCCESS 0
221#define SOCKET_ERROR (-1)
222
223//Return values used by getaddrinfo and getnameinfo
224#define EAI_ADDRFAMILY 1
225#define EAI_AGAIN 2
226#define EAI_BADFLAGS 3
227#define EAI_FAIL 4
228#define EAI_FAMILY 5
229#define EAI_MEMORY 6
230#define EAI_NODATA 7
231#define EAI_NONAME 8
232#define EAI_SERVICE 9
233#define EAI_SOCKTYPE 10
234#define EAI_SYSTEM 11
235#define EAI_OVERFLOW 12
236
237//Error codes
238#define EINTR 4
239#define EAGAIN 11
240#define EWOULDBLOCK 11
241#define EFAULT 14
242#define EINVAL 22
243#define EINPROGRESS 36
244#define ETIMEDOUT 60
245#define ENAMETOOLONG 63
246#define ENOPROTOOPT 92
247#define ECONNRESET 104
248#define EISCONN 106
249#define ENOTCONN 107
250#define ESHUTDOWN 108
251#define ECONNREFUSED 111
252
253//Error codes used by gethostbyname_r
254#define NETDB_SUCCESS 0
255#define HOST_NOT_FOUND 1
256#define TRY_AGAIN 2
257#define NO_RECOVERY 3
258#define NO_ADDRESS 4
259
260//Return codes
261#define INADDR_NONE ((in_addr_t) (-1))
262
263//Maximum length for string representation of IPv4 address
264#define INET_ADDRSTRLEN 16
265
266//Maximum length for string representation of IPv6 address
267#define INET6_ADDRSTRLEN 40
268
269//C++ guard
270#ifdef __cplusplus
271extern "C" {
272#endif
273
274
279typedef int_t socklen_t;
280
281
286typedef uint32_t in_addr_t;
287
288
293typedef struct sockaddr
294{
295 uint16_t sa_family;
296 uint8_t sa_data[14];
298
299
304typedef struct sockaddr_storage
305{
306 uint16_t ss_family;
307 uint8_t ss_pad[26];
309
310
315typedef struct in_addr
316{
317 in_addr_t s_addr;
319
320
325typedef struct sockaddr_in
326{
327 uint16_t sin_family;
328 uint16_t sin_port;
329 struct in_addr sin_addr;
330 uint8_t sin_zero[8];
332
333
338typedef struct in6_addr
339{
340 uint8_t s6_addr[16];
342
343
348typedef struct sockaddr_in6
349{
350 uint16_t sin6_family;
351 uint16_t sin6_port;
352 uint32_t sin6_flowinfo;
353 struct in6_addr sin6_addr;
354 uint32_t sin6_scope_id;
356
357
362typedef struct ip_mreq
363{
364 struct in_addr imr_multiaddr;
365 struct in_addr imr_interface;
367
368
373typedef struct ipv6_mreq
374{
375 struct in6_addr ipv6mr_multiaddr;
376 int_t ipv6mr_interface;
378
379
384typedef struct linger
385{
386 int_t l_onoff;
387 int_t l_linger;
389
390
395struct iovec
396{
397 void *iov_base;
398 size_t iov_len;
399};
400
401
406typedef struct msghdr
407{
408 void *msg_name;
409 socklen_t msg_namelen;
410 struct iovec *msg_iov;
411 int_t msg_iovlen;
412 void *msg_control;
413 size_t msg_controllen;
414 int_t msg_flags;
416
417
422typedef struct cmsghdr
423{
424 size_t cmsg_len;
425 int_t cmsg_level;
426 int_t cmsg_type;
428
429
434typedef struct in_pktinfo
435{
436 int ipi_ifindex;
437 struct in_addr ipi_addr;
439
440
445typedef struct in6_pktinfo
446{
447 int ipi6_ifindex;
448 struct in6_addr ipi6_addr;
450
451
456typedef struct fd_set
457{
458 int_t fd_count;
459 int_t fd_array[FD_SETSIZE];
461
462
467typedef struct hostent
468{
469 uint16_t h_addrtype;
470 uint16_t h_length;
471 uint8_t h_addr[16];
473
474
479typedef struct addrinfo
480{
481 int_t ai_flags;
482 int_t ai_family;
483 int_t ai_socktype;
484 int_t ai_protocol;
485 socklen_t ai_addrlen;
486 struct sockaddr *ai_addr;
487 char_t *ai_canonname;
488 struct addrinfo *ai_next;
490
491
492#ifndef _TIMEVAL_DEFINED
493
498typedef struct timeval
499{
500 int32_t tv_sec;
501 int32_t tv_usec;
503
504#endif
505
506
507//Forward declaration of functions
508struct cmsghdr *socketCmsgFirstHdr(struct msghdr *msg);
509struct cmsghdr *socketCmsgNextHdr(struct msghdr *msg, struct cmsghdr *cmsg);
510
511//Macros for manipulating ancillary data object information
512#define CMSG_DATA(cmsg) ((uint8_t *) (cmsg) + sizeof(struct cmsghdr))
513#define CMSG_ALIGN(len) (((len) + sizeof(int_t) - 1) & ~(sizeof(int_t) - 1))
514#define CMSG_SPACE(len) (sizeof(struct cmsghdr) + CMSG_ALIGN(len))
515#define CMSG_LEN(len) (sizeof(struct cmsghdr) + (len))
516#define CMSG_FIRSTHDR(msg) socketCmsgFirstHdr(msg)
517#define CMSG_NXTHDR(msg, cmsg) socketCmsgNextHdr(msg, cmsg)
518
519//Forward declaration of functions
520void socketFdZero(fd_set *fds);
521void socketFdSet(fd_set *fds, int_t s);
522void socketFdClr(fd_set *fds, int_t s);
523int_t socketFdIsSet(fd_set *fds, int_t s);
524
525//Macros for manipulating and checking descriptor sets
526#define FD_ZERO(fds) socketFdZero(fds)
527#define FD_SET(s, fds) socketFdSet(fds, s)
528#define FD_CLR(s, fds) socketFdClr(fds, s)
529#define FD_ISSET(s, fds) socketFdIsSet(fds, s)
530
531//BSD socket related constants
532extern const struct in6_addr in6addr_any;
533extern const struct in6_addr in6addr_loopback;
534
535//BSD socket related functions
536int_t socket(int_t family, int_t type, int_t protocol);
537int_t bind(int_t s, const struct sockaddr *addr, socklen_t addrlen);
538int_t connect(int_t s, const struct sockaddr *addr, socklen_t addrlen);
539int_t listen(int_t s, int_t backlog);
540int_t accept(int_t s, struct sockaddr *addr, socklen_t *addrlen);
541int_t send(int_t s, const void *data, size_t length, int_t flags);
542
543int_t sendto(int_t s, const void *data, size_t length, int_t flags,
544 const struct sockaddr *addr, socklen_t addrlen);
545
546int_t sendmsg(int_t s, struct msghdr *msg, int_t flags);
547
548int_t recv(int_t s, void *data, size_t size, int_t flags);
549
550int_t recvfrom(int_t s, void *data, size_t size, int_t flags,
551 struct sockaddr *addr, socklen_t *addrlen);
552
553int_t recvmsg(int_t s, struct msghdr *msg, int_t flags);
554
555int_t getsockname(int_t s, struct sockaddr *addr, socklen_t *addrlen);
556int_t getpeername(int_t s, struct sockaddr *addr, socklen_t *addrlen);
557
558int_t setsockopt(int_t s, int_t level, int_t optname, const void *optval,
559 socklen_t optlen);
560
561int_t getsockopt(int_t s, int_t level, int_t optname, void *optval,
562 socklen_t *optlen);
563
564int_t ioctlsocket(int_t s, uint32_t cmd, void *arg);
565int_t fcntl(int_t s, int_t cmd, void *arg);
566
567int_t shutdown(int_t s, int_t how);
568int_t closesocket(int_t s);
569
570int_t select(int_t nfds, fd_set *readfds, fd_set *writefds,
571 fd_set *exceptfds, const struct timeval *timeout);
572
573int_t gethostname(char_t *name, size_t len);
574struct hostent *gethostbyname(const char_t *name);
575
576struct hostent *gethostbyname_r(const char_t *name, struct hostent *result,
577 char_t *buf, size_t buflen, int_t *h_errnop);
578
579int_t getaddrinfo(const char_t *node, const char_t *service,
580 const struct addrinfo *hints, struct addrinfo **res);
581
582void freeaddrinfo(struct addrinfo *res);
583
584int_t getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
585 char_t *host, size_t hostlen, char_t *serv, size_t servlen, int flags);
586
587in_addr_t inet_addr(const char_t *cp);
588
589int_t inet_aton(const char_t *cp, struct in_addr *inp);
590const char_t *inet_ntoa(struct in_addr in);
591const char_t *inet_ntoa_r(struct in_addr in, char_t *buf, socklen_t buflen);
592
593int_t inet_pton(int_t af, const char_t *src, void *dst);
594const char_t *inet_ntop(int_t af, const void *src, char_t *dst, socklen_t size);
595
596//C++ guard
597#ifdef __cplusplus
598}
599#endif
600
601#endif
602#endif
int_t socklen_t
Length type.
Definition bsd_socket.h:279
struct cmsghdr CMSGHDR
Ancillary data header.
struct hostent HOSTENT
Information about a given host.
struct in6_addr IN6_ADDR
Structure that represents an IPv6 address.
struct linger LINGER
Linger structure.
struct sockaddr_in SOCKADDR_IN
IPv4 address information.
struct in_addr IN_ADDR
Structure that represents an IPv4 address.
struct msghdr MSGHDR
Message header.
struct sockaddr_in6 SOCKADDR_IN6
IPv6 address information.
struct timeval TIMEVAL
Timeout structure.
struct ipv6_mreq IPV6_MREQ
Multicast group information for IPv6 addresses.
struct ip_mreq IP_MREQ
Multicast group information for IPv4 addresses.
uint32_t in_addr_t
IPv4 address.
Definition bsd_socket.h:286
struct sockaddr_storage SOCKADDR_STORAGE
Socket address storage.
struct in6_pktinfo IN6_PKTINFO
IPv6 packet information.
struct fd_set fd_set
Set of sockets.
struct addrinfo ADDRINFO
Information about address of a service provider.
struct sockaddr SOCKADDR
Socket address.
struct in_pktinfo IN_PKTINFO
IPv4 packet information.
RTOS abstraction layer.
Information about address of a service provider.
Definition bsd_socket.h:480
Ancillary data header.
Definition bsd_socket.h:423
Set of sockets.
Definition bsd_socket.h:457
Information about a given host.
Definition bsd_socket.h:468
Structure that represents an IPv6 address.
Definition bsd_socket.h:339
IPv6 packet information.
Definition bsd_socket.h:446
Structure that represents an IPv4 address.
Definition bsd_socket.h:316
IPv4 packet information.
Definition bsd_socket.h:435
Scatter/gather array.
Definition bsd_socket.h:396
Multicast group information for IPv4 addresses.
Definition bsd_socket.h:363
Multicast group information for IPv6 addresses.
Definition bsd_socket.h:374
Linger structure.
Definition bsd_socket.h:385
Message header.
Definition bsd_socket.h:407
IPv6 address information.
Definition bsd_socket.h:349
IPv4 address information.
Definition bsd_socket.h:326
Socket address storage.
Definition bsd_socket.h:305
Socket address.
Definition bsd_socket.h:294
Timeout structure.
Definition bsd_socket.h:499