/
socket-io.c
2882 lines (2404 loc) · 66.5 KB
/
socket-io.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
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
/*
* socket-io.c: Socket IO internal calls
*
* Authors:
* Dick Porter (dick@ximian.com)
* Gonzalo Paniagua Javier (gonzalo@ximian.com)
*
* (C) 2001 Ximian, Inc.
* Copyright (c) 2005-2006 Novell, Inc. (http://www.novell.com)
*/
#include <config.h>
#include <glib.h>
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
#include <mono/metadata/object.h>
#include <mono/io-layer/io-layer.h>
#include <mono/metadata/socket-io.h>
#include <mono/metadata/exception.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/threads.h>
#include <mono/metadata/threads-types.h>
#include <mono/utils/mono-poll.h>
/* FIXME change this code to not mess so much with the internals */
#include <mono/metadata/class-internals.h>
#include <mono/metadata/threadpool-internals.h>
#include <mono/metadata/domain-internals.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_NET_IF_H
#include <net/if.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h> /* defines FIONBIO and FIONREAD */
#endif
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h> /* defines SIOCATMARK */
#endif
#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
#endif
#include "mono/io-layer/socket-wrappers.h"
#ifdef PLATFORM_WIN32
/* This is a kludge to make this file build under cygwin:
* w32api/ws2tcpip.h has definitions for some AF_INET6 values and
* prototypes for some but not all required functions (notably
* inet_ntop() is missing), but the libws2_32 library is missing the
* actual implementations of these functions.
*/
#undef AF_INET6
#endif
#undef DEBUG
static gint32 convert_family(MonoAddressFamily mono_family)
{
gint32 family=-1;
switch(mono_family) {
case AddressFamily_Unknown:
case AddressFamily_ImpLink:
case AddressFamily_Pup:
case AddressFamily_Chaos:
case AddressFamily_Iso:
case AddressFamily_Ecma:
case AddressFamily_DataKit:
case AddressFamily_Ccitt:
case AddressFamily_DataLink:
case AddressFamily_Lat:
case AddressFamily_HyperChannel:
case AddressFamily_NetBios:
case AddressFamily_VoiceView:
case AddressFamily_FireFox:
case AddressFamily_Banyan:
case AddressFamily_Atm:
case AddressFamily_Cluster:
case AddressFamily_Ieee12844:
case AddressFamily_NetworkDesigners:
g_warning("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family);
break;
case AddressFamily_Unspecified:
family=AF_UNSPEC;
break;
case AddressFamily_Unix:
family=AF_UNIX;
break;
case AddressFamily_InterNetwork:
family=AF_INET;
break;
case AddressFamily_Ipx:
#ifdef AF_IPX
family=AF_IPX;
#endif
break;
case AddressFamily_Sna:
family=AF_SNA;
break;
case AddressFamily_DecNet:
family=AF_DECnet;
break;
case AddressFamily_AppleTalk:
family=AF_APPLETALK;
break;
case AddressFamily_InterNetworkV6:
#ifdef AF_INET6
family=AF_INET6;
#endif
break;
case AddressFamily_Irda:
#ifdef AF_IRDA
family=AF_IRDA;
#endif
break;
default:
g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
}
return(family);
}
static MonoAddressFamily convert_to_mono_family(guint16 af_family)
{
MonoAddressFamily family=AddressFamily_Unknown;
switch(af_family) {
case AF_UNSPEC:
family=AddressFamily_Unspecified;
break;
case AF_UNIX:
family=AddressFamily_Unix;
break;
case AF_INET:
family=AddressFamily_InterNetwork;
break;
#ifdef AF_IPX
case AF_IPX:
family=AddressFamily_Ipx;
break;
#endif
case AF_SNA:
family=AddressFamily_Sna;
break;
case AF_DECnet:
family=AddressFamily_DecNet;
break;
case AF_APPLETALK:
family=AddressFamily_AppleTalk;
break;
#ifdef AF_INET6
case AF_INET6:
family=AddressFamily_InterNetworkV6;
break;
#endif
#ifdef AF_IRDA
case AF_IRDA:
family=AddressFamily_Irda;
break;
#endif
default:
g_warning("unknown address family 0x%x", af_family);
}
return(family);
}
static gint32 convert_type(MonoSocketType mono_type)
{
gint32 type=-1;
switch(mono_type) {
case SocketType_Stream:
type=SOCK_STREAM;
break;
case SocketType_Dgram:
type=SOCK_DGRAM;
break;
case SocketType_Raw:
type=SOCK_RAW;
break;
case SocketType_Rdm:
type=SOCK_RDM;
break;
case SocketType_Seqpacket:
type=SOCK_SEQPACKET;
break;
case SocketType_Unknown:
g_warning("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type);
break;
default:
g_warning("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type);
}
return(type);
}
static gint32 convert_proto(MonoProtocolType mono_proto)
{
gint32 proto=-1;
switch(mono_proto) {
case ProtocolType_IP:
case ProtocolType_IPv6:
case ProtocolType_Icmp:
case ProtocolType_Igmp:
case ProtocolType_Ggp:
case ProtocolType_Tcp:
case ProtocolType_Pup:
case ProtocolType_Udp:
case ProtocolType_Idp:
/* These protocols are known (on my system at least) */
proto=mono_proto;
break;
case ProtocolType_ND:
case ProtocolType_Raw:
case ProtocolType_Ipx:
case ProtocolType_Spx:
case ProtocolType_SpxII:
case ProtocolType_Unknown:
/* These protocols arent */
g_warning("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto);
break;
default:
break;
}
return(proto);
}
/* Convert MonoSocketFlags */
static gint32 convert_socketflags (gint32 sflags)
{
gint32 flags = 0;
if (!sflags)
/* SocketFlags.None */
return 0;
if (sflags & ~(SocketFlags_OutOfBand | SocketFlags_MaxIOVectorLength | SocketFlags_Peek |
SocketFlags_DontRoute | SocketFlags_Partial))
/* Contains invalid flag values */
return -1;
if (sflags & SocketFlags_OutOfBand)
flags |= MSG_OOB;
if (sflags & SocketFlags_Peek)
flags |= MSG_PEEK;
if (sflags & SocketFlags_DontRoute)
flags |= MSG_DONTROUTE;
if (sflags & SocketFlags_Partial)
#ifdef MSG_MORE
flags |= MSG_MORE;
#else
return -1;
#endif
if (sflags & SocketFlags_MaxIOVectorLength)
/* FIXME: Don't know what to do for MaxIOVectorLength query */
return -1;
return (flags ? flags : -1);
}
/*
* Returns:
* 0 on success (mapped mono_level and mono_name to system_level and system_name
* -1 on error
* -2 on non-fatal error (ie, must ignore)
*/
static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
MonoSocketOptionName mono_name,
int *system_level,
int *system_name)
{
switch (mono_level) {
case SocketOptionLevel_Socket:
*system_level = SOL_SOCKET;
switch(mono_name) {
case SocketOptionName_DontLinger:
/* This is SO_LINGER, because the setsockopt
* internal call maps DontLinger to SO_LINGER
* with l_onoff=0
*/
*system_name = SO_LINGER;
break;
case SocketOptionName_Debug:
*system_name = SO_DEBUG;
break;
#ifdef SO_ACCEPTCONN
case SocketOptionName_AcceptConnection:
*system_name = SO_ACCEPTCONN;
break;
#endif
case SocketOptionName_ReuseAddress:
*system_name = SO_REUSEADDR;
break;
case SocketOptionName_KeepAlive:
*system_name = SO_KEEPALIVE;
break;
case SocketOptionName_DontRoute:
*system_name = SO_DONTROUTE;
break;
case SocketOptionName_Broadcast:
*system_name = SO_BROADCAST;
break;
case SocketOptionName_Linger:
*system_name = SO_LINGER;
break;
case SocketOptionName_OutOfBandInline:
*system_name = SO_OOBINLINE;
break;
case SocketOptionName_SendBuffer:
*system_name = SO_SNDBUF;
break;
case SocketOptionName_ReceiveBuffer:
*system_name = SO_RCVBUF;
break;
case SocketOptionName_SendLowWater:
*system_name = SO_SNDLOWAT;
break;
case SocketOptionName_ReceiveLowWater:
*system_name = SO_RCVLOWAT;
break;
case SocketOptionName_SendTimeout:
*system_name = SO_SNDTIMEO;
break;
case SocketOptionName_ReceiveTimeout:
*system_name = SO_RCVTIMEO;
break;
case SocketOptionName_Error:
*system_name = SO_ERROR;
break;
case SocketOptionName_Type:
*system_name = SO_TYPE;
break;
#ifdef SO_PEERCRED
case SocketOptionName_PeerCred:
*system_name = SO_PEERCRED;
break;
#endif
case SocketOptionName_ExclusiveAddressUse:
case SocketOptionName_UseLoopback:
case SocketOptionName_MaxConnections:
/* Can't figure out how to map these, so fall
* through
*/
default:
g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
return(-1);
}
break;
case SocketOptionLevel_IP:
#ifdef HAVE_SOL_IP
*system_level = SOL_IP;
#else
if (1) {
static int cached = 0;
static int proto;
if (!cached) {
struct protoent *pent;
pent = getprotobyname ("IP");
proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
cached = 1;
}
*system_level = proto;
}
#endif /* HAVE_SOL_IP */
switch(mono_name) {
case SocketOptionName_IPOptions:
*system_name = IP_OPTIONS;
break;
#ifdef IP_HDRINCL
case SocketOptionName_HeaderIncluded:
*system_name = IP_HDRINCL;
break;
#endif
#ifdef IP_TOS
case SocketOptionName_TypeOfService:
*system_name = IP_TOS;
break;
#endif
#ifdef IP_TTL
case SocketOptionName_IpTimeToLive:
*system_name = IP_TTL;
break;
#endif
case SocketOptionName_MulticastInterface:
*system_name = IP_MULTICAST_IF;
break;
case SocketOptionName_MulticastTimeToLive:
*system_name = IP_MULTICAST_TTL;
break;
case SocketOptionName_MulticastLoopback:
*system_name = IP_MULTICAST_LOOP;
break;
case SocketOptionName_AddMembership:
*system_name = IP_ADD_MEMBERSHIP;
break;
case SocketOptionName_DropMembership:
*system_name = IP_DROP_MEMBERSHIP;
break;
#ifdef HAVE_IP_PKTINFO
case SocketOptionName_PacketInformation:
*system_name = IP_PKTINFO;
break;
#endif /* HAVE_IP_PKTINFO */
case SocketOptionName_DontFragment:
#ifdef HAVE_IP_DONTFRAGMENT
*system_name = IP_DONTFRAGMENT;
break;
#elif defined HAVE_IP_MTU_DISCOVER
/* Not quite the same */
*system_name = IP_MTU_DISCOVER;
break;
#else
/* If the flag is not available on this system, we can ignore this error */
return (-2);
#endif /* HAVE_IP_DONTFRAGMENT */
case SocketOptionName_AddSourceMembership:
case SocketOptionName_DropSourceMembership:
case SocketOptionName_BlockSource:
case SocketOptionName_UnblockSource:
/* Can't figure out how to map these, so fall
* through
*/
default:
g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
return(-1);
}
break;
#ifdef AF_INET6
case SocketOptionLevel_IPv6:
#ifdef HAVE_SOL_IPV6
*system_level = SOL_IPV6;
#else
if (1) {
static int cached = 0;
static int proto;
if (!cached) {
struct protoent *pent;
pent = getprotobyname ("IPV6");
proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
cached = 1;
}
*system_level = proto;
}
#endif /* HAVE_SOL_IPV6 */
switch(mono_name) {
case SocketOptionName_IpTimeToLive:
*system_name = IPV6_UNICAST_HOPS;
break;
case SocketOptionName_MulticastInterface:
*system_name = IPV6_MULTICAST_IF;
break;
case SocketOptionName_MulticastTimeToLive:
*system_name = IPV6_MULTICAST_HOPS;
break;
case SocketOptionName_MulticastLoopback:
*system_name = IPV6_MULTICAST_LOOP;
break;
case SocketOptionName_AddMembership:
*system_name = IPV6_JOIN_GROUP;
break;
case SocketOptionName_DropMembership:
*system_name = IPV6_LEAVE_GROUP;
break;
case SocketOptionName_PacketInformation:
#ifdef HAVE_IPV6_PKTINFO
*system_name = IPV6_PKTINFO;
#endif
break;
case SocketOptionName_HeaderIncluded:
case SocketOptionName_IPOptions:
case SocketOptionName_TypeOfService:
case SocketOptionName_DontFragment:
case SocketOptionName_AddSourceMembership:
case SocketOptionName_DropSourceMembership:
case SocketOptionName_BlockSource:
case SocketOptionName_UnblockSource:
/* Can't figure out how to map these, so fall
* through
*/
default:
g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
return(-1);
}
break; /* SocketOptionLevel_IPv6 */
#endif
case SocketOptionLevel_Tcp:
#ifdef HAVE_SOL_TCP
*system_level = SOL_TCP;
#else
if (1) {
static int cached = 0;
static int proto;
if (!cached) {
struct protoent *pent;
pent = getprotobyname ("TCP");
proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
cached = 1;
}
*system_level = proto;
}
#endif /* HAVE_SOL_TCP */
switch(mono_name) {
case SocketOptionName_NoDelay:
*system_name = TCP_NODELAY;
break;
#if 0
/* The documentation is talking complete
* bollocks here: rfc-1222 is titled
* 'Advancing the NSFNET Routing Architecture'
* and doesn't mention either of the words
* "expedite" or "urgent".
*/
case SocketOptionName_BsdUrgent:
case SocketOptionName_Expedited:
#endif
default:
g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
return(-1);
}
break;
case SocketOptionLevel_Udp:
g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
switch(mono_name) {
case SocketOptionName_NoChecksum:
case SocketOptionName_ChecksumCoverage:
default:
g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
return(-1);
}
return(-1);
break;
default:
g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
return(-1);
}
return(0);
}
#define STASH_SYS_ASS(this) \
if(system_assembly == NULL) { \
system_assembly=mono_image_loaded ("System"); \
if (!system_assembly) { \
MonoAssembly *sa = mono_assembly_open ("System.dll", NULL); \
if (!sa) g_assert_not_reached (); \
else {system_assembly = mono_assembly_get_image (sa);} \
} \
}
static MonoImage *system_assembly=NULL;
#ifdef AF_INET6
static gint32 get_family_hint(void)
{
MonoClass *socket_class;
MonoClassField *ipv6_field, *ipv4_field;
gint32 ipv6_enabled = -1, ipv4_enabled = -1;
MonoVTable *vtable;
socket_class = mono_class_from_name (system_assembly,
"System.Net.Sockets", "Socket");
ipv4_field = mono_class_get_field_from_name (socket_class,
"ipv4Supported");
ipv6_field = mono_class_get_field_from_name (socket_class,
"ipv6Supported");
vtable = mono_class_vtable (mono_domain_get (), socket_class);
mono_field_static_get_value(vtable, ipv4_field, &ipv4_enabled);
mono_field_static_get_value(vtable, ipv6_field, &ipv6_enabled);
if(ipv4_enabled == 1 && ipv6_enabled == 1) {
return(PF_UNSPEC);
} else if(ipv4_enabled == 1) {
return(PF_INET);
} else {
return(PF_INET6);
}
}
#endif
gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
{
SOCKET sock;
gint32 sock_family;
gint32 sock_proto;
gint32 sock_type;
MONO_ARCH_SAVE_REGS;
STASH_SYS_ASS(this);
*error = 0;
sock_family=convert_family(family);
if(sock_family==-1) {
*error = WSAEAFNOSUPPORT;
return(NULL);
}
sock_proto=convert_proto(proto);
if(sock_proto==-1) {
*error = WSAEPROTONOSUPPORT;
return(NULL);
}
sock_type=convert_type(type);
if(sock_type==-1) {
*error = WSAESOCKTNOSUPPORT;
return(NULL);
}
sock = _wapi_socket (sock_family, sock_type, sock_proto,
NULL, 0, WSA_FLAG_OVERLAPPED);
if(sock==INVALID_SOCKET) {
*error = WSAGetLastError ();
return(NULL);
}
if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
return (GUINT_TO_POINTER (sock));
}
#ifdef AF_INET6
if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
return (GUINT_TO_POINTER (sock));
}
#endif
return(GUINT_TO_POINTER (sock));
}
/* FIXME: the SOCKET parameter (here and in other functions in this
* file) is really an IntPtr which needs to be converted to a guint32.
*/
void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
gint32 *error)
{
MONO_ARCH_SAVE_REGS;
#ifdef DEBUG
g_message (G_GNUC_PRETTY_FUNCTION ": closing 0x%x", sock);
#endif
*error = 0;
/* Clear any pending work item from this socket if the underlying
* polling system does not notify when the socket is closed */
mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
closesocket(sock);
}
gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
{
MONO_ARCH_SAVE_REGS;
#ifdef DEBUG
g_message(G_GNUC_PRETTY_FUNCTION ": returning %d", WSAGetLastError());
#endif
return(WSAGetLastError());
}
gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
gint32 *error)
{
int ret;
gulong amount;
MONO_ARCH_SAVE_REGS;
*error = 0;
ret=ioctlsocket(sock, FIONREAD, &amount);
if(ret==SOCKET_ERROR) {
*error = WSAGetLastError ();
return(0);
}
return(amount);
}
void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
gboolean block,
gint32 *error)
{
int ret;
MONO_ARCH_SAVE_REGS;
*error = 0;
/*
* block == TRUE/FALSE means we will block/not block.
* But the ioctlsocket call takes TRUE/FALSE for non-block/block
*/
block = !block;
ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
if(ret==SOCKET_ERROR) {
*error = WSAGetLastError ();
}
}
gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
gint32 *error)
{
SOCKET newsock;
MONO_ARCH_SAVE_REGS;
*error = 0;
newsock = _wapi_accept (sock, NULL, 0);
if(newsock==INVALID_SOCKET) {
*error = WSAGetLastError ();
return(NULL);
}
return(GUINT_TO_POINTER (newsock));
}
void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
guint32 backlog,
gint32 *error)
{
int ret;
MONO_ARCH_SAVE_REGS;
*error = 0;
ret = _wapi_listen (sock, backlog);
if(ret==SOCKET_ERROR) {
*error = WSAGetLastError ();
}
}
static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
int sa_size, gint32 *error)
{
MonoDomain *domain = mono_domain_get ();
MonoObject *sockaddr_obj;
MonoClass *sockaddr_class;
MonoClassField *field;
MonoArray *data;
MonoAddressFamily family;
/* Build a System.Net.SocketAddress object instance */
sockaddr_class=mono_class_from_name(system_assembly, "System.Net", "SocketAddress");
sockaddr_obj=mono_object_new(domain, sockaddr_class);
/* Locate the SocketAddress data buffer in the object */
field=mono_class_get_field_from_name(sockaddr_class, "data");
/* Make sure there is space for the family and size bytes */
#ifdef HAVE_SYS_UN_H
if (saddr->sa_family == AF_UNIX) {
/* sa_len includes the entire sockaddr size, so we don't need the
* N bytes (sizeof (unsigned short)) of the family. */
data=mono_array_new(domain, mono_get_byte_class (), sa_size);
} else
#endif
{
/* May be the +2 here is too conservative, as sa_len returns
* the length of the entire sockaddr_in/in6, including
* sizeof (unsigned short) of the family */
data=mono_array_new(domain, mono_get_byte_class (), sa_size+2);
}
/* The data buffer is laid out as follows:
* bytes 0 and 1 are the address family
* bytes 2 and 3 are the port info
* the rest is the address info
*/
family=convert_to_mono_family(saddr->sa_family);
if(family==AddressFamily_Unknown) {
*error = WSAEAFNOSUPPORT;
return(NULL);
}
mono_array_set(data, guint8, 0, family & 0x0FF);
mono_array_set(data, guint8, 1, (family >> 8) & 0x0FF);
if(saddr->sa_family==AF_INET) {
struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
guint16 port=ntohs(sa_in->sin_port);
guint32 address=ntohl(sa_in->sin_addr.s_addr);
if(sa_size<8) {
mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
}
mono_array_set(data, guint8, 2, (port>>8) & 0xff);
mono_array_set(data, guint8, 3, (port) & 0xff);
mono_array_set(data, guint8, 4, (address>>24) & 0xff);
mono_array_set(data, guint8, 5, (address>>16) & 0xff);
mono_array_set(data, guint8, 6, (address>>8) & 0xff);
mono_array_set(data, guint8, 7, (address) & 0xff);
mono_field_set_value (sockaddr_obj, field, data);
return(sockaddr_obj);
#ifdef AF_INET6
} else if (saddr->sa_family == AF_INET6) {
struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
int i;
guint16 port=ntohs(sa_in->sin6_port);
if(sa_size<28) {
mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
}
mono_array_set(data, guint8, 2, (port>>8) & 0xff);
mono_array_set(data, guint8, 3, (port) & 0xff);
for(i=0; i<16; i++) {
mono_array_set(data, guint8, 8+i,
sa_in->sin6_addr.s6_addr[i]);
}
mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
mono_array_set(data, guint8, 25,
(sa_in->sin6_scope_id >> 8) & 0xff);
mono_array_set(data, guint8, 26,
(sa_in->sin6_scope_id >> 16) & 0xff);
mono_array_set(data, guint8, 27,
(sa_in->sin6_scope_id >> 24) & 0xff);
mono_field_set_value (sockaddr_obj, field, data);
return(sockaddr_obj);
#endif
#ifdef HAVE_SYS_UN_H
} else if (saddr->sa_family == AF_UNIX) {
int i;
for (i = 0; i < sa_size; i++) {
mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
}
mono_field_set_value (sockaddr_obj, field, data);
return sockaddr_obj;
#endif
} else {
*error = WSAEAFNOSUPPORT;
return(NULL);
}
}
extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 *error)
{
gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
socklen_t salen;
int ret;
MONO_ARCH_SAVE_REGS;
*error = 0;
salen=sizeof(sa);
ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
if(ret==SOCKET_ERROR) {
*error = WSAGetLastError ();
return(NULL);
}
#ifdef DEBUG
g_message(G_GNUC_PRETTY_FUNCTION ": bound to %s port %d", inet_ntoa(((struct sockaddr_in *)&sa)->sin_addr), ntohs(((struct sockaddr_in *)&sa)->sin_port));
#endif
return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
error));
}
extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 *error)
{
gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
socklen_t salen;
int ret;
MONO_ARCH_SAVE_REGS;
*error = 0;
salen=sizeof(sa);
ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
if(ret==SOCKET_ERROR) {
*error = WSAGetLastError ();
return(NULL);
}
#ifdef DEBUG
g_message(G_GNUC_PRETTY_FUNCTION ": connected to %s port %d", inet_ntoa(((struct sockaddr_in *)&sa)->sin_addr), ntohs(((struct sockaddr_in *)&sa)->sin_port));
#endif
return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
error));
}
static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
socklen_t *sa_size,
gint32 *error)
{
MonoClassField *field;
MonoArray *data;
gint32 family;
int len;
/* Dig the SocketAddress data buffer out of the object */
field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
/* The data buffer is laid out as follows:
* byte 0 is the address family low byte
* byte 1 is the address family high byte
* INET:
* bytes 2 and 3 are the port info
* the rest is the address info
* UNIX:
* the rest is the file name
*/
len = mono_array_length (data);
if (len < 2) {
mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
}
family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
if (family == AF_INET) {
struct sockaddr_in *sa;