forked from cloudbase/wnbd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathberkeley.c
698 lines (532 loc) · 13.2 KB
/
berkeley.c
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
#include "berkeley.h"
#include "ksocket.h"
//////////////////////////////////////////////////////////////////////////
// Definitions.
//////////////////////////////////////////////////////////////////////////
#define MEMORY_TAG ' bsK'
#define SOCKETFD_MAX 512
#define TO_SOCKETFD(index) ((index % SOCKETFD_MAX) + 1)
#define FROM_SOCKETFD(sockfd) ((sockfd) - 1)
//////////////////////////////////////////////////////////////////////////
// Function prototypes.
//////////////////////////////////////////////////////////////////////////
NTSTATUS
NTAPI
KspUtilAddrInfoToAddrInfoEx(
_In_ PADDRINFOA AddrInfo,
_Out_ PADDRINFOEXW* AddrInfoEx
);
NTSTATUS
NTAPI
KspUtilAddrInfoExToAddrInfo(
_In_ PADDRINFOEXW AddrInfoEx,
_Out_ PADDRINFOA* AddrInfo
);
VOID
NTAPI
KspUtilFreeAddrInfo(
_In_ PADDRINFOA AddrInfo
);
VOID
NTAPI
KspUtilFreeAddrInfoEx(
_In_ PADDRINFOEXW AddrInfo
);
//////////////////////////////////////////////////////////////////////////
// Variables.
//////////////////////////////////////////////////////////////////////////
PKSOCKET KsArray[SOCKETFD_MAX] = { 0 };
ULONG KsIndex = 0;
//////////////////////////////////////////////////////////////////////////
// Private functions.
//////////////////////////////////////////////////////////////////////////
NTSTATUS
NTAPI
KspUtilAddrInfoToAddrInfoEx(
_In_ PADDRINFOA AddrInfo,
_Out_ PADDRINFOEXW* AddrInfoEx
)
{
NTSTATUS Status;
//
// Convert NULL input into NULL output.
//
if (AddrInfo == NULL)
{
*AddrInfoEx = NULL;
return STATUS_SUCCESS;
}
//
// Allocate memory for the output structure.
//
PADDRINFOEXW Result = ExAllocatePoolWithTag(PagedPool, sizeof(ADDRINFOEXW), MEMORY_TAG);
if (Result == NULL)
{
Status = STATUS_INSUFFICIENT_RESOURCES;
goto Error1;
}
//
// Copy numeric values.
//
RtlZeroMemory(Result, sizeof(ADDRINFOEXW));
Result->ai_flags = AddrInfo->ai_flags;
Result->ai_family = AddrInfo->ai_family;
Result->ai_socktype = AddrInfo->ai_socktype;
Result->ai_protocol = AddrInfo->ai_protocol;
Result->ai_addrlen = AddrInfo->ai_addrlen;
//
// Copy canonical name.
//
ANSI_STRING CanonicalNameAnsi;
RtlInitAnsiString(&CanonicalNameAnsi, "");
UNICODE_STRING CanonicalNameUnicode;
if (AddrInfo->ai_canonname)
{
RtlInitAnsiString(&CanonicalNameAnsi, AddrInfo->ai_canonname);
Status = RtlAnsiStringToUnicodeString(&CanonicalNameUnicode, &CanonicalNameAnsi, TRUE);
if (!NT_SUCCESS(Status))
{
goto Error2;
}
Result->ai_canonname = CanonicalNameUnicode.Buffer;
}
//
// Copy address.
//
Result->ai_addr = AddrInfo->ai_addr;
//
// Copy the next structure (recursively).
//
PADDRINFOEXW NextAddrInfo;
Status = KspUtilAddrInfoToAddrInfoEx(AddrInfo->ai_next, &NextAddrInfo);
if (!NT_SUCCESS(Status))
{
goto Error3;
}
Result->ai_next = NextAddrInfo;
//
// All done!
//
*AddrInfoEx = Result;
return Status;
Error3:
RtlFreeAnsiString(&CanonicalNameAnsi);
Error2:
ExFreePoolWithTag(Result, MEMORY_TAG);
Error1:
return Status;
}
NTSTATUS
NTAPI
KspUtilAddrInfoExToAddrInfo(
_In_ PADDRINFOEXW AddrInfoEx,
_Out_ PADDRINFOA* AddrInfo
)
{
NTSTATUS Status;
//
// Convert NULL input into NULL output.
//
if (AddrInfoEx == NULL)
{
*AddrInfo = NULL;
return STATUS_SUCCESS;
}
//
// Allocate memory for the output structure.
//
PADDRINFOA Result = ExAllocatePoolWithTag(PagedPool, sizeof(ADDRINFOA), MEMORY_TAG);
if (Result == NULL)
{
Status = STATUS_INSUFFICIENT_RESOURCES;
goto Error1;
}
//
// Copy numeric values.
//
RtlZeroMemory(Result, sizeof(ADDRINFOA));
Result->ai_flags = AddrInfoEx->ai_flags;
Result->ai_family = AddrInfoEx->ai_family;
Result->ai_socktype = AddrInfoEx->ai_socktype;
Result->ai_protocol = AddrInfoEx->ai_protocol;
Result->ai_addrlen = AddrInfoEx->ai_addrlen;
Result->ai_addr = ExAllocatePoolWithTag(PagedPool, sizeof(struct sockaddr), MEMORY_TAG);
if (Result->ai_addr == NULL)
{
Status = STATUS_INSUFFICIENT_RESOURCES;
goto Error2;
}
//
// Copy canonical name.
//
UNICODE_STRING CanonicalNameUnicode;
ANSI_STRING CanonicalNameAnsi;
RtlInitAnsiString(&CanonicalNameAnsi, "");
if (AddrInfoEx->ai_canonname)
{
RtlInitUnicodeString(&CanonicalNameUnicode, AddrInfoEx->ai_canonname);
Status = RtlUnicodeStringToAnsiString(&CanonicalNameAnsi, &CanonicalNameUnicode, TRUE);
if (!NT_SUCCESS(Status))
{
goto Error2;
}
Result->ai_canonname = CanonicalNameAnsi.Buffer;
}
//
// Copy address.
//
RtlCopyMemory(Result->ai_addr, AddrInfoEx->ai_addr, sizeof(struct sockaddr));
//
// Copy the next structure (recursively).
//
PADDRINFOA NextAddrInfo;
Status = KspUtilAddrInfoExToAddrInfo(AddrInfoEx->ai_next, &NextAddrInfo);
if (!NT_SUCCESS(Status))
{
goto Error3;
}
Result->ai_next = NextAddrInfo;
//
// All done!
//
*AddrInfo = Result;
return Status;
Error3:
RtlFreeAnsiString(&CanonicalNameAnsi);
ExFreePoolWithTag(Result->ai_addr, MEMORY_TAG);
Error2:
ExFreePoolWithTag(Result, MEMORY_TAG);
Error1:
return Status;
}
VOID
NTAPI
KspUtilFreeAddrInfo(
_In_ PADDRINFOA AddrInfo
)
{
//
// Free all structures recursively.
//
if (AddrInfo->ai_next)
{
KspUtilFreeAddrInfo(AddrInfo->ai_next);
}
//
// Free the canonical name buffer.
//
if (AddrInfo->ai_canonname)
{
ANSI_STRING CanonicalName;
RtlInitAnsiString(&CanonicalName, AddrInfo->ai_canonname);
RtlFreeAnsiString(&CanonicalName);
}
if (AddrInfo->ai_addr)
{
ExFreePoolWithTag(AddrInfo->ai_addr, MEMORY_TAG);
}
//
// Finally, free the structure itself.
//
ExFreePoolWithTag(AddrInfo, MEMORY_TAG);
}
VOID
NTAPI
KspUtilFreeAddrInfoEx(
_In_ PADDRINFOEXW AddrInfo
)
{
//
// Free all structures recursively.
//
if (AddrInfo->ai_next)
{
KspUtilFreeAddrInfoEx(AddrInfo->ai_next);
}
//
// Free the canonical name buffer.
//
if (AddrInfo->ai_canonname)
{
UNICODE_STRING CanonicalName;
RtlInitUnicodeString(&CanonicalName, AddrInfo->ai_canonname);
RtlFreeUnicodeString(&CanonicalName);
}
//
// Finally, free the structure itself.
//
ExFreePoolWithTag(AddrInfo, MEMORY_TAG);
}
//////////////////////////////////////////////////////////////////////////
// Public functions.
//////////////////////////////////////////////////////////////////////////
uint32_t htonl(uint32_t hostlong)
{
return RtlUlongByteSwap(hostlong);
}
uint16_t htons(uint16_t hostshort)
{
return RtlUshortByteSwap(hostshort);
}
uint32_t ntohl(uint32_t netlong)
{
return RtlUlongByteSwap(netlong);
}
uint16_t ntohs(uint16_t netshort)
{
return RtlUshortByteSwap(netshort);
}
int GetAddrInfo(const char* node, const char* service, const struct addrinfo* hints, struct addrinfo** res)
{
NTSTATUS Status;
//
// Convert node name to the UNICODE_STRING (if present).
//
ANSI_STRING NodeNameAnsi;
UNICODE_STRING NodeNameUnicode = { 0 };
PUNICODE_STRING NodeName = NULL;
RtlInitAnsiString(&NodeNameAnsi, "");
RtlAnsiStringToUnicodeString(&NodeNameUnicode, &NodeNameAnsi, TRUE);
if (node)
{
RtlFreeUnicodeString(&NodeNameUnicode);
RtlInitAnsiString(&NodeNameAnsi, node);
Status = RtlAnsiStringToUnicodeString(&NodeNameUnicode, &NodeNameAnsi, TRUE);
if (!NT_SUCCESS(Status))
{
goto Error1;
}
NodeName = &NodeNameUnicode;
}
//
// Convert service name to the UNICODE_STRING (if present).
//
ANSI_STRING ServiceNameAnsi;
RtlInitAnsiString(&ServiceNameAnsi, "");
UNICODE_STRING ServiceNameUnicode = { 0 };
RtlAnsiStringToUnicodeString(&ServiceNameUnicode, &ServiceNameAnsi, TRUE);
PUNICODE_STRING ServiceName = NULL;
if (service)
{
RtlFreeUnicodeString(&ServiceNameUnicode);
RtlInitAnsiString(&ServiceNameAnsi, service);
Status = RtlAnsiStringToUnicodeString(&ServiceNameUnicode, &ServiceNameAnsi, TRUE);
if (!NT_SUCCESS(Status))
{
goto Error2;
}
ServiceName = &ServiceNameUnicode;
}
//
// Convert "struct addrinfo" to the "ADDRINFOEXW".
//
PADDRINFOEXW Hints;
Status = KspUtilAddrInfoToAddrInfoEx((PADDRINFOA)hints, &Hints);
if (!NT_SUCCESS(Status))
{
goto Error3;
}
//
// All data is prepared, call the underlying API.
//
PADDRINFOEXW Result;
Status = KsGetAddrInfo(NodeName, ServiceName, Hints, &Result);
//
// Free the memory of the converted "Hints".
//
KspUtilFreeAddrInfoEx(Hints);
if (!NT_SUCCESS(Status))
{
goto Error3;
}
//
// Convert the result "ADDRINFOEXW" to the "struct addrinfo".
//
Status = KspUtilAddrInfoExToAddrInfo(Result, res);
//
// Free the original result.
//
KsFreeAddrInfo(Result);
if (!NT_SUCCESS(Status))
{
goto Error3;
}
//return STATUS_SUCCESS;
Status = STATUS_SUCCESS;
Error3:
RtlFreeUnicodeString(&ServiceNameUnicode);
Error2:
RtlFreeUnicodeString(&NodeNameUnicode);
Error1:
return Status;
}
void FreeAddrInfo(struct addrinfo *res)
{
//
// Call our implementation.
//
KspUtilFreeAddrInfo(res);
}
int Socket(int domain, int type, int protocol)
{
NTSTATUS Status;
PKSOCKET Socket;
Status = KsCreateConnectionSocket(
&Socket,
(ADDRESS_FAMILY)domain,
(USHORT)type,
(ULONG)protocol
);
if (NT_SUCCESS(Status))
{
int sockfd = TO_SOCKETFD(KsIndex++);
KsArray[FROM_SOCKETFD(sockfd)] = Socket;
return sockfd;
}
return -1;
}
int socket_listen(int domain, int type, int protocol)
{
NTSTATUS Status;
PKSOCKET Socket;
//
// WskSocket() returns STATUS_PROTOCOL_UNREACHABLE (0xC000023E)
// when Protocol == 0, so coerce this value to IPPROTO_TCP here.
//
Status = KsCreateListenSocket(
&Socket,
(ADDRESS_FAMILY)domain,
(USHORT)type,
protocol ? (ULONG)protocol : IPPROTO_TCP
);
if (NT_SUCCESS(Status))
{
int sockfd = TO_SOCKETFD(KsIndex++);
KsArray[FROM_SOCKETFD(sockfd)] = Socket;
return sockfd;
}
return -1;
}
int socket_datagram(int domain, int type, int protocol)
{
NTSTATUS Status;
PKSOCKET Socket;
Status = KsCreateDatagramSocket(
&Socket,
(ADDRESS_FAMILY)domain,
(USHORT)type,
(ULONG)protocol
);
if (NT_SUCCESS(Status))
{
int sockfd = TO_SOCKETFD(KsIndex++);
KsArray[FROM_SOCKETFD(sockfd)] = Socket;
return sockfd;
}
return -1;
}
int Connect(int sockfd, const struct sockaddr* addr, socklen_t addrlen)
{
UNREFERENCED_PARAMETER(addrlen);
NTSTATUS Status;
PKSOCKET Socket = KsArray[FROM_SOCKETFD(sockfd)];
Status = KsConnect(Socket, (PSOCKADDR)addr);
return NT_SUCCESS(Status)
? 0
: -1;
}
int Listen(int sockfd, int backlog)
{
UNREFERENCED_PARAMETER(sockfd);
UNREFERENCED_PARAMETER(backlog);
return 0;
}
int Bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
UNREFERENCED_PARAMETER(addrlen);
NTSTATUS Status;
PKSOCKET Socket = KsArray[FROM_SOCKETFD(sockfd)];
Status = KsBind(Socket, (PSOCKADDR)addr);
return NT_SUCCESS(Status)
? 0
: -1;
}
int Accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
{
NTSTATUS Status;
PKSOCKET Socket = KsArray[FROM_SOCKETFD(sockfd)];
PKSOCKET NewSocket;
Status = KsAccept(Socket, &NewSocket, NULL, (PSOCKADDR)addr);
*addrlen = sizeof(SOCKADDR);
if (NT_SUCCESS(Status))
{
int newsockfd = TO_SOCKETFD(KsIndex++);
KsArray[FROM_SOCKETFD(newsockfd)] = NewSocket;
return newsockfd;
}
return -1;
}
int Send(int sockfd, const void* buf, size_t len, int flags, PNTSTATUS error)
{
NTSTATUS Status;
PKSOCKET Socket = KsArray[FROM_SOCKETFD(sockfd)];
ULONG Length = (ULONG)len;
Status = KsSend(Socket, (PVOID)buf, &Length, flags);
*error = Status;
return NT_SUCCESS(Status)
? (int)Length
: -1;
}
int SendTo(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen)
{
UNREFERENCED_PARAMETER(addrlen);
NTSTATUS Status;
PKSOCKET Socket = KsArray[FROM_SOCKETFD(sockfd)];
ULONG Length = (ULONG)len;
Status = KsSendTo(Socket, (PVOID)buf, &Length, (ULONG)flags, (PSOCKADDR)dest_addr);
return NT_SUCCESS(Status)
? (int)Length
: -1;
}
int Recv(int sockfd, void* buf, size_t len, int flags, PNTSTATUS error)
{
NTSTATUS Status;
PKSOCKET Socket = KsArray[FROM_SOCKETFD(sockfd)];
ULONG Length = (ULONG)len;
Status = KsRecv(Socket, (PVOID)buf, &Length, (ULONG)flags);
*error = Status;
return NT_SUCCESS(Status)
? (int)Length
: -1;
}
int RecvFrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen)
{
UNREFERENCED_PARAMETER(addrlen);
NTSTATUS Status;
PKSOCKET Socket = KsArray[FROM_SOCKETFD(sockfd)];
ULONG Length = (ULONG)len;
Status = KsSendTo(Socket, (PVOID)buf, &Length, (ULONG)flags, (PSOCKADDR)src_addr);
*addrlen = sizeof(SOCKADDR);
return NT_SUCCESS(Status)
? (int)Length
: -1;
}
int Disconnect(int sockfd)
{
NTSTATUS Status;
PKSOCKET Socket = KsArray[FROM_SOCKETFD(sockfd)];
Status = KsDisconnectSocket(Socket);
return NT_SUCCESS(Status)
? 0
: -1;
}
int Close(int sockfd)
{
NTSTATUS Status;
PKSOCKET Socket = KsArray[FROM_SOCKETFD(sockfd)];
Status = KsCloseSocket(Socket);
KsArray[FROM_SOCKETFD(sockfd)] = NULL;
return NT_SUCCESS(Status)
? 0
: -1;
}