1 /**
2  * D header file for POSIX.
3  *
4  * Copyright: Copyright Sean Kelly 2005 - 2009.
5  * License:   $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
6  * Authors:   Sean Kelly
7  * Standards: The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition
8  */
9 
10 /*          Copyright Sean Kelly 2005 - 2009.
11  * Distributed under the Boost Software License, Version 1.0.
12  *    (See accompanying file LICENSE or copy at
13  *          http://www.boost.org/LICENSE_1_0.txt)
14  */
15 module core.sys.posix.arpa.inet;
16 
17 import core.sys.posix.config;
18 public import core.stdc.inttypes; // for uint32_t, uint16_t
19 public import core.sys.posix.sys.socket; // for socklen_t
20 
21 version (OSX)
22     version = Darwin;
23 else version (iOS)
24     version = Darwin;
25 else version (TVOS)
26     version = Darwin;
27 else version (WatchOS)
28     version = Darwin;
29 
30 version (Posix):
31 extern (C) nothrow @nogc:
32 
33 //
34 // Required
35 //
36 /*
37 NOTE: The following must must be defined in core.sys.posix.arpa.inet to break
38       a circular import: in_port_t, in_addr_t, struct in_addr, INET_ADDRSTRLEN.
39 
40 in_port_t // from core.sys.posix.netinet.in_
41 in_addr_t // from core.sys.posix.netinet.in_
42 
43 struct in_addr  // from core.sys.posix.netinet.in_
44 INET_ADDRSTRLEN // from core.sys.posix.netinet.in_
45 
46 uint32_t // from core.stdc.inttypes
47 uint16_t // from core.stdc.inttypes
48 
49 uint32_t htonl(uint32_t);
50 uint16_t htons(uint16_t);
51 uint32_t ntohl(uint32_t);
52 uint16_t ntohs(uint16_t);
53 
54 in_addr_t inet_addr(const scope char*);
55 char*     inet_ntoa(in_addr);
56 // per spec: const char* inet_ntop(int, const void*, char*, socklen_t);
57 char*     inet_ntop(int, const scope void*, char*, socklen_t);
58 int       inet_pton(int, const scope char*, void*);
59 */
60 
61 version (CRuntime_Glibc)
62 {
63     alias uint16_t in_port_t;
64     alias uint32_t in_addr_t;
65 
66     struct in_addr
67     {
68         in_addr_t s_addr;
69     }
70 
71     @trusted pure
72     {
73     uint32_t htonl(uint32_t);
74     uint16_t htons(uint16_t);
75     uint32_t ntohl(uint32_t);
76     uint16_t ntohs(uint16_t);
77     }
78 
79     in_addr_t       inet_addr(const scope char*);
80     char*           inet_ntoa(in_addr);
81     const(char)*    inet_ntop(int, const scope void*, char*, socklen_t);
82     int             inet_pton(int, const scope char*, void*);
83 }
84 else version (Darwin)
85 {
86     alias uint16_t in_port_t;
87     alias uint32_t in_addr_t;
88 
89     struct in_addr
90     {
91         in_addr_t s_addr;
92     }
93 
94     @trusted pure
95     {
96     uint32_t htonl(uint32_t);
97     uint16_t htons(uint16_t);
98     uint32_t ntohl(uint32_t);
99     uint16_t ntohs(uint16_t);
100     }
101 
102     in_addr_t       inet_addr(const scope char*);
103     char*           inet_ntoa(in_addr);
104     const(char)*    inet_ntop(int, const scope void*, char*, socklen_t);
105     int             inet_pton(int, const scope char*, void*);
106 }
107 else version (FreeBSD)
108 {
109     alias uint16_t in_port_t;
110     alias uint32_t in_addr_t;
111 
112     struct in_addr
113     {
114         in_addr_t s_addr;
115     }
116 
117     @trusted pure
118     {
119     uint32_t htonl(uint32_t);
120     uint16_t htons(uint16_t);
121     uint32_t ntohl(uint32_t);
122     uint16_t ntohs(uint16_t);
123     }
124 
125     in_addr_t       inet_addr(const scope char*);
126     char*           inet_ntoa(in_addr);
127     const(char)*    inet_ntop(int, const scope void*, char*, socklen_t);
128     int             inet_pton(int, const scope char*, void*);
129 }
130 else version (NetBSD)
131 {
132     alias uint16_t in_port_t;
133     alias uint32_t in_addr_t;
134 
135     struct in_addr
136     {
137         in_addr_t s_addr;
138     }
139 
140     @trusted pure
141     {
142     uint32_t htonl(uint32_t);
143     uint16_t htons(uint16_t);
144     uint32_t ntohl(uint32_t);
145     uint16_t ntohs(uint16_t);
146     }
147 
148     in_addr_t       inet_addr(const scope char*);
149     char*           inet_ntoa(in_addr);
150     const(char)*    inet_ntop(int, const scope void*, char*, socklen_t);
151     int             inet_pton(int, const scope char*, void*);
152 }
153 else version (OpenBSD)
154 {
155     alias uint16_t in_port_t;
156     alias uint32_t in_addr_t;
157 
158     struct in_addr
159     {
160         in_addr_t s_addr;
161     }
162 
163     @safe pure extern (D)
164     {
165         version (BigEndian)
166         {
167             uint32_t htonl(uint32_t x) { return x; }
168             uint16_t htons(uint16_t x) { return x; }
169         }
170         else
171         {
172             import core.bitop : bswap, byteswap;
173 
174             uint32_t htonl(uint32_t x) { return bswap(x); }
175             uint16_t htons(uint16_t x) { return byteswap(x); }
176         }
177         alias ntohl = htonl;
178         alias ntohs = htons;
179     }
180 
181     in_addr_t       inet_addr(const scope char*);
182     char*           inet_ntoa(in_addr);
183     const(char)*    inet_ntop(int, const scope void*, char*, socklen_t);
184     int             inet_pton(int, const scope char*, void*);
185 }
186 else version (DragonFlyBSD)
187 {
188     alias uint16_t in_port_t;
189     alias uint32_t in_addr_t;
190 
191     struct in_addr
192     {
193         in_addr_t s_addr;
194     }
195 
196     @trusted pure
197     {
198     uint32_t htonl(uint32_t);
199     uint16_t htons(uint16_t);
200     uint32_t ntohl(uint32_t);
201     uint16_t ntohs(uint16_t);
202     }
203 
204     in_addr_t       inet_addr(const scope char*);
205     char*           inet_ntoa(in_addr);
206     const(char)*    inet_ntop(int, const scope void*, char*, socklen_t);
207     int             inet_pton(int, const scope char*, void*);
208 }
209 else version (Solaris)
210 {
211     alias uint16_t in_port_t;
212     alias uint32_t in_addr_t;
213 
214     struct in_addr
215     {
216         in_addr_t s_addr;
217     }
218 
219     @trusted pure
220     {
221     uint32_t htonl(uint32_t);
222     uint16_t htons(uint16_t);
223     uint32_t ntohl(uint32_t);
224     uint16_t ntohs(uint16_t);
225     }
226 
227     in_addr_t       inet_addr(const scope char*);
228     char*           inet_ntoa(in_addr);
229     const(char)*    inet_ntop(int, const scope void*, char*, socklen_t);
230     int             inet_pton(int, const scope char*, void*);
231 }
232 else version (CRuntime_Bionic)
233 {
234     alias uint32_t in_addr_t;
235 
236     struct in_addr
237     {
238         in_addr_t s_addr;
239     }
240 
241     @safe pure extern (D)
242     {
243         version (BigEndian)
244         {
245             uint32_t htonl(uint32_t x) { return x; }
246             uint16_t htons(uint16_t x) { return x; }
247         }
248         else
249         {
250             import core.bitop : bswap, byteswap;
251 
252             uint32_t htonl(uint32_t x) { return bswap(x); }
253             uint16_t htons(uint16_t x) { return byteswap(x); }
254         }
255         alias ntohl = htonl;
256         alias ntohs = htons;
257     }
258 
259     in_addr_t       inet_addr(const scope char*);
260     char*           inet_ntoa(in_addr);
261     const(char)*    inet_ntop(int, const scope void*, char*, size_t);
262     int             inet_pton(int, const scope char*, void*);
263 }
264 else version (CRuntime_Musl)
265 {
266     alias uint16_t in_port_t;
267     alias uint32_t in_addr_t;
268 
269     struct in_addr
270     {
271         in_addr_t s_addr;
272     }
273 
274     @trusted pure
275     {
276     uint32_t htonl(uint32_t);
277     uint16_t htons(uint16_t);
278     uint32_t ntohl(uint32_t);
279     uint16_t ntohs(uint16_t);
280     }
281 
282     in_addr_t       inet_addr(const scope char*);
283     char*           inet_ntoa(in_addr);
284     const(char)*    inet_ntop(int, const scope void*, char*, socklen_t);
285     int             inet_pton(int, const scope char*, void*);
286 }
287 else version (CRuntime_UClibc)
288 {
289     alias uint16_t in_port_t;
290     alias uint32_t in_addr_t;
291 
292     struct in_addr
293     {
294         in_addr_t s_addr;
295     }
296 
297     @trusted pure
298     {
299     uint32_t htonl(uint32_t);
300     uint16_t htons(uint16_t);
301     uint32_t ntohl(uint32_t);
302     uint16_t ntohs(uint16_t);
303     }
304 
305     in_addr_t       inet_addr(const scope char*);
306     char*           inet_ntoa(in_addr);
307     const(char)*    inet_ntop(int, const scope void*, char*, socklen_t);
308     int             inet_pton(int, const scope char*, void*);
309 }
310 
311 /*
312 NOTE: The following must must be defined in core.sys.posix.arpa.inet to break
313       a circular import: INET6_ADDRSTRLEN.
314 
315 INET6_ADDRSTRLEN // from core.sys.posix.netinet.in_
316 */
317 
318 enum INET_ADDRSTRLEN  = 16;
319 enum INET6_ADDRSTRLEN = 46;