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 <Windows.h>
|
---|
8 | # include <iprt/err.h>
|
---|
9 | # include <iprt/net.h>
|
---|
10 | # include <iprt/log.h>
|
---|
11 | /**
|
---|
12 | * Inclusion of lwip/def.h was added here to avoid conflict of definitions
|
---|
13 | * of hton-family functions in LWIP and windock's headers.
|
---|
14 | */
|
---|
15 | # include <lwip/def.h>
|
---|
16 |
|
---|
17 | # ifndef PF_LOCAL
|
---|
18 | # define PF_LOCAL AF_INET
|
---|
19 | # endif
|
---|
20 |
|
---|
21 | # define warn(...) DPRINTF2((__VA_ARGS__))
|
---|
22 | # define warnx warn
|
---|
23 | # ifdef DEBUG
|
---|
24 | # define err(code,...) do { \
|
---|
25 | AssertMsgFailed((__VA_ARGS__)); \
|
---|
26 | }while(0)
|
---|
27 | #else
|
---|
28 | # define err(code,...) do { \
|
---|
29 | DPRINTF0((__VA_ARGS__)); \
|
---|
30 | ExitProcess(code); \
|
---|
31 | }while(0)
|
---|
32 | #endif
|
---|
33 | # define errx err
|
---|
34 | # define __func__ __FUNCTION__
|
---|
35 | # define __attribute__(x) /* IGNORE */
|
---|
36 | # define inet_ntop(dom, pvaddr, pstrbuf, cbstrbuf) InetNtop((dom),(pvaddr),(pstrbuf),(cbstrbuf))
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * inet_pton(3) returns 1 on success (network address was successfully
|
---|
40 | * converted). 0 is returned if src does not contain a character string
|
---|
41 | * representing a valid network address in the specified address family.
|
---|
42 | * If af does not contain a valid address family, -1 is returned and
|
---|
43 | * errno is set to EAFNOSUPPORT(value 97).
|
---|
44 | * WSA has value most close in our case
|
---|
45 | * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx#WSAEAFNOSUPPORT (10047)
|
---|
46 | */
|
---|
47 | DECLINLINE(int) inet_pton(int af_family, const char *str, void *dst)
|
---|
48 | {
|
---|
49 | AssertPtrReturn(str, 0);
|
---|
50 | AssertPtrReturn(dst, 0);
|
---|
51 |
|
---|
52 | switch (af_family) {
|
---|
53 | case AF_INET: {
|
---|
54 | RTNETADDRIPV4 a4;
|
---|
55 | int rc = RTNetStrToIPv4Addr(str, &a4);
|
---|
56 | AssertRCReturn(rc, -1);
|
---|
57 | memcpy(dst, &a4, sizeof(RTNETADDRIPV4));
|
---|
58 | break;
|
---|
59 | }
|
---|
60 | case AF_INET6: {
|
---|
61 | #if 0
|
---|
62 | RTNETADDRIPV6 a6;
|
---|
63 | int rc = RTNetStrToIPv6Addr(str, &a6);
|
---|
64 | AssertRCReturn(rc, -1);
|
---|
65 | memcpy(dst, &a6, sizeof(RTNETADDRIPV6));
|
---|
66 | #else
|
---|
67 | /* XXX: RTNetStrToIPv6Addr isn't implemented yet. */
|
---|
68 | WSASetLastError(WSAEAFNOSUPPORT);
|
---|
69 | return -1;
|
---|
70 | #endif
|
---|
71 | break;
|
---|
72 | }
|
---|
73 | default:
|
---|
74 | WSASetLastError(WSAEAFNOSUPPORT);
|
---|
75 | AssertMsgFailedReturn(("Unsupported internet family"), -1);
|
---|
76 | }
|
---|
77 | return 1;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * tftpd emulation we're using POSIX operations which needs "DOS errno". see proxy_tftpd.c
|
---|
82 | */
|
---|
83 | # ifndef _USE_WINSTD_ERRNO
|
---|
84 | /**
|
---|
85 | * http://msdn.microsoft.com/en-us/library/windows/desktop/ms737828(v=vs.85).aspx
|
---|
86 | * "Error Codes - errno, h_errno and WSAGetLastError" says "Error codes set by Windows Sockets are
|
---|
87 | * not made available through the errno variable."
|
---|
88 | */
|
---|
89 | # include <errno.h>
|
---|
90 | # ifdef errno
|
---|
91 | # undef errno
|
---|
92 | # endif
|
---|
93 | # define errno (WSAGetLastError())
|
---|
94 | # endif
|
---|
95 | /* Missing errno codes */
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx#WSAEHOSTDOWN
|
---|
99 | * Corresponds to WSAEHOSTDOWN (10064) WSAGetLastError()
|
---|
100 | */
|
---|
101 | # define EHOSTDOWN WSAEHOSTDOWN
|
---|
102 | /**
|
---|
103 | * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx#WSAEHOSTUNREACH
|
---|
104 | */
|
---|
105 | # define EHOSTUNREAH WSAEHOSTUNREACH
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * parameters to shutdown (2) with Winsock2
|
---|
109 | * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740481(v=vs.85).aspx
|
---|
110 | */
|
---|
111 | # define SHUT_RD SD_RECEIVE
|
---|
112 | # define SHUT_WR SD_SEND
|
---|
113 | # define SHUT_RDWR SD_BOTH
|
---|
114 |
|
---|
115 | typedef int socklen_t;
|
---|
116 |
|
---|
117 | typedef ULONG nfds_t;
|
---|
118 |
|
---|
119 | typedef WSABUF IOVEC;
|
---|
120 |
|
---|
121 | # define IOVEC_GET_BASE(iov) ((iov).buf)
|
---|
122 | # define IOVEC_SET_BASE(iov, b) ((iov).buf = (b))
|
---|
123 |
|
---|
124 | # define IOVEC_GET_LEN(iov) ((iov).len)
|
---|
125 | # define IOVEC_SET_LEN(iov, l) ((iov).len = (ULONG)(l))
|
---|
126 |
|
---|
127 | RT_C_DECLS_BEGIN
|
---|
128 | int RTWinSocketPair(int domain, int type, int protocol, SOCKET socket_vector[2]);
|
---|
129 | RT_C_DECLS_END
|
---|
130 |
|
---|
131 | # else /* !RT_OS_WINDOWS */
|
---|
132 | # define ioctlsocket ioctl
|
---|
133 | # define closesocket close
|
---|
134 | # define SOCKET int
|
---|
135 | # define INVALID_SOCKET (-1)
|
---|
136 | # define SOCKET_ERROR (-1)
|
---|
137 |
|
---|
138 | typedef struct iovec IOVEC;
|
---|
139 |
|
---|
140 | # define IOVEC_GET_BASE(iov) ((iov).iov_base)
|
---|
141 | # define IOVEC_SET_BASE(iov, b) ((iov).iov_base = (b))
|
---|
142 |
|
---|
143 | # define IOVEC_GET_LEN(iov) ((iov).iov_len)
|
---|
144 | # define IOVEC_SET_LEN(iov, l) ((iov).iov_len = (l))
|
---|
145 | # endif
|
---|
146 | #endif
|
---|