-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnapi.h
435 lines (376 loc) · 9.57 KB
/
napi.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
* napi.h
*
*/
#pragma once
#pragma warning(disable:4200)
#include "nt/nt.h"
#include <ws2def.h>
#include <mstcpip.h>
#include <ws2tcpip.h>
//
// Socket types
//
#define __MAKE_SOCKET_PARAMS(type, proto) ((ULONG(type) << 16) | ULONG(proto))
#define __SOCKET_PARAMS_GET_TYPE(params) (params >> 16)
#define __SOCKET_PARAMS_GET_PROTO(params) (params & 0xFFFF)
#define SOCK_UDP __MAKE_SOCKET_PARAMS(SOCK_DGRAM, IPPROTO_UDP)
#define SOCK_TCP __MAKE_SOCKET_PARAMS(SOCK_STREAM, IPPROTO_TCP)
//
// Socket options
//
#define __MAKE_SOCKET_OPTION(level, name) ((ULONG(level) << 16) | ULONG(name))
#define __SOCKET_OPTION_GET_LEVEL(params) (params >> 16)
#define __SOCKET_OPTION_GET_NAME(params) (params & 0xFFFF)
// SOL_SOCKET
#undef SO_SNDBUF
#define SO_SNDBUF __MAKE_SOCKET_OPTION(SOL_SOCKET, 0x1001)
#undef SO_RCVBUF
#define SO_RCVBUF __MAKE_SOCKET_OPTION(SOL_SOCKET, 0x1002)
// IPPROTO_TCP
#undef TCP_NODELAY
#define TCP_NODELAY __MAKE_SOCKET_OPTION(IPPROTO_TCP, 0x0001)
// Bind dispositions
#define BIND_SHARE_NORMAL 0 /* Do not reuse address if already in use but allow subsequent reuse by others */
#define BIND_SHARE_REUSE 1 /* Reuse address if necessary */
#define BIND_SHARE_WILDCARD 2 /* Address is a wildcard, no checking. */
#define BIND_SHARE_EXCLUSIVE 3 /* Do not allow reuse of this address (elevated token required). */
// Reserved trailing buffer size for address control information returned from NxAccept operations.
#define MAX_NETWORK_ADDRESS_SZ 32
#define MAX_ENDPOINT_ADDRESS_SZ (MAX_NETWORK_ADDRESS_SZ*2)
// Socket creation flags
#define SOCK_FLAG_RIO (1<<0) /* Enable support for Registered I/O with this socket. */
// TODO: AFD internals
#define AFD_PARTIAL_DISCONNECT_SEND (1<<0)
#define AFD_PARTIAL_DISCONNECT_RECEIVE (1<<1)
#define AFD_ABORTIVE_DISCONNECT (1<<2)
#define AFD_UNCONNECT_DATAGRAM (1<<3)
// Disconnect (shutdown) modes
#define SHUTDOWN_RECEIVE AFD_PARTIAL_DISCONNECT_RECEIVE /* Terminates reception. If there is any pending receive data this causes an abortive termination. */
#define SHUTDOWN_SEND AFD_PARTIAL_DISCONNECT_SEND /* Terminates transmission, initiating a graceful teardown of the virtual circuit. */
#define SHUTDOWN_ABORTIVE AFD_ABORTIVE_DISCONNECT /* Causes an abortive virtual circuit termination. It is not necessary (albeit benign) to specify other flags in conjunction. */
//
// Registered I/O
//
// Completion notification parameters
#define RIO_NOTIFY_EVENT 1
#define RIO_NOTIFY_PORT 2
struct RIO_NOTIFY
{
ULONG Type;
union
{
struct
{
HANDLE EventHandle;
BOOL NotifyReset;
} Event;
struct
{
HANDLE PortHandle;
PVOID Context;
IO_STATUS_BLOCK* IoStatusBlock;
} IOCP;
};
};
// Buffer descriptor
struct RIO_BUF
{
ULONG RegionId;
ULONG Offset;
ULONG Length;
};
// Request ring entry
struct RIO_REQUEST_ENTRY
{
RIO_BUF Data;
RIO_BUF LocalAddr;
RIO_BUF RemoteAddr;
RIO_BUF Control;
RIO_BUF ReceiveFlags;
ULONG Flags;
PVOID64 Context;
};
// Completion ring entry
struct RIO_COMPLETION_ENTRY
{
ULONG Status;
ULONG Information;
PVOID64 Context;
PVOID64 RequestContext;
};
// Ring control area
__declspec(align(16)) struct RIO_RING
{
ULONG Head;
ULONG Tail;
};
// Request ring layout.
struct RIO_REQUEST_QUEUE
{
RIO_RING Hdr;
RIO_REQUEST_ENTRY Ring[];
};
// Completion ring layout
struct RIO_COMPLETION_QUEUE
{
RIO_RING Hdr;
RIO_COMPLETION_ENTRY Ring[];
};
// SGIO buffer descriptor
struct NETBUF
{
ULONG Length;
VOID* Data;
};
/*
* NxSocket
* Creates a socket.
*
* Parameters:
* [in] 'Family' -- Specifies the address family; see AF_*
* [in] 'Protocol' -- Specifies the transport protocol; see SOCK_*
* [in] 'Flags' -- Various flags; see SOCK_FLAG_*
* [out] 'SocketHandle' -- Receives the resulting socket on success. Undefined on failure.
*
* Return:
* On success returns STATUS_SUCCESS, elsewise returns an error status code.
*
*/
NTSTATUS NxSocket
(
USHORT Family,
ULONG Protocol,
ULONG Flags,
HANDLE* SocketHandle
);
/*
* NxBind
* Bind socket to local address.
*
* Parameters:
* [in] 'Socket' -- Handle to the socket to be bound.
* [in] 'Address' -- Pointer to the address of which to bind the socket to.
* [in] 'AddressLength' -- Length of the address pointed to by 'Address'.
* [in] 'Share' -- Address sharing mode; see: BIND_SHARE_*
*
* Return:
* On success the return value is zero, elsewise on failure the return value is a status
* code indicating the error condition.
*
*/
NTSTATUS NxBind
(
HANDLE Socket,
sockaddr* Address,
ULONG AddressLength,
ULONG Share
);
/*
* NxListen
* Enable a connection-oriented socket to begin accepting connections. The socket must
* be bound prior to this call.
*
* Parameters:
* [in] 'Socket' -- Handle to the socket to begin listening.
* [in] 'Backlog' -- Hint of the internal maximum size of the accept queue. May be zero.
*
* Return:
* On success returns STATUS_SUCCESS, elsewise returns an error status code.
*
* Remarks:
* Similarly to socket buffers, the accept queue functions as a fallback for kernel socket
* allocations that cannot be satisfied by a pending user accept operation.
*
*/
NTSTATUS NxListen
(
HANDLE hSocket,
ULONG Backlog
);
/*
* NxShutdown
* Controls virtual circuit finalization.
*
* Parameters:
* [in] 'Socket' -- Handle to the socket to shutdown.
* [in] 'Flags' -- Flags controlling the shutdown behaviour; see: SHUTDOWN_*
*
* Return:
* On success returns STATUS_SUCCESS, elsewise returns an error status code.
*
*/
NTSTATUS NxShutdown
(
HANDLE hSocket,
ULONG Flags
);
/*
* NxConnect
* Initiate connection to remote endpoint.
*
* Parameters:
* [in] 'Socket' -- The socket to connect.
* [in] 'IoStatus' --
*/
NTSTATUS NxConnect
(
HANDLE hSocket,
IO_STATUS_BLOCK* IoStatus,
sockaddr* Address,
USHORT AddressLength
);
/*
* NxAccept
*
*/
NTSTATUS NxAccept
(
HANDLE hSocket,
IO_STATUS_BLOCK* IoStatus,
HANDLE hAcceptSocket,
PVOID Buffer,
ULONG BufferLength
);
/*
* NxSend
*
*/
NTSTATUS NxSend
(
HANDLE hSocket,
IO_STATUS_BLOCK* IoStatus,
NETBUF* Buffers,
ULONG BufferCount
);
/*
* NxReceive
*
*/
NTSTATUS NxReceive
(
HANDLE hSocket,
IO_STATUS_BLOCK* IoStatus,
NETBUF* Buffers,
ULONG BufferCount,
ULONG Flags
);
/*
* NxSetOption
*
*/
NTSTATUS NxSetOption
(
HANDLE hSocket,
ULONG Option,
ULONG Value
);
/*
* NxIoControl
*
*/
NTSTATUS NxIoControl
(
HANDLE Socket,
IO_STATUS_BLOCK* IoStatus,
ULONG IoControlCode,
PVOID InputBuffer,
ULONG InputLength,
PVOID OutputBuffer,
ULONG OutputLength,
ULONG* OutputReturnedLength
);
/*
* NxEnableRegisteredIo
* Required one-shot initialization for subsequent usage of the Registered I/O (RIO) API.
*
* Parameters:
* None.
*
* Return:
* On success returns STATUS_SUCCESS, elsewise returns an error status code.
*
*/
NTSTATUS NxEnableRegisteredIo();
/*
* NxRegisterIoRegion
* Registers a region of memory to enable it to be used as the source/destination for buffers passed
* to RIO send/receive operations.
*
* Parameters:
* [in] 'RegionBase' -- Pointer to the base address of the region to be registered.
* [in] 'Size' -- Size of the region, in bytes.
* [out] 'RegionId' -- Receives an identifier that uniquely identifies the region.
*
* Return:
* On success returns STATUS_SUCCESS, elsewise returns an error status code.
*
*/
NTSTATUS NxRegisterIoRegion
(
PVOID RegionBase,
ULONG Size,
ULONG* RegionId
);
/*
* NxRegisterCompletionRing
* Registers a ring which will receive I/O completion notifications.
*
* Parameters:
* [in] 'Ring' -- A pointer to the region of memory which will serve as the completion ring.
* [in] 'Size' -- Size of the ring, in entries.
* [in] 'Notify' -- Specifies parameters which control the mechanism by which the kernel notifies of entry insertion.
* [out] 'RingId' -- Receives an identifier that uniquely identifies the ring.
*
* Return:
* On success returns STATUS_SUCCESS, elsewise returns an error status code.
*
*/
NTSTATUS NxRegisterCompletionRing
(
PVOID Ring,
ULONG Size,
RIO_NOTIFY* Notify,
ULONG* RingId
);
/*
* NxRegisterRequestRing
* Registers a pair of rings for submitting send and receive operations on a socket.
*
* Parameters:
* [in] 'Socket' -- The socket for which the rings are to be bound to.
* [in, opt] 'TxRing' -- A pointer to the ring to be used for queuing send operations.
* [in, opt] 'TxRingSize' -- The size of 'TxRing', in entries.
* [in, opt] 'TxCompletionRingId' -- The completion ring which will receive notifications of send operation completion.
* [in, opt] 'RxRing' -- A pointer to the ring to be used for queuing receive operations.
* [in, opt] 'RxRingSize' -- The size of 'RxRing', in entries.
* [in, opt] 'RxCompletionRingId' -- The completion ring which will receive notifications of receive operation completion.
* [in, opt] 'Context' -- User context associated with the socke,
*
* Return:
* On success returns STATUS_SUCCESS, elsewise returns an error status code.
*
*/
NTSTATUS NxRegisterRequestRing
(
HANDLE Socket,
PVOID TxRing,
ULONG TxRingSize,
ULONG TxCompletionRingId,
PVOID RxRing,
ULONG RxRingSize,
ULONG RxCompletionRingId,
PVOID Context
);
/*
* NxPokeTx
* Submits accumulated send operations in the provided socket's transmit ring.
*
*/
NTSTATUS NxPokeTx(HANDLE Socket);
/*
* NxPokeRx
* Submits accumulated receive operations in the provided socket's receive ring.
*
*/
NTSTATUS NxPokeRx(HANDLE Socket);