forked from openssh/openssh-portable
-
Notifications
You must be signed in to change notification settings - Fork 399
Expand file tree
/
Copy pathsocketio.c
More file actions
1098 lines (983 loc) · 30.8 KB
/
Copy pathsocketio.c
File metadata and controls
1098 lines (983 loc) · 30.8 KB
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Author: Manoj Ampalam <manoj.ampalam@microsoft.com>
*
* Copyright (c) 2015 Microsoft Corp.
* All rights reserved
*
* Microsoft openssh win32 port
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <winsock2.h>
#include <ws2tcpip.h>
#include <mswsock.h>
#include <errno.h>
#include <VersionHelpers.h>
#include <stddef.h>
#include "w32fd.h"
#include "inc\utf.h"
#include "misc_internal.h"
#include "debug.h"
#include "../../../config.h"
#ifdef HAVE_AFUNIX_H
#include <afunix.h>
#endif
#define INTERNAL_SEND_BUFFER_SIZE 70*1024 //70KB
#define INTERNAL_RECV_BUFFER_SIZE 70*1024 //70KB
#define errno_from_WSALastError() errno_from_WSAError(WSAGetLastError())
/* state info that needs to be persisted for an inprocess acceptEx call*/
struct acceptEx_context {
char lpOutputBuf[1024];
SOCKET accept_socket;
LPFN_ACCEPTEX lpfnAcceptEx;
LPFN_GETACCEPTEXSOCKADDRS lpfnGuidGetAcceptExSockaddrs;
DWORD bytes_received;
};
/* maps WSAError to errno */
static int
errno_from_WSAError(int wsaerrno)
{
/* TODO - create a mapping table - more efficient */
switch (wsaerrno) {
case WSAEWOULDBLOCK:
return EAGAIN;
case WSAEFAULT:
return EFAULT;
case WSAEINVAL:
return EINVAL;
case WSAECONNABORTED:
return ECONNABORTED;
case WSAETIMEDOUT:
return ETIMEDOUT;
case WSAECONNREFUSED:
return ECONNREFUSED;
case WSAEINPROGRESS:
return EINPROGRESS;
case WSAESHUTDOWN:
return ECONNRESET;
case WSAENOTCONN:
return ENOTCONN;
case WSAECONNRESET:
return ECONNRESET;
default:
return wsaerrno - 10000;
}
}
/* called before any other calls to socketio_ functions */
int
socketio_initialize()
{
WSADATA wsaData = { 0 };
return WSAStartup(MAKEWORD(2, 2), &wsaData);
}
/* cleanup */
int
socketio_done()
{
WSACleanup();
return 0;
}
/* initiate async acceptEx*/
/* TODO - always return 0, set error in context, accept() will pick it up*/
int
socketio_acceptEx(struct w32_io* pio)
{
struct acceptEx_context *context;
struct sockaddr_storage addr; int addrlen = sizeof addr;
debug5("acceptEx - io:%p", pio);
context = (struct acceptEx_context *)pio->internal.context;
ResetEvent(pio->read_overlapped.hEvent);
if (getsockname(pio->sock, (struct sockaddr*)&addr, &addrlen) == SOCKET_ERROR) {
errno = errno_from_WSALastError();
debug("acceptEx - getsockname() ERROR:%d, io:%p", WSAGetLastError(), pio);
return -1;
}
/* create accepting socket */
#ifdef HAVE_AFUNIX_H
context->accept_socket = socket(addr.ss_family, SOCK_STREAM, IPPROTO_IP);
#else
context->accept_socket = socket(addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
#endif
if (context->accept_socket == INVALID_SOCKET) {
errno = errno_from_WSALastError();
debug3("acceptEx - socket() ERROR:%d, io:%p", WSAGetLastError(), pio);
return -1;
}
if (TRUE == context->lpfnAcceptEx(pio->sock,
context->accept_socket,
context->lpOutputBuf,
0,
sizeof(SOCKADDR_STORAGE) + 16,
sizeof(SOCKADDR_STORAGE) + 16,
&context->bytes_received,
&pio->read_overlapped))
{
/* we are already connected. Set event so subsequent select will catch */
SetEvent(pio->read_overlapped.hEvent);
} else {
/* if overlapped io is in progress, we are good */
if (WSAGetLastError() != ERROR_IO_PENDING) {
errno = errno_from_WSALastError();
debug3("acceptEx - AcceptEx() ERROR:%d, io:%p", WSAGetLastError(), pio);
return -1;
}
}
pio->read_details.pending = TRUE;
return 0;
}
void
CALLBACK WSARecvCompletionRoutine(IN DWORD dwError,
IN DWORD cbTransferred,
IN LPWSAOVERLAPPED lpOverlapped,
IN DWORD dwFlags)
{
struct w32_io* pio =
(struct w32_io*)((char*)lpOverlapped - offsetof(struct w32_io, read_overlapped));
debug4("WSARecvCompletionCB - io:%p, pending_state:%d, flags:%d, error:%d, received:%d",
pio, pio->read_details.pending, dwFlags, dwError, cbTransferred);
if (!dwError && !cbTransferred)
dwError = ERROR_GRACEFUL_DISCONNECT;
pio->read_details.error = dwError;
pio->read_details.remaining = cbTransferred;
pio->read_details.completed = 0;
pio->read_details.pending = FALSE;
}
/* initiates async receive operation*/
/* TODO - always return 0, or make this a void func. any error should be put in context*/
static int
socketio_WSARecv(struct w32_io* pio, BOOL* completed, int len)
{
int ret = 0;
WSABUF wsabuf;
DWORD recv_flags = 0;
debug5("WSARecv - pio: %p", pio);
if (completed)
*completed = FALSE;
/* initialize recv buffers if needed */
wsabuf.len = INTERNAL_RECV_BUFFER_SIZE;
if (pio->read_details.buf == NULL) {
wsabuf.buf = malloc(wsabuf.len);
if (!wsabuf.buf) {
errno = ENOMEM;
debug3("WSARecv - ERROR:%d, io:%p", errno, pio);
return -1;
}
pio->read_details.buf = wsabuf.buf;
pio->read_details.buf_size = wsabuf.len;
} else
wsabuf.buf = pio->read_details.buf;
if (len)
wsabuf.len = min((ULONG)len, wsabuf.len);
ret = WSARecv(pio->sock, &wsabuf, 1, NULL, &recv_flags, &pio->read_overlapped, &WSARecvCompletionRoutine);
if (ret == 0) {
pio->read_details.pending = TRUE;
/* receive has completed but APC is pending to be scheduled */
debug4("WSARecv - WSARecv() returned 0, io:%p", pio);
if (completed)
*completed = TRUE;
} else { /* (ret == SOCKET_ERROR) */
if (WSAGetLastError() == WSA_IO_PENDING) {
/* io is initiated and pending */
debug4("WSARecv - reported IO pending");
pio->read_details.pending = TRUE;
} else {
errno = errno_from_WSALastError();
debug3("WSARecv - WSARecv() ERROR: io:%p %d", pio, WSAGetLastError());
return -1;
}
}
return 0;
}
/* implements socket() */
struct w32_io*
socketio_socket(int domain, int type, int protocol)
{
struct w32_io *pio = (struct w32_io*)malloc(sizeof(struct w32_io));
if (!pio) {
errno = ENOMEM;
debug3("socket - ERROR:%d, io:%p", errno, pio);
return NULL;
}
memset(pio, 0, sizeof(struct w32_io));
pio->sock = socket(domain, type, protocol);
if (pio->sock == INVALID_SOCKET) {
errno = errno_from_WSALastError();
debug3("socket - socket() ERROR:%d, io:%p", WSAGetLastError(), pio);
free(pio);
return NULL;
}
pio->internal.state = SOCK_INITIALIZED;
return pio;
}
#define SET_ERRNO_ON_ERROR(expr) do { \
int ret = (expr); \
if (ret == SOCKET_ERROR) { \
errno = errno_from_WSALastError(); \
debug3("%s - ERROR:%d", __FUNCTION__, WSAGetLastError()); \
} \
return ret; \
} while (0)
/* implements setsockopt() */
int
socketio_setsockopt(struct w32_io* pio, int level, int optname, const char* optval, int optlen)
{
if ((optname == SO_KEEPALIVE) || (optname == SO_REUSEADDR) ||
(optname == TCP_NODELAY) || (optname == IPV6_V6ONLY))
SET_ERRNO_ON_ERROR(setsockopt(pio->sock, level, optname, optval, optlen));
else {
debug3("setsockop - ERROR: unsupported optname:%d io:%p", optname, pio);
errno = ENOTSUP;
return -1;
}
}
/* implements getsockopt() */
int
socketio_getsockopt(struct w32_io* pio, int level, int optname, char* optval, int* optlen)
{
SET_ERRNO_ON_ERROR(getsockopt(pio->sock, level, optname, optval, optlen));
}
/* implements getsockname() */
int
socketio_getsockname(struct w32_io* pio, struct sockaddr* name, int* namelen)
{
SET_ERRNO_ON_ERROR(getsockname(pio->sock, name, namelen));
}
/* implements getpeername */
int
socketio_getpeername(struct w32_io* pio, struct sockaddr* name, int* namelen)
{
SET_ERRNO_ON_ERROR(getpeername(pio->sock, name, namelen));
}
/* implements listen() */
int
socketio_listen(struct w32_io* pio, int backlog)
{
struct acceptEx_context* context;
if (SOCKET_ERROR == listen(pio->sock, backlog)) {
errno = errno_from_WSALastError();
debug3("listen - listen() ERROR:%d io:%p", WSAGetLastError(), pio);
return -1;
}
/* prep for accept*/
{
GUID GuidAcceptEx = WSAID_ACCEPTEX;
GUID GuidGetAcceptExSockaddrs = WSAID_GETACCEPTEXSOCKADDRS;
DWORD dwBytes;
context = (struct acceptEx_context*)malloc(sizeof(struct acceptEx_context));
if (context == NULL) {
errno = ENOMEM;
debug3("listen - ERROR:%d, io:%p", errno, pio);
return -1;
}
memset(context, 0, sizeof(struct acceptEx_context));
if (SOCKET_ERROR == WSAIoctl(pio->sock,
SIO_GET_EXTENSION_FUNCTION_POINTER,
&GuidAcceptEx, sizeof(GuidAcceptEx),
&context->lpfnAcceptEx, sizeof(context->lpfnAcceptEx),
&dwBytes, NULL, NULL)) {
free(context);
errno = errno_from_WSALastError();
debug3("listen - Ioctl1 ERROR:%d, io:%p", WSAGetLastError(), pio);
return -1;
}
if (SOCKET_ERROR == WSAIoctl(pio->sock,
SIO_GET_EXTENSION_FUNCTION_POINTER,
&GuidGetAcceptExSockaddrs, sizeof(GuidGetAcceptExSockaddrs),
&context->lpfnGuidGetAcceptExSockaddrs, sizeof(context->lpfnGuidGetAcceptExSockaddrs),
&dwBytes, NULL, NULL)) {
free(context);
errno = errno_from_WSALastError();
debug3("listen - Ioctl2 ERROR:%d, io:%p", WSAGetLastError(), pio);
return -1;
}
pio->read_overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if ((pio->read_overlapped.hEvent) == NULL) {
free(context);
errno = ENOMEM;
debug3("listen - CreateEvent() ERROR:%d, io:%p", errno, pio);
return -1;
}
context->accept_socket = INVALID_SOCKET;
pio->internal.context = context;
}
pio->internal.state = SOCK_LISTENING;
return 0;
}
/* implements bind() */
int
socketio_bind(struct w32_io* pio, const struct sockaddr *name, int namelen)
{
SET_ERRNO_ON_ERROR(bind(pio->sock, name, namelen));
}
/* implements recv() */
int
socketio_recv(struct w32_io* pio, void *buf, size_t len, int flags)
{
BOOL completed = FALSE;
errno_t r = 0;
debug5("recv - io:%p state:%d", pio, pio->internal.state);
if ((buf == NULL) || (len == 0)) {
errno = EINVAL;
debug3("recv - ERROR: invalid arguments, buf:%p, len:%d, io:%p", buf, len, pio);
return -1;
}
if (flags != 0) {
errno = ENOTSUP;
debug3("recv - ERROR: flags are not currently supported, io:%p", pio);
return -1;
}
/* TODO - ensure socket is in accepted or connected state */
/* /io is initiated and pending */
if (pio->read_details.pending) {
/* if recv is now in blocking mode, wait for data to be available */
if (w32_io_is_blocking(pio)) {
debug4("recv - io is pending, call is blocking, io:%p", pio);
while (socketio_is_io_available(pio, TRUE) == FALSE) {
if (0 != wait_for_any_event(NULL, 0, INFINITE))
return -1;
}
} else {
errno = EAGAIN;
debug4("recv - io is already pending, io:%p", pio);
return -1;
}
}
/* if we have some buffer copy it and return #bytes copied */
if (pio->read_details.remaining) {
int num_bytes_copied = min((int)len, pio->read_details.remaining);
if ((r = memcpy_s(buf, len, pio->read_details.buf + pio->read_details.completed,
num_bytes_copied)) != 0) {
debug4("memcpy_s failed with error: %d.", r);
return -1;
}
pio->read_details.remaining -= num_bytes_copied;
pio->read_details.completed += num_bytes_copied;
debug5("recv - returning %d bytes from prior completed IO, remaining:%d, io:%p",
num_bytes_copied, pio->read_details.remaining, pio);
return num_bytes_copied;
}
/* if there was an error on async call, return */
if (pio->read_details.error) {
if (pio->read_details.error == ERROR_GRACEFUL_DISCONNECT) {
debug4("recv - connection closed, io:%p", pio);
/* connection is closed */
return 0;
} else {
errno = errno_from_WSAError(pio->read_details.error);
debug3("recv - from CB ERROR:%d, io:%p", pio->read_details.error, pio);
pio->read_details.error = 0;
return -1;
}
}
if (0 != socketio_WSARecv(pio, &completed, (int)len))
return -1;
if (completed) {
/* Let APC be scheduled */
debug4("recv - Letting APC to execute, io:%p", pio);
SleepEx(0, TRUE);
if (pio->read_details.pending) {
/* this shouldn't be happening */
errno = EOTHER;
debug3("recv - ERROR: Unexpected IO state, io:%p", pio);
return -1;
}
}
if (w32_io_is_blocking(pio)) {
/* wait until io is done */
debug5("recv - socket in blocking mode, io:%p", pio);
while (socketio_is_io_available(pio, TRUE) == FALSE) {
if (0 != wait_for_any_event(NULL, 0, INFINITE))
return -1;
}
} else {
if (socketio_is_io_available(pio, TRUE) == FALSE) {
errno = EAGAIN;
debug4("recv - IO is pending, io:%p", pio);
return -1;
}
}
/*
* by this time we should have some bytes in internal buffer
* or an error from callback
*/
if (pio->read_details.error) {
if (pio->read_details.error == ERROR_GRACEFUL_DISCONNECT) {
/* connection is closed */
debug4("recv - connection closed(2), io:%p", pio);
return 0;
} else {
errno = errno_from_WSAError(pio->read_details.error);
pio->read_details.error = 0;
debug3("recv - from CB(2) ERROR:%d, io:%p", errno, pio);
return -1;
}
}
if (pio->read_details.remaining) {
int num_bytes_copied = min((int)len, pio->read_details.remaining);
if ((r = memcpy_s(buf, len, pio->read_details.buf, num_bytes_copied)) != 0) {
debug3("memcpy_s failed with error: %d.", r);
return -1;
}
pio->read_details.remaining -= num_bytes_copied;
pio->read_details.completed = num_bytes_copied;
debug4("recv - (2) returning %d bytes from completed IO, remaining:%d, io:%p",
num_bytes_copied, pio->read_details.remaining, pio);
return num_bytes_copied;
} else {
/* this should not happen */
errno = EOTHER;
debug3("recv - (2) ERROR:Unexpected IO state, io:%p", pio);
return -1;
}
}
void
CALLBACK WSASendCompletionRoutine(IN DWORD dwError,
IN DWORD cbTransferred,
IN LPWSAOVERLAPPED lpOverlapped,
IN DWORD dwFlags)
{
struct w32_io* pio = (struct w32_io*)((char*)lpOverlapped - offsetof(struct w32_io, write_overlapped));
debug4("WSASendCB - io:%p, pending_state:%d, error:%d, sent:%d of remaining:%d",
pio, pio->write_details.pending, dwError, cbTransferred,
pio->write_details.remaining);
pio->write_details.error = dwError;
/* TODO - assert that remaining == cbTransferred */
if ((dwError == 0) && (pio->write_details.remaining != cbTransferred)) {
error("WSASendCB - ERROR: broken assumption, io:%p, sent:%d, remaining:%d", pio,
cbTransferred, pio->write_details.remaining);
debug_assert_internal();
}
pio->write_details.remaining -= cbTransferred;
pio->write_details.pending = FALSE;
}
/* implementation of send() */
int
socketio_send(struct w32_io* pio, const void *buf, size_t len, int flags)
{
int ret = 0;
WSABUF wsabuf;
errno_t r = 0;
debug5("send - io:%p state:%d", pio, pio->internal.state);
if ((buf == NULL) || (len == 0)) {
errno = EINVAL;
debug3("send - ERROR invalid arguments, buf:%p, len:%d, io:%p", buf, len, pio);
return -1;
}
if (flags != 0) {
errno = ENOTSUP;
debug3("send - ERROR: flags are not currently supported, io:%p", pio);
return -1;
}
/* TODO - ensure socket is in accepted or connected state */
/* if io is already pending */
if (pio->write_details.pending) {
if (w32_io_is_blocking(pio)) {
debug4("send - io is pending, call is blocking, io:%p", pio);
while (pio->write_details.pending)
if (wait_for_any_event(NULL, 0, INFINITE) == -1)
return -1;
} else {
errno = EAGAIN;
debug4("send - IO currently pending, EAGAIN, io:%p", pio);
return -1;
}
}
if (pio->write_details.error) {
errno = errno_from_WSAError(pio->write_details.error);
debug3("ERROR:%d, io:%p", pio->write_details.error, pio);
return -1;
}
/* initialize buffers if needed */
wsabuf.len = INTERNAL_SEND_BUFFER_SIZE;
if (pio->write_details.buf == NULL) {
wsabuf.buf = malloc(wsabuf.len);
if (!wsabuf.buf) {
errno = ENOMEM;
debug3("send - ERROR:%d, io:%p", errno, pio);
return -1;
}
pio->write_details.buf = wsabuf.buf;
pio->write_details.buf_size = wsabuf.len;
} else
wsabuf.buf = pio->write_details.buf;
wsabuf.len = min(wsabuf.len, (int)len);
if ((r = memcpy_s(wsabuf.buf, wsabuf.len, buf, wsabuf.len)) != 0) {
debug3("memcpy_s failed with error: %d.", r);
return -1;
}
/* TODO - implement flags support if needed */
ret = WSASend(pio->sock, &wsabuf, 1, NULL, 0, &pio->write_overlapped, &WSASendCompletionRoutine);
if (ret == 0) {
/* send has completed and APC is scheduled, let it run */
pio->write_details.pending = TRUE;
pio->write_details.remaining = wsabuf.len;
SleepEx(0, TRUE);
if ((pio->write_details.pending) || (pio->write_details.remaining != 0)) {
errno = EOTHER;
debug3("send - ERROR: Unexpected IO state, io:%p", pio);
return -1;
}
/* return num of bytes written */
return wsabuf.len;
} else {
if (WSAGetLastError() == WSA_IO_PENDING) {
/* io is initiated and pending */
debug4("send - WSASend reported IO pending, io:%p", pio);
pio->write_details.pending = TRUE;
pio->write_details.remaining = wsabuf.len;
if (w32_io_is_blocking(pio)) {
/* wait until io is done */
debug5("send - waiting as socket is in blocking mode, io:%p", pio);
while (pio->write_details.pending)
if (wait_for_any_event(NULL, 0, INFINITE) == -1) {
/* if interrupted but send has completed, we are good*/
if ((errno != EINTR) || (pio->write_details.pending))
return -1;
errno = 0;
}
}
debug5("send - returning %d, io:%p", wsabuf.len, pio);
return wsabuf.len;
} else {
errno = errno_from_WSALastError();
debug3("send - WSASend() ERROR:%d, io:%p", WSAGetLastError(), pio);
return -1;
}
}
}
/* shutdown() implementation */
int
socketio_shutdown(struct w32_io* pio, int how)
{
SET_ERRNO_ON_ERROR(shutdown(pio->sock, how));
}
/* socket close() implementation */
int
socketio_close(struct w32_io* pio)
{
debug4("close - io:%p", pio);
closesocket(pio->sock);
/* wait for pending io to abort */
SleepEx(0, TRUE);
if ((pio->internal.state == SOCK_READY) &&
(pio->read_details.pending || pio->write_details.pending)) {
error("close - IO is still pending on closed socket. read:%d, write:%d, io:%p",
pio->read_details.pending, pio->write_details.pending, pio);
debug_assert_internal();
}
if (pio->internal.state == SOCK_LISTENING) {
if (pio->read_overlapped.hEvent)
CloseHandle(pio->read_overlapped.hEvent);
if (pio->internal.context) {
struct acceptEx_context *ctx = (struct acceptEx_context*)pio->internal.context;
if (ctx->accept_socket != INVALID_SOCKET)
closesocket(ctx->accept_socket);
free(pio->internal.context);
}
} else if (pio->internal.state == SOCK_CONNECTING) {
if (pio->write_overlapped.hEvent)
CloseHandle(pio->write_overlapped.hEvent);
} else {
if (pio->read_details.buf)
free(pio->read_details.buf);
if (pio->write_details.buf)
free(pio->write_details.buf);
}
free(pio);
return 0;
}
/* accept() implementation */
struct w32_io*
socketio_accept(struct w32_io* pio, struct sockaddr* addr, int* addrlen)
{
struct w32_io *accept_io = NULL;
int iResult = 0;
struct acceptEx_context* context;
struct sockaddr *local_address, *remote_address;
int local_address_len, remote_address_len;
errno_t r = 0;
debug5("accept - io:%p", pio);
/* start io if not already started */
if (pio->read_details.pending == FALSE) {
if (socketio_acceptEx(pio) != 0)
return NULL;
}
if (w32_io_is_blocking(pio)) {
/* block until accept io is complete */
while (FALSE == socketio_is_io_available(pio, TRUE))
if (-1 == wait_for_any_event(&pio->read_overlapped.hEvent,
1, INFINITE))
return NULL;
} else {
/* if i/o is not ready */
if (FALSE == socketio_is_io_available(pio, TRUE)) {
errno = EAGAIN;
debug4("accept is pending, io:%p", pio);
return NULL;
}
}
context = (struct acceptEx_context*)pio->internal.context;
pio->read_details.pending = FALSE;
ResetEvent(pio->read_overlapped.hEvent);
if (pio->read_details.error) {
errno = errno_from_WSAError(pio->read_details.error);
debug3("accept - ERROR: async io completed with error: %d, io:%p", pio->read_details.error, pio);
pio->read_details.error = 0;
goto on_error;
}
if (0 != setsockopt(context->accept_socket, SOL_SOCKET,
SO_UPDATE_ACCEPT_CONTEXT, (char*)&pio->sock, sizeof(pio->sock))) {
errno = errno_from_WSALastError();
debug3("accept - ERROR: setsockopt failed:%d, io:%p", WSAGetLastError(), pio);
goto on_error;
}
accept_io = (struct w32_io*)malloc(sizeof(struct w32_io));
if (!accept_io) {
errno = ENOMEM;
debug3("accept - ERROR:%d, io:%p", errno, pio);
goto on_error;
}
memset(accept_io, 0, sizeof(struct w32_io));
accept_io->sock = context->accept_socket;
accept_io->internal.state = SOCK_READY;
context->accept_socket = INVALID_SOCKET;
debug4("accept io:%p", accept_io);
if ((addr != NULL) && (addrlen != NULL)) {
context->lpfnGuidGetAcceptExSockaddrs(context->lpOutputBuf, 0,
sizeof(SOCKADDR_STORAGE) + 16,
sizeof(SOCKADDR_STORAGE) + 16, &local_address,
&local_address_len, &remote_address, &remote_address_len);
if (remote_address_len) {
if((r = memcpy_s(addr, remote_address_len, remote_address, remote_address_len)) != 0) {
debug3("memcpy_s failed with error: %d.", r);
goto on_error;
}
*addrlen = remote_address_len;
}
}
return accept_io;
on_error:
if (context->accept_socket != INVALID_SOCKET) {
closesocket(context->accept_socket);
context->accept_socket = INVALID_SOCKET;
}
return NULL;
}
/* initiates an async connect*/
int
socketio_connectex(struct w32_io* pio, const struct sockaddr* name, int namelen)
{
#ifdef HAVE_AFUNIX_H
struct sockaddr_un tmp_unix;
#endif
struct sockaddr_in tmp_addr4;
struct sockaddr_in6 tmp_addr6;
SOCKADDR* tmp_addr;
size_t tmp_addr_len;
DWORD tmp_bytes;
GUID connectex_guid = WSAID_CONNECTEX;
LPFN_CONNECTEX ConnectEx;
debug5("connectex - io:%p", pio);
if (name->sa_family == AF_INET6) {
ZeroMemory(&tmp_addr6, sizeof(tmp_addr6));
tmp_addr6.sin6_family = AF_INET6;
tmp_addr6.sin6_port = 0;
tmp_addr = (SOCKADDR*)&tmp_addr6;
tmp_addr_len = sizeof(tmp_addr6);
} else if (name->sa_family == AF_INET) {
ZeroMemory(&tmp_addr4, sizeof(tmp_addr4));
tmp_addr4.sin_family = AF_INET;
tmp_addr4.sin_port = 0;
tmp_addr = (SOCKADDR*)&tmp_addr4;
tmp_addr_len = sizeof(tmp_addr4);
#ifdef HAVE_AFUNIX_H
} else if (name->sa_family == AF_UNIX) {
ZeroMemory(&tmp_unix, sizeof(tmp_unix));
tmp_unix.sun_family = AF_UNIX;
tmp_addr = (SOCKADDR*)&tmp_unix;
tmp_addr_len = sizeof(tmp_unix);
#endif
} else {
errno = ENOTSUP;
debug3("connectex - ERROR: unsuppored address family:%d, io:%p", name->sa_family, pio);
return -1;
}
if (SOCKET_ERROR == bind(pio->sock, tmp_addr, (int)tmp_addr_len)) {
errno = errno_from_WSALastError();
/*
* When use bind_address or bind_interface, this bind will return WSAEINVAL. But it doesn't matter.
* https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-bind
*/
if (errno != EINVAL) {
debug3("connectex - ERROR: bind failed :%d, io:%p", WSAGetLastError(), pio);
return -1;
}
}
if (SOCKET_ERROR == WSAIoctl(pio->sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
&connectex_guid, sizeof(connectex_guid),
&ConnectEx, sizeof(ConnectEx),
&tmp_bytes, NULL, NULL)) {
errno = errno_from_WSALastError();
debug3("connectex - ioctl ERROR:%d, io:%p", WSAGetLastError(), pio);
return -1;
}
if ((!pio->write_overlapped.hEvent)
&& ((pio->write_overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)) == NULL)) {
errno = ENOMEM;
debug3("connectex - ERROR CreateEvent failed:%d, io:%p", errno, pio);
return -1;
}
ResetEvent(pio->write_overlapped.hEvent);
if (TRUE == ConnectEx(pio->sock, name, namelen, NULL, 0, NULL,
&pio->write_overlapped)) {
/* set completion event to indicates that async connect has completed */
SetEvent(pio->write_overlapped.hEvent);
} else {
if (WSAGetLastError() != ERROR_IO_PENDING) {
CloseHandle(pio->write_overlapped.hEvent);
pio->write_overlapped.hEvent = 0;
errno = errno_from_WSALastError();
debug3("connectex - ERROR ConnectEx() :%d, io:%p", WSAGetLastError(), pio);
return -1;
}
}
pio->write_details.pending = TRUE;
pio->internal.state = SOCK_CONNECTING;
return 0;
}
/* connect implementation */
int
socketio_connect(struct w32_io* pio, const struct sockaddr* name, int namelen)
{
debug5("connect - io:%p", pio);
if (pio->write_details.pending == FALSE) {
if (-1 == socketio_connectex(pio, name, namelen))
return -1;
}
if (w32_io_is_blocking(pio)) {
/* block until connect io is complete */
while (FALSE == socketio_is_io_available(pio, TRUE)) {
if (-1 == wait_for_any_event(&pio->write_overlapped.hEvent, 1, INFINITE))
return -1;
}
} else {
/* if i/o is not ready */
if (FALSE == socketio_is_io_available(pio, TRUE)) {
errno = EINPROGRESS;
debug4("connect - in progress, io:%p", pio);
return -1;
}
}
return socketio_finish_connect(pio);
}
int
socketio_finish_connect(struct w32_io* pio)
{
DWORD wsa_error = 0;
debug5("finish_connect, io:%p", pio);
if (pio->write_details.error) {
wsa_error = pio->write_details.error;
debug3("finish_connect - ERROR: async io completed with error: %d, io:%p", wsa_error, pio);
goto done;
}
if (0 != setsockopt(pio->sock, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, NULL, 0)) {
wsa_error = WSAGetLastError();
debug3("finish_connect - ERROR: setsockopt failed:%d, io:%p", wsa_error, pio);
goto done;
}
done:
CloseHandle(pio->write_overlapped.hEvent);
pio->write_overlapped.hEvent = 0;
pio->write_details.pending = FALSE;
if (wsa_error) {
pio->read_details.error = wsa_error;
pio->write_details.error = wsa_error;
errno = errno_from_WSAError(wsa_error);
} else /* reset write_detail that were previously used for async connect */
ZeroMemory(&pio->write_details, sizeof(pio->write_details));
pio->internal.state = SOCK_READY;
return (wsa_error? -1 : 0);
}
/* checks if a given io is ready/available */
BOOL
socketio_is_io_available(struct w32_io* pio, BOOL rd)
{
if ((pio->internal.state == SOCK_LISTENING) ||
(pio->internal.state == SOCK_CONNECTING)) {
DWORD numBytes = 0;
DWORD flags;
BOOL sock_listening = (pio->internal.state == SOCK_LISTENING);
OVERLAPPED *overlapped = sock_listening ? &pio->read_overlapped : &pio->write_overlapped;
BOOL pending = sock_listening ? pio->read_details.pending : pio->write_details.pending;
if (pending) {
/* if there is an error to be picked up */
if (sock_listening) {
if (pio->read_details.error)
return TRUE;
} else {
if (pio->write_details.error)
return TRUE;
}
}
if (WSAGetOverlappedResult(pio->sock, overlapped, &numBytes, FALSE, &flags))
return TRUE;
else if (WSAGetLastError() != WSA_IO_INCOMPLETE) {
if (sock_listening)
pio->read_details.error = WSAGetLastError();
else
pio->write_details.error = WSAGetLastError();
return TRUE;
}
return FALSE;
} else if (rd) {
if (pio->read_details.remaining || pio->read_details.error)
return TRUE;
else
return FALSE;
} else
return (pio->write_details.pending == FALSE) ? TRUE : FALSE;
}
/*start async io (if needed) for accept and recv*/
void
socketio_on_select(struct w32_io* pio, BOOL rd)
{
enum w32_io_sock_state sock_state = pio->internal.state;
debug4("on_select - io:%p type:%d rd:%d", pio, pio->type, rd);
/* nothing to do for writes (that includes connect) */
if (!rd)
return;
/* listening socket - acceptEx if needed */
if (sock_state == SOCK_LISTENING) {
if (pio->read_details.pending == FALSE)
if (socketio_acceptEx(pio) != 0) {
/* set error, accept will pick it*/
pio->read_details.error = errno;
errno = 0;
pio->read_details.pending = TRUE;
SetEvent(pio->read_overlapped.hEvent);
return;
}
} else if(sock_state == SOCK_READY) {
/* connected socket - WSARecv if needed */
if ((!pio->read_details.pending) && (!socketio_is_io_available(pio, rd)) && (socketio_WSARecv(pio, NULL, 0) != 0))
{
/* set error, recv() will pick it */
pio->read_details.error = errno;
errno = 0;
return;
}
}
}
int
w32_gethostname(char *name_utf8, size_t len)
{
char* tmp_name_utf8 = NULL;
if (IsWindows8OrGreater()) {
/* TODO - GetHostNameW not present in Win7, do GetProcAddr on Win8+*/