Debian Codegraph @

libmowgli-2/src/libmowgli/platform/win32/inet.c

raw
1 /*
2 * Copyright (c) 2012 William Pitcock <[email protected]>.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice is present in all copies.
7 *
8 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
11 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
12 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
14 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
16 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
17 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
18 * POSSIBILITY OF SUCH DAMAGE.
19 */
20
21 #include "mowgli.h"
22
23 #ifdef _WIN32
24
25 int
26 inet_pton(int af, const char *src, void *dst)
27 {
28 struct sockaddr_storage ss;
29
30 int size = sizeof(struct sockaddr_storage);
31 char src_copy[INET6_ADDRSTRLEN + 1];
32
33 mowgli_strlcpy(src_copy, src, sizeof src_copy);
34
35 if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *) &ss, &size) != SOCKET_ERROR)
36 switch (af)
37 {
38 case AF_INET:
39 *(struct in_addr *) dst = ((struct sockaddr_in *) &ss)->sin_addr;
40 return 1;
41
42 case AF_INET6:
43 *(struct in6_addr *) dst = ((struct sockaddr_in6 *) &ss)->sin6_addr;
44 return 1;
45
46 default:
47 return 0;
48 }
49
50 return -1;
51 }
52
53 const char *
54 inet_ntop(int af, const void *addr, char *host, size_t hostlen)
55 {
56 struct sockaddr_storage ss;
57
58 int size = sizeof(struct sockaddr_storage);
59
60 ss.ss_family = af;
61
62 switch (af)
63 {
64 case AF_INET:
65 memcpy(&(((struct sockaddr_in *) &ss)->sin_addr), (struct in_addr *) addr, sizeof(struct in_addr));
66 break;
67
68 case AF_INET6:
69 memcpy(&(((struct sockaddr_in6 *) &ss)->sin6_addr), (struct in6_addr *) addr, sizeof(struct in6_addr));
70 break;
71
72 default:
73 return NULL;
74 }
75
76 if (WSAAddressToString((struct sockaddr *) &ss, size, 0, host, (LPDWORD) &hostlen) != SOCKET_ERROR)
77 return host;
78
79 return NULL;
80 }
81
82 #endif