84b7724e555912926abd9892880a459fa0aaf771
Stefan Schuermans UDP address classes for Win...

Stefan Schuermans authored 7 years ago

1) /*
2) implementations in this file are taken from
3) https://github.com/pkulchenko/luasocket/blob/5a58786a39bbef7ed4805821cc921f1d40f12068/src/inet.c
4) 
5) LuaSocket 2.1 license
6) Copyright © 2004-2012 Diego Nehab
7) 
8) Permission is hereby granted, free of charge, to any person obtaining a
9) copy of this software and associated documentation files (the "Software"),
10) to deal in the Software without restriction, including without limitation
11) the rights to use, copy, modify, merge, publish, distribute, sublicense,
12) and/or sell copies of the Software, and to permit persons to whom the
13) Software is furnished to do so, subject to the following conditions:
14) 
15) The above copyright notice and this permission notice shall be included in
16) all copies or substantial portions of the Software.
17) 
18) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19) IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20) FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21) AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22) LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23) FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24) DEALINGS IN THE SOFTWARE.
25) */
26) 
27) #include <winsock2.h>
28) #include <windows.h>
29) #include <ws2tcpip.h>
30) 
31) #include "InetTools.h"
32) 
33) #define NS_INT16SZ 2
34) #define NS_INADDRSZ 4
35) #define NS_IN6ADDRSZ 16
36) 
37) namespace Blinker {
38) 
39) const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
40) {
41)   if (af == AF_INET) {
42)     struct sockaddr_in in;
43)     memset(&in, 0, sizeof(in));
44)     in.sin_family = AF_INET;
45)     memcpy(&in.sin_addr, src, sizeof(struct in_addr));
46)     getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
47)     return dst;
48)   } else if (af == AF_INET6) {
49)     struct sockaddr_in6 in;
50)     memset(&in, 0, sizeof(in));
51)     in.sin6_family = AF_INET6;
52)     memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
53)     getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
54)     return dst;
55)   }
56)   return NULL;
57) }
58) 
59) int inet_pton(int af, const char *src, void *dst)
60) {
Stefan Schuermans fix inet_pton for Windows

Stefan Schuermans authored 7 years ago

61)   struct addrinfo hints, *res;
Stefan Schuermans UDP address classes for Win...

Stefan Schuermans authored 7 years ago

62)   memset(&hints, 0, sizeof(struct addrinfo));
63)   hints.ai_family = af;
64)   if (getaddrinfo(src, NULL, &hints, &res) != 0) {
Stefan Schuermans fix inet_pton for Windows

Stefan Schuermans authored 7 years ago

65)     return 0; // error
Stefan Schuermans UDP address classes for Win...

Stefan Schuermans authored 7 years ago

66)   }
Stefan Schuermans fix inet_pton for Windows

Stefan Schuermans authored 7 years ago

67)   if (!res) {
68)     return 0; // error
Stefan Schuermans UDP address classes for Win...

Stefan Schuermans authored 7 years ago

69)   }
Stefan Schuermans fix inet_pton for Windows

Stefan Schuermans authored 7 years ago

70)   int ret;
71)   if (af == AF_INET) {
72)     struct sockaddr_in * psa = (struct sockaddr_in *)res->ai_addr;
73)     memcpy(dst, &psa->sin_addr, sizeof(psa->sin_addr));
74)     ret = 1; // success
75)   } else if (af == AF_INET6) {
76)     struct sockaddr_in6 * psa = (struct sockaddr_in6 *)res->ai_addr;
77)     memcpy(dst, &psa->sin6_addr, sizeof(psa->sin6_addr));
78)     ret = 1; // success
79)   } else {
80)     ret = 0; // error
81)   }
82)   freeaddrinfo(res);
83)   return ret;