Browse code

* make ipv6_payload compile under windowze - create inet_ntop() and inet_pton() wrap-implementations using WSAAddressToString() and WSAStringToAddress() functions - add relevant win32-only headers to syshead.h NOTE: syshead.h changes are already included in ipv6_transport

JuanJo Ciarlante authored on 2010/02/22 02:46:59
Showing 3 changed files
... ...
@@ -2407,6 +2407,58 @@ link_socket_write_udp_posix_sendmsg (struct link_socket *sock,
2407 2407
 
2408 2408
 #ifdef WIN32
2409 2409
 
2410
+/*
2411
+ * inet_ntop() and inet_pton() wrap-implementations using
2412
+ * WSAAddressToString() and WSAStringToAddress() functions
2413
+ */
2414
+const char *
2415
+inet_ntop(int af, const void *src, char *dst, socklen_t size)
2416
+{
2417
+  struct sockaddr_storage ss;
2418
+  unsigned long s = size;
2419
+
2420
+  CLEAR(ss);
2421
+  ss.ss_family = af;
2422
+
2423
+  switch(af) {
2424
+    case AF_INET:
2425
+      ((struct sockaddr_in *)&ss)->sin_addr = *(struct in_addr *)src;
2426
+      break;
2427
+    case AF_INET6:
2428
+      ((struct sockaddr_in6 *)&ss)->sin6_addr = *(struct in6_addr *)src;
2429
+      break;
2430
+    default:
2431
+      ASSERT (0);
2432
+  }
2433
+  // cannot direclty use &size because of strict aliasing rules
2434
+  return (WSAAddressToString((struct sockaddr *)&ss, sizeof(ss), NULL, dst, &s) == 0)?
2435
+          dst : NULL;
2436
+}
2437
+
2438
+int
2439
+inet_pton(int af, const char *src, void *dst)
2440
+{
2441
+  struct sockaddr_storage ss;
2442
+  int size = sizeof(ss);
2443
+  char src_copy[INET6_ADDRSTRLEN+1];
2444
+
2445
+  CLEAR(ss);
2446
+  // stupid non-const API
2447
+  strncpynt(src_copy, src, INET6_ADDRSTRLEN+1);
2448
+
2449
+  if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0) {
2450
+    switch(af) {
2451
+      case AF_INET:
2452
+	*(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
2453
+	return 1;
2454
+      case AF_INET6:
2455
+	*(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
2456
+	return 1;
2457
+    }
2458
+  }
2459
+  return 0;
2460
+}
2461
+
2410 2462
 int
2411 2463
 socket_recv_queue (struct link_socket *sock, int maxsize)
2412 2464
 {
... ...
@@ -28,6 +28,10 @@
28 28
 /*
29 29
  * Only include if not during configure
30 30
  */
31
+#ifdef WIN32
32
+/* USE_PF_INET6: win32 ipv6 exists only after 0x0501 (XP) */
33
+#define WINVER 0x0501
34
+#endif
31 35
 #ifndef PACKAGE_NAME
32 36
 #include "config.h"
33 37
 #endif
... ...
@@ -339,6 +343,9 @@
339 339
 #ifdef WIN32
340 340
 #include <iphlpapi.h>
341 341
 #include <wininet.h>
342
+/* The following two headers are needed of USE_PF_INET6 */
343
+#include <winsock2.h>
344
+#include <ws2tcpip.h>
342 345
 #endif
343 346
 
344 347
 #ifdef HAVE_SYS_MMAN_H
... ...
@@ -269,6 +269,8 @@ char *get_win_sys_path (void);
269 269
 
270 270
 /* call self in a subprocess */
271 271
 void fork_to_self (const char *cmdline);
272
+const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
273
+int inet_pton(int af, const char *src, void *st);
272 274
 
273 275
 /* Find temporary directory */
274 276
 const char *win_get_tempdir();