1 /*	$NetBSD: net.h,v 1.7 2014/12/10 04:38:02 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2007, 2013  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 2000, 2001  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Id: net.h,v 1.6 2007/06/19 23:47:23 tbox Exp  */
21 
22 #ifndef LWRES_NET_H
23 #define LWRES_NET_H 1
24 
25 /*****
26  ***** Module Info
27  *****/
28 
29 /*
30  * Basic Networking Types
31  *
32  * This module is responsible for defining the following basic networking
33  * types:
34  *
35  *		struct in_addr
36  *		struct in6_addr
37  *		struct sockaddr
38  *		struct sockaddr_in
39  *		struct sockaddr_in6
40  *
41  * It ensures that the AF_ and PF_ macros are defined.
42  *
43  * It declares ntoh[sl]() and hton[sl]().
44  *
45  * It declares lwres_net_aton(), lwres_net_ntop(), and lwres_net_pton().
46  *
47  * It ensures that INADDR_LOOPBACK, INADDR_ANY and IN6ADDR_ANY_INIT
48  * are defined.
49  */
50 
51 /***
52  *** Imports.
53  ***/
54 
55 /*
56  * Because of some sort of problem in the MS header files, this cannot
57  * be simple "#include <winsock2.h>", because winsock2.h tries to include
58  * windows.h, which then generates an error out of mswsock.h.  _You_
59  * figure it out.
60  */
61 #ifndef _WINSOCKAPI_
62 #define _WINSOCKAPI_   /* Prevent inclusion of winsock.h in windows.h */
63 #endif
64 
65 #include <winsock2.h>
66 #include <sys/types.h>
67 
68 #include <lwres/ipv6.h>
69 #include <lwres/platform.h>	/* Required for LWRES_PLATFORM_*. */
70 
71 #include <lwres/lang.h>
72 
73 #ifndef INADDR_LOOPBACK
74 #define INADDR_LOOPBACK 0x7f000001UL
75 #endif
76 /*
77  * Fix the FD_SET and FD_CLR Macros to properly cast
78  */
79 #undef FD_CLR
80 #define FD_CLR(fd, set) do { \
81     u_int __i; \
82     for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count; __i++) { \
83 	if (((fd_set FAR *)(set))->fd_array[__i] == (SOCKET) fd) { \
84 	    while (__i < ((fd_set FAR *)(set))->fd_count-1) { \
85 		((fd_set FAR *)(set))->fd_array[__i] = \
86 		    ((fd_set FAR *)(set))->fd_array[__i+1]; \
87 		__i++; \
88 	    } \
89 	    ((fd_set FAR *)(set))->fd_count--; \
90 	    break; \
91 	} \
92     } \
93 } while (0)
94 
95 #undef FD_SET
96 #define FD_SET(fd, set) do { \
97     u_int __i; \
98     for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count; __i++) { \
99 	if (((fd_set FAR *)(set))->fd_array[__i] == (SOCKET)(fd)) { \
100 	    break; \
101 	} \
102     } \
103     if (__i == ((fd_set FAR *)(set))->fd_count) { \
104 	if (((fd_set FAR *)(set))->fd_count < FD_SETSIZE) { \
105 	    ((fd_set FAR *)(set))->fd_array[__i] = (SOCKET)(fd); \
106 	    ((fd_set FAR *)(set))->fd_count++; \
107 	} \
108     } \
109 } while (0)
110 
111 /*
112  * Windows Sockets errors redefined as regular Berkeley error constants.
113  * These are usually commented out in Windows NT to avoid conflicts with errno.h.
114  * Use the WSA constants instead.
115  */
116 
117 #include <errno.h>
118 
119 #ifndef EWOULDBLOCK
120 #define EWOULDBLOCK             WSAEWOULDBLOCK
121 #endif
122 #ifndef EINPROGRESS
123 #define EINPROGRESS             WSAEINPROGRESS
124 #endif
125 #ifndef EALREADY
126 #define EALREADY                WSAEALREADY
127 #endif
128 #ifndef ENOTSOCK
129 #define ENOTSOCK                WSAENOTSOCK
130 #endif
131 #ifndef EDESTADDRREQ
132 #define EDESTADDRREQ            WSAEDESTADDRREQ
133 #endif
134 #ifndef EMSGSIZE
135 #define EMSGSIZE                WSAEMSGSIZE
136 #endif
137 #ifndef EPROTOTYPE
138 #define EPROTOTYPE              WSAEPROTOTYPE
139 #endif
140 #ifndef ENOPROTOOPT
141 #define ENOPROTOOPT             WSAENOPROTOOPT
142 #endif
143 #ifndef EPROTONOSUPPORT
144 #define EPROTONOSUPPORT         WSAEPROTONOSUPPORT
145 #endif
146 #ifndef ESOCKTNOSUPPORT
147 #define ESOCKTNOSUPPORT         WSAESOCKTNOSUPPORT
148 #endif
149 #ifndef EOPNOTSUPP
150 #define EOPNOTSUPP              WSAEOPNOTSUPP
151 #endif
152 #ifndef EPFNOSUPPORT
153 #define EPFNOSUPPORT            WSAEPFNOSUPPORT
154 #endif
155 #ifndef EAFNOSUPPORT
156 #define EAFNOSUPPORT            WSAEAFNOSUPPORT
157 #endif
158 #ifndef EADDRINUSE
159 #define EADDRINUSE              WSAEADDRINUSE
160 #endif
161 #ifndef EADDRNOTAVAIL
162 #define EADDRNOTAVAIL           WSAEADDRNOTAVAIL
163 #endif
164 #ifndef ENETDOWN
165 #define ENETDOWN                WSAENETDOWN
166 #endif
167 #ifndef ENETUNREACH
168 #define ENETUNREACH             WSAENETUNREACH
169 #endif
170 #ifndef ENETRESET
171 #define ENETRESET               WSAENETRESET
172 #endif
173 #ifndef ECONNABORTED
174 #define ECONNABORTED            WSAECONNABORTED
175 #endif
176 #ifndef ECONNRESET
177 #define ECONNRESET              WSAECONNRESET
178 #endif
179 #ifndef ENOBUFS
180 #define ENOBUFS                 WSAENOBUFS
181 #endif
182 #ifndef EISCONN
183 #define EISCONN                 WSAEISCONN
184 #endif
185 #ifndef ENOTCONN
186 #define ENOTCONN                WSAENOTCONN
187 #endif
188 #ifndef ESHUTDOWN
189 #define ESHUTDOWN               WSAESHUTDOWN
190 #endif
191 #ifndef ETOOMANYREFS
192 #define ETOOMANYREFS            WSAETOOMANYREFS
193 #endif
194 #ifndef ETIMEDOUT
195 #define ETIMEDOUT               WSAETIMEDOUT
196 #endif
197 #ifndef ECONNREFUSED
198 #define ECONNREFUSED            WSAECONNREFUSED
199 #endif
200 #ifndef ELOOP
201 #define ELOOP                   WSAELOOP
202 #endif
203 #ifndef EHOSTDOWN
204 #define EHOSTDOWN               WSAEHOSTDOWN
205 #endif
206 #ifndef EHOSTUNREACH
207 #define EHOSTUNREACH            WSAEHOSTUNREACH
208 #endif
209 #ifndef EPROCLIM
210 #define EPROCLIM                WSAEPROCLIM
211 #endif
212 #ifndef EUSERS
213 #define EUSERS                  WSAEUSERS
214 #endif
215 #ifndef EDQUOT
216 #define EDQUOT                  WSAEDQUOT
217 #endif
218 #ifndef ESTALE
219 #define ESTALE                  WSAESTALE
220 #endif
221 #ifndef EREMOTE
222 #define EREMOTE                 WSAEREMOTE
223 #endif
224 
225 LWRES_LANG_BEGINDECLS
226 
227 const char *
228 lwres_net_ntop(int af, const void *src, char *dst, size_t size);
229 
230 int
231 lwres_net_pton(int af, const char *src, void *dst);
232 
233 int
234 lwres_net_aton(const char *cp, struct in_addr *addr);
235 
236 LWRES_LANG_ENDDECLS
237 
238 #endif /* LWRES_NET_H */
239