PageRenderTime 61ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 1ms

/mono/metadata/socket-io.c

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C | 3079 lines | 2390 code | 503 blank | 186 comment | 466 complexity | 8a97aac8e88d2e17c3bee3d14e211f80 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * socket-io.c: Socket IO internal calls
  3. *
  4. * Authors:
  5. * Dick Porter (dick@ximian.com)
  6. * Gonzalo Paniagua Javier (gonzalo@ximian.com)
  7. *
  8. * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
  9. * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  10. */
  11. #include <config.h>
  12. #include <glib.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #ifdef HAVE_UNISTD_H
  16. #include <unistd.h>
  17. #endif
  18. #include <errno.h>
  19. #include <sys/types.h>
  20. #ifndef PLATFORM_WIN32
  21. #include <sys/socket.h>
  22. #include <sys/ioctl.h>
  23. #include <netinet/in.h>
  24. #include <netinet/tcp.h>
  25. #include <netdb.h>
  26. #include <arpa/inet.h>
  27. #endif
  28. #include <mono/metadata/object.h>
  29. #include <mono/io-layer/io-layer.h>
  30. #include <mono/metadata/socket-io.h>
  31. #include <mono/metadata/exception.h>
  32. #include <mono/metadata/assembly.h>
  33. #include <mono/metadata/appdomain.h>
  34. #include <mono/metadata/file-io.h>
  35. #include <mono/metadata/threads.h>
  36. #include <mono/metadata/threads-types.h>
  37. #include <mono/utils/mono-poll.h>
  38. /* FIXME change this code to not mess so much with the internals */
  39. #include <mono/metadata/class-internals.h>
  40. #include <mono/metadata/threadpool-internals.h>
  41. #include <mono/metadata/domain-internals.h>
  42. #ifdef HAVE_SYS_TIME_H
  43. #include <sys/time.h>
  44. #endif
  45. #ifdef HAVE_SYS_IOCTL_H
  46. #include <sys/ioctl.h>
  47. #endif
  48. #ifdef HAVE_NET_IF_H
  49. #include <net/if.h>
  50. #endif
  51. #ifdef HAVE_NETDB_H
  52. #include <netdb.h>
  53. #endif
  54. #ifdef HAVE_SYS_FILIO_H
  55. #include <sys/filio.h> /* defines FIONBIO and FIONREAD */
  56. #endif
  57. #ifdef HAVE_SYS_SOCKIO_H
  58. #include <sys/sockio.h> /* defines SIOCATMARK */
  59. #endif
  60. #ifdef HAVE_SYS_UN_H
  61. #include <sys/un.h>
  62. #endif
  63. #include "mono/io-layer/socket-wrappers.h"
  64. #ifdef PLATFORM_WIN32
  65. /* This is a kludge to make this file build under cygwin:
  66. * w32api/ws2tcpip.h has definitions for some AF_INET6 values and
  67. * prototypes for some but not all required functions (notably
  68. * inet_ntop() is missing), but the libws2_32 library is missing the
  69. * actual implementations of these functions.
  70. */
  71. #undef AF_INET6
  72. #endif
  73. #ifdef PLATFORM_ANDROID
  74. // not yet actually implemented...
  75. #undef AF_INET6
  76. #endif
  77. #define LOGDEBUG(...)
  78. /* define LOGDEBUG(...) g_message(__VA_ARGS__) */
  79. /*
  80. * Some older versions of libc provide IPV6 support without defining the AI_ADDRCONFIG
  81. * flag for getaddrinfo.
  82. */
  83. #ifndef AI_ADDRCONFIG
  84. #define AI_ADDRCONFIG 0
  85. #endif
  86. static gint32 convert_family(MonoAddressFamily mono_family)
  87. {
  88. gint32 family=-1;
  89. switch(mono_family) {
  90. case AddressFamily_Unknown:
  91. case AddressFamily_ImpLink:
  92. case AddressFamily_Pup:
  93. case AddressFamily_Chaos:
  94. case AddressFamily_Iso:
  95. case AddressFamily_Ecma:
  96. case AddressFamily_DataKit:
  97. case AddressFamily_Ccitt:
  98. case AddressFamily_DataLink:
  99. case AddressFamily_Lat:
  100. case AddressFamily_HyperChannel:
  101. case AddressFamily_NetBios:
  102. case AddressFamily_VoiceView:
  103. case AddressFamily_FireFox:
  104. case AddressFamily_Banyan:
  105. case AddressFamily_Atm:
  106. case AddressFamily_Cluster:
  107. case AddressFamily_Ieee12844:
  108. case AddressFamily_NetworkDesigners:
  109. g_warning("System.Net.Sockets.AddressFamily has unsupported value 0x%x", mono_family);
  110. break;
  111. case AddressFamily_Unspecified:
  112. family=AF_UNSPEC;
  113. break;
  114. case AddressFamily_Unix:
  115. family=AF_UNIX;
  116. break;
  117. case AddressFamily_InterNetwork:
  118. family=AF_INET;
  119. break;
  120. case AddressFamily_Ipx:
  121. #ifdef AF_IPX
  122. family=AF_IPX;
  123. #endif
  124. break;
  125. case AddressFamily_Sna:
  126. family=AF_SNA;
  127. break;
  128. case AddressFamily_DecNet:
  129. family=AF_DECnet;
  130. break;
  131. case AddressFamily_AppleTalk:
  132. family=AF_APPLETALK;
  133. break;
  134. case AddressFamily_InterNetworkV6:
  135. #ifdef AF_INET6
  136. family=AF_INET6;
  137. #endif
  138. break;
  139. case AddressFamily_Irda:
  140. #ifdef AF_IRDA
  141. family=AF_IRDA;
  142. #endif
  143. break;
  144. default:
  145. g_warning("System.Net.Sockets.AddressFamily has unknown value 0x%x", mono_family);
  146. }
  147. return(family);
  148. }
  149. static MonoAddressFamily convert_to_mono_family(guint16 af_family)
  150. {
  151. MonoAddressFamily family=AddressFamily_Unknown;
  152. switch(af_family) {
  153. case AF_UNSPEC:
  154. family=AddressFamily_Unspecified;
  155. break;
  156. case AF_UNIX:
  157. family=AddressFamily_Unix;
  158. break;
  159. case AF_INET:
  160. family=AddressFamily_InterNetwork;
  161. break;
  162. #ifdef AF_IPX
  163. case AF_IPX:
  164. family=AddressFamily_Ipx;
  165. break;
  166. #endif
  167. case AF_SNA:
  168. family=AddressFamily_Sna;
  169. break;
  170. case AF_DECnet:
  171. family=AddressFamily_DecNet;
  172. break;
  173. case AF_APPLETALK:
  174. family=AddressFamily_AppleTalk;
  175. break;
  176. #ifdef AF_INET6
  177. case AF_INET6:
  178. family=AddressFamily_InterNetworkV6;
  179. break;
  180. #endif
  181. #ifdef AF_IRDA
  182. case AF_IRDA:
  183. family=AddressFamily_Irda;
  184. break;
  185. #endif
  186. default:
  187. g_warning("unknown address family 0x%x", af_family);
  188. }
  189. return(family);
  190. }
  191. static gint32 convert_type(MonoSocketType mono_type)
  192. {
  193. gint32 type=-1;
  194. switch(mono_type) {
  195. case SocketType_Stream:
  196. type=SOCK_STREAM;
  197. break;
  198. case SocketType_Dgram:
  199. type=SOCK_DGRAM;
  200. break;
  201. case SocketType_Raw:
  202. type=SOCK_RAW;
  203. break;
  204. case SocketType_Rdm:
  205. type=SOCK_RDM;
  206. break;
  207. case SocketType_Seqpacket:
  208. type=SOCK_SEQPACKET;
  209. break;
  210. case SocketType_Unknown:
  211. g_warning("System.Net.Sockets.SocketType has unsupported value 0x%x", mono_type);
  212. break;
  213. default:
  214. g_warning("System.Net.Sockets.SocketType has unknown value 0x%x", mono_type);
  215. }
  216. return(type);
  217. }
  218. static gint32 convert_proto(MonoProtocolType mono_proto)
  219. {
  220. gint32 proto=-1;
  221. switch(mono_proto) {
  222. case ProtocolType_IP:
  223. case ProtocolType_IPv6:
  224. case ProtocolType_Icmp:
  225. case ProtocolType_Igmp:
  226. case ProtocolType_Ggp:
  227. case ProtocolType_Tcp:
  228. case ProtocolType_Pup:
  229. case ProtocolType_Udp:
  230. case ProtocolType_Idp:
  231. /* These protocols are known (on my system at least) */
  232. proto=mono_proto;
  233. break;
  234. case ProtocolType_ND:
  235. case ProtocolType_Raw:
  236. case ProtocolType_Ipx:
  237. case ProtocolType_Spx:
  238. case ProtocolType_SpxII:
  239. case ProtocolType_Unknown:
  240. /* These protocols arent */
  241. g_warning("System.Net.Sockets.ProtocolType has unsupported value 0x%x", mono_proto);
  242. break;
  243. default:
  244. break;
  245. }
  246. return(proto);
  247. }
  248. /* Convert MonoSocketFlags */
  249. static gint32 convert_socketflags (gint32 sflags)
  250. {
  251. gint32 flags = 0;
  252. if (!sflags)
  253. /* SocketFlags.None */
  254. return 0;
  255. if (sflags & ~(SocketFlags_OutOfBand | SocketFlags_MaxIOVectorLength | SocketFlags_Peek |
  256. SocketFlags_DontRoute | SocketFlags_Partial))
  257. /* Contains invalid flag values */
  258. return -1;
  259. if (sflags & SocketFlags_OutOfBand)
  260. flags |= MSG_OOB;
  261. if (sflags & SocketFlags_Peek)
  262. flags |= MSG_PEEK;
  263. if (sflags & SocketFlags_DontRoute)
  264. flags |= MSG_DONTROUTE;
  265. /* Ignore Partial - see bug 349688. Don't return -1, because
  266. * according to the comment in that bug ms runtime doesn't for
  267. * UDP sockets (this means we will silently ignore it for TCP
  268. * too)
  269. */
  270. #ifdef MSG_MORE
  271. if (sflags & SocketFlags_Partial)
  272. flags |= MSG_MORE;
  273. #endif
  274. #if 0
  275. /* Don't do anything for MaxIOVectorLength */
  276. if (sflags & SocketFlags_MaxIOVectorLength)
  277. return -1;
  278. #endif
  279. return flags;
  280. }
  281. /*
  282. * Returns:
  283. * 0 on success (mapped mono_level and mono_name to system_level and system_name
  284. * -1 on error
  285. * -2 on non-fatal error (ie, must ignore)
  286. */
  287. static gint32 convert_sockopt_level_and_name(MonoSocketOptionLevel mono_level,
  288. MonoSocketOptionName mono_name,
  289. int *system_level,
  290. int *system_name)
  291. {
  292. switch (mono_level) {
  293. case SocketOptionLevel_Socket:
  294. *system_level = SOL_SOCKET;
  295. switch(mono_name) {
  296. case SocketOptionName_DontLinger:
  297. /* This is SO_LINGER, because the setsockopt
  298. * internal call maps DontLinger to SO_LINGER
  299. * with l_onoff=0
  300. */
  301. *system_name = SO_LINGER;
  302. break;
  303. case SocketOptionName_Debug:
  304. *system_name = SO_DEBUG;
  305. break;
  306. #ifdef SO_ACCEPTCONN
  307. case SocketOptionName_AcceptConnection:
  308. *system_name = SO_ACCEPTCONN;
  309. break;
  310. #endif
  311. case SocketOptionName_ReuseAddress:
  312. *system_name = SO_REUSEADDR;
  313. break;
  314. case SocketOptionName_KeepAlive:
  315. *system_name = SO_KEEPALIVE;
  316. break;
  317. case SocketOptionName_DontRoute:
  318. *system_name = SO_DONTROUTE;
  319. break;
  320. case SocketOptionName_Broadcast:
  321. *system_name = SO_BROADCAST;
  322. break;
  323. case SocketOptionName_Linger:
  324. *system_name = SO_LINGER;
  325. break;
  326. case SocketOptionName_OutOfBandInline:
  327. *system_name = SO_OOBINLINE;
  328. break;
  329. case SocketOptionName_SendBuffer:
  330. *system_name = SO_SNDBUF;
  331. break;
  332. case SocketOptionName_ReceiveBuffer:
  333. *system_name = SO_RCVBUF;
  334. break;
  335. case SocketOptionName_SendLowWater:
  336. *system_name = SO_SNDLOWAT;
  337. break;
  338. case SocketOptionName_ReceiveLowWater:
  339. *system_name = SO_RCVLOWAT;
  340. break;
  341. case SocketOptionName_SendTimeout:
  342. *system_name = SO_SNDTIMEO;
  343. break;
  344. case SocketOptionName_ReceiveTimeout:
  345. *system_name = SO_RCVTIMEO;
  346. break;
  347. case SocketOptionName_Error:
  348. *system_name = SO_ERROR;
  349. break;
  350. case SocketOptionName_Type:
  351. *system_name = SO_TYPE;
  352. break;
  353. #ifdef SO_PEERCRED
  354. case SocketOptionName_PeerCred:
  355. *system_name = SO_PEERCRED;
  356. break;
  357. #endif
  358. case SocketOptionName_ExclusiveAddressUse:
  359. #ifdef SO_EXCLUSIVEADDRUSE
  360. *system_name = SO_EXCLUSIVEADDRUSE;
  361. break;
  362. #endif
  363. case SocketOptionName_UseLoopback:
  364. #ifdef SO_USELOOPBACK
  365. *system_name = SO_USELOOPBACK;
  366. break;
  367. #endif
  368. case SocketOptionName_MaxConnections:
  369. #ifdef SO_MAXCONN
  370. *system_name = SO_MAXCONN;
  371. break;
  372. #elif defined(SOMAXCONN)
  373. *system_name = SOMAXCONN;
  374. break;
  375. #endif
  376. default:
  377. g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at Socket level", mono_name);
  378. return(-1);
  379. }
  380. break;
  381. case SocketOptionLevel_IP:
  382. #ifdef HAVE_SOL_IP
  383. *system_level = SOL_IP;
  384. #else
  385. if (1) {
  386. static int cached = 0;
  387. static int proto;
  388. if (!cached) {
  389. struct protoent *pent;
  390. pent = getprotobyname ("IP");
  391. proto = pent ? pent->p_proto : 0 /* 0 a good default value?? */;
  392. cached = 1;
  393. }
  394. *system_level = proto;
  395. }
  396. #endif /* HAVE_SOL_IP */
  397. switch(mono_name) {
  398. case SocketOptionName_IPOptions:
  399. *system_name = IP_OPTIONS;
  400. break;
  401. #ifdef IP_HDRINCL
  402. case SocketOptionName_HeaderIncluded:
  403. *system_name = IP_HDRINCL;
  404. break;
  405. #endif
  406. #ifdef IP_TOS
  407. case SocketOptionName_TypeOfService:
  408. *system_name = IP_TOS;
  409. break;
  410. #endif
  411. #ifdef IP_TTL
  412. case SocketOptionName_IpTimeToLive:
  413. *system_name = IP_TTL;
  414. break;
  415. #endif
  416. case SocketOptionName_MulticastInterface:
  417. *system_name = IP_MULTICAST_IF;
  418. break;
  419. case SocketOptionName_MulticastTimeToLive:
  420. *system_name = IP_MULTICAST_TTL;
  421. break;
  422. case SocketOptionName_MulticastLoopback:
  423. *system_name = IP_MULTICAST_LOOP;
  424. break;
  425. case SocketOptionName_AddMembership:
  426. *system_name = IP_ADD_MEMBERSHIP;
  427. break;
  428. case SocketOptionName_DropMembership:
  429. *system_name = IP_DROP_MEMBERSHIP;
  430. break;
  431. #ifdef HAVE_IP_PKTINFO
  432. case SocketOptionName_PacketInformation:
  433. *system_name = IP_PKTINFO;
  434. break;
  435. #endif /* HAVE_IP_PKTINFO */
  436. case SocketOptionName_DontFragment:
  437. #ifdef HAVE_IP_DONTFRAGMENT
  438. *system_name = IP_DONTFRAGMENT;
  439. break;
  440. #elif defined HAVE_IP_MTU_DISCOVER
  441. /* Not quite the same */
  442. *system_name = IP_MTU_DISCOVER;
  443. break;
  444. #else
  445. /* If the flag is not available on this system, we can ignore this error */
  446. return (-2);
  447. #endif /* HAVE_IP_DONTFRAGMENT */
  448. case SocketOptionName_AddSourceMembership:
  449. case SocketOptionName_DropSourceMembership:
  450. case SocketOptionName_BlockSource:
  451. case SocketOptionName_UnblockSource:
  452. /* Can't figure out how to map these, so fall
  453. * through
  454. */
  455. default:
  456. g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IP level", mono_name);
  457. return(-1);
  458. }
  459. break;
  460. #ifdef AF_INET6
  461. case SocketOptionLevel_IPv6:
  462. #ifdef HAVE_SOL_IPV6
  463. *system_level = SOL_IPV6;
  464. #else
  465. if (1) {
  466. static int cached = 0;
  467. static int proto;
  468. if (!cached) {
  469. struct protoent *pent;
  470. pent = getprotobyname ("IPV6");
  471. proto = pent ? pent->p_proto : 41 /* 41 a good default value?? */;
  472. cached = 1;
  473. }
  474. *system_level = proto;
  475. }
  476. #endif /* HAVE_SOL_IPV6 */
  477. switch(mono_name) {
  478. case SocketOptionName_IpTimeToLive:
  479. *system_name = IPV6_UNICAST_HOPS;
  480. break;
  481. case SocketOptionName_MulticastInterface:
  482. *system_name = IPV6_MULTICAST_IF;
  483. break;
  484. case SocketOptionName_MulticastTimeToLive:
  485. *system_name = IPV6_MULTICAST_HOPS;
  486. break;
  487. case SocketOptionName_MulticastLoopback:
  488. *system_name = IPV6_MULTICAST_LOOP;
  489. break;
  490. case SocketOptionName_AddMembership:
  491. *system_name = IPV6_JOIN_GROUP;
  492. break;
  493. case SocketOptionName_DropMembership:
  494. *system_name = IPV6_LEAVE_GROUP;
  495. break;
  496. case SocketOptionName_PacketInformation:
  497. #ifdef HAVE_IPV6_PKTINFO
  498. *system_name = IPV6_PKTINFO;
  499. #endif
  500. break;
  501. case SocketOptionName_HeaderIncluded:
  502. case SocketOptionName_IPOptions:
  503. case SocketOptionName_TypeOfService:
  504. case SocketOptionName_DontFragment:
  505. case SocketOptionName_AddSourceMembership:
  506. case SocketOptionName_DropSourceMembership:
  507. case SocketOptionName_BlockSource:
  508. case SocketOptionName_UnblockSource:
  509. /* Can't figure out how to map these, so fall
  510. * through
  511. */
  512. default:
  513. g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at IPv6 level", mono_name);
  514. return(-1);
  515. }
  516. break; /* SocketOptionLevel_IPv6 */
  517. #endif
  518. case SocketOptionLevel_Tcp:
  519. #ifdef HAVE_SOL_TCP
  520. *system_level = SOL_TCP;
  521. #else
  522. if (1) {
  523. static int cached = 0;
  524. static int proto;
  525. if (!cached) {
  526. struct protoent *pent;
  527. pent = getprotobyname ("TCP");
  528. proto = pent ? pent->p_proto : 6 /* is 6 a good default value?? */;
  529. cached = 1;
  530. }
  531. *system_level = proto;
  532. }
  533. #endif /* HAVE_SOL_TCP */
  534. switch(mono_name) {
  535. case SocketOptionName_NoDelay:
  536. *system_name = TCP_NODELAY;
  537. break;
  538. #if 0
  539. /* The documentation is talking complete
  540. * bollocks here: rfc-1222 is titled
  541. * 'Advancing the NSFNET Routing Architecture'
  542. * and doesn't mention either of the words
  543. * "expedite" or "urgent".
  544. */
  545. case SocketOptionName_BsdUrgent:
  546. case SocketOptionName_Expedited:
  547. #endif
  548. default:
  549. g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at TCP level", mono_name);
  550. return(-1);
  551. }
  552. break;
  553. case SocketOptionLevel_Udp:
  554. g_warning("System.Net.Sockets.SocketOptionLevel has unsupported value 0x%x", mono_level);
  555. switch(mono_name) {
  556. case SocketOptionName_NoChecksum:
  557. case SocketOptionName_ChecksumCoverage:
  558. default:
  559. g_warning("System.Net.Sockets.SocketOptionName 0x%x is not supported at UDP level", mono_name);
  560. return(-1);
  561. }
  562. return(-1);
  563. break;
  564. default:
  565. g_warning("System.Net.Sockets.SocketOptionLevel has unknown value 0x%x", mono_level);
  566. return(-1);
  567. }
  568. return(0);
  569. }
  570. static MonoImage *get_socket_assembly (void)
  571. {
  572. static const char *version = NULL;
  573. static gboolean moonlight;
  574. static MonoImage *socket_assembly = NULL;
  575. if (version == NULL) {
  576. version = mono_get_runtime_info ()->framework_version;
  577. moonlight = !strcmp (version, "2.1");
  578. }
  579. if (socket_assembly == NULL) {
  580. if (moonlight) {
  581. socket_assembly = mono_image_loaded ("System.Net");
  582. if (!socket_assembly) {
  583. MonoAssembly *sa = mono_assembly_open ("System.Net.dll", NULL);
  584. if (!sa) {
  585. g_assert_not_reached ();
  586. } else {
  587. socket_assembly = mono_assembly_get_image (sa);
  588. }
  589. }
  590. } else {
  591. socket_assembly = mono_image_loaded ("System");
  592. if (!socket_assembly) {
  593. MonoAssembly *sa = mono_assembly_open ("System.dll", NULL);
  594. if (!sa) {
  595. g_assert_not_reached ();
  596. } else {
  597. socket_assembly = mono_assembly_get_image (sa);
  598. }
  599. }
  600. }
  601. }
  602. return(socket_assembly);
  603. }
  604. #ifdef AF_INET6
  605. static gint32 get_family_hint(void)
  606. {
  607. MonoDomain *domain = mono_domain_get ();
  608. if (!domain->inet_family_hint) {
  609. MonoClass *socket_class;
  610. MonoClassField *ipv6_field, *ipv4_field;
  611. gint32 ipv6_enabled = -1, ipv4_enabled = -1;
  612. MonoVTable *vtable;
  613. socket_class = mono_class_from_name (get_socket_assembly (), "System.Net.Sockets", "Socket");
  614. ipv4_field = mono_class_get_field_from_name (socket_class, "ipv4Supported");
  615. ipv6_field = mono_class_get_field_from_name (socket_class, "ipv6Supported");
  616. vtable = mono_class_vtable (mono_domain_get (), socket_class);
  617. g_assert (vtable);
  618. mono_runtime_class_init (vtable);
  619. mono_field_static_get_value (vtable, ipv4_field, &ipv4_enabled);
  620. mono_field_static_get_value (vtable, ipv6_field, &ipv6_enabled);
  621. mono_domain_lock (domain);
  622. if (ipv4_enabled == 1 && ipv6_enabled == 1) {
  623. domain->inet_family_hint = 1;
  624. } else if (ipv4_enabled == 1) {
  625. domain->inet_family_hint = 2;
  626. } else {
  627. domain->inet_family_hint = 3;
  628. }
  629. mono_domain_unlock (domain);
  630. }
  631. switch (domain->inet_family_hint) {
  632. case 1: return PF_UNSPEC;
  633. case 2: return PF_INET;
  634. case 3: return PF_INET6;
  635. default:
  636. return PF_UNSPEC;
  637. }
  638. }
  639. #endif
  640. gpointer ves_icall_System_Net_Sockets_Socket_Socket_internal(MonoObject *this, gint32 family, gint32 type, gint32 proto, gint32 *error)
  641. {
  642. SOCKET sock;
  643. gint32 sock_family;
  644. gint32 sock_proto;
  645. gint32 sock_type;
  646. MONO_ARCH_SAVE_REGS;
  647. *error = 0;
  648. sock_family=convert_family(family);
  649. if(sock_family==-1) {
  650. *error = WSAEAFNOSUPPORT;
  651. return(NULL);
  652. }
  653. sock_proto=convert_proto(proto);
  654. if(sock_proto==-1) {
  655. *error = WSAEPROTONOSUPPORT;
  656. return(NULL);
  657. }
  658. sock_type=convert_type(type);
  659. if(sock_type==-1) {
  660. *error = WSAESOCKTNOSUPPORT;
  661. return(NULL);
  662. }
  663. sock = _wapi_socket (sock_family, sock_type, sock_proto,
  664. NULL, 0, WSA_FLAG_OVERLAPPED);
  665. if(sock==INVALID_SOCKET) {
  666. *error = WSAGetLastError ();
  667. return(NULL);
  668. }
  669. if (sock_family == AF_INET && sock_type == SOCK_DGRAM) {
  670. return (GUINT_TO_POINTER (sock));
  671. }
  672. #ifdef AF_INET6
  673. if (sock_family == AF_INET6 && sock_type == SOCK_DGRAM) {
  674. return (GUINT_TO_POINTER (sock));
  675. }
  676. #endif
  677. return(GUINT_TO_POINTER (sock));
  678. }
  679. /* FIXME: the SOCKET parameter (here and in other functions in this
  680. * file) is really an IntPtr which needs to be converted to a guint32.
  681. */
  682. void ves_icall_System_Net_Sockets_Socket_Close_internal(SOCKET sock,
  683. gint32 *error)
  684. {
  685. MONO_ARCH_SAVE_REGS;
  686. LOGDEBUG (g_message ("%s: closing 0x%x", __func__, sock));
  687. *error = 0;
  688. /* Clear any pending work item from this socket if the underlying
  689. * polling system does not notify when the socket is closed */
  690. mono_thread_pool_remove_socket (GPOINTER_TO_INT (sock));
  691. closesocket(sock);
  692. }
  693. gint32 ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal(void)
  694. {
  695. MONO_ARCH_SAVE_REGS;
  696. LOGDEBUG (g_message("%s: returning %d", __func__, WSAGetLastError()));
  697. return(WSAGetLastError());
  698. }
  699. gint32 ves_icall_System_Net_Sockets_Socket_Available_internal(SOCKET sock,
  700. gint32 *error)
  701. {
  702. int ret;
  703. int amount;
  704. MONO_ARCH_SAVE_REGS;
  705. *error = 0;
  706. ret=ioctlsocket(sock, FIONREAD, &amount);
  707. if(ret==SOCKET_ERROR) {
  708. *error = WSAGetLastError ();
  709. return(0);
  710. }
  711. return(amount);
  712. }
  713. void ves_icall_System_Net_Sockets_Socket_Blocking_internal(SOCKET sock,
  714. gboolean block,
  715. gint32 *error)
  716. {
  717. int ret;
  718. MONO_ARCH_SAVE_REGS;
  719. *error = 0;
  720. /*
  721. * block == TRUE/FALSE means we will block/not block.
  722. * But the ioctlsocket call takes TRUE/FALSE for non-block/block
  723. */
  724. block = !block;
  725. ret = ioctlsocket (sock, FIONBIO, (gulong *) &block);
  726. if(ret==SOCKET_ERROR) {
  727. *error = WSAGetLastError ();
  728. }
  729. }
  730. gpointer ves_icall_System_Net_Sockets_Socket_Accept_internal(SOCKET sock,
  731. gint32 *error,
  732. gboolean blocking)
  733. {
  734. SOCKET newsock;
  735. MONO_ARCH_SAVE_REGS;
  736. *error = 0;
  737. #ifdef PLATFORM_WIN32
  738. {
  739. MonoThread* curthread = mono_thread_current ();
  740. curthread->interrupt_on_stop = (gpointer)TRUE;
  741. newsock = _wapi_accept (sock, NULL, 0);
  742. curthread->interrupt_on_stop = (gpointer)FALSE;
  743. }
  744. #else
  745. newsock = _wapi_accept (sock, NULL, 0);
  746. #endif
  747. if(newsock==INVALID_SOCKET) {
  748. *error = WSAGetLastError ();
  749. return(NULL);
  750. }
  751. return(GUINT_TO_POINTER (newsock));
  752. }
  753. void ves_icall_System_Net_Sockets_Socket_Listen_internal(SOCKET sock,
  754. guint32 backlog,
  755. gint32 *error)
  756. {
  757. int ret;
  758. MONO_ARCH_SAVE_REGS;
  759. *error = 0;
  760. ret = _wapi_listen (sock, backlog);
  761. if(ret==SOCKET_ERROR) {
  762. *error = WSAGetLastError ();
  763. }
  764. }
  765. static MonoObject *create_object_from_sockaddr(struct sockaddr *saddr,
  766. int sa_size, gint32 *error)
  767. {
  768. MonoDomain *domain = mono_domain_get ();
  769. MonoObject *sockaddr_obj;
  770. MonoClass *sockaddr_class;
  771. MonoClassField *field;
  772. MonoArray *data;
  773. MonoAddressFamily family;
  774. /* Build a System.Net.SocketAddress object instance */
  775. sockaddr_class=mono_class_from_name_cached (get_socket_assembly (), "System.Net", "SocketAddress");
  776. sockaddr_obj=mono_object_new(domain, sockaddr_class);
  777. /* Locate the SocketAddress data buffer in the object */
  778. field=mono_class_get_field_from_name_cached (sockaddr_class, "data");
  779. /* Make sure there is space for the family and size bytes */
  780. #ifdef HAVE_SYS_UN_H
  781. if (saddr->sa_family == AF_UNIX) {
  782. /* sa_len includes the entire sockaddr size, so we don't need the
  783. * N bytes (sizeof (unsigned short)) of the family. */
  784. data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size);
  785. } else
  786. #endif
  787. {
  788. /* May be the +2 here is too conservative, as sa_len returns
  789. * the length of the entire sockaddr_in/in6, including
  790. * sizeof (unsigned short) of the family */
  791. data=mono_array_new_cached(domain, mono_get_byte_class (), sa_size+2);
  792. }
  793. /* The data buffer is laid out as follows:
  794. * bytes 0 and 1 are the address family
  795. * bytes 2 and 3 are the port info
  796. * the rest is the address info
  797. */
  798. family=convert_to_mono_family(saddr->sa_family);
  799. if(family==AddressFamily_Unknown) {
  800. *error = WSAEAFNOSUPPORT;
  801. return(NULL);
  802. }
  803. mono_array_set(data, guint8, 0, family & 0x0FF);
  804. mono_array_set(data, guint8, 1, (family >> 8) & 0x0FF);
  805. if(saddr->sa_family==AF_INET) {
  806. struct sockaddr_in *sa_in=(struct sockaddr_in *)saddr;
  807. guint16 port=ntohs(sa_in->sin_port);
  808. guint32 address=ntohl(sa_in->sin_addr.s_addr);
  809. if(sa_size<8) {
  810. mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
  811. }
  812. mono_array_set(data, guint8, 2, (port>>8) & 0xff);
  813. mono_array_set(data, guint8, 3, (port) & 0xff);
  814. mono_array_set(data, guint8, 4, (address>>24) & 0xff);
  815. mono_array_set(data, guint8, 5, (address>>16) & 0xff);
  816. mono_array_set(data, guint8, 6, (address>>8) & 0xff);
  817. mono_array_set(data, guint8, 7, (address) & 0xff);
  818. mono_field_set_value (sockaddr_obj, field, data);
  819. return(sockaddr_obj);
  820. #ifdef AF_INET6
  821. } else if (saddr->sa_family == AF_INET6) {
  822. struct sockaddr_in6 *sa_in=(struct sockaddr_in6 *)saddr;
  823. int i;
  824. guint16 port=ntohs(sa_in->sin6_port);
  825. if(sa_size<28) {
  826. mono_raise_exception((MonoException *)mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
  827. }
  828. mono_array_set(data, guint8, 2, (port>>8) & 0xff);
  829. mono_array_set(data, guint8, 3, (port) & 0xff);
  830. for(i=0; i<16; i++) {
  831. mono_array_set(data, guint8, 8+i,
  832. sa_in->sin6_addr.s6_addr[i]);
  833. }
  834. mono_array_set(data, guint8, 24, sa_in->sin6_scope_id & 0xff);
  835. mono_array_set(data, guint8, 25,
  836. (sa_in->sin6_scope_id >> 8) & 0xff);
  837. mono_array_set(data, guint8, 26,
  838. (sa_in->sin6_scope_id >> 16) & 0xff);
  839. mono_array_set(data, guint8, 27,
  840. (sa_in->sin6_scope_id >> 24) & 0xff);
  841. mono_field_set_value (sockaddr_obj, field, data);
  842. return(sockaddr_obj);
  843. #endif
  844. #ifdef HAVE_SYS_UN_H
  845. } else if (saddr->sa_family == AF_UNIX) {
  846. int i;
  847. for (i = 0; i < sa_size; i++) {
  848. mono_array_set (data, guint8, i+2, saddr->sa_data[i]);
  849. }
  850. mono_field_set_value (sockaddr_obj, field, data);
  851. return sockaddr_obj;
  852. #endif
  853. } else {
  854. *error = WSAEAFNOSUPPORT;
  855. return(NULL);
  856. }
  857. }
  858. extern MonoObject *ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal(SOCKET sock, gint32 *error)
  859. {
  860. gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
  861. socklen_t salen;
  862. int ret;
  863. MONO_ARCH_SAVE_REGS;
  864. *error = 0;
  865. salen=sizeof(sa);
  866. ret = _wapi_getsockname (sock, (struct sockaddr *)sa, &salen);
  867. if(ret==SOCKET_ERROR) {
  868. *error = WSAGetLastError ();
  869. return(NULL);
  870. }
  871. LOGDEBUG (g_message("%s: bound to %s port %d", __func__, inet_ntoa(((struct sockaddr_in *)&sa)->sin_addr), ntohs(((struct sockaddr_in *)&sa)->sin_port)));
  872. return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
  873. error));
  874. }
  875. extern MonoObject *ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal(SOCKET sock, gint32 *error)
  876. {
  877. gchar sa[32]; /* sockaddr in not big enough for sockaddr_in6 */
  878. socklen_t salen;
  879. int ret;
  880. MONO_ARCH_SAVE_REGS;
  881. *error = 0;
  882. salen=sizeof(sa);
  883. ret = _wapi_getpeername (sock, (struct sockaddr *)sa, &salen);
  884. if(ret==SOCKET_ERROR) {
  885. *error = WSAGetLastError ();
  886. return(NULL);
  887. }
  888. LOGDEBUG (g_message("%s: connected to %s port %d", __func__, inet_ntoa(((struct sockaddr_in *)&sa)->sin_addr), ntohs(((struct sockaddr_in *)&sa)->sin_port)));
  889. return(create_object_from_sockaddr((struct sockaddr *)sa, salen,
  890. error));
  891. }
  892. static struct sockaddr *create_sockaddr_from_object(MonoObject *saddr_obj,
  893. socklen_t *sa_size,
  894. gint32 *error)
  895. {
  896. MonoClassField *field;
  897. MonoArray *data;
  898. gint32 family;
  899. int len;
  900. /* Dig the SocketAddress data buffer out of the object */
  901. field=mono_class_get_field_from_name(saddr_obj->vtable->klass, "data");
  902. data=*(MonoArray **)(((char *)saddr_obj) + field->offset);
  903. /* The data buffer is laid out as follows:
  904. * byte 0 is the address family low byte
  905. * byte 1 is the address family high byte
  906. * INET:
  907. * bytes 2 and 3 are the port info
  908. * the rest is the address info
  909. * UNIX:
  910. * the rest is the file name
  911. */
  912. len = mono_array_length (data);
  913. if (len < 2) {
  914. mono_raise_exception (mono_exception_from_name(mono_get_corlib (), "System", "SystemException"));
  915. }
  916. family = convert_family (mono_array_get (data, guint8, 0) + (mono_array_get (data, guint8, 1) << 8));
  917. if (family == AF_INET) {
  918. struct sockaddr_in *sa;
  919. guint16 port;
  920. guint32 address;
  921. if (len < 8) {
  922. mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
  923. }
  924. sa = g_new0 (struct sockaddr_in, 1);
  925. port = (mono_array_get (data, guint8, 2) << 8) +
  926. mono_array_get (data, guint8, 3);
  927. address = (mono_array_get (data, guint8, 4) << 24) +
  928. (mono_array_get (data, guint8, 5) << 16 ) +
  929. (mono_array_get (data, guint8, 6) << 8) +
  930. mono_array_get (data, guint8, 7);
  931. sa->sin_family = family;
  932. sa->sin_addr.s_addr = htonl (address);
  933. sa->sin_port = htons (port);
  934. *sa_size = sizeof(struct sockaddr_in);
  935. return((struct sockaddr *)sa);
  936. #ifdef AF_INET6
  937. } else if (family == AF_INET6) {
  938. struct sockaddr_in6 *sa;
  939. int i;
  940. guint16 port;
  941. guint32 scopeid;
  942. if (len < 28) {
  943. mono_raise_exception (mono_exception_from_name (mono_get_corlib (), "System", "SystemException"));
  944. }
  945. sa = g_new0 (struct sockaddr_in6, 1);
  946. port = mono_array_get (data, guint8, 3) +
  947. (mono_array_get (data, guint8, 2) << 8);
  948. scopeid = mono_array_get (data, guint8, 24) +
  949. (mono_array_get (data, guint8, 25) << 8) +
  950. (mono_array_get (data, guint8, 26) << 16) +
  951. (mono_array_get (data, guint8, 27) << 24);
  952. sa->sin6_family = family;
  953. sa->sin6_port = htons (port);
  954. sa->sin6_scope_id = scopeid;
  955. for(i=0; i<16; i++) {
  956. sa->sin6_addr.s6_addr[i] = mono_array_get (data, guint8, 8+i);
  957. }
  958. *sa_size = sizeof(struct sockaddr_in6);
  959. return((struct sockaddr *)sa);
  960. #endif
  961. #ifdef HAVE_SYS_UN_H
  962. } else if (family == AF_UNIX) {
  963. struct sockaddr_un *sock_un;
  964. int i;
  965. /* Need a byte for the '\0' terminator/prefix, and the first
  966. * two bytes hold the SocketAddress family
  967. */
  968. if (len - 2 >= MONO_SIZEOF_SUNPATH) {
  969. mono_raise_exception (mono_get_exception_index_out_of_range ());
  970. }
  971. sock_un = g_new0 (struct sockaddr_un, 1);
  972. sock_un->sun_family = family;
  973. for (i = 0; i < len - 2; i++) {
  974. sock_un->sun_path [i] = mono_array_get (data, guint8,
  975. i + 2);
  976. }
  977. *sa_size = len;
  978. return (struct sockaddr *)sock_un;
  979. #endif
  980. } else {
  981. *error = WSAEAFNOSUPPORT;
  982. return(0);
  983. }
  984. }
  985. extern void ves_icall_System_Net_Sockets_Socket_Bind_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
  986. {
  987. struct sockaddr *sa;
  988. socklen_t sa_size;
  989. int ret;
  990. MONO_ARCH_SAVE_REGS;
  991. *error = 0;
  992. sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
  993. if (*error != 0) {
  994. return;
  995. }
  996. LOGDEBUG (g_message("%s: binding to %s port %d", __func__, inet_ntoa(((struct sockaddr_in *)sa)->sin_addr), ntohs (((struct sockaddr_in *)sa)->sin_port)));
  997. ret = _wapi_bind (sock, sa, sa_size);
  998. if(ret==SOCKET_ERROR) {
  999. *error = WSAGetLastError ();
  1000. }
  1001. g_free(sa);
  1002. }
  1003. enum {
  1004. SelectModeRead,
  1005. SelectModeWrite,
  1006. SelectModeError
  1007. };
  1008. MonoBoolean
  1009. ves_icall_System_Net_Sockets_Socket_Poll_internal (SOCKET sock, gint mode,
  1010. gint timeout, gint32 *error)
  1011. {
  1012. MonoThread *thread = NULL;
  1013. mono_pollfd *pfds;
  1014. int ret;
  1015. time_t start;
  1016. MONO_ARCH_SAVE_REGS;
  1017. pfds = g_new0 (mono_pollfd, 1);
  1018. pfds[0].fd = GPOINTER_TO_INT (sock);
  1019. pfds[0].events = (mode == SelectModeRead) ? MONO_POLLIN :
  1020. (mode == SelectModeWrite) ? MONO_POLLOUT :
  1021. (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL);
  1022. timeout = (timeout >= 0) ? (timeout / 1000) : -1;
  1023. start = time (NULL);
  1024. do {
  1025. *error = 0;
  1026. ret = mono_poll (pfds, 1, timeout);
  1027. if (timeout > 0 && ret < 0) {
  1028. int err = errno;
  1029. int sec = time (NULL) - start;
  1030. timeout -= sec * 1000;
  1031. if (timeout < 0) {
  1032. timeout = 0;
  1033. }
  1034. errno = err;
  1035. }
  1036. if (ret == -1 && errno == EINTR) {
  1037. int leave = 0;
  1038. if (thread == NULL) {
  1039. thread = mono_thread_current ();
  1040. }
  1041. leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
  1042. if (leave != 0) {
  1043. g_free (pfds);
  1044. return(FALSE);
  1045. } else {
  1046. /* Suspend requested? */
  1047. mono_thread_interruption_checkpoint ();
  1048. }
  1049. errno = EINTR;
  1050. }
  1051. } while (ret == -1 && errno == EINTR);
  1052. if (ret == -1) {
  1053. #ifdef PLATFORM_WIN32
  1054. *error = WSAGetLastError ();
  1055. #else
  1056. *error = errno_to_WSA (errno, __func__);
  1057. #endif
  1058. g_free (pfds);
  1059. return(FALSE);
  1060. }
  1061. g_free (pfds);
  1062. if (ret == 0) {
  1063. return(FALSE);
  1064. } else {
  1065. return (TRUE);
  1066. }
  1067. }
  1068. extern void ves_icall_System_Net_Sockets_Socket_Connect_internal(SOCKET sock, MonoObject *sockaddr, gint32 *error)
  1069. {
  1070. struct sockaddr *sa;
  1071. socklen_t sa_size;
  1072. int ret;
  1073. MONO_ARCH_SAVE_REGS;
  1074. *error = 0;
  1075. sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
  1076. if (*error != 0) {
  1077. return;
  1078. }
  1079. LOGDEBUG (g_message("%s: connecting to %s port %d", __func__, inet_ntoa(((struct sockaddr_in *)sa)->sin_addr), ntohs (((struct sockaddr_in *)sa)->sin_port)));
  1080. ret = _wapi_connect (sock, sa, sa_size);
  1081. if(ret==SOCKET_ERROR) {
  1082. *error = WSAGetLastError ();
  1083. }
  1084. g_free(sa);
  1085. }
  1086. /* These #defines from mswsock.h from wine. Defining them here allows
  1087. * us to build this file on a mingw box that doesn't know the magic
  1088. * numbers, but still run on a newer windows box that does.
  1089. */
  1090. #ifndef WSAID_DISCONNECTEX
  1091. #define WSAID_DISCONNECTEX {0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
  1092. typedef BOOL (WINAPI *LPFN_DISCONNECTEX)(SOCKET, LPOVERLAPPED, DWORD, DWORD);
  1093. #endif
  1094. #ifndef WSAID_TRANSMITFILE
  1095. #define WSAID_TRANSMITFILE {0xb5367df0,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
  1096. typedef BOOL (WINAPI *LPFN_TRANSMITFILE)(SOCKET, HANDLE, DWORD, DWORD, LPOVERLAPPED, LPTRANSMIT_FILE_BUFFERS, DWORD);
  1097. #endif
  1098. extern void ves_icall_System_Net_Sockets_Socket_Disconnect_internal(SOCKET sock, MonoBoolean reuse, gint32 *error)
  1099. {
  1100. int ret;
  1101. glong output_bytes = 0;
  1102. GUID disco_guid = WSAID_DISCONNECTEX;
  1103. GUID trans_guid = WSAID_TRANSMITFILE;
  1104. LPFN_DISCONNECTEX _wapi_disconnectex = NULL;
  1105. LPFN_TRANSMITFILE _wapi_transmitfile = NULL;
  1106. gboolean bret;
  1107. MONO_ARCH_SAVE_REGS;
  1108. *error = 0;
  1109. LOGDEBUG (g_message("%s: disconnecting from socket %p (reuse %d)", __func__, sock, reuse));
  1110. /* I _think_ the extension function pointers need to be looked
  1111. * up for each socket. FIXME: check the best way to store
  1112. * pointers to functions in managed objects that still works
  1113. * on 64bit platforms.
  1114. */
  1115. ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
  1116. (void *)&disco_guid, sizeof(GUID),
  1117. (void *)&_wapi_disconnectex, sizeof(void *),
  1118. &output_bytes, NULL, NULL);
  1119. if (ret != 0) {
  1120. /* make sure that WSAIoctl didn't put crap in the
  1121. * output pointer
  1122. */
  1123. _wapi_disconnectex = NULL;
  1124. /* Look up the TransmitFile extension function pointer
  1125. * instead of calling TransmitFile() directly, because
  1126. * apparently "Several of the extension functions have
  1127. * been available since WinSock 1.1 and are exported
  1128. * from MSWsock.dll, however it's not advisable to
  1129. * link directly to this dll as this ties you to the
  1130. * Microsoft WinSock provider. A provider neutral way
  1131. * of accessing these extension functions is to load
  1132. * them dynamically via WSAIoctl using the
  1133. * SIO_GET_EXTENSION_FUNCTION_POINTER op code. This
  1134. * should, theoretically, allow you to access these
  1135. * functions from any provider that supports them..."
  1136. * (http://www.codeproject.com/internet/jbsocketserver3.asp)
  1137. */
  1138. ret = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
  1139. (void *)&trans_guid, sizeof(GUID),
  1140. (void *)&_wapi_transmitfile, sizeof(void *),
  1141. &output_bytes, NULL, NULL);
  1142. if (ret != 0) {
  1143. _wapi_transmitfile = NULL;
  1144. }
  1145. }
  1146. if (_wapi_disconnectex != NULL) {
  1147. bret = _wapi_disconnectex (sock, NULL, TF_REUSE_SOCKET, 0);
  1148. } else if (_wapi_transmitfile != NULL) {
  1149. bret = _wapi_transmitfile (sock, NULL, 0, 0, NULL, NULL,
  1150. TF_DISCONNECT | TF_REUSE_SOCKET);
  1151. } else {
  1152. *error = ERROR_NOT_SUPPORTED;
  1153. return;
  1154. }
  1155. if (bret == FALSE) {
  1156. *error = WSAGetLastError ();
  1157. }
  1158. }
  1159. gint32 ves_icall_System_Net_Sockets_Socket_Receive_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
  1160. {
  1161. int ret;
  1162. guchar *buf;
  1163. gint32 alen;
  1164. int recvflags=0;
  1165. MONO_ARCH_SAVE_REGS;
  1166. *error = 0;
  1167. alen = mono_array_length (buffer);
  1168. if (offset > alen - count) {
  1169. return(0);
  1170. }
  1171. buf=mono_array_addr(buffer, guchar, offset);
  1172. recvflags = convert_socketflags (flags);
  1173. if (recvflags == -1) {
  1174. *error = WSAEOPNOTSUPP;
  1175. return (0);
  1176. }
  1177. #ifdef PLATFORM_WIN32
  1178. {
  1179. MonoThread* curthread = mono_thread_current ();
  1180. curthread->interrupt_on_stop = (gpointer)TRUE;
  1181. ret = _wapi_recv (sock, buf, count, recvflags);
  1182. curthread->interrupt_on_stop = (gpointer)FALSE;
  1183. }
  1184. #else
  1185. ret = _wapi_recv (sock, buf, count, recvflags);
  1186. #endif
  1187. if(ret==SOCKET_ERROR) {
  1188. *error = WSAGetLastError ();
  1189. return(0);
  1190. }
  1191. return(ret);
  1192. }
  1193. gint32 ves_icall_System_Net_Sockets_Socket_Receive_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
  1194. {
  1195. int ret, count;
  1196. DWORD recv;
  1197. WSABUF *wsabufs;
  1198. DWORD recvflags = 0;
  1199. MONO_ARCH_SAVE_REGS;
  1200. *error = 0;
  1201. wsabufs = mono_array_addr (buffers, WSABUF, 0);
  1202. count = mono_array_length (buffers);
  1203. recvflags = convert_socketflags (flags);
  1204. if (recvflags == -1) {
  1205. *error = WSAEOPNOTSUPP;
  1206. return(0);
  1207. }
  1208. ret = WSARecv (sock, wsabufs, count, &recv, &recvflags, NULL, NULL);
  1209. if (ret == SOCKET_ERROR) {
  1210. *error = WSAGetLastError ();
  1211. return(0);
  1212. }
  1213. return(recv);
  1214. }
  1215. gint32 ves_icall_System_Net_Sockets_Socket_RecvFrom_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject **sockaddr, gint32 *error)
  1216. {
  1217. int ret;
  1218. guchar *buf;
  1219. gint32 alen;
  1220. int recvflags=0;
  1221. struct sockaddr *sa;
  1222. socklen_t sa_size;
  1223. MONO_ARCH_SAVE_REGS;
  1224. *error = 0;
  1225. alen = mono_array_length (buffer);
  1226. if (offset > alen - count) {
  1227. return(0);
  1228. }
  1229. sa=create_sockaddr_from_object(*sockaddr, &sa_size, error);
  1230. if (*error != 0) {
  1231. return(0);
  1232. }
  1233. buf=mono_array_addr(buffer, guchar, offset);
  1234. recvflags = convert_socketflags (flags);
  1235. if (recvflags == -1) {
  1236. *error = WSAEOPNOTSUPP;
  1237. return (0);
  1238. }
  1239. ret = _wapi_recvfrom (sock, buf, count, recvflags, sa, &sa_size);
  1240. if(ret==SOCKET_ERROR) {
  1241. g_free(sa);
  1242. *error = WSAGetLastError ();
  1243. return(0);
  1244. }
  1245. /* If we didn't get a socket size, then we're probably a
  1246. * connected connection-oriented socket and the stack hasn't
  1247. * returned the remote address. All we can do is return null.
  1248. */
  1249. if ( sa_size != 0 )
  1250. *sockaddr=create_object_from_sockaddr(sa, sa_size, error);
  1251. else
  1252. *sockaddr=NULL;
  1253. g_free(sa);
  1254. return(ret);
  1255. }
  1256. gint32 ves_icall_System_Net_Sockets_Socket_Send_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, gint32 *error)
  1257. {
  1258. int ret;
  1259. guchar *buf;
  1260. gint32 alen;
  1261. int sendflags=0;
  1262. MONO_ARCH_SAVE_REGS;
  1263. *error = 0;
  1264. alen = mono_array_length (buffer);
  1265. if (offset > alen - count) {
  1266. return(0);
  1267. }
  1268. LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
  1269. buf=mono_array_addr(buffer, guchar, offset);
  1270. LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
  1271. sendflags = convert_socketflags (flags);
  1272. if (sendflags == -1) {
  1273. *error = WSAEOPNOTSUPP;
  1274. return (0);
  1275. }
  1276. ret = _wapi_send (sock, buf, count, sendflags);
  1277. if(ret==SOCKET_ERROR) {
  1278. *error = WSAGetLastError ();
  1279. return(0);
  1280. }
  1281. return(ret);
  1282. }
  1283. gint32 ves_icall_System_Net_Sockets_Socket_Send_array_internal(SOCKET sock, MonoArray *buffers, gint32 flags, gint32 *error)
  1284. {
  1285. int ret, count;
  1286. DWORD sent;
  1287. WSABUF *wsabufs;
  1288. DWORD sendflags = 0;
  1289. MONO_ARCH_SAVE_REGS;
  1290. *error = 0;
  1291. wsabufs = mono_array_addr (buffers, WSABUF, 0);
  1292. count = mono_array_length (buffers);
  1293. sendflags = convert_socketflags (flags);
  1294. if (sendflags == -1) {
  1295. *error = WSAEOPNOTSUPP;
  1296. return(0);
  1297. }
  1298. ret = WSASend (sock, wsabufs, count, &sent, sendflags, NULL, NULL);
  1299. if (ret == SOCKET_ERROR) {
  1300. *error = WSAGetLastError ();
  1301. return(0);
  1302. }
  1303. return(sent);
  1304. }
  1305. gint32 ves_icall_System_Net_Sockets_Socket_SendTo_internal(SOCKET sock, MonoArray *buffer, gint32 offset, gint32 count, gint32 flags, MonoObject *sockaddr, gint32 *error)
  1306. {
  1307. int ret;
  1308. guchar *buf;
  1309. gint32 alen;
  1310. int sendflags=0;
  1311. struct sockaddr *sa;
  1312. socklen_t sa_size;
  1313. MONO_ARCH_SAVE_REGS;
  1314. *error = 0;
  1315. alen = mono_array_length (buffer);
  1316. if (offset > alen - count) {
  1317. return(0);
  1318. }
  1319. sa=create_sockaddr_from_object(sockaddr, &sa_size, error);
  1320. if(*error != 0) {
  1321. return(0);
  1322. }
  1323. LOGDEBUG (g_message("%s: alen: %d", __func__, alen));
  1324. buf=mono_array_addr(buffer, guchar, offset);
  1325. LOGDEBUG (g_message("%s: Sending %d bytes", __func__, count));
  1326. sendflags = convert_socketflags (flags);
  1327. if (sendflags == -1) {
  1328. *error = WSAEOPNOTSUPP;
  1329. return (0);
  1330. }
  1331. ret = _wapi_sendto (sock, buf, count, sendflags, sa, sa_size);
  1332. if(ret==SOCKET_ERROR) {
  1333. *error = WSAGetLastError ();
  1334. }
  1335. g_free(sa);
  1336. return(ret);
  1337. }
  1338. static SOCKET Socket_to_SOCKET(MonoObject *sockobj)
  1339. {
  1340. SOCKET sock;
  1341. MonoClassField *field;
  1342. field=mono_class_get_field_from_name(sockobj->vtable->klass, "socket");
  1343. sock=GPOINTER_TO_INT (*(gpointer *)(((char *)sockobj)+field->offset));
  1344. return(sock);
  1345. }
  1346. #define POLL_ERRORS (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL)
  1347. void ves_icall_System_Net_Sockets_Socket_Select_internal(MonoArray **sockets, gint32 timeout, gint32 *error)
  1348. {
  1349. MonoThread *thread = NULL;
  1350. MonoObject *obj;
  1351. mono_pollfd *pfds;
  1352. int nfds, idx;
  1353. int ret;
  1354. int i, count;
  1355. int mode;
  1356. MonoClass *sock_arr_class;
  1357. MonoArray *socks;
  1358. time_t start;
  1359. mono_array_size_t socks_size;
  1360. MONO_ARCH_SAVE_REGS;
  1361. /* *sockets -> READ, null, WRITE, null, ERROR, null */
  1362. count = mono_array_length (*sockets);
  1363. nfds = count - 3; /* NULL separators */
  1364. pfds = g_new0 (mono_pollfd, nfds);
  1365. mode = idx = 0;
  1366. for (i = 0; i < count; i++) {
  1367. obj = mono_array_get (*sockets, MonoObject *, i);
  1368. if (obj == NULL) {
  1369. mode++;
  1370. continue;
  1371. }
  1372. if (idx >= nfds) {
  1373. /* The socket array was bogus */
  1374. g_free (pfds);
  1375. *error = WSAEFAULT;
  1376. return;
  1377. }
  1378. pfds [idx].fd = Socket_to_SOCKET (obj);
  1379. pfds [idx].events = (mode == 0) ? MONO_POLLIN : (mode == 1) ? MONO_POLLOUT : POLL_ERRORS;
  1380. idx++;
  1381. }
  1382. timeout = (timeout >= 0) ? (timeout / 1000) : -1;
  1383. start = time (NULL);
  1384. do {
  1385. *error = 0;
  1386. ret = mono_poll (pfds, nfds, timeout);
  1387. if (timeout > 0 && ret < 0) {
  1388. int err = errno;
  1389. int sec = time (NULL) - start;
  1390. timeout -= sec * 1000;
  1391. if (timeout < 0)
  1392. timeout = 0;
  1393. errno = err;
  1394. }
  1395. if (ret == -1 && errno == EINTR) {
  1396. int leave = 0;
  1397. if (thread == NULL)
  1398. thread = mono_thread_current ();
  1399. leave = mono_thread_test_state (thread, ThreadState_AbortRequested | ThreadState_StopRequested);
  1400. if (leave != 0) {
  1401. g_free (pfds);
  1402. *sockets = NULL;
  1403. return;
  1404. } else {
  1405. /* Suspend requested? */
  1406. mono_thread_interruption_checkpoint ();
  1407. }
  1408. errno = EINTR;
  1409. }
  1410. } while (ret == -1 && errno == EINTR);
  1411. if (ret == -1) {
  1412. #ifdef PLATFORM_WIN32
  1413. *error = WSAGetLastError ();
  1414. #else
  1415. *error = errno_to_WSA (errno, __func__);
  1416. #endif
  1417. g_free (pfds);
  1418. return;
  1419. }
  1420. if (ret == 0) {
  1421. g_free (pfds);
  1422. *sockets = NULL;
  1423. return;
  1424. }
  1425. sock_arr_class= ((MonoObject *)*sockets)->vtable->klass;
  1426. socks_size = ((mono_array_size_t)ret) + 3; /* space for the NULL delimiters */
  1427. socks = mono_array_new_full (mono_domain_get (), sock_arr_class, &socks_size, NULL);
  1428. mode = idx = 0;
  1429. for (i = 0; i < count && ret > 0; i++) {
  1430. mono_pollfd *pfd;
  1431. obj = mono_array_get (*sockets, MonoObject *, i);
  1432. if (obj == NULL) {
  1433. mode++;
  1434. idx++;
  1435. continue;
  1436. }
  1437. pfd = &pfds [i - mode];
  1438. if (pfd->revents == 0)
  1439. continue;
  1440. ret--;
  1441. if (mode == 0 && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
  1442. mono_array_setref (socks, idx++, obj);
  1443. } else if (mode == 1 && (pfd->revents & (MONO_POLLOUT | POLL_ERRORS)) != 0) {
  1444. mono_array_setref (socks, idx++, obj);
  1445. } else if ((pfd->revents & POLL_ERRORS) != 0) {
  1446. mono_array_setref (socks, idx++, obj);
  1447. }
  1448. }
  1449. *sockets = socks;
  1450. g_free (pfds);
  1451. }
  1452. static MonoObject* int_to_object (MonoDomain *domain, int val)
  1453. {
  1454. return mono_value_box (domain, mono_get_int32_class (), &val);
  1455. }
  1456. void ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal(SOCKET sock, gint32 level, gint32 name, MonoObject **obj_val, gint32 *error)
  1457. {
  1458. int system_level;
  1459. int system_name;
  1460. int ret;
  1461. int val;
  1462. socklen_t valsize=sizeof(val);
  1463. struct linger linger;
  1464. socklen_t lingersize=sizeof(linger);
  1465. int time_ms = 0;
  1466. socklen_t time_ms_size = sizeof (time_ms);
  1467. #ifdef SO_PEERCRED
  1468. struct ucred cred;
  1469. socklen_t credsize = sizeof(cred);
  1470. #endif
  1471. MonoDomain *domain=mono_domain_get();
  1472. MonoObject *obj;
  1473. MonoClass *obj_class;
  1474. MonoClassField *field;
  1475. MONO_ARCH_SAVE_REGS;
  1476. *error = 0;
  1477. ret=convert_sockopt_level_and_name(level, name, &system_level,
  1478. &system_name);
  1479. if(ret==-1) {
  1480. *error = WSAENOPROTOOPT;
  1481. return;
  1482. }
  1483. if (ret == -2) {
  1484. *obj_val = int_to_object (domain, 0);
  1485. return;
  1486. }
  1487. /* No need to deal with MulticastOption names here, because
  1488. * you cant getsockopt AddMembership or DropMembership (the
  1489. * int getsockopt will error, causing an exception)
  1490. */
  1491. switch(name) {
  1492. case SocketOptionName_Linger:
  1493. case SocketOptionName_DontLinger:
  1494. ret = _wapi_getsockopt(sock, system_level, system_name, &linger,
  1495. &lingersize);
  1496. break;
  1497. case SocketOptionName_SendTimeout:
  1498. case SocketOptionName_ReceiveTimeout:
  1499. ret = _wapi_getsockopt (sock, system_level, system_name, (char *) &time_ms, &time_ms_size);
  1500. break;
  1501. #ifdef SO_PEERCRED
  1502. case SocketOptionName_PeerCred:
  1503. ret = _wapi_getsockopt (sock, system_level, system_name, &cred,
  1504. &credsize);
  1505. break;
  1506. #endif
  1507. default:
  1508. ret = _wapi_getsockopt (sock, system_level, system_name, &val,
  1509. &valsize);
  1510. }
  1511. if(ret==SOCKET_ERROR) {
  1512. *error = WSAGetLastError ();
  1513. return;
  1514. }
  1515. switch(name) {
  1516. case SocketOptionName_Linger:
  1517. /* build a System.Net.Sockets.LingerOption */
  1518. obj_class=mono_class_from_name(get_socket_assembly (),
  1519. "System.Net.Sockets",
  1520. "LingerOption");
  1521. obj=mono_object_new(domain, obj_class);
  1522. /* Locate and set the fields "bool enabled" and "int
  1523. * seconds"
  1524. */
  1525. field=mono_class_get_field_from_name(obj_class, "enabled");
  1526. *(guint8 *)(((char *)obj)+field->offset)=linger.l_onoff;
  1527. field=mono_class_get_field_from_name(obj_class, "seconds");
  1528. *(guint32 *)(((char *)obj)+field->offset)=linger.l_linger;
  1529. break;
  1530. case SocketOptionName_DontLinger:
  1531. /* construct a bool int in val - true if linger is off */
  1532. obj = int_to_object (domain, !linger.l_onoff);
  1533. break;
  1534. case SocketOptionName_SendTimeout:
  1535. case SocketOptionName_ReceiveTimeout:
  1536. obj = int_to_object (domain, time_ms);
  1537. break;
  1538. #ifdef SO_PEERCRED
  1539. case SocketOptionName_PeerCred:
  1540. {
  1541. /* build a Mono.Posix.PeerCred+PeerCredData if
  1542. * possible
  1543. */
  1544. static MonoImage *mono_posix_image = NULL;
  1545. MonoPeerCredData *cred_data;
  1546. if (mono_posix_image == NULL) {
  1547. mono_posix_image=mono_image_loaded ("Mono.Posix");
  1548. if (!mono_posix_image) {
  1549. MonoAssembly *sa = mono_assembly_open ("Mono.Posix.dll", NULL);
  1550. if (!sa) {
  1551. *error = WSAENOPROTOOPT;
  1552. return;
  1553. } else {
  1554. mono_posix_image = mono_assembly_get_image (sa);
  1555. }
  1556. }
  1557. }
  1558. obj_class = mono_class_from_name(mono_posix_image,
  1559. "Mono.Posix",
  1560. "PeerCredData");
  1561. obj = mono_object_new(domain, obj_class);
  1562. cred_data = (MonoPeerCredData *)obj;
  1563. cred_data->pid = cred.pid;
  1564. cred_data->uid = cred.uid;
  1565. cred_data->gid = cred.gid;
  1566. break;
  1567. }
  1568. #endif
  1569. default:
  1570. obj = int_to_object (domain, val);
  1571. }
  1572. *obj_val=obj;
  1573. }
  1574. void ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal(SOCKET sock, gint32 level, gint32 name, MonoArray **byte_val, gint32 *error)
  1575. {
  1576. int system_level;
  1577. int system_name;
  1578. int ret;
  1579. guchar *buf;
  1580. socklen_t valsize;
  1581. MONO_ARCH_SAVE_REGS;
  1582. *error = 0;
  1583. ret=convert_sockopt_level_and_name(level, name, &system_level,
  1584. &system_name);
  1585. if(ret==-1) {
  1586. *error = WSAENOPROTOOPT;
  1587. return;
  1588. }
  1589. if(ret==-2)
  1590. return;
  1591. valsize=mono_array_length(*byte_val);
  1592. buf=mono_array_addr(*byte_val, guchar, 0);
  1593. ret = _wapi_getsockopt (sock, system_level, system_name, buf, &valsize);
  1594. if(ret==SOCKET_ERROR) {
  1595. *error = WSAGetLastError ();
  1596. }
  1597. }
  1598. #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
  1599. static struct in_addr ipaddress_to_struct_in_addr(MonoObject *ipaddr)
  1600. {
  1601. struct in_addr inaddr;
  1602. MonoClassField *field;
  1603. field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Address");
  1604. /* No idea why .net uses a 64bit type to hold a 32bit value...
  1605. *
  1606. * Internal value of IPAddess is in little-endian order
  1607. */
  1608. inaddr.s_addr=GUINT_FROM_LE ((guint32)*(guint64 *)(((char *)ipaddr)+field->offset));
  1609. return(inaddr);
  1610. }
  1611. #ifdef AF_INET6
  1612. static struct in6_addr ipaddress_to_struct_in6_addr(MonoObject *ipaddr)
  1613. {
  1614. struct in6_addr in6addr;
  1615. MonoClassField *field;
  1616. MonoArray *data;
  1617. int i;
  1618. field=mono_class_get_field_from_name(ipaddr->vtable->klass, "m_Numbers");
  1619. data=*(MonoArray **)(((char *)ipaddr) + field->offset);
  1620. /* Solaris has only the 8 bit version. */
  1621. #ifndef s6_addr16
  1622. for(i=0; i<8; i++) {
  1623. guint16 s = mono_array_get (data, guint16, i);
  1624. in6addr.s6_addr[2 * i] = (s >> 8) & 0xff;
  1625. in6addr.s6_addr[2 * i + 1] = s & 0xff;
  1626. }
  1627. #else
  1628. for(i=0; i<8; i++)
  1629. in6addr.s6_addr16[i] = mono_array_get (data, guint16, i);
  1630. #endif
  1631. return(in6addr);
  1632. }
  1633. #endif /* AF_INET6 */
  1634. #endif
  1635. void ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal(SOCKET sock, gint32 level, gint32 name, MonoObject *obj_val, MonoArray *byte_val, gint32 int_val, gint32 *error)
  1636. {
  1637. struct linger linger;
  1638. int system_level;
  1639. int system_name;
  1640. int ret;
  1641. #ifdef AF_INET6
  1642. int sol_ip;
  1643. int sol_ipv6;
  1644. *error = 0;
  1645. #ifdef HAVE_SOL_IPV6
  1646. sol_ipv6 = SOL_IPV6;
  1647. #else
  1648. {
  1649. struct protoent *pent;
  1650. pent = getprotobyname ("ipv6");
  1651. sol_ipv6 = (pent != NULL) ? pent->p_proto : 41;
  1652. }
  1653. #endif
  1654. #ifdef HAVE_SOL_IP
  1655. sol_ip = SOL_IP;
  1656. #else
  1657. {
  1658. struct protoent *pent;
  1659. pent = getprotobyname ("ip");
  1660. sol_ip = (pent != NULL) ? pent->p_proto : 0;
  1661. }
  1662. #endif
  1663. #endif /* AF_INET6 */
  1664. MONO_ARCH_SAVE_REGS;
  1665. ret=convert_sockopt_level_and_name(level, name, &system_level,
  1666. &system_name);
  1667. if(ret==-1) {
  1668. *error = WSAENOPROTOOPT;
  1669. return;
  1670. }
  1671. if(ret==-2){
  1672. return;
  1673. }
  1674. /* Only one of obj_val, byte_val or int_val has data */
  1675. if(obj_val!=NULL) {
  1676. MonoClassField *field;
  1677. int valsize;
  1678. switch(name) {
  1679. case SocketOptionName_Linger:
  1680. /* Dig out "bool enabled" and "int seconds"
  1681. * fields
  1682. */
  1683. field=mono_class_get_field_from_name(obj_val->vtable->klass, "enabled");
  1684. linger.l_onoff=*(guint8 *)(((char *)obj_val)+field->offset);
  1685. field=mono_class_get_field_from_name(obj_val->vtable->klass, "seconds");
  1686. linger.l_linger=*(guint32 *)(((char *)obj_val)+field->offset);
  1687. valsize=sizeof(linger);
  1688. ret = _wapi_setsockopt (sock, system_level,
  1689. system_name, &linger, valsize);
  1690. break;
  1691. case SocketOptionName_AddMembership:
  1692. case SocketOptionName_DropMembership:
  1693. #if defined(HAVE_STRUCT_IP_MREQN) || defined(HAVE_STRUCT_IP_MREQ)
  1694. {
  1695. MonoObject *address = NULL;
  1696. #ifdef AF_INET6
  1697. if(system_level == sol_ipv6) {
  1698. struct ipv6_mreq mreq6;
  1699. /*
  1700. * Get group address
  1701. */
  1702. field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
  1703. address = *(gpointer *)(((char *)obj_val) + field->offset);
  1704. if(address) {
  1705. mreq6.ipv6mr_multiaddr = ipaddress_to_struct_in6_addr (address);
  1706. }
  1707. field=mono_class_get_field_from_name(obj_val->vtable->klass, "ifIndex");
  1708. mreq6.ipv6mr_interface =*(guint64 *)(((char *)obj_val)+field->offset);
  1709. ret = _wapi_setsockopt (sock, system_level,
  1710. system_name, &mreq6,
  1711. sizeof (mreq6));
  1712. } else if(system_level == sol_ip)
  1713. #endif /* AF_INET6 */
  1714. {
  1715. #ifdef HAVE_STRUCT_IP_MREQN
  1716. struct ip_mreqn mreq = {{0}};
  1717. #else
  1718. struct ip_mreq mreq = {{0}};
  1719. #endif /* HAVE_STRUCT_IP_MREQN */
  1720. /* pain! MulticastOption holds two IPAddress
  1721. * members, so I have to dig the value out of
  1722. * those :-(
  1723. */
  1724. field = mono_class_get_field_from_name (obj_val->vtable->klass, "group");
  1725. address = *(gpointer *)(((char *)obj_val) + field->offset);
  1726. /* address might not be defined and if so, set the address to ADDR_ANY.
  1727. */
  1728. if(address) {
  1729. mreq.imr_multiaddr = ipaddress_to_struct_in_addr (address);

Large files files are truncated, but you can click here to view the full file