]> git.meshlink.io Git - catta/blob - src/compat/windows/wincompat.h
f76dbc4835a1012160d863060f2928dda145ce82
[catta] / src / compat / windows / wincompat.h
1 #ifndef foowincompatfoo
2 #define foowincompatfoo
3
4 // This file and its companion wincompat.c provide some Posix interfaces to
5 // Windows APIs so the rest of the code can keep using them.
6
7
8 // require at least Windows Vista
9 #undef WINVER
10 #undef _WIN32_WINNT
11 #define WINVER 0x0600
12 #define _WIN32_WINNT WINVER
13
14 #include <winsock2.h>
15 #include <ws2tcpip.h>
16 #include <mswsock.h>
17
18
19 // Winsock doesn't have recvmsg/sendmsg but offers the same functionality
20 // with WSARecvMsg/WSASendMsg, so we implement the former in terms of the
21 // latter.
22
23 struct iovec {                   /* Scatter/gather array items */
24    void  *iov_base;              /* Starting address */
25    size_t iov_len;               /* Number of bytes to transfer */
26 };
27
28 struct msghdr {
29    void         *msg_name;       /* optional address */
30    socklen_t     msg_namelen;    /* size of address */
31    struct iovec *msg_iov;        /* scatter/gather array */
32    size_t        msg_iovlen;     /* # elements in msg_iov */
33    void         *msg_control;    /* ancillary data, see below */
34    size_t        msg_controllen; /* ancillary data buffer len */
35    int           msg_flags;      /* flags on received message */
36 };
37
38 // MSDN says this struct is called wsacmsghdr but MingW uses _WSACMSGHDR.
39 // TODO: Verify what it is on actual Windows.
40 // cf. http://msdn.microsoft.com/en-us/library/ms741645(v=vs.85).aspx
41 #ifdef __MINGW32__
42 #define cmsghdr _WSACMSGHDR     // as in 'struct cmsghdr'
43 #else
44 #define cmsghdr wsacmsghdr      // as in 'struct cmsghdr'
45 #endif
46
47 static inline struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *m) {
48     WSAMSG wm;
49     wm.Control.len = m->msg_controllen;
50     wm.Control.buf = m->msg_control;
51     return WSA_CMSG_FIRSTHDR(&wm);
52 }
53
54 static inline struct cmsghdr *CMSG_NXTHDR(struct msghdr *m, struct cmsghdr *c) {
55     WSAMSG wm;
56     wm.Control.len = m->msg_controllen;
57     wm.Control.buf = m->msg_control;
58     return WSA_CMSG_NXTHDR(&wm, c);
59 }
60
61 #define CMSG_SPACE(len) WSA_CMSG_SPACE(len)
62 #define CMSG_LEN(len) WSA_CMSG_LEN(len)
63
64 // we're going to be naughty and redefine CMSG_DATA as an alias even though it
65 // is also a constant defined in wincrypt.h which we don't care about.
66 #undef CMSG_DATA
67 #define CMSG_DATA(c) WSA_CMSG_DATA(c)
68
69 ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
70 ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
71
72 // ESHUTDOWN does not seem to exist on Windows, even though WSAESHUTDOWN does.
73 // MingW doesn't define it and MSDN doesn't list it, so we alias it to EBADF.
74 // cf. http://msdn.microsoft.com/en-us/library/5814770t.aspx
75 #ifndef ESHUTDOWN
76 #define ESHUTDOWN EBADF
77 #endif
78
79
80 // Windows logically doesn't have uname, so we supply a replacement.
81
82 struct utsname {
83    char sysname[9];    /* Operating system name (e.g., "Linux") */
84    char nodename[MAX_COMPUTERNAME_LENGTH+1];
85                        /* Name within "some implementation-defined network" */
86    char release[9];    /* Operating system release (e.g., "2.6.28") */
87    char version[9];    /* Operating system version */
88    char machine[9];    /* Hardware identifier */
89 };
90
91 int uname(struct utsname *buf);
92
93
94 #endif