1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 
22 #ifndef UV_WIN_WINSOCK_H_
23 #define UV_WIN_WINSOCK_H_
24 
25 #include <winsock2.h>
26 #include <iptypes.h>
27 #include <mswsock.h>
28 #include <ws2tcpip.h>
29 #include <windows.h>
30 
31 #include "winapi.h"
32 
33 
34 /*
35  * MinGW is missing these too
36  */
37 #ifndef SO_UPDATE_CONNECT_CONTEXT
38 # define SO_UPDATE_CONNECT_CONTEXT 0x7010
39 #endif
40 
41 #ifndef TCP_KEEPALIVE
42 # define TCP_KEEPALIVE 3
43 #endif
44 
45 #ifndef IPV6_V6ONLY
46 # define IPV6_V6ONLY 27
47 #endif
48 
49 #ifndef IPV6_HOPLIMIT
50 # define IPV6_HOPLIMIT 21
51 #endif
52 
53 #ifndef SIO_BASE_HANDLE
54 # define SIO_BASE_HANDLE 0x48000022
55 #endif
56 
57 /*
58  * TDI defines that are only in the DDK.
59  * We only need receive flags so far.
60  */
61 #ifndef TDI_RECEIVE_NORMAL
62   #define TDI_RECEIVE_BROADCAST           0x00000004
63   #define TDI_RECEIVE_MULTICAST           0x00000008
64   #define TDI_RECEIVE_PARTIAL             0x00000010
65   #define TDI_RECEIVE_NORMAL              0x00000020
66   #define TDI_RECEIVE_EXPEDITED           0x00000040
67   #define TDI_RECEIVE_PEEK                0x00000080
68   #define TDI_RECEIVE_NO_RESPONSE_EXP     0x00000100
69   #define TDI_RECEIVE_COPY_LOOKAHEAD      0x00000200
70   #define TDI_RECEIVE_ENTIRE_MESSAGE      0x00000400
71   #define TDI_RECEIVE_AT_DISPATCH_LEVEL   0x00000800
72   #define TDI_RECEIVE_CONTROL_INFO        0x00001000
73   #define TDI_RECEIVE_FORCE_INDICATION    0x00002000
74   #define TDI_RECEIVE_NO_PUSH             0x00004000
75 #endif
76 
77 /*
78  * The "Auxiliary Function Driver" is the windows kernel-mode driver that does
79  * TCP, UDP etc. Winsock is just a layer that dispatches requests to it.
80  * Having these definitions allows us to bypass winsock and make an AFD kernel
81  * call directly, avoiding a bug in winsock's recvfrom implementation.
82  */
83 
84 #define AFD_NO_FAST_IO   0x00000001
85 #define AFD_OVERLAPPED   0x00000002
86 #define AFD_IMMEDIATE    0x00000004
87 
88 #define AFD_POLL_RECEIVE_BIT            0
89 #define AFD_POLL_RECEIVE                (1 << AFD_POLL_RECEIVE_BIT)
90 #define AFD_POLL_RECEIVE_EXPEDITED_BIT  1
91 #define AFD_POLL_RECEIVE_EXPEDITED      (1 << AFD_POLL_RECEIVE_EXPEDITED_BIT)
92 #define AFD_POLL_SEND_BIT               2
93 #define AFD_POLL_SEND                   (1 << AFD_POLL_SEND_BIT)
94 #define AFD_POLL_DISCONNECT_BIT         3
95 #define AFD_POLL_DISCONNECT             (1 << AFD_POLL_DISCONNECT_BIT)
96 #define AFD_POLL_ABORT_BIT              4
97 #define AFD_POLL_ABORT                  (1 << AFD_POLL_ABORT_BIT)
98 #define AFD_POLL_LOCAL_CLOSE_BIT        5
99 #define AFD_POLL_LOCAL_CLOSE            (1 << AFD_POLL_LOCAL_CLOSE_BIT)
100 #define AFD_POLL_CONNECT_BIT            6
101 #define AFD_POLL_CONNECT                (1 << AFD_POLL_CONNECT_BIT)
102 #define AFD_POLL_ACCEPT_BIT             7
103 #define AFD_POLL_ACCEPT                 (1 << AFD_POLL_ACCEPT_BIT)
104 #define AFD_POLL_CONNECT_FAIL_BIT       8
105 #define AFD_POLL_CONNECT_FAIL           (1 << AFD_POLL_CONNECT_FAIL_BIT)
106 #define AFD_POLL_QOS_BIT                9
107 #define AFD_POLL_QOS                    (1 << AFD_POLL_QOS_BIT)
108 #define AFD_POLL_GROUP_QOS_BIT          10
109 #define AFD_POLL_GROUP_QOS              (1 << AFD_POLL_GROUP_QOS_BIT)
110 
111 #define AFD_NUM_POLL_EVENTS             11
112 #define AFD_POLL_ALL                    ((1 << AFD_NUM_POLL_EVENTS) - 1)
113 
114 typedef struct _AFD_RECV_DATAGRAM_INFO {
115     LPWSABUF BufferArray;
116     ULONG BufferCount;
117     ULONG AfdFlags;
118     ULONG TdiFlags;
119     struct sockaddr* Address;
120     int* AddressLength;
121 } AFD_RECV_DATAGRAM_INFO, *PAFD_RECV_DATAGRAM_INFO;
122 
123 typedef struct _AFD_RECV_INFO {
124     LPWSABUF BufferArray;
125     ULONG BufferCount;
126     ULONG AfdFlags;
127     ULONG TdiFlags;
128 } AFD_RECV_INFO, *PAFD_RECV_INFO;
129 
130 
131 #define _AFD_CONTROL_CODE(operation, method) \
132     ((FSCTL_AFD_BASE) << 12 | (operation << 2) | method)
133 
134 #define FSCTL_AFD_BASE FILE_DEVICE_NETWORK
135 
136 #define AFD_RECEIVE            5
137 #define AFD_RECEIVE_DATAGRAM   6
138 #define AFD_POLL               9
139 
140 #define IOCTL_AFD_RECEIVE \
141     _AFD_CONTROL_CODE(AFD_RECEIVE, METHOD_NEITHER)
142 
143 #define IOCTL_AFD_RECEIVE_DATAGRAM \
144     _AFD_CONTROL_CODE(AFD_RECEIVE_DATAGRAM, METHOD_NEITHER)
145 
146 #define IOCTL_AFD_POLL \
147     _AFD_CONTROL_CODE(AFD_POLL, METHOD_BUFFERED)
148 
149 #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
150 typedef struct _IP_ADAPTER_UNICAST_ADDRESS_XP {
151   /* FIXME: __C89_NAMELESS was removed */
152   /* __C89_NAMELESS */ union {
153     ULONGLONG Alignment;
154     /* __C89_NAMELESS */ struct {
155       ULONG Length;
156       DWORD Flags;
157     };
158   };
159   struct _IP_ADAPTER_UNICAST_ADDRESS_XP *Next;
160   SOCKET_ADDRESS Address;
161   IP_PREFIX_ORIGIN PrefixOrigin;
162   IP_SUFFIX_ORIGIN SuffixOrigin;
163   IP_DAD_STATE DadState;
164   ULONG ValidLifetime;
165   ULONG PreferredLifetime;
166   ULONG LeaseLifetime;
167 } IP_ADAPTER_UNICAST_ADDRESS_XP,*PIP_ADAPTER_UNICAST_ADDRESS_XP;
168 
169 #endif
170 
171 #endif /* UV_WIN_WINSOCK_H_ */
172