comparison lib/inet_pton.c @ 15634:bcecbef40a05

inet_pton: Support for MSVC on Windows Vista or newer. * lib/arpa_inet.in.h (inet_pton): Also consider REPLACE_INET_PTON. * lib/inet_pton.c (rpl_inet_pton): Use a simple wrapper if HAVE_DECL_INET_PTON is defined. * m4/inet_pton.m4 (gl_FUNC_INET_PTON): Invoke gl_PREREQ_SYS_H_WINSOCK2. On platforms with <winsock2.h>, test whether inet_pton is declared in <ws2tcpip.h>. If so, arrange to replace it. * m4/arpa_inet_h.m4 (gl_ARPA_INET_H_DEFAULTS): Initialize REPLACE_INET_PTON. * modules/arpa_inet (Makefile.am): Substitute REPLACE_INET_PTON. * modules/inet_pton (Files): Add m4/sys_socket_h.m4. (Depends-on, configure.ac): Update condition. * doc/posix-functions/inet_pton.texi: Mention the MSVC problem.
author Bruno Haible <bruno@clisp.org>
date Sat, 17 Sep 2011 15:09:53 +0200
parents 97fc9a21a8fb
children 8250f2777afc
comparison
equal deleted inserted replaced
15633:c2c292001fc0 15634:bcecbef40a05
35 #include <config.h> 35 #include <config.h>
36 36
37 /* Specification. */ 37 /* Specification. */
38 #include <arpa/inet.h> 38 #include <arpa/inet.h>
39 39
40 #include <c-ctype.h> 40 #if HAVE_DECL_INET_PTON
41 #include <string.h> 41
42 #include <errno.h> 42 # undef inet_pton
43 43
44 #define NS_INADDRSZ 4 44 int
45 #define NS_IN6ADDRSZ 16 45 rpl_inet_pton (int af, const char *restrict src, void *restrict dst)
46 #define NS_INT16SZ 2 46 {
47 return inet_pton (af, src, dst);
48 }
49
50 #else
51
52 # include <c-ctype.h>
53 # include <string.h>
54 # include <errno.h>
55
56 # define NS_INADDRSZ 4
57 # define NS_IN6ADDRSZ 16
58 # define NS_INT16SZ 2
47 59
48 /* 60 /*
49 * WARNING: Don't even consider trying to compile this on a system where 61 * WARNING: Don't even consider trying to compile this on a system where
50 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. 62 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
51 */ 63 */
52 64
53 static int inet_pton4 (const char *src, unsigned char *dst); 65 static int inet_pton4 (const char *src, unsigned char *dst);
54 #if HAVE_IPV6 66 # if HAVE_IPV6
55 static int inet_pton6 (const char *src, unsigned char *dst); 67 static int inet_pton6 (const char *src, unsigned char *dst);
56 #endif 68 # endif
57 69
58 /* int 70 /* int
59 * inet_pton(af, src, dst) 71 * inet_pton(af, src, dst)
60 * convert from presentation format (which usually means ASCII printable) 72 * convert from presentation format (which usually means ASCII printable)
61 * to network format (which is usually some kind of binary format). 73 * to network format (which is usually some kind of binary format).
72 switch (af) 84 switch (af)
73 { 85 {
74 case AF_INET: 86 case AF_INET:
75 return (inet_pton4 (src, dst)); 87 return (inet_pton4 (src, dst));
76 88
77 #if HAVE_IPV6 89 # if HAVE_IPV6
78 case AF_INET6: 90 case AF_INET6:
79 return (inet_pton6 (src, dst)); 91 return (inet_pton6 (src, dst));
80 #endif 92 # endif
81 93
82 default: 94 default:
83 errno = EAFNOSUPPORT; 95 errno = EAFNOSUPPORT;
84 return (-1); 96 return (-1);
85 } 97 }
139 return (0); 151 return (0);
140 memcpy (dst, tmp, NS_INADDRSZ); 152 memcpy (dst, tmp, NS_INADDRSZ);
141 return (1); 153 return (1);
142 } 154 }
143 155
144 #if HAVE_IPV6 156 # if HAVE_IPV6
145 157
146 /* int 158 /* int
147 * inet_pton6(src, dst) 159 * inet_pton6(src, dst)
148 * convert presentation level address to network order binary form. 160 * convert presentation level address to network order binary form.
149 * return: 161 * return:
248 if (tp != endp) 260 if (tp != endp)
249 return (0); 261 return (0);
250 memcpy (dst, tmp, NS_IN6ADDRSZ); 262 memcpy (dst, tmp, NS_IN6ADDRSZ);
251 return (1); 263 return (1);
252 } 264 }
265
266 # endif
267
253 #endif 268 #endif