]> git.meshlink.io Git - catta/blob - src/compat/windows/wincompat.c
3e6417ef2d2115acf87b3fdc6640938393c076c3
[catta] / src / compat / windows / wincompat.c
1 #include "wincompat.h"
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <assert.h>
5 #include <stdint.h>
6
7 #include <catta/log.h>
8
9 // helper: convert WSAGetLastError() to an errno constant
10 static int wsa_errno(void)
11 {
12     switch(WSAGetLastError()) {
13         case WSAEACCES:         return EACCES;
14         case WSAECONNRESET:     return ECONNRESET;
15         case WSAEFAULT:         return EFAULT;
16         case WSAEINPROGRESS:    return EINPROGRESS;
17         case WSAEINTR:          return EINTR;
18         case WSAEINVAL:         return EINVAL;
19         case WSAEMSGSIZE:       return EMSGSIZE;
20         case WSAENETDOWN:       return ENETDOWN;
21         case WSAENETRESET:      return ENETRESET;
22         case WSAENOBUFS:        return ENOBUFS;
23         case WSAENOTCONN:       return ENOTCONN;
24         case WSAENOTSOCK:       return ENOTSOCK;
25         case WSAEOPNOTSUPP:     return EOPNOTSUPP;
26         case WSAESHUTDOWN:      return ESHUTDOWN;
27         case WSAETIMEDOUT:      return ETIMEDOUT;
28         case WSAEWOULDBLOCK:    return EWOULDBLOCK;
29         default:
30             return EINVAL;
31     }
32 }
33
34 void winsock_init(void)
35 {
36     WSADATA wsa;
37     int error;
38
39     if((error = WSAStartup(MAKEWORD(2,2), &wsa)) != 0)
40         catta_log_error("WSAStartup() failed: %d", error);
41 }
42
43 void winsock_exit(void)
44 {
45     if(WSACleanup() == SOCKET_ERROR)
46         catta_log_warn("WSACleanup() failed: %d", WSAGetLastError());
47 }
48
49 char *errnostrsocket(void)
50 {
51     static char buf[256];
52
53     FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
54                   NULL, WSAGetLastError(), 0, buf, sizeof(buf), NULL);
55
56     return buf;
57 }
58
59 ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)
60 {
61     LPFN_WSARECVMSG WSARecvMsg = NULL;
62     GUID wsaid = WSAID_WSARECVMSG;
63     DWORD b;
64
65     DWORD bytesrcvd;
66     WSAMSG wsamsg;
67     size_t i;
68     int r;
69
70     // size_t is larger than DWORD on 64bit
71     if(msg->msg_iovlen > UINT32_MAX) {
72         errno = EINVAL;
73         return -1;
74     }
75
76     // obtain the function pointer to WSARecvMsg
77     r = WSAIoctl(sockfd, SIO_GET_EXTENSION_FUNCTION_POINTER,
78                  &wsaid, sizeof(wsaid), &WSARecvMsg, sizeof(WSARecvMsg),
79                  &b, NULL, NULL);
80     if(r == SOCKET_ERROR) {
81         errno = wsa_errno();
82         return -1;
83     }
84     assert(b == sizeof(WSARecvMsg));
85     assert(WSARecvMsg != NULL);
86
87     // convert msghdr to WSAMSG structure
88     wsamsg.name = msg->msg_name;
89     wsamsg.namelen = msg->msg_namelen;
90     wsamsg.lpBuffers = malloc(msg->msg_iovlen * sizeof(WSABUF));
91     wsamsg.dwBufferCount = msg->msg_iovlen;
92     wsamsg.Control.len = msg->msg_controllen;
93     wsamsg.Control.buf = msg->msg_control;
94     wsamsg.dwFlags = (DWORD)flags;
95
96     // all flags that fit into dwFlags also fit through the flags argument
97     assert(sizeof(DWORD) <= sizeof(flags));
98
99     if(wsamsg.lpBuffers == NULL) {
100         // malloc will have set errno
101         return -1;
102     }
103
104     // re-wrap iovecs as WSABUFs
105     for(i=0; i<msg->msg_iovlen; i++) {
106         // size_t vs. u_long
107         if(msg->msg_iov[i].iov_len > ULONG_MAX) {
108             free(wsamsg.lpBuffers);
109             errno = EINVAL;
110             return -1;
111         }
112
113         wsamsg.lpBuffers[i].len = msg->msg_iov[i].iov_len;
114         wsamsg.lpBuffers[i].buf = msg->msg_iov[i].iov_base;
115     }
116
117     r = WSARecvMsg(sockfd, &wsamsg, &bytesrcvd, NULL, NULL);
118
119     // the allocated WSABUF wrappers are no longer needed
120     free(wsamsg.lpBuffers);
121
122     if(r == SOCKET_ERROR) {
123         // XXX do we need special handling for ENETRESET, EMSGSIZE, ETIMEDOUT?
124         errno = wsa_errno();
125         return -1;
126     }
127
128     // DWORD has one bit more than ssize_t on 32bit
129     // XXX check for this condition before the WSARecvMsg call
130     if(bytesrcvd > SSIZE_MAX) {
131         errno = EINVAL;
132         return -1;
133     }
134
135     // transfer results from wsamsg to msg
136     // NB: the data and control buffers are shared
137     msg->msg_controllen = wsamsg.Control.len;
138     msg->msg_flags = (int)wsamsg.dwFlags;
139         // all flags that fit into dwFlags also fit into msg_flags (see above)
140
141     return bytesrcvd;
142 }
143
144 ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags)
145 {
146     LPFN_WSASENDMSG WSASendMsg = NULL;
147     GUID wsaid = WSAID_WSASENDMSG;
148     DWORD b;
149
150     DWORD bytessent;
151     WSAMSG wsamsg;
152     size_t i;
153     int r;
154
155     // size_t is larger than DWORD on 64bit
156     if(msg->msg_iovlen > UINT32_MAX) {
157         errno = EINVAL;
158         return -1;
159     }
160
161     // obtain the function pointer to WSASendMsg
162     r = WSAIoctl(sockfd, SIO_GET_EXTENSION_FUNCTION_POINTER,
163                  &wsaid, sizeof(wsaid), &WSASendMsg, sizeof(WSASendMsg),
164                  &b, NULL, NULL);
165     if(r == SOCKET_ERROR) {
166         errno = wsa_errno();
167         return -1;
168     }
169     assert(b == sizeof(WSASendMsg));
170     assert(WSASendMsg != NULL);
171
172     // convert msghdr to WSAMSG structure
173     wsamsg.name = msg->msg_name;
174     wsamsg.namelen = msg->msg_namelen;
175     wsamsg.lpBuffers = malloc(msg->msg_iovlen * sizeof(WSABUF));
176     wsamsg.dwBufferCount = msg->msg_iovlen;
177     wsamsg.Control.len = msg->msg_controllen;
178     wsamsg.Control.buf = msg->msg_control;
179     wsamsg.dwFlags = 0; // ignored
180
181     if(wsamsg.lpBuffers == NULL) {
182         // malloc will have set errno
183         return -1;
184     }
185
186     // re-wrap iovecs as WSABUFs
187     for(i=0; i<msg->msg_iovlen; i++) {
188         // size_t vs. u_long
189         if(msg->msg_iov[i].iov_len > ULONG_MAX) {
190             free(wsamsg.lpBuffers);
191             errno = EINVAL;
192             return -1;
193         }
194
195         wsamsg.lpBuffers[i].len = msg->msg_iov[i].iov_len;
196         wsamsg.lpBuffers[i].buf = msg->msg_iov[i].iov_base;
197     }
198
199     r = WSASendMsg(sockfd, &wsamsg, flags, &bytessent, NULL, NULL);
200
201     // the allocated WSABUF wrappers are no longer needed
202     free(wsamsg.lpBuffers);
203
204     if(r == SOCKET_ERROR) {
205         // XXX do we need special handling for ENETRESET, ETIMEDOUT?
206         errno = wsa_errno();
207         return -1;
208     }
209
210     // DWORD has one bit more than ssize_t on 32bit
211     // XXX check for this condition before sending anything
212     if(bytessent > SSIZE_MAX) {
213         errno = EINVAL;
214         return -1;
215     }
216
217     return bytessent;
218 }
219
220 int ioctl(int d, unsigned long request, int *p)
221 {
222     u_long arg = 0;
223
224     if(ioctlsocket(d, request, &arg) == SOCKET_ERROR) {
225         errno = wsa_errno();
226         return -1;
227     }
228
229     if(arg > INT_MAX) {
230         errno = EINVAL;
231         return -1;
232     }
233
234     *p = arg;
235     return 0;
236 }
237
238 int pipe(int pipefd[2])
239 {
240     int lsock = INVALID_SOCKET;
241     struct sockaddr_in laddr;
242     socklen_t laddrlen = sizeof(laddr);
243
244     pipefd[0] = pipefd[1] = INVALID_SOCKET;
245
246     // bind a listening socket to a TCP port on localhost
247     laddr.sin_family = AF_INET;
248     laddr.sin_port = 0;
249     laddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
250     if((lsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR)
251         goto fail;
252     if(bind(lsock, (struct sockaddr *)&laddr, sizeof(laddr)) == SOCKET_ERROR)
253         goto fail;
254     if(listen(lsock, 1) == SOCKET_ERROR)
255         goto fail;
256
257     // determine which address (i.e. port) we got bound to
258     if(getsockname(lsock, (struct sockaddr *)&laddr, &laddrlen) == SOCKET_ERROR)
259         goto fail;
260     assert(laddrlen == sizeof(laddr));
261     laddr.sin_family = AF_INET;
262     laddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
263
264     // connect and accept
265     if((pipefd[0] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR)
266         goto fail;
267     if(connect(pipefd[0], (const struct sockaddr *)&laddr, sizeof(laddr)) == SOCKET_ERROR)
268         goto fail;
269     if((pipefd[1] = accept(lsock, NULL, NULL)) == SOCKET_ERROR)
270         goto fail;
271
272     // close the listener
273     closesocket(lsock);
274
275     return 0;
276
277 fail:
278     errno = wsa_errno();
279     closesocket(pipefd[0]);
280     closesocket(lsock);
281     return -1;
282 }
283
284 int uname(struct utsname *buf)
285 {
286     SYSTEM_INFO si;
287     const char *arch = "unknown";
288
289     memset(buf, 0, sizeof(struct utsname));
290
291     // operating system
292     strncpy(buf->sysname, "Windows", sizeof(buf->sysname)-1);
293     strncpy(buf->release, "unknown", sizeof(buf->sysname)-1);   // we don't need it
294     strncpy(buf->version, "unknown", sizeof(buf->sysname)-1);   // we don't need it
295
296     // computer (node) name
297     if(GetComputerName(buf->nodename, sizeof(buf->nodename)-1) == 0) {
298         errno = EFAULT;
299         return -1;
300     }
301
302     // hardware type
303     GetSystemInfo(&si);
304     switch(si.wProcessorArchitecture) {
305         case PROCESSOR_ARCHITECTURE_AMD64: arch = "amd64"; break;
306         case PROCESSOR_ARCHITECTURE_ARM:   arch = "arm";   break;
307         case PROCESSOR_ARCHITECTURE_IA64:  arch = "ia64";  break;
308         case PROCESSOR_ARCHITECTURE_INTEL: arch = "x86";   break;
309         default: arch = "unknown";
310     }
311     strncpy(buf->machine, arch, sizeof(buf->machine)-1);
312
313     return 0;
314 }