fix inet_pton for Windows
Stefan Schuermans

Stefan Schuermans commited on 2017-10-28 22:53:55
Showing 1 changed files, with 18 additions and 8 deletions.

... ...
@@ -58,19 +58,29 @@ const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
58 58
 
59 59
 int inet_pton(int af, const char *src, void *dst)
60 60
 {
61
-  struct addrinfo hints, *res, *ressave;
61
+  struct addrinfo hints, *res;
62 62
   memset(&hints, 0, sizeof(struct addrinfo));
63 63
   hints.ai_family = af;
64 64
   if (getaddrinfo(src, NULL, &hints, &res) != 0) {
65
-    return -1;
65
+    return 0; // error
66 66
   }
67
-  ressave = res;
68
-  while (res) {
69
-    memcpy(dst, res->ai_addr, res->ai_addrlen);
70
-    res = res->ai_next;
67
+  if (!res) {
68
+    return 0; // error
71 69
   }
72
-  freeaddrinfo(ressave);
73
-  return 0;
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;
74 84
 }
75 85
 
76 86
 } // namespace Blinker
77 87