]> git.meshlink.io Git - catta/blobdiff - src/compat/windows/wincompat.c
make ioctl() work for setting values
[catta] / src / compat / windows / wincompat.c
index 4433b5b54648be93ff1fec50c7baa2674a74d11f..98bc2d788aac5222f4007b8d7cb5e76fb7103a2a 100644 (file)
@@ -4,6 +4,8 @@
 #include <assert.h>
 #include <stdint.h>
 
+#include <catta/log.h>
+
 // helper: convert WSAGetLastError() to an errno constant
 static int wsa_errno(void)
 {
@@ -29,6 +31,31 @@ static int wsa_errno(void)
     }
 }
 
+void winsock_init(void)
+{
+    WSADATA wsa;
+    int error;
+
+    if((error = WSAStartup(MAKEWORD(2,2), &wsa)) != 0)
+        catta_log_error("WSAStartup() failed: %d", error);
+}
+
+void winsock_exit(void)
+{
+    if(WSACleanup() == SOCKET_ERROR)
+        catta_log_warn("WSACleanup() failed: %d", WSAGetLastError());
+}
+
+char *errnostrsocket(void)
+{
+    static char buf[256];
+
+    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+                  NULL, WSAGetLastError(), 0, buf, sizeof(buf), NULL);
+
+    return buf;
+}
+
 ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)
 {
     LPFN_WSARECVMSG WSARecvMsg = NULL;
@@ -111,6 +138,12 @@ ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)
     msg->msg_flags = (int)wsamsg.dwFlags;
         // all flags that fit into dwFlags also fit into msg_flags (see above)
 
+    // XXX debug, remove
+    {
+        char where[64];
+        getnameinfo(msg->msg_name, msg->msg_namelen, where, sizeof(where), NULL, 0, NI_NUMERICHOST);
+        catta_log_debug("recvmsg: %u bytes from %s", (unsigned int)bytesrcvd, where);
+    }
     return bytesrcvd;
 }
 
@@ -187,12 +220,18 @@ ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags)
         return -1;
     }
 
+    // XXX debug, remove
+    {
+        char where[64];
+        getnameinfo(msg->msg_name, msg->msg_namelen, where, sizeof(where), NULL, 0, NI_NUMERICHOST);
+        catta_log_debug("sendmsg: %u bytes to %s", (unsigned int)bytessent, where);
+    }
     return bytessent;
 }
 
 int ioctl(int d, unsigned long request, int *p)
 {
-    u_long arg = 0;
+    u_long arg = *p;
 
     if(ioctlsocket(d, request, &arg) == SOCKET_ERROR) {
         errno = wsa_errno();