[patch@31988] Revised Module::Build fixes for VMS.
[p5sagit/p5-mst-13.2.git] / win32 / win32sck.c
index 0e2be30..26bef5e 100644 (file)
 #      define TO_SOCKET(x)     (x)
 #endif /* USE_SOCKETS_AS_HANDLES */
 
-#if defined(USE_ITHREADS)
 #define StartSockets() \
     STMT_START {                                       \
        if (!wsock_started)                             \
            start_sockets();                            \
-       set_socktype();                                 \
     } STMT_END
-#else
-#define StartSockets() \
-    STMT_START {                                       \
-       if (!wsock_started) {                           \
-           start_sockets();                            \
-           set_socktype();                             \
-       }                                               \
-    } STMT_END
-#endif
 
 #define SOCKET_TEST(x, y) \
     STMT_START {                                       \
@@ -98,12 +87,6 @@ start_sockets(void)
     wsock_started = 1;
 }
 
-void
-set_socktype(void)
-{
-}
-
-
 #ifndef USE_SOCKETS_AS_HANDLES
 #undef fdopen
 FILE *
@@ -125,7 +108,7 @@ my_fdopen(int fd, char *mode)
     /*
      * If we get here, then fd is actually a socket.
      */
-    Newz(1310, fp, 1, FILE);   /* XXX leak, good thing this code isn't used */
+    Newxz(fp, 1, FILE);        /* XXX leak, good thing this code isn't used */
     if(fp == NULL) {
        errno = ENOMEM;
        return NULL;
@@ -312,11 +295,11 @@ win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const
     for (i = 0; i < nfds; i++) {
        fd = TO_SOCKET(i);
        if (PERL_FD_ISSET(i,rd))
-           FD_SET(fd, &nrd);
+           FD_SET((unsigned)fd, &nrd);
        if (PERL_FD_ISSET(i,wr))
-           FD_SET(fd, &nwr);
+           FD_SET((unsigned)fd, &nwr);
        if (PERL_FD_ISSET(i,ex))
-           FD_SET(fd, &nex);
+           FD_SET((unsigned)fd, &nex);
     }
 
     errno = save_errno;
@@ -398,17 +381,22 @@ convert_proto_info_w2a(WSAPROTOCOL_INFOW *in, WSAPROTOCOL_INFOA *out)
 SOCKET
 open_ifs_socket(int af, int type, int protocol)
 {
+    dTHX;
+    char *s;
     unsigned long proto_buffers_len = 0;
     int error_code;
     SOCKET out = INVALID_SOCKET;
 
+    if ((s = PerlEnv_getenv("PERL_ALLOW_NON_IFS_LSP")) && atoi(s))
+        return WSASocket(af, type, protocol, NULL, 0, 0);
+
     if (WSCEnumProtocols(NULL, NULL, &proto_buffers_len, &error_code) == SOCKET_ERROR
         && error_code == WSAENOBUFS)
     {
        WSAPROTOCOL_INFOW *proto_buffers;
         int protocols_available = 0;       
  
-        New(1, proto_buffers, proto_buffers_len / sizeof(WSAPROTOCOL_INFOW),
+        Newx(proto_buffers, proto_buffers_len / sizeof(WSAPROTOCOL_INFOW),
             WSAPROTOCOL_INFOW);
 
         if ((protocols_available = WSCEnumProtocols(NULL, proto_buffers, 
@@ -421,7 +409,8 @@ open_ifs_socket(int af, int type, int protocol)
 
                 if ((af != AF_UNSPEC && af != proto_buffers[i].iAddressFamily)
                     || (type != proto_buffers[i].iSocketType)
-                    || (protocol != 0 && protocol != proto_buffers[i].iProtocol))
+                    || (protocol != 0 && proto_buffers[i].iProtocol != 0 &&
+                        protocol != proto_buffers[i].iProtocol))
                     continue;
 
                 if ((proto_buffers[i].dwServiceFlags1 & XP1_IFS_HANDLES) == 0)
@@ -660,15 +649,19 @@ int
 win32_ioctl(int i, unsigned int u, char *data)
 {
     dTHX;
-    u_long argp = (u_long)data;
+    u_long u_long_arg; 
     int retval;
-
+    
     if (!wsock_started) {
        Perl_croak_nocontext("ioctl implemented only on sockets");
        /* NOTREACHED */
     }
 
-    retval = ioctlsocket(TO_SOCKET(i), (long)u, &argp);
+    /* mauke says using memcpy avoids alignment issues */
+    memcpy(&u_long_arg, data, sizeof u_long_arg); 
+    retval = ioctlsocket(TO_SOCKET(i), (long)u, &u_long_arg);
+    memcpy(data, &u_long_arg, sizeof u_long_arg);
+    
     if (retval == SOCKET_ERROR) {
        if (WSAGetLastError() == WSAENOTSOCK) {
            Perl_croak_nocontext("ioctl implemented only on sockets");