/mono/metadata/socket-io.c

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga · C · 3079 lines · 2390 code · 503 blank · 186 comment · 491 complexity · 8a97aac8e88d2e17c3bee3d14e211f80 MD5 · raw 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);
  1730. }
  1731. field = mono_class_get_field_from_name (obj_val->vtable->klass, "local");
  1732. address = *(gpointer *)(((char *)obj_val) + field->offset);
  1733. #ifdef HAVE_STRUCT_IP_MREQN
  1734. if(address) {
  1735. mreq.imr_address = ipaddress_to_struct_in_addr (address);
  1736. }
  1737. #else
  1738. if(address) {
  1739. mreq.imr_interface = ipaddress_to_struct_in_addr (address);
  1740. }
  1741. #endif /* HAVE_STRUCT_IP_MREQN */
  1742. ret = _wapi_setsockopt (sock, system_level,
  1743. system_name, &mreq,
  1744. sizeof (mreq));
  1745. }
  1746. break;
  1747. }
  1748. #endif /* HAVE_STRUCT_IP_MREQN || HAVE_STRUCT_IP_MREQ */
  1749. default:
  1750. /* Cause an exception to be thrown */
  1751. *error = WSAEINVAL;
  1752. return;
  1753. }
  1754. } else if (byte_val!=NULL) {
  1755. int valsize = mono_array_length (byte_val);
  1756. guchar *buf = mono_array_addr (byte_val, guchar, 0);
  1757. switch(name) {
  1758. case SocketOptionName_DontLinger:
  1759. if (valsize == 1) {
  1760. linger.l_onoff = (*buf) ? 0 : 1;
  1761. linger.l_linger = 0;
  1762. ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
  1763. } else {
  1764. *error = WSAEINVAL;
  1765. }
  1766. break;
  1767. default:
  1768. ret = _wapi_setsockopt (sock, system_level, system_name, buf, valsize);
  1769. break;
  1770. }
  1771. } else {
  1772. /* ReceiveTimeout/SendTimeout get here */
  1773. switch(name) {
  1774. case SocketOptionName_DontLinger:
  1775. linger.l_onoff = !int_val;
  1776. linger.l_linger = 0;
  1777. ret = _wapi_setsockopt (sock, system_level, system_name, &linger, sizeof (linger));
  1778. break;
  1779. case SocketOptionName_DontFragment:
  1780. #ifdef HAVE_IP_MTU_DISCOVER
  1781. /* Fiddle with the value slightly if we're
  1782. * turning DF on
  1783. */
  1784. if (int_val == 1) {
  1785. int_val = IP_PMTUDISC_DO;
  1786. }
  1787. /* Fall through */
  1788. #endif
  1789. default:
  1790. ret = _wapi_setsockopt (sock, system_level, system_name, (char *) &int_val, sizeof (int_val));
  1791. }
  1792. }
  1793. if(ret==SOCKET_ERROR) {
  1794. *error = WSAGetLastError ();
  1795. }
  1796. }
  1797. void ves_icall_System_Net_Sockets_Socket_Shutdown_internal(SOCKET sock,
  1798. gint32 how,
  1799. gint32 *error)
  1800. {
  1801. int ret;
  1802. MONO_ARCH_SAVE_REGS;
  1803. *error = 0;
  1804. /* Currently, the values for how (recv=0, send=1, both=2) match
  1805. * the BSD API
  1806. */
  1807. ret = _wapi_shutdown (sock, how);
  1808. if(ret==SOCKET_ERROR) {
  1809. *error = WSAGetLastError ();
  1810. }
  1811. }
  1812. gint
  1813. ves_icall_System_Net_Sockets_Socket_WSAIoctl (SOCKET sock, gint32 code,
  1814. MonoArray *input,
  1815. MonoArray *output, gint32 *error)
  1816. {
  1817. glong output_bytes = 0;
  1818. gchar *i_buffer, *o_buffer;
  1819. gint i_len, o_len;
  1820. gint ret;
  1821. MONO_ARCH_SAVE_REGS;
  1822. *error = 0;
  1823. if (code == FIONBIO) {
  1824. /* Invalid command. Must use Socket.Blocking */
  1825. return -1;
  1826. }
  1827. if (input == NULL) {
  1828. i_buffer = NULL;
  1829. i_len = 0;
  1830. } else {
  1831. i_buffer = mono_array_addr (input, gchar, 0);
  1832. i_len = mono_array_length (input);
  1833. }
  1834. if (output == NULL) {
  1835. o_buffer = NULL;
  1836. o_len = 0;
  1837. } else {
  1838. o_buffer = mono_array_addr (output, gchar, 0);
  1839. o_len = mono_array_length (output);
  1840. }
  1841. ret = WSAIoctl (sock, code, i_buffer, i_len, o_buffer, o_len, &output_bytes, NULL, NULL);
  1842. if (ret == SOCKET_ERROR) {
  1843. *error = WSAGetLastError ();
  1844. return(-1);
  1845. }
  1846. return (gint) output_bytes;
  1847. }
  1848. #ifdef HAVE_SIOCGIFCONF
  1849. static gboolean
  1850. is_loopback (int family, void *ad)
  1851. {
  1852. char *ptr = (char *) ad;
  1853. if (family == AF_INET) {
  1854. return (ptr [0] == 127);
  1855. }
  1856. #ifdef AF_INET6
  1857. else {
  1858. return (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) ptr));
  1859. }
  1860. #endif
  1861. return FALSE;
  1862. }
  1863. static void *
  1864. get_local_ips (int family, int *nips)
  1865. {
  1866. int addr_size, offset, fd, i, count;
  1867. int max_ifaces = 50; /* 50 interfaces should be enough... */
  1868. struct ifconf ifc;
  1869. struct ifreq *ifr;
  1870. struct ifreq iflags;
  1871. char *result, *tmp_ptr;
  1872. gboolean ignore_loopback = FALSE;
  1873. *nips = 0;
  1874. if (family == AF_INET) {
  1875. addr_size = sizeof (struct in_addr);
  1876. offset = G_STRUCT_OFFSET (struct sockaddr_in, sin_addr);
  1877. #ifdef AF_INET6
  1878. } else if (family == AF_INET6) {
  1879. addr_size = sizeof (struct in6_addr);
  1880. offset = G_STRUCT_OFFSET (struct sockaddr_in6, sin6_addr);
  1881. #endif
  1882. } else {
  1883. return NULL;
  1884. }
  1885. fd = socket (family, SOCK_STREAM, 0);
  1886. ifc.ifc_len = max_ifaces * sizeof (struct ifreq);
  1887. ifc.ifc_buf = g_malloc (ifc.ifc_len);
  1888. if (ioctl (fd, SIOCGIFCONF, &ifc) < 0) {
  1889. close (fd);
  1890. g_free (ifc.ifc_buf);
  1891. return NULL;
  1892. }
  1893. count = ifc.ifc_len / sizeof (struct ifreq);
  1894. *nips = count;
  1895. if (count == 0) {
  1896. g_free (ifc.ifc_buf);
  1897. close (fd);
  1898. return NULL;
  1899. }
  1900. for (i = 0, ifr = ifc.ifc_req; i < *nips; i++, ifr++) {
  1901. strcpy (iflags.ifr_name, ifr->ifr_name);
  1902. if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
  1903. continue;
  1904. }
  1905. if ((iflags.ifr_flags & IFF_UP) == 0) {
  1906. ifr->ifr_name [0] = '\0';
  1907. continue;
  1908. }
  1909. if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
  1910. ignore_loopback = TRUE;
  1911. }
  1912. }
  1913. close (fd);
  1914. result = g_malloc (addr_size * count);
  1915. tmp_ptr = result;
  1916. for (i = 0, ifr = ifc.ifc_req; i < count; i++, ifr++) {
  1917. if (ifr->ifr_name [0] == '\0') {
  1918. (*nips)--;
  1919. continue;
  1920. }
  1921. if (ignore_loopback && is_loopback (family, ((char *) &ifr->ifr_addr) + offset)) {
  1922. (*nips)--;
  1923. continue;
  1924. }
  1925. memcpy (tmp_ptr, ((char *) &ifr->ifr_addr) + offset, addr_size);
  1926. tmp_ptr += addr_size;
  1927. }
  1928. g_free (ifc.ifc_buf);
  1929. return result;
  1930. }
  1931. #else
  1932. static void *
  1933. get_local_ips (int family, int *nips)
  1934. {
  1935. *nips = 0;
  1936. return NULL;
  1937. }
  1938. #endif /* HAVE_SIOCGIFCONF */
  1939. #ifndef AF_INET6
  1940. static gboolean hostent_to_IPHostEntry(struct hostent *he, MonoString **h_name,
  1941. MonoArray **h_aliases,
  1942. MonoArray **h_addr_list,
  1943. gboolean add_local_ips)
  1944. {
  1945. MonoDomain *domain = mono_domain_get ();
  1946. int i = 0;
  1947. struct in_addr *local_in = NULL;
  1948. int nlocal_in = 0;
  1949. if (he != NULL) {
  1950. if(he->h_length!=4 || he->h_addrtype!=AF_INET) {
  1951. return(FALSE);
  1952. }
  1953. *h_name=mono_string_new(domain, he->h_name);
  1954. while(he->h_aliases[i]!=NULL) {
  1955. i++;
  1956. }
  1957. *h_aliases=mono_array_new(domain, mono_get_string_class (), i);
  1958. i=0;
  1959. while(he->h_aliases[i]!=NULL) {
  1960. MonoString *alias;
  1961. alias=mono_string_new(domain, he->h_aliases[i]);
  1962. mono_array_setref (*h_aliases, i, alias);
  1963. i++;
  1964. }
  1965. } else if (!add_local_ips) {
  1966. return FALSE;
  1967. }
  1968. if (add_local_ips) {
  1969. local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
  1970. if (nlocal_in) {
  1971. *h_addr_list = mono_array_new(domain, mono_get_string_class (), nlocal_in);
  1972. for (i = 0; i < nlocal_in; i++) {
  1973. MonoString *addr_string;
  1974. char addr [16], *ptr;
  1975. ptr = (char *) &local_in [i];
  1976. g_snprintf(addr, 16, "%u.%u.%u.%u",
  1977. (unsigned char) ptr [0],
  1978. (unsigned char) ptr [1],
  1979. (unsigned char) ptr [2],
  1980. (unsigned char) ptr [3]);
  1981. addr_string = mono_string_new (domain, addr);
  1982. mono_array_setref (*h_addr_list, i, addr_string);
  1983. i++;
  1984. }
  1985. g_free (local_in);
  1986. } else if (he == NULL) {
  1987. /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
  1988. *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
  1989. mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
  1990. return TRUE;
  1991. }
  1992. }
  1993. if (nlocal_in == 0 && he != NULL) {
  1994. i = 0;
  1995. while (he->h_addr_list[i]!=NULL) {
  1996. i++;
  1997. }
  1998. *h_addr_list=mono_array_new(domain, mono_get_string_class (), i);
  1999. i=0;
  2000. while(he->h_addr_list[i]!=NULL) {
  2001. MonoString *addr_string;
  2002. char addr[16];
  2003. g_snprintf(addr, 16, "%u.%u.%u.%u",
  2004. (unsigned char)he->h_addr_list[i][0],
  2005. (unsigned char)he->h_addr_list[i][1],
  2006. (unsigned char)he->h_addr_list[i][2],
  2007. (unsigned char)he->h_addr_list[i][3]);
  2008. addr_string=mono_string_new(domain, addr);
  2009. mono_array_setref (*h_addr_list, i, addr_string);
  2010. i++;
  2011. }
  2012. }
  2013. return(TRUE);
  2014. }
  2015. static gboolean ipaddr_to_IPHostEntry(const char *addr, MonoString **h_name,
  2016. MonoArray **h_aliases,
  2017. MonoArray **h_addr_list)
  2018. {
  2019. MonoDomain *domain = mono_domain_get ();
  2020. *h_name=mono_string_new(domain, addr);
  2021. *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
  2022. *h_addr_list=mono_array_new(domain, mono_get_string_class (), 1);
  2023. mono_array_setref (*h_addr_list, 0, *h_name);
  2024. return(TRUE);
  2025. }
  2026. #endif
  2027. #if defined(AF_INET6) && defined(HAVE_GETHOSTBYNAME2_R)
  2028. static gboolean hostent_to_IPHostEntry2(struct hostent *he1,struct hostent *he2, MonoString **h_name,
  2029. MonoArray **h_aliases, MonoArray **h_addr_list, gboolean add_local_ips)
  2030. {
  2031. MonoDomain *domain = mono_domain_get ();
  2032. int i, host_count, host_index, family_hint;
  2033. struct in_addr *local_in = NULL;
  2034. int nlocal_in = 0;
  2035. struct in6_addr *local_in6 = NULL;
  2036. int nlocal_in6 = 0;
  2037. gboolean from_local = FALSE;
  2038. family_hint = get_family_hint ();
  2039. /*
  2040. * Check if address length and family are correct
  2041. */
  2042. if (he1 != NULL && (he1->h_length!=4 || he1->h_addrtype!=AF_INET)) {
  2043. return(FALSE);
  2044. }
  2045. if (he2 != NULL && (he2->h_length!=16 || he2->h_addrtype!=AF_INET6)) {
  2046. return(FALSE);
  2047. }
  2048. /*
  2049. * Get the aliases and host name from he1 or he2 whichever is
  2050. * not null, if he1 is not null then take aliases from he1
  2051. */
  2052. if (he1 != NULL && (family_hint == PF_UNSPEC ||
  2053. family_hint == PF_INET)) {
  2054. *h_name=mono_string_new (domain, he1->h_name);
  2055. i=0;
  2056. while(he1->h_aliases[i]!=NULL) {
  2057. i++;
  2058. }
  2059. *h_aliases=mono_array_new (domain, mono_get_string_class (),
  2060. i);
  2061. i=0;
  2062. while(he1->h_aliases[i]!=NULL) {
  2063. MonoString *alias;
  2064. alias=mono_string_new (domain, he1->h_aliases[i]);
  2065. mono_array_setref (*h_aliases, i, alias);
  2066. i++;
  2067. }
  2068. } else if (he2 != NULL && (family_hint == PF_UNSPEC ||
  2069. family_hint == PF_INET6)) {
  2070. *h_name=mono_string_new (domain, he2->h_name);
  2071. i=0;
  2072. while(he2->h_aliases [i] != NULL) {
  2073. i++;
  2074. }
  2075. *h_aliases=mono_array_new (domain, mono_get_string_class (),
  2076. i);
  2077. i=0;
  2078. while(he2->h_aliases[i]!=NULL) {
  2079. MonoString *alias;
  2080. alias=mono_string_new (domain, he2->h_aliases[i]);
  2081. mono_array_setref (*h_aliases, i, alias);
  2082. i++;
  2083. }
  2084. } else if (!add_local_ips) {
  2085. return(FALSE);
  2086. }
  2087. /*
  2088. * Count the number of addresses in he1 + he2
  2089. */
  2090. host_count = 0;
  2091. if (he1 != NULL && (family_hint == PF_UNSPEC ||
  2092. family_hint == PF_INET)) {
  2093. i=0;
  2094. while(he1->h_addr_list[i]!=NULL) {
  2095. i++;
  2096. host_count++;
  2097. }
  2098. }
  2099. if (he2 != NULL && (family_hint == PF_UNSPEC ||
  2100. family_hint == PF_INET6)) {
  2101. i=0;
  2102. while(he2->h_addr_list[i]!=NULL) {
  2103. i++;
  2104. host_count++;
  2105. }
  2106. }
  2107. /*
  2108. * Fills the array
  2109. */
  2110. host_index = 0;
  2111. if (add_local_ips) {
  2112. if (family_hint == PF_UNSPEC || family_hint == PF_INET)
  2113. local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
  2114. if (family_hint == PF_UNSPEC || family_hint == PF_INET6)
  2115. local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
  2116. if (nlocal_in || nlocal_in6) {
  2117. from_local = TRUE;
  2118. *h_addr_list = mono_array_new (domain, mono_get_string_class (),
  2119. nlocal_in + nlocal_in6);
  2120. if (nlocal_in6) {
  2121. int n;
  2122. for (n = 0; n < nlocal_in6; n++) {
  2123. MonoString *addr_string;
  2124. const char *ret;
  2125. char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes with the trailing NULL */
  2126. ret = inet_ntop (AF_INET6, &local_in6 [n], addr, sizeof(addr));
  2127. if (ret != NULL) {
  2128. addr_string = mono_string_new (domain, addr);
  2129. mono_array_setref (*h_addr_list, host_index, addr_string);
  2130. host_index++;
  2131. }
  2132. }
  2133. }
  2134. if (nlocal_in) {
  2135. int n;
  2136. for (n = 0; n < nlocal_in; n++) {
  2137. MonoString *addr_string;
  2138. const char *ret;
  2139. char addr[16]; /* INET_ADDRSTRLEN == 16 */
  2140. ret = inet_ntop (AF_INET, &local_in [n], addr, sizeof(addr));
  2141. if (ret != NULL) {
  2142. addr_string = mono_string_new (domain, addr);
  2143. mono_array_setref (*h_addr_list, host_index, addr_string);
  2144. host_index++;
  2145. }
  2146. }
  2147. }
  2148. g_free (local_in);
  2149. g_free (local_in6);
  2150. return TRUE;
  2151. } else if (he1 == NULL && he2 == NULL) {
  2152. /* If requesting "" and there are no other interfaces up, MS returns 127.0.0.1 */
  2153. *h_addr_list = mono_array_new(domain, mono_get_string_class (), 1);
  2154. mono_array_setref (*h_addr_list, 0, mono_string_new (domain, "127.0.0.1"));
  2155. g_free (local_in);
  2156. g_free (local_in6);
  2157. return TRUE;
  2158. }
  2159. g_free (local_in);
  2160. g_free (local_in6);
  2161. }
  2162. *h_addr_list=mono_array_new (domain, mono_get_string_class (), host_count);
  2163. if (he2 != NULL && (family_hint == PF_UNSPEC ||
  2164. family_hint == PF_INET6)) {
  2165. i = 0;
  2166. while(he2->h_addr_list[i] != NULL) {
  2167. MonoString *addr_string;
  2168. const char *ret;
  2169. char addr[48]; /* INET6_ADDRSTRLEN == 46, but IPv6 addresses can be 48 bytes long with the trailing NULL */
  2170. ret = inet_ntop (AF_INET6, he2->h_addr_list[i], addr,
  2171. sizeof(addr));
  2172. if (ret != NULL) {
  2173. addr_string = mono_string_new (domain, addr);
  2174. mono_array_setref (*h_addr_list, host_index, addr_string);
  2175. i++;
  2176. host_index++;
  2177. }
  2178. }
  2179. }
  2180. if (he1 != NULL && (family_hint == PF_UNSPEC ||
  2181. family_hint == PF_INET)) {
  2182. i=0;
  2183. while(he1->h_addr_list[i] != NULL) {
  2184. MonoString *addr_string;
  2185. const char *ret;
  2186. char addr[16]; /* INET_ADDRSTRLEN == 16 */
  2187. ret = inet_ntop (AF_INET, he1->h_addr_list[i], addr,
  2188. sizeof(addr));
  2189. if (ret != NULL) {
  2190. addr_string=mono_string_new (domain, addr);
  2191. mono_array_setref (*h_addr_list, host_index, addr_string);
  2192. i++;
  2193. host_index++;
  2194. }
  2195. }
  2196. }
  2197. return(TRUE);
  2198. }
  2199. #endif
  2200. #if defined(AF_INET6)
  2201. static gboolean
  2202. addrinfo_to_IPHostEntry(struct addrinfo *info, MonoString **h_name,
  2203. MonoArray **h_aliases,
  2204. MonoArray **h_addr_list,
  2205. gboolean add_local_ips)
  2206. {
  2207. gint32 count, i;
  2208. struct addrinfo *ai = NULL;
  2209. struct in_addr *local_in = NULL;
  2210. int nlocal_in = 0;
  2211. struct in6_addr *local_in6 = NULL;
  2212. int nlocal_in6 = 0;
  2213. int addr_index;
  2214. MonoDomain *domain = mono_domain_get ();
  2215. addr_index = 0;
  2216. *h_aliases=mono_array_new(domain, mono_get_string_class (), 0);
  2217. if (add_local_ips) {
  2218. local_in = (struct in_addr *) get_local_ips (AF_INET, &nlocal_in);
  2219. local_in6 = (struct in6_addr *) get_local_ips (AF_INET6, &nlocal_in6);
  2220. if (nlocal_in || nlocal_in6) {
  2221. *h_addr_list=mono_array_new(domain, mono_get_string_class (), nlocal_in + nlocal_in6);
  2222. if (nlocal_in) {
  2223. MonoString *addr_string;
  2224. char addr [16];
  2225. int i;
  2226. for (i = 0; i < nlocal_in; i++) {
  2227. inet_ntop (AF_INET, &local_in [i], addr, sizeof (addr));
  2228. addr_string = mono_string_new (domain, addr);
  2229. mono_array_setref (*h_addr_list, addr_index, addr_string);
  2230. addr_index++;
  2231. }
  2232. }
  2233. if (nlocal_in6) {
  2234. MonoString *addr_string;
  2235. const char *ret;
  2236. char addr [48];
  2237. int i;
  2238. for (i = 0; i < nlocal_in6; i++) {
  2239. ret = inet_ntop (AF_INET6, &local_in6 [i], addr, sizeof (addr));
  2240. if (ret != NULL) {
  2241. addr_string = mono_string_new (domain, addr);
  2242. mono_array_setref (*h_addr_list, addr_index, addr_string);
  2243. addr_index++;
  2244. }
  2245. }
  2246. }
  2247. g_free (local_in);
  2248. g_free (local_in6);
  2249. if (info) {
  2250. freeaddrinfo (info);
  2251. }
  2252. return TRUE;
  2253. }
  2254. g_free (local_in);
  2255. g_free (local_in6);
  2256. }
  2257. for (count=0, ai=info; ai!=NULL; ai=ai->ai_next) {
  2258. if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
  2259. continue;
  2260. count++;
  2261. }
  2262. *h_addr_list=mono_array_new(domain, mono_get_string_class (), count);
  2263. for (ai=info, i=0; ai!=NULL; ai=ai->ai_next) {
  2264. MonoString *addr_string;
  2265. const char *ret;
  2266. char buffer [48]; /* Max. size for IPv6 */
  2267. if((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6)) {
  2268. continue;
  2269. }
  2270. if(ai->ai_family == PF_INET) {
  2271. ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
  2272. } else {
  2273. ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 48);
  2274. }
  2275. if(ret) {
  2276. addr_string=mono_string_new(domain, buffer);
  2277. } else {
  2278. addr_string=mono_string_new(domain, "");
  2279. }
  2280. mono_array_setref (*h_addr_list, addr_index, addr_string);
  2281. if(!i) {
  2282. if (ai->ai_canonname != NULL) {
  2283. *h_name=mono_string_new(domain, ai->ai_canonname);
  2284. } else {
  2285. *h_name=mono_string_new(domain, buffer);
  2286. }
  2287. }
  2288. addr_index++;
  2289. }
  2290. if(info) {
  2291. freeaddrinfo(info);
  2292. }
  2293. return(TRUE);
  2294. }
  2295. #endif
  2296. #ifdef AF_INET6
  2297. MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
  2298. {
  2299. gboolean add_local_ips = FALSE;
  2300. #ifdef HAVE_SIOCGIFCONF
  2301. gchar this_hostname [256];
  2302. #endif
  2303. #if !defined(HAVE_GETHOSTBYNAME2_R)
  2304. struct addrinfo *info = NULL, hints;
  2305. char *hostname;
  2306. MONO_ARCH_SAVE_REGS;
  2307. hostname=mono_string_to_utf8 (host);
  2308. if (*hostname == '\0')
  2309. add_local_ips = TRUE;
  2310. #ifdef HAVE_SIOCGIFCONF
  2311. if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
  2312. if (!strcmp (hostname, this_hostname))
  2313. add_local_ips = TRUE;
  2314. }
  2315. #endif
  2316. memset(&hints, 0, sizeof(hints));
  2317. hints.ai_family = get_family_hint ();
  2318. hints.ai_socktype = SOCK_STREAM;
  2319. hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
  2320. if (*hostname && getaddrinfo(hostname, NULL, &hints, &info) == -1) {
  2321. return(FALSE);
  2322. }
  2323. g_free(hostname);
  2324. return(addrinfo_to_IPHostEntry(info, h_name, h_aliases, h_addr_list, add_local_ips));
  2325. #else
  2326. struct hostent he1,*hp1, he2, *hp2;
  2327. int buffer_size1, buffer_size2;
  2328. char *buffer1, *buffer2;
  2329. int herr;
  2330. gboolean return_value;
  2331. char *hostname;
  2332. MONO_ARCH_SAVE_REGS;
  2333. hostname=mono_string_to_utf8 (host);
  2334. if (*hostname == '\0')
  2335. add_local_ips = TRUE;
  2336. #ifdef HAVE_SIOCGIFCONF
  2337. if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
  2338. if (!strcmp (hostname, this_hostname))
  2339. add_local_ips = TRUE;
  2340. }
  2341. #endif
  2342. buffer_size1 = 512;
  2343. buffer_size2 = 512;
  2344. buffer1 = g_malloc0(buffer_size1);
  2345. buffer2 = g_malloc0(buffer_size2);
  2346. hp1 = NULL;
  2347. hp2 = NULL;
  2348. while (*hostname && gethostbyname2_r(hostname, AF_INET, &he1, buffer1, buffer_size1,
  2349. &hp1, &herr) == ERANGE) {
  2350. buffer_size1 *= 2;
  2351. buffer1 = g_realloc(buffer1, buffer_size1);
  2352. }
  2353. if (*hostname && hp1 == NULL)
  2354. {
  2355. while (gethostbyname2_r(hostname, AF_INET6, &he2, buffer2,
  2356. buffer_size2, &hp2, &herr) == ERANGE) {
  2357. buffer_size2 *= 2;
  2358. buffer2 = g_realloc(buffer2, buffer_size2);
  2359. }
  2360. }
  2361. return_value = hostent_to_IPHostEntry2(hp1, hp2, h_name, h_aliases,
  2362. h_addr_list, add_local_ips);
  2363. g_free(buffer1);
  2364. g_free(buffer2);
  2365. g_free(hostname);
  2366. return(return_value);
  2367. #endif /* HAVE_GETHOSTBYNAME2_R */
  2368. }
  2369. #else /* AF_INET6 */
  2370. MonoBoolean ves_icall_System_Net_Dns_GetHostByName_internal(MonoString *host, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
  2371. {
  2372. struct hostent *he;
  2373. char *hostname;
  2374. gboolean add_local_ips = FALSE;
  2375. #ifdef HAVE_SIOCGIFCONF
  2376. gchar this_hostname [256];
  2377. #endif
  2378. MONO_ARCH_SAVE_REGS;
  2379. hostname=mono_string_to_utf8(host);
  2380. if (*hostname == '\0')
  2381. add_local_ips = TRUE;
  2382. #ifdef HAVE_SIOCGIFCONF
  2383. if (!add_local_ips && gethostname (this_hostname, sizeof (this_hostname)) != -1) {
  2384. if (!strcmp (hostname, this_hostname))
  2385. add_local_ips = TRUE;
  2386. }
  2387. #endif
  2388. #ifndef HOST_WIN32
  2389. he = NULL;
  2390. if (*hostname)
  2391. he = _wapi_gethostbyname (hostname);
  2392. #else
  2393. he = _wapi_gethostbyname (hostname);
  2394. #endif
  2395. g_free(hostname);
  2396. if (*hostname && he==NULL)
  2397. return(FALSE);
  2398. return(hostent_to_IPHostEntry(he, h_name, h_aliases, h_addr_list, add_local_ips));
  2399. }
  2400. #endif /* AF_INET6 */
  2401. #ifndef HAVE_INET_PTON
  2402. static int
  2403. inet_pton (int family, const char *address, void *inaddrp)
  2404. {
  2405. if (family == AF_INET) {
  2406. #ifdef HAVE_INET_ATON
  2407. struct in_addr inaddr;
  2408. if (!inet_aton (address, &inaddr))
  2409. return 0;
  2410. memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
  2411. return 1;
  2412. #else
  2413. /* assume the system has inet_addr(), if it doesn't
  2414. have that we're pretty much screwed... */
  2415. guint32 inaddr;
  2416. if (!strcmp (address, "255.255.255.255")) {
  2417. /* special-case hack */
  2418. inaddr = 0xffffffff;
  2419. } else {
  2420. inaddr = inet_addr (address);
  2421. #ifndef INADDR_NONE
  2422. #define INADDR_NONE ((in_addr_t) -1)
  2423. #endif
  2424. if (inaddr == INADDR_NONE)
  2425. return 0;
  2426. }
  2427. memcpy (inaddrp, &inaddr, sizeof (guint32));
  2428. return 1;
  2429. #endif /* HAVE_INET_ATON */
  2430. }
  2431. return -1;
  2432. }
  2433. #endif /* !HAVE_INET_PTON */
  2434. extern MonoBoolean ves_icall_System_Net_Dns_GetHostByAddr_internal(MonoString *addr, MonoString **h_name, MonoArray **h_aliases, MonoArray **h_addr_list)
  2435. {
  2436. char *address;
  2437. gboolean v1;
  2438. #ifdef AF_INET6
  2439. struct sockaddr_in saddr;
  2440. struct sockaddr_in6 saddr6;
  2441. struct addrinfo *info = NULL, hints;
  2442. gint32 family;
  2443. char hostname[1024] = {0};
  2444. int flags = 0;
  2445. #else
  2446. struct in_addr inaddr;
  2447. struct hostent *he;
  2448. gboolean ret;
  2449. #endif
  2450. v1 = mono_framework_version () == 1;
  2451. address = mono_string_to_utf8 (addr);
  2452. #ifdef AF_INET6
  2453. if (inet_pton (AF_INET, address, &saddr.sin_addr ) <= 0) {
  2454. /* Maybe an ipv6 address */
  2455. if (inet_pton (AF_INET6, address, &saddr6.sin6_addr) <= 0) {
  2456. g_free (address);
  2457. return FALSE;
  2458. }
  2459. else {
  2460. family = AF_INET6;
  2461. saddr6.sin6_family = AF_INET6;
  2462. }
  2463. }
  2464. else {
  2465. family = AF_INET;
  2466. saddr.sin_family = AF_INET;
  2467. }
  2468. g_free(address);
  2469. if (v1) {
  2470. flags = NI_NAMEREQD;
  2471. }
  2472. if(family == AF_INET) {
  2473. #if HAVE_SOCKADDR_IN_SIN_LEN
  2474. saddr.sin_len = sizeof (saddr);
  2475. #endif
  2476. if(getnameinfo ((struct sockaddr*)&saddr, sizeof(saddr),
  2477. hostname, sizeof(hostname), NULL, 0,
  2478. flags) != 0) {
  2479. return(FALSE);
  2480. }
  2481. } else if(family == AF_INET6) {
  2482. #if HAVE_SOCKADDR_IN6_SIN_LEN
  2483. saddr6.sin6_len = sizeof (saddr6);
  2484. #endif
  2485. if(getnameinfo ((struct sockaddr*)&saddr6, sizeof(saddr6),
  2486. hostname, sizeof(hostname), NULL, 0,
  2487. flags) != 0) {
  2488. return(FALSE);
  2489. }
  2490. }
  2491. memset (&hints, 0, sizeof(hints));
  2492. hints.ai_family = get_family_hint ();
  2493. hints.ai_socktype = SOCK_STREAM;
  2494. hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
  2495. if( getaddrinfo (hostname, NULL, &hints, &info) == -1 ) {
  2496. return(FALSE);
  2497. }
  2498. return(addrinfo_to_IPHostEntry (info, h_name, h_aliases, h_addr_list, FALSE));
  2499. #else
  2500. if (inet_pton (AF_INET, address, &inaddr) <= 0) {
  2501. g_free (address);
  2502. return(FALSE);
  2503. }
  2504. if ((he = gethostbyaddr ((char *) &inaddr, sizeof (inaddr), AF_INET)) == NULL) {
  2505. if (v1) {
  2506. ret = FALSE;
  2507. } else {
  2508. ret = ipaddr_to_IPHostEntry (address, h_name,
  2509. h_aliases, h_addr_list);
  2510. }
  2511. } else {
  2512. ret = hostent_to_IPHostEntry (he, h_name, h_aliases,
  2513. h_addr_list, FALSE);
  2514. }
  2515. g_free (address);
  2516. return(ret);
  2517. #endif
  2518. }
  2519. extern MonoBoolean ves_icall_System_Net_Dns_GetHostName_internal(MonoString **h_name)
  2520. {
  2521. gchar hostname[256];
  2522. int ret;
  2523. MONO_ARCH_SAVE_REGS;
  2524. ret = gethostname (hostname, sizeof (hostname));
  2525. if(ret==-1) {
  2526. return(FALSE);
  2527. }
  2528. *h_name=mono_string_new(mono_domain_get (), hostname);
  2529. return(TRUE);
  2530. }
  2531. gboolean
  2532. ves_icall_System_Net_Sockets_Socket_SendFile (SOCKET sock, MonoString *filename, MonoArray *pre_buffer, MonoArray *post_buffer, gint flags)
  2533. {
  2534. HANDLE file;
  2535. gint32 error;
  2536. TRANSMIT_FILE_BUFFERS buffers;
  2537. MONO_ARCH_SAVE_REGS;
  2538. if (filename == NULL)
  2539. return FALSE;
  2540. file = ves_icall_System_IO_MonoIO_Open (filename, FileMode_Open, FileAccess_Read, FileShare_Read, 0, &error);
  2541. if (file == INVALID_HANDLE_VALUE) {
  2542. SetLastError (error);
  2543. return FALSE;
  2544. }
  2545. memset (&buffers, 0, sizeof (buffers));
  2546. if (pre_buffer != NULL) {
  2547. buffers.Head = mono_array_addr (pre_buffer, guchar, 0);
  2548. buffers.HeadLength = mono_array_length (pre_buffer);
  2549. }
  2550. if (post_buffer != NULL) {
  2551. buffers.Tail = mono_array_addr (post_buffer, guchar, 0);
  2552. buffers.TailLength = mono_array_length (post_buffer);
  2553. }
  2554. if (!TransmitFile (sock, file, 0, 0, NULL, &buffers, flags)) {
  2555. CloseHandle (file);
  2556. return FALSE;
  2557. }
  2558. CloseHandle (file);
  2559. return TRUE;
  2560. }
  2561. void mono_network_init(void)
  2562. {
  2563. WSADATA wsadata;
  2564. int err;
  2565. err=WSAStartup(MAKEWORD(2,0), &wsadata);
  2566. if(err!=0) {
  2567. g_error("%s: Couldn't initialise networking", __func__);
  2568. exit(-1);
  2569. }
  2570. LOGDEBUG (g_message("%s: Using socket library: %s", __func__, wsadata.szDescription));
  2571. LOGDEBUG (g_message("%s: Socket system status: %s", __func__, wsadata.szSystemStatus));
  2572. }
  2573. void mono_network_cleanup(void)
  2574. {
  2575. WSACleanup();
  2576. }