1 | #ifndef __WINUTILS_H_
|
---|
2 | # define __WINUTILS_H_
|
---|
3 |
|
---|
4 | # ifdef RT_OS_WINDOWS
|
---|
5 | # include <iprt/cdefs.h>
|
---|
6 | # include <WinSock2.h>
|
---|
7 | # include <ws2tcpip.h>
|
---|
8 | # include <mswsock.h>
|
---|
9 | # include <Windows.h>
|
---|
10 | # include <iprt/err.h>
|
---|
11 | # include <iprt/net.h>
|
---|
12 | # include <iprt/log.h>
|
---|
13 | /**
|
---|
14 | * Inclusion of lwip/def.h was added here to avoid conflict of definitions
|
---|
15 | * of hton-family functions in LWIP and windock's headers.
|
---|
16 | */
|
---|
17 | # include <lwip/def.h>
|
---|
18 |
|
---|
19 | # ifndef PF_LOCAL
|
---|
20 | # define PF_LOCAL AF_INET
|
---|
21 | # endif
|
---|
22 |
|
---|
23 | # define warn(...) DPRINTF2((__VA_ARGS__))
|
---|
24 | # define warnx warn
|
---|
25 | # ifdef DEBUG
|
---|
26 | # define err(code,...) do { \
|
---|
27 | AssertMsgFailed((__VA_ARGS__)); \
|
---|
28 | }while(0)
|
---|
29 | #else
|
---|
30 | # define err(code,...) do { \
|
---|
31 | DPRINTF0((__VA_ARGS__)); \
|
---|
32 | ExitProcess(code); \
|
---|
33 | }while(0)
|
---|
34 | #endif
|
---|
35 | # define errx err
|
---|
36 | # define __func__ __FUNCTION__
|
---|
37 | # define __attribute__(x) /* IGNORE */
|
---|
38 |
|
---|
39 | /*
|
---|
40 | * XXX: inet_ntop() is only available starting from Vista.
|
---|
41 | */
|
---|
42 | DECLINLINE(PCSTR)
|
---|
43 | inet_ntop(INT Family, PVOID pAddr, PSTR pStringBuf, size_t StringBufSize)
|
---|
44 | {
|
---|
45 | DWORD size = (DWORD)StringBufSize;
|
---|
46 | int status;
|
---|
47 |
|
---|
48 | if (Family == AF_INET)
|
---|
49 | {
|
---|
50 | struct sockaddr_in sin;
|
---|
51 | memset(&sin, 0, sizeof(sin));
|
---|
52 | sin.sin_family = AF_INET;
|
---|
53 | memcpy(&sin.sin_addr, pAddr, sizeof(sin.sin_addr));
|
---|
54 | sin.sin_port = 0;
|
---|
55 | status = WSAAddressToStringA((LPSOCKADDR)&sin, sizeof(sin), NULL,
|
---|
56 | pStringBuf, &size);
|
---|
57 | }
|
---|
58 | else if (Family == AF_INET6)
|
---|
59 | {
|
---|
60 | struct sockaddr_in6 sin6;
|
---|
61 | memset(&sin6, 0, sizeof(sin6));
|
---|
62 | sin6.sin6_family = AF_INET6;
|
---|
63 | memcpy(&sin6.sin6_addr, pAddr, sizeof(sin6.sin6_addr));
|
---|
64 | sin6.sin6_port = 0;
|
---|
65 | status = WSAAddressToStringA((LPSOCKADDR)&sin6, sizeof(sin6), NULL,
|
---|
66 | pStringBuf, &size);
|
---|
67 | }
|
---|
68 | else
|
---|
69 | {
|
---|
70 | WSASetLastError(WSAEAFNOSUPPORT);
|
---|
71 | return NULL;
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (status == SOCKET_ERROR)
|
---|
75 | {
|
---|
76 | return NULL;
|
---|
77 | }
|
---|
78 |
|
---|
79 | return pStringBuf;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * tftpd emulation we're using POSIX operations which needs "DOS errno". see proxy_tftpd.c
|
---|
85 | */
|
---|
86 | # ifndef _USE_WINSTD_ERRNO
|
---|
87 | /**
|
---|
88 | * http://msdn.microsoft.com/en-us/library/windows/desktop/ms737828(v=vs.85).aspx
|
---|
89 | * "Error Codes - errno, h_errno and WSAGetLastError" says "Error codes set by Windows Sockets are
|
---|
90 | * not made available through the errno variable."
|
---|
91 | */
|
---|
92 | # include <errno.h>
|
---|
93 | # ifdef errno
|
---|
94 | # undef errno
|
---|
95 | # endif
|
---|
96 | # define errno (WSAGetLastError())
|
---|
97 | # endif
|
---|
98 | /* Missing errno codes */
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * "Windows Sockets Error Codes" obtained with WSAGetLastError().
|
---|
102 | * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx
|
---|
103 | */
|
---|
104 | # undef EMSGSIZE
|
---|
105 | # define EMSGSIZE WSAEMSGSIZE
|
---|
106 | # undef ENETDOWN
|
---|
107 | # define ENETDOWN WSAENETDOWN
|
---|
108 | # undef ENETUNREACH
|
---|
109 | # define ENETUNREACH WSAENETUNREACH
|
---|
110 | # undef EHOSTDOWN
|
---|
111 | # define EHOSTDOWN WSAEHOSTDOWN
|
---|
112 | # undef EHOSTUNREACH
|
---|
113 | # define EHOSTUNREACH WSAEHOSTUNREACH
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * parameters to shutdown (2) with Winsock2
|
---|
117 | * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740481(v=vs.85).aspx
|
---|
118 | */
|
---|
119 | # define SHUT_RD SD_RECEIVE
|
---|
120 | # define SHUT_WR SD_SEND
|
---|
121 | # define SHUT_RDWR SD_BOTH
|
---|
122 |
|
---|
123 | typedef ULONG nfds_t;
|
---|
124 |
|
---|
125 | typedef WSABUF IOVEC;
|
---|
126 |
|
---|
127 | # define IOVEC_GET_BASE(iov) ((iov).buf)
|
---|
128 | # define IOVEC_SET_BASE(iov, b) ((iov).buf = (b))
|
---|
129 |
|
---|
130 | # define IOVEC_GET_LEN(iov) ((iov).len)
|
---|
131 | # define IOVEC_SET_LEN(iov, l) ((iov).len = (ULONG)(l))
|
---|
132 |
|
---|
133 | #if _WIN32_WINNT < 0x0600
|
---|
134 | /* otherwise defined the other way around in ws2def.h */
|
---|
135 | #define cmsghdr _WSACMSGHDR
|
---|
136 |
|
---|
137 | #undef CMSG_DATA /* wincrypt.h can byte my shiny metal #undef */
|
---|
138 | #define CMSG_DATA WSA_CMSG_DATA
|
---|
139 | #define CMSG_LEN WSA_CMSG_LEN
|
---|
140 | #define CMSG_SPACE WSA_CMSG_SPACE
|
---|
141 |
|
---|
142 | #define CMSG_FIRSTHDR WSA_CMSG_FIRSTHDR
|
---|
143 | #define CMSG_NXTHDR WSA_CMSG_NXTHDR
|
---|
144 | #endif /* _WIN32_WINNT < 0x0600 - provide unglified CMSG names */
|
---|
145 |
|
---|
146 | RT_C_DECLS_BEGIN
|
---|
147 | int RTWinSocketPair(int domain, int type, int protocol, SOCKET socket_vector[2]);
|
---|
148 | RT_C_DECLS_END
|
---|
149 |
|
---|
150 | # else /* !RT_OS_WINDOWS */
|
---|
151 | # define ioctlsocket ioctl
|
---|
152 | # define closesocket close
|
---|
153 | # define SOCKET int
|
---|
154 | # define INVALID_SOCKET (-1)
|
---|
155 | # define SOCKET_ERROR (-1)
|
---|
156 |
|
---|
157 | typedef struct iovec IOVEC;
|
---|
158 |
|
---|
159 | # define IOVEC_GET_BASE(iov) ((iov).iov_base)
|
---|
160 | # define IOVEC_SET_BASE(iov, b) ((iov).iov_base = (b))
|
---|
161 |
|
---|
162 | # define IOVEC_GET_LEN(iov) ((iov).iov_len)
|
---|
163 | # define IOVEC_SET_LEN(iov, l) ((iov).iov_len = (l))
|
---|
164 | # endif
|
---|
165 | #endif
|
---|