1 | /* |
---|
2 | * Portability wrapper around <sys/socket.h> and friends. |
---|
3 | * |
---|
4 | * This header file is the equivalent of: |
---|
5 | * |
---|
6 | * #include <arpa/inet.h> |
---|
7 | * #include <netinet/in.h> |
---|
8 | * #include <netdb.h> |
---|
9 | * #include <sys/socket.h> |
---|
10 | * |
---|
11 | * but also cleans up various messes, mostly related to IPv6 support, and |
---|
12 | * provides a set of portability interfaces that work on both UNIX and |
---|
13 | * Windows. It ensures that inet_aton, inet_ntoa, and inet_ntop are available |
---|
14 | * and properly prototyped. |
---|
15 | * |
---|
16 | * Copyright 2008 Board of Trustees, Leland Stanford Jr. University |
---|
17 | * Copyright (c) 2004, 2005, 2006, 2007 |
---|
18 | * by Internet Systems Consortium, Inc. ("ISC") |
---|
19 | * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
---|
20 | * 2002, 2003 by The Internet Software Consortium and Rich Salz |
---|
21 | * |
---|
22 | * See LICENSE for licensing terms. |
---|
23 | */ |
---|
24 | |
---|
25 | #ifndef PORTABLE_SOCKET_H |
---|
26 | #define PORTABLE_SOCKET_H 1 |
---|
27 | |
---|
28 | #include <config.h> |
---|
29 | #include <portable/macros.h> |
---|
30 | |
---|
31 | #include <errno.h> |
---|
32 | #include <sys/types.h> |
---|
33 | |
---|
34 | /* BSDI needs <netinet/in.h> before <arpa/inet.h>. */ |
---|
35 | #ifdef _WIN32 |
---|
36 | # include <winsock2.h> |
---|
37 | # include <ws2tcpip.h> |
---|
38 | #else |
---|
39 | # include <netinet/in.h> |
---|
40 | # include <arpa/inet.h> |
---|
41 | # include <netdb.h> |
---|
42 | # include <sys/socket.h> |
---|
43 | #endif |
---|
44 | |
---|
45 | /* |
---|
46 | * Pick up definitions of getaddrinfo and getnameinfo if not otherwise |
---|
47 | * available. |
---|
48 | */ |
---|
49 | #include <portable/getaddrinfo.h> |
---|
50 | #include <portable/getnameinfo.h> |
---|
51 | |
---|
52 | BEGIN_DECLS |
---|
53 | |
---|
54 | /* |
---|
55 | * Provide prototypes for inet_aton and inet_ntoa if not prototyped in the |
---|
56 | * system header files since they're occasionally available without proper |
---|
57 | * prototypes. |
---|
58 | */ |
---|
59 | #if !HAVE_DECL_INET_ATON |
---|
60 | extern int inet_aton(const char *, struct in_addr *); |
---|
61 | #endif |
---|
62 | #if !HAVE_DECL_INET_NTOA |
---|
63 | extern const char * inet_ntoa(const struct in_addr); |
---|
64 | #endif |
---|
65 | #if !HAVE_INET_NTOP |
---|
66 | # ifdef _WIN32 |
---|
67 | extern const char * inet_ntop(int, const void *, char *, int); |
---|
68 | # else |
---|
69 | extern const char * inet_ntop(int, const void *, char *, socklen_t) |
---|
70 | __attribute__((__visibility__("hidden"))); |
---|
71 | # endif |
---|
72 | #endif |
---|
73 | |
---|
74 | /* |
---|
75 | * Used for portability to Windows, which requires different functions be |
---|
76 | * called to close sockets, send data to or read from sockets, and get socket |
---|
77 | * errors than the regular functions and variables. |
---|
78 | * |
---|
79 | * socket_init must be called before socket functions are used and |
---|
80 | * socket_shutdown at the end of the program. socket_init may return failure, |
---|
81 | * but this interface doesn't have a way to retrieve the exact error. |
---|
82 | * |
---|
83 | * socket_close, socket_read, and socket_write must be used instead of the |
---|
84 | * standard functions. On Windows, closesocket must be called instead of |
---|
85 | * close for sockets and recv and send must always be used instead of read and |
---|
86 | * write. |
---|
87 | * |
---|
88 | * When reporting errors from socket functions, use socket_errno and |
---|
89 | * socket_strerror instead of errno and strerror. When setting errno to |
---|
90 | * something for socket errors (to preserve errors through close, for |
---|
91 | * example), use socket_set_errno instead of just assigning to errno. |
---|
92 | */ |
---|
93 | #ifdef _WIN32 |
---|
94 | int socket_init(void); |
---|
95 | # define socket_shutdown() WSACleanup() |
---|
96 | # define socket_close(fd) closesocket(fd) |
---|
97 | # define socket_read(fd, b, s) recv((fd), (b), (s), 0) |
---|
98 | # define socket_write(fd, b, s) send((fd), (b), (s), 0) |
---|
99 | # define socket_errno WSAGetLastError() |
---|
100 | # define socket_set_errno(e) WSASetLastError(e) |
---|
101 | const char *socket_strerror(int); |
---|
102 | #else |
---|
103 | # define socket_init() 1 |
---|
104 | # define socket_shutdown() /* empty */ |
---|
105 | # define socket_close(fd) close(fd) |
---|
106 | # define socket_read(fd, b, s) read((fd), (b), (s)) |
---|
107 | # define socket_write(fd, b, s) write((fd), (b), (s)) |
---|
108 | # define socket_errno errno |
---|
109 | # define socket_set_errno(e) errno = (e) |
---|
110 | # define socket_strerror(e) strerror(e) |
---|
111 | #endif |
---|
112 | |
---|
113 | /* Some systems don't define INADDR_LOOPBACK. */ |
---|
114 | #ifndef INADDR_LOOPBACK |
---|
115 | # define INADDR_LOOPBACK 0x7f000001UL |
---|
116 | #endif |
---|
117 | |
---|
118 | /* |
---|
119 | * Defined by RFC 3493, used to store a generic address. Note that this |
---|
120 | * doesn't do the alignment mangling that RFC 3493 does; it's not clear if |
---|
121 | * that needs be added. However, I've not gotten any complaints yet (probably |
---|
122 | * because nearly everyone now has sockaddr_storage). |
---|
123 | */ |
---|
124 | #if !HAVE_STRUCT_SOCKADDR_STORAGE |
---|
125 | # if HAVE_STRUCT_SOCKADDR_SA_LEN |
---|
126 | struct sockaddr_storage { |
---|
127 | unsigned char ss_len; |
---|
128 | unsigned char ss_family; |
---|
129 | unsigned char __padding[128 - 2]; |
---|
130 | }; |
---|
131 | # else |
---|
132 | struct sockaddr_storage { |
---|
133 | unsigned short ss_family; |
---|
134 | unsigned char __padding[128 - 2]; |
---|
135 | }; |
---|
136 | # endif |
---|
137 | #endif |
---|
138 | |
---|
139 | /* |
---|
140 | * RFC 2553 used underscores, so some old implementations may have that |
---|
141 | * instead of the non-uglified names from RFC 3493. |
---|
142 | */ |
---|
143 | #if HAVE_STRUCT_SOCKADDR_STORAGE && !HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY |
---|
144 | # define ss_family __ss_family |
---|
145 | # define ss_len __ss_len |
---|
146 | #endif |
---|
147 | |
---|
148 | /* Fix IN6_ARE_ADDR_EQUAL if required. */ |
---|
149 | #ifdef HAVE_BROKEN_IN6_ARE_ADDR_EQUAL |
---|
150 | # undef IN6_ARE_ADDR_EQUAL |
---|
151 | # define IN6_ARE_ADDR_EQUAL(a, b) \ |
---|
152 | (memcmp((a), (b), sizeof(struct in6_addr)) == 0) |
---|
153 | #endif |
---|
154 | |
---|
155 | /* Define an SA_LEN macro that gives us the length of a sockaddr. */ |
---|
156 | #if !HAVE_SA_LEN |
---|
157 | # if HAVE_STRUCT_SOCKADDR_SA_LEN |
---|
158 | # define SA_LEN(s) ((s)->sa_len) |
---|
159 | # else |
---|
160 | /* Hack courtesy of the USAGI project. */ |
---|
161 | # if HAVE_INET6 |
---|
162 | # define SA_LEN(s) \ |
---|
163 | ((((const struct sockaddr *)(s))->sa_family == AF_INET6) \ |
---|
164 | ? sizeof(struct sockaddr_in6) \ |
---|
165 | : ((((const struct sockaddr *)(s))->sa_family == AF_INET) \ |
---|
166 | ? sizeof(struct sockaddr_in) \ |
---|
167 | : sizeof(struct sockaddr))) |
---|
168 | # else |
---|
169 | # define SA_LEN(s) \ |
---|
170 | ((((const struct sockaddr *)(s))->sa_family == AF_INET) \ |
---|
171 | ? sizeof(struct sockaddr_in) \ |
---|
172 | : sizeof(struct sockaddr)) |
---|
173 | # endif |
---|
174 | # endif /* HAVE_SOCKADDR_LEN */ |
---|
175 | #endif /* !HAVE_SA_LEN_MACRO */ |
---|
176 | |
---|
177 | /* |
---|
178 | * AI_ADDRCONFIG results in an error from getaddrinfo on BSD/OS and possibly |
---|
179 | * other platforms. If configure determined it didn't work, pretend it |
---|
180 | * doesn't exist. |
---|
181 | */ |
---|
182 | #if !defined(HAVE_GETADDRINFO_ADDRCONFIG) && defined(AI_ADDRCONFIG) |
---|
183 | # undef AI_ADDRCONFIG |
---|
184 | #endif |
---|
185 | |
---|
186 | /* |
---|
187 | * POSIX requires AI_ADDRCONFIG and AI_NUMERICSERV, but some implementations |
---|
188 | * don't have them yet. We also may have hidden AI_ADDRCONFIG if it doesn't |
---|
189 | * work. It's only used in a bitwise OR of flags, so defining them to 0 makes |
---|
190 | * them harmlessly go away. |
---|
191 | */ |
---|
192 | #ifndef AI_ADDRCONFIG |
---|
193 | # define AI_ADDRCONFIG 0 |
---|
194 | #endif |
---|
195 | #ifndef AI_NUMERICSERV |
---|
196 | # define AI_NUMERICSERV 0 |
---|
197 | #endif |
---|
198 | |
---|
199 | /* |
---|
200 | * Constants required by the IPv6 API. The buffer size required to hold any |
---|
201 | * nul-terminated text representation of the given address type. |
---|
202 | */ |
---|
203 | #ifndef INET_ADDRSTRLEN |
---|
204 | # define INET_ADDRSTRLEN 16 |
---|
205 | #endif |
---|
206 | #ifndef INET6_ADDRSTRLEN |
---|
207 | # define INET6_ADDRSTRLEN 46 |
---|
208 | #endif |
---|
209 | |
---|
210 | /* |
---|
211 | * This is one of the defined error codes from inet_ntop, but it may not be |
---|
212 | * available on systems too old to have that function. |
---|
213 | */ |
---|
214 | #ifndef EAFNOSUPPORT |
---|
215 | # define EAFNOSUPPORT EDOM |
---|
216 | #endif |
---|
217 | |
---|
218 | END_DECLS |
---|
219 | |
---|
220 | #endif /* !PORTABLE_SOCKET_H */ |
---|