|
| 1 | +#include <hxcpp.h> |
| 2 | +#include "NativeSocketAddress.h" |
| 3 | + |
| 4 | +#include <string.h> |
| 5 | + |
| 6 | +#if defined(HX_WINDOWS) || defined(NEKO_WINDOWS) |
| 7 | +#include <winsock2.h> |
| 8 | +#include <Ws2tcpip.h> |
| 9 | +typedef int SocketLen; |
| 10 | +#else |
| 11 | +#include <arpa/inet.h> |
| 12 | +#include <netdb.h> |
| 13 | +#include <netinet/in.h> |
| 14 | +#include <sys/socket.h> |
| 15 | +#include <sys/types.h> |
| 16 | +#include <unistd.h> |
| 17 | +typedef int SOCKET; |
| 18 | +#define INVALID_SOCKET (-1) |
| 19 | +#define SOCKET_ERROR (-1) |
| 20 | +typedef socklen_t SocketLen; |
| 21 | +#endif |
| 22 | + |
| 23 | +namespace { |
| 24 | + |
| 25 | +static int stdSocketType = 0; |
| 26 | + |
| 27 | +static int crossbyte_socket_type() { |
| 28 | + if (stdSocketType == 0) { |
| 29 | + Dynamic probe = _hx_std_socket_new(false, false); |
| 30 | + stdSocketType = probe->__GetType(); |
| 31 | + _hx_std_socket_close(probe); |
| 32 | + } |
| 33 | + return stdSocketType; |
| 34 | +} |
| 35 | + |
| 36 | +struct SocketWrapper : public hx::Object { |
| 37 | + HX_IS_INSTANCE_OF enum { _hx_ClassId = hx::clsIdSocket }; |
| 38 | + |
| 39 | + SOCKET socket; |
| 40 | + |
| 41 | + int __GetType() const { |
| 42 | + return crossbyte_socket_type(); |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +static SOCKET crossbyte_val_sock(Dynamic inValue) { |
| 47 | + if (inValue.mPtr == 0) { |
| 48 | + hx::Throw(HX_CSTRING("Invalid socket handle")); |
| 49 | + return INVALID_SOCKET; |
| 50 | + } |
| 51 | + |
| 52 | + if (inValue->__GetType() == vtClass) { |
| 53 | + inValue = inValue->__Field(HX_CSTRING("__s"), hx::paccNever); |
| 54 | + if (inValue.mPtr == 0) { |
| 55 | + hx::Throw(HX_CSTRING("Invalid socket handle")); |
| 56 | + return INVALID_SOCKET; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return reinterpret_cast<SocketWrapper*>(inValue.mPtr)->socket; |
| 61 | +} |
| 62 | + |
| 63 | +static Array<int> crossbyte_address_to_array(const sockaddr* address, SocketLen length) { |
| 64 | + if (address == 0) { |
| 65 | + return null(); |
| 66 | + } |
| 67 | + |
| 68 | + if (address->sa_family == AF_INET && length >= (SocketLen)sizeof(sockaddr_in)) { |
| 69 | + const sockaddr_in* ipv4 = reinterpret_cast<const sockaddr_in*>(address); |
| 70 | + Array<int> result = Array_obj<int>::__new(2, 2); |
| 71 | + result[0] = *(const int*)&ipv4->sin_addr; |
| 72 | + result[1] = ntohs(ipv4->sin_port); |
| 73 | + return result; |
| 74 | + } |
| 75 | + |
| 76 | + if (address->sa_family == AF_INET6 && length >= (SocketLen)sizeof(sockaddr_in6)) { |
| 77 | + const sockaddr_in6* ipv6 = reinterpret_cast<const sockaddr_in6*>(address); |
| 78 | + const unsigned char* bytes = reinterpret_cast<const unsigned char*>(&ipv6->sin6_addr); |
| 79 | + Array<int> result = Array_obj<int>::__new(18, 18); |
| 80 | + result[0] = 0; |
| 81 | + result[1] = ntohs(ipv6->sin6_port); |
| 82 | + for (int i = 0; i < 16; ++i) { |
| 83 | + result[i + 2] = bytes[i]; |
| 84 | + } |
| 85 | + return result; |
| 86 | + } |
| 87 | + |
| 88 | + return null(); |
| 89 | +} |
| 90 | + |
| 91 | +static Array<int> crossbyte_socket_name_info(Dynamic socket, bool peer) { |
| 92 | + SOCKET nativeSocket = crossbyte_val_sock(socket); |
| 93 | + sockaddr_storage address; |
| 94 | + memset(&address, 0, sizeof(address)); |
| 95 | + SocketLen addressLength = sizeof(address); |
| 96 | + |
| 97 | + hx::EnterGCFreeZone(); |
| 98 | + int status = peer |
| 99 | + ? getpeername(nativeSocket, reinterpret_cast<sockaddr*>(&address), &addressLength) |
| 100 | + : getsockname(nativeSocket, reinterpret_cast<sockaddr*>(&address), &addressLength); |
| 101 | + hx::ExitGCFreeZone(); |
| 102 | + |
| 103 | + if (status == SOCKET_ERROR) { |
| 104 | + return null(); |
| 105 | + } |
| 106 | + |
| 107 | + return crossbyte_address_to_array(reinterpret_cast<sockaddr*>(&address), addressLength); |
| 108 | +} |
| 109 | + |
| 110 | +static void crossbyte_block_error() { |
| 111 | +#if defined(HX_WINDOWS) || defined(NEKO_WINDOWS) |
| 112 | + int error = WSAGetLastError(); |
| 113 | + hx::ExitGCFreeZone(); |
| 114 | + if (error == WSAEWOULDBLOCK || error == WSAEALREADY) { |
| 115 | + hx::Throw(HX_CSTRING("Blocking")); |
| 116 | + } |
| 117 | +#else |
| 118 | + hx::ExitGCFreeZone(); |
| 119 | + if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINPROGRESS || errno == EALREADY) { |
| 120 | + hx::Throw(HX_CSTRING("Blocking")); |
| 121 | + } |
| 122 | +#endif |
| 123 | + hx::Throw(HX_CSTRING("Socket operation failed")); |
| 124 | +} |
| 125 | + |
| 126 | +} // namespace |
| 127 | + |
| 128 | +Dynamic crossbyte_socket_accept(Dynamic socket) { |
| 129 | + SOCKET nativeSocket = crossbyte_val_sock(socket); |
| 130 | + sockaddr_storage address; |
| 131 | + memset(&address, 0, sizeof(address)); |
| 132 | + SocketLen addressLength = sizeof(address); |
| 133 | + |
| 134 | + hx::EnterGCFreeZone(); |
| 135 | + SOCKET accepted = accept(nativeSocket, reinterpret_cast<sockaddr*>(&address), &addressLength); |
| 136 | + if (accepted == INVALID_SOCKET) { |
| 137 | + crossbyte_block_error(); |
| 138 | + } |
| 139 | + hx::ExitGCFreeZone(); |
| 140 | + |
| 141 | + SocketWrapper* wrapper = new SocketWrapper(); |
| 142 | + wrapper->socket = accepted; |
| 143 | + return wrapper; |
| 144 | +} |
| 145 | + |
| 146 | +Array<int> crossbyte_socket_host_info(Dynamic socket) { |
| 147 | + return crossbyte_socket_name_info(socket, false); |
| 148 | +} |
| 149 | + |
| 150 | +Array<int> crossbyte_socket_peer_info(Dynamic socket) { |
| 151 | + return crossbyte_socket_name_info(socket, true); |
| 152 | +} |
0 commit comments