123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427 |
- #include "il2cpp-config.h"
-
- #if !IL2CPP_USE_GENERIC_SOCKET_IMPL && IL2CPP_TARGET_WINDOWS && IL2CPP_SUPPORT_SOCKETS
-
- #include <time.h>
- #include <string.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <sys/stat.h>
-
- #include "WindowsHelpers.h"
- #include <WinSock2.h>
- #include <WS2tcpip.h>
- #include <mswsock.h>
- #include "os/Error.h"
- #include "os/Socket.h"
- #include "os/ErrorCodes.h"
- #include "os/Win32/SocketImpl.h"
- #include "utils/Il2CppError.h"
- #include "utils/StringUtils.h"
- #include "utils/Memory.h"
-
- typedef int socklen_t;
-
- namespace il2cpp
- {
- namespace os
- {
- static bool is_loopback(int32_t family, uint8_t *addr)
- {
- if (family == AF_INET)
- return addr[0] == 127;
- #if IL2CPP_SUPPORT_IPV6
- else if (family == AF_INET6)
- return (IN6_IS_ADDR_LOOPBACK((struct in6_addr *)addr));
- #endif
- return false;
- }
-
- static struct in_addr *get_local_ips(int32_t family, int32_t *nips)
- {
- *nips = 0;
-
- return 0;
- }
-
- static bool hostent_get_info(struct hostent *he, std::string &name, std::vector<std::string> &aliases, std::vector<std::string> &addresses)
- {
- if (he == NULL)
- return false;
-
- if (he->h_length != 4 || he->h_addrtype != AF_INET)
- return false;
-
- name.assign(he->h_name);
-
- for (int32_t i = 0; he->h_aliases[i] != NULL; ++i)
- aliases.push_back(he->h_aliases[i]);
-
- if (he->h_addrtype == AF_INET)
- {
- struct in_addr addr = {0};
- for (int32_t i = 0; he->h_addr_list[i] != NULL; ++i)
- {
- addr.s_addr = *(u_long*)he->h_addr_list[i];
- addresses.push_back(inet_ntoa(addr));
- }
- }
-
- return true;
- }
-
- static bool hostent_get_info_with_local_ips(struct hostent *he, std::string &name, std::vector<std::string> &aliases, std::vector<std::string> &addresses)
- {
- int32_t i = 0;
- int32_t nlocal_in = 0;
-
- if (he != NULL)
- {
- if (he->h_length != 4 || he->h_addrtype != AF_INET)
- return false;
-
- name.assign(he->h_name);
-
- for (int32_t i = 0; he->h_aliases[i] != NULL; ++i)
- aliases.push_back(he->h_aliases[i]);
- }
-
- struct in_addr *local_in = get_local_ips(AF_INET, &nlocal_in);
-
- if (nlocal_in)
- {
- for (int32_t i = 0; i < nlocal_in; ++i)
- addresses.push_back(inet_ntoa(local_in[i]));
-
- free(local_in);
- }
- else if (he == NULL)
- {
- // If requesting "" and there are no other interfaces up, MS returns 127.0.0.1
- addresses.push_back("127.0.0.1");
- return true;
- }
-
- if (nlocal_in == 0 && he != NULL)
- {
- if (he->h_addrtype == AF_INET)
- {
- struct in_addr addr = {0};
- for (int32_t i = 0; he->h_addr_list[i] != NULL; ++i)
- {
- addr.s_addr = *(u_long*)he->h_addr_list[i];
- addresses.push_back(inet_ntoa(addr));
- }
- }
- }
-
- return true;
- }
-
- static int32_t convert_socket_flags(os::SocketFlags flags)
- {
- int32_t c_flags = 0;
-
- if (flags)
- {
- // Check if contains invalid flag values
- if (flags & ~(os::kSocketFlagsOutOfBand | os::kSocketFlagsMaxIOVectorLength | os::kSocketFlagsPeek | os::kSocketFlagsDontRoute | os::kSocketFlagsPartial))
- {
- return -1;
- }
-
- if (flags & os::kSocketFlagsOutOfBand)
- c_flags |= MSG_OOB;
-
- if (flags & os::kSocketFlagsPeek)
- c_flags |= MSG_PEEK;
-
- if (flags & os::kSocketFlagsDontRoute)
- c_flags |= MSG_DONTROUTE;
-
- // Ignore Partial - see bug 349688. Don't return -1, because
- // according to the comment in that bug ms runtime doesn't for
- // UDP sockets (this means we will silently ignore it for TCP
- // too)
-
- #ifdef MSG_MORE
- if (flags & os::kSocketFlagsPartial)
- c_flags |= MSG_MORE;
- #endif
- }
-
- return c_flags;
- }
-
- #if IL2CPP_SUPPORT_IPV6
- static void AddrinfoGetAddresses(struct addrinfo *info, std::string& name, bool add_local_ips,
- std::vector<std::string> &addr_list)
- {
- if (add_local_ips)
- {
- int nlocal_in = 0;
- int nlocal_in6 = 0;
- in_addr* local_in = (struct in_addr *)get_local_ips(AF_INET, &nlocal_in);
- in6_addr* local_in6 = (struct in6_addr *)get_local_ips(AF_INET6, &nlocal_in6);
- if (nlocal_in || nlocal_in6)
- {
- if (nlocal_in)
- {
- for (int i = 0; i < nlocal_in; i++)
- {
- char addr[16];
- inet_ntop(AF_INET, &local_in[i], addr, sizeof(addr));
- addr_list.push_back(std::string(addr));
- }
- }
-
- if (nlocal_in6)
- {
- for (int i = 0; i < nlocal_in6; i++)
- {
- char addr[48];
- const char* ret = inet_ntop(AF_INET6, &local_in6[i], addr, sizeof(addr));
- if (ret != NULL)
- addr_list.push_back(std::string(addr));
- }
- }
- }
-
- free(local_in);
- free(local_in6);
- }
-
- bool nameSet = false;
- for (addrinfo* ai = info; ai != NULL; ai = ai->ai_next)
- {
- const char *ret;
- char buffer[48]; /* Max. size for IPv6 */
-
- if ((ai->ai_family != PF_INET) && (ai->ai_family != PF_INET6))
- continue;
-
- if (ai->ai_family == PF_INET)
- ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in*)ai->ai_addr)->sin_addr), buffer, 16);
- else
- ret = inet_ntop(ai->ai_family, (void*)&(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr), buffer, 48);
-
- if (ret)
- addr_list.push_back(std::string(buffer));
- else
- addr_list.push_back(std::string());
-
- if (!nameSet)
- {
- if (ai->ai_canonname != NULL)
- name = std::string(ai->ai_canonname);
- else
- name = std::string();
-
- nameSet = true;
- }
- }
- }
-
- WaitStatus GetAddressInfo(const char* hostname, bool add_local_ips, std::string &name, std::vector<std::string> &addr_list)
- {
- addrinfo *info = NULL;
-
- addrinfo hints;
- memset(&hints, 0, sizeof(hints));
-
- // Here Mono inspects the ipv4Supported and ipv6Supported properties on the managed Socket class.
- // This seems to be unnecessary though, as we can use PF_UNSPEC in all cases, and getaddrinfo works.
- hints.ai_family = PF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
-
- if (getaddrinfo(hostname, NULL, &hints, &info) == -1)
- return kWaitStatusFailure;
-
- AddrinfoGetAddresses(info, name, add_local_ips, addr_list);
-
- if (info)
- freeaddrinfo(info);
-
- if (name.empty())
- name.assign(hostname);
-
- return kWaitStatusSuccess;
- }
-
- #endif
-
- static WaitStatus GetHostByAddrIPv4(const std::string &address, std::string &name, std::vector<std::string> &aliases, std::vector<std::string> &addr_list)
- {
- struct in_addr inaddr;
- if (inet_pton(AF_INET, address.c_str(), &inaddr) <= 0)
- return kWaitStatusFailure;
-
- struct hostent *he = gethostbyaddr((char*)&inaddr, sizeof(inaddr), AF_INET);
-
- if (he == NULL)
- {
- name = address;
- addr_list.push_back(name);
-
- return kWaitStatusSuccess;
- }
-
- return hostent_get_info(he, name, aliases, addr_list)
- ? kWaitStatusSuccess
- : kWaitStatusFailure;
- }
-
- WaitStatus SocketImpl::GetHostByAddr(const std::string &address, std::string &name, std::vector<std::string> &aliases, std::vector<std::string> &addr_list)
- {
- #if IL2CPP_SUPPORT_IPV6
- struct sockaddr_in saddr;
- struct sockaddr_in6 saddr6;
- int32_t family;
- char hostname[1024] = { 0 };
- int flags = 0;
-
- if (inet_pton(AF_INET, address.c_str(), &saddr.sin_addr) <= 0)
- {
- /* Maybe an ipv6 address */
- if (inet_pton(AF_INET6, address.c_str(), &saddr6.sin6_addr) <= 0)
- {
- return kWaitStatusFailure;
- }
- else
- {
- family = AF_INET6;
- saddr6.sin6_family = AF_INET6;
- }
- }
- else
- {
- family = AF_INET;
- saddr.sin_family = AF_INET;
- }
-
- if (family == AF_INET)
- {
- return GetHostByAddrIPv4(address, name, aliases, addr_list);
- }
- else if (family == AF_INET6)
- {
- #if HAVE_SOCKADDR_IN6_SIN_LEN
- saddr6.sin6_len = sizeof(saddr6);
- #endif
- if (getnameinfo((struct sockaddr*)&saddr6, sizeof(saddr6),
- hostname, sizeof(hostname), NULL, 0,
- flags) != 0)
- {
- return kWaitStatusFailure;
- }
- }
-
- return GetAddressInfo(hostname, true, name, addr_list);
- #else
- return GetHostByAddrIPv4(address, name, aliases, addr_list);
- #endif
- }
-
- WaitStatus SocketImpl::GetHostByName(const std::string &host, std::string &name, std::vector<std::string> &aliases, std::vector<std::string> &addresses)
- {
- char this_hostname[256] = {0};
-
- const char *hostname = host.c_str();
- bool add_local_ips = (*hostname == '\0');
-
- if (!add_local_ips && gethostname(this_hostname, sizeof(this_hostname)) != -1)
- {
- if (!strcmp(hostname, this_hostname))
- add_local_ips = true;
- }
-
- #if IL2CPP_SUPPORT_IPV6
- return GetAddressInfo(hostname, add_local_ips, name, addresses);
- #else
- struct hostent *he = NULL;
- if (*hostname)
- he = gethostbyname(hostname);
-
- if (*hostname && he == NULL)
- return kWaitStatusFailure;
-
- return (add_local_ips
- ? hostent_get_info_with_local_ips(he, name, aliases, addresses)
- : hostent_get_info(he, name, aliases, addresses))
- ? kWaitStatusSuccess
- : kWaitStatusFailure;
- #endif
- }
-
- WaitStatus SocketImpl::GetHostByName(const std::string &host, std::string &name, int32_t &family, std::vector<std::string> &aliases, std::vector<void*> &addr_list, int32_t &addr_size)
- {
- std::vector<std::string> addresses;
- WaitStatus result = GetHostByName(host, name, aliases, addresses);
-
- addr_size = sizeof(in_addr);
- family = AF_INET;
-
- for (std::vector<std::string>::iterator it = addresses.begin(); it != addresses.end(); ++it)
- {
- void* addressLocation = il2cpp::utils::Memory::Malloc(addr_size);
- in_addr address;
- if (inet_pton(family, it->c_str(), &address))
- {
- memcpy(addressLocation, &address.S_un.S_addr, addr_size);
- addr_list.push_back(addressLocation);
- }
- }
-
- return result;
- }
-
- void SocketImpl::Startup()
- {
- WSADATA wsadata;
-
- const int32_t ret = WSAStartup(MAKEWORD(2, 0), &wsadata);
- if (ret != 0)
- {
- exit(-1);
- }
- }
-
- void SocketImpl::Cleanup()
- {
- WSACleanup();
- }
-
- WaitStatus SocketImpl::GetHostName(std::string &name)
- {
- char hostname[256];
- int32_t ret = gethostname(hostname, sizeof(hostname));
-
- if (ret == -1)
- return kWaitStatusFailure;
-
- name.assign(hostname);
-
- return kWaitStatusSuccess;
- }
-
- SocketImpl::SocketImpl(ThreadStatusCallback thread_status_callback)
- : _is_valid(false)
- , _fd(-1)
- , _domain(-1)
- , _type(-1)
- , _protocol(-1)
- , _saved_error(kErrorCodeSuccess)
- , _still_readable(0)
- , _thread_status_callback(thread_status_callback)
- {
- }
-
- SocketImpl::~SocketImpl()
- {
- }
-
- static int32_t convert_address_family(AddressFamily family)
- {
- switch (family)
- {
- case kAddressFamilyUnspecified:
- return AF_UNSPEC;
-
- // NOTE(gab): not supported on Windows
- //case kAddressFamilyUnix:
- // return AF_UNIX;
-
- case kAddressFamilyInterNetwork:
- return AF_INET;
-
- case kAddressFamilyIpx:
- return AF_IPX;
-
- case kAddressFamilySna:
- return AF_SNA;
-
- case kAddressFamilyDecNet:
- return AF_DECnet;
-
- case kAddressFamilyAppleTalk:
- return AF_APPLETALK;
-
- #if IL2CPP_SUPPORT_IPV6
- case kAddressFamilyInterNetworkV6:
- return AF_INET6;
- #endif
-
- #ifdef AF_IRDA
- case kAddressFamilyIrda:
- return AF_IRDA;
- #endif
-
- default:
- break;
- }
-
- return -1;
- }
-
- static int32_t convert_socket_type(SocketType type)
- {
- switch (type)
- {
- case kSocketTypeStream:
- return SOCK_STREAM;
-
- case kSocketTypeDgram:
- return SOCK_DGRAM;
-
- case kSocketTypeRaw:
- return SOCK_RAW;
-
- case kSocketTypeRdm:
- return SOCK_RDM;
-
- case kSocketTypeSeqpacket:
- return SOCK_SEQPACKET;
-
- default:
- break;
- }
-
- return -1;
- }
-
- static int32_t convert_socket_protocol(ProtocolType protocol)
- {
- switch (protocol)
- {
- case kProtocolTypeIP:
- case kProtocolTypeIPv6:
- case kProtocolTypeIcmp:
- case kProtocolTypeIgmp:
- case kProtocolTypeGgp:
- case kProtocolTypeTcp:
- case kProtocolTypePup:
- case kProtocolTypeUdp:
- case kProtocolTypeIdp:
- case kProtocolTypeIcmpV6:
- // In this case the enum values map exactly.
- return (int32_t)protocol;
-
- default:
- break;
- }
-
- // Everything else in unsupported and unexpected
- return -1;
- }
-
- static int SocketExceptionFilter(unsigned int code)
- {
- // Sometimes, we call the socket functions and close the socket right after,
- // and in some rare cases, it throws EXCEPTION_INVALID_HANDLE SEH exception
- // rather than returning an error code. Although this is undocumented on MSDN,
- // it causes a crash just because it thinks we gave it an invalid handle.
- // We guard against it by wrapping every socket call with __try/__except
- if (code == EXCEPTION_INVALID_HANDLE)
- return EXCEPTION_CONTINUE_EXECUTION;
-
- return EXCEPTION_CONTINUE_SEARCH;
- }
-
- WaitStatus SocketImpl::Create(AddressFamily family, SocketType type, ProtocolType protocol)
- {
- _fd = -1;
- _is_valid = false;
- _still_readable = 1;
- _domain = convert_address_family(family);
- _type = convert_socket_type(type);
- _protocol = convert_socket_protocol(protocol);
-
- IL2CPP_ASSERT(_type != -1 && "Unsupported socket type");
- IL2CPP_ASSERT(_domain != -1 && "Unsupported address family");
- IL2CPP_ASSERT(_protocol != -1 && "Unsupported protocol type");
-
- _fd = socket(_domain, _type, _protocol);
- if (_fd == -1 && _domain == AF_INET && _type == SOCK_RAW && _protocol == 0)
- {
- // Retry with protocol == 4 (see bug #54565)
- _protocol = 4;
- _fd = socket(AF_INET, SOCK_RAW, 4);
- }
-
- if (_fd == -1)
- {
- StoreLastError();
-
- return kWaitStatusFailure;
- }
-
- _is_valid = true;
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::Create(SocketDescriptor fd, int32_t family, int32_t type, int32_t protocol)
- {
- _fd = fd;
- _is_valid = (fd != -1);
- _still_readable = 1;
- _domain = family;
- _type = type;
- _protocol = protocol;
-
- IL2CPP_ASSERT(_type != -1 && "Unsupported socket type");
- IL2CPP_ASSERT(_domain != -1 && "Unsupported address family");
- IL2CPP_ASSERT(_protocol != -1 && "Unsupported protocol type");
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::Close()
- {
- _saved_error = kErrorCodeSuccess;
-
- SOCKET fd = (SOCKET)_fd;
- if (_is_valid && fd != -1)
- {
- if (closesocket(fd) == -1)
- StoreLastError();
- }
-
- _fd = -1;
- _is_valid = false;
- _still_readable = 0;
- _domain = -1;
- _type = -1;
- _protocol = -1;
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::SetBlocking(bool blocking)
- {
- /*
- * block == TRUE/FALSE means we will block/not block.
- * But the ioctlsocket call takes TRUE/FALSE for non-block/block
- */
- u_long ioctl_socket_blocking = (u_long) !blocking;
-
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- int32_t ret = -1;
-
- __try
- {
- ret = ioctlsocket(fd, FIONBIO, &ioctl_socket_blocking);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- }
-
- if (ret == SOCKET_ERROR)
- {
- StoreLastError();
-
- return kWaitStatusFailure;
- }
-
- return kWaitStatusSuccess;
- }
-
- ErrorCode SocketImpl::GetLastError() const
- {
- return _saved_error;
- }
-
- void SocketImpl::StoreLastError()
- {
- const ErrorCode error = (ErrorCode)WSAGetLastError();
-
- Error::SetLastError(error);
-
- _saved_error = error;
- }
-
- void SocketImpl::StoreLastError(int32_t error_no)
- {
- const ErrorCode error = (ErrorCode)error_no;
-
- Error::SetLastError(error);
-
- _saved_error = error;
- }
-
- static void sockaddr_from_address(uint32_t address, uint16_t port, struct sockaddr *sa, socklen_t *sa_size)
- {
- struct sockaddr_in sa_in = {0};
-
- sa_in.sin_family = AF_INET;
- sa_in.sin_port = port;
- sa_in.sin_addr.s_addr = address;
-
- *sa_size = sizeof(struct sockaddr_in);
- *sa = *((struct sockaddr*)&sa_in);
- }
-
- #if IL2CPP_SUPPORT_IPV6
- static void sockaddr_from_address(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port, sockaddr_in6* sa, socklen_t *sa_size)
- {
- sa->sin6_family = AF_INET6;
- sa->sin6_port = port;
- memcpy(&sa->sin6_addr, &address[0], 16);
- sa->sin6_scope_id = scope;
-
- *sa_size = sizeof(struct sockaddr_in6);
- }
-
- #endif
-
- static AddressFamily convert_define_to_address_family(int32_t family)
- {
- switch (family)
- {
- case AF_UNSPEC:
- return kAddressFamilyUnspecified;
-
- case AF_UNIX:
- return kAddressFamilyUnix;
-
- case AF_INET:
- return kAddressFamilyInterNetwork;
- #ifdef AF_IPX
- case AF_IPX:
- return kAddressFamilyIpx;
- #endif
- #ifdef AF_SNA
- case AF_SNA:
- return kAddressFamilySna;
- #endif
- #ifdef AF_DECnet
- case AF_DECnet:
- return kAddressFamilyDecNet;
- #endif
- #ifdef AF_APPLETALK
- case AF_APPLETALK:
- return kAddressFamilyAppleTalk;
- #endif
- #ifdef AF_INET6
- case AF_INET6:
- return kAddressFamilyInterNetworkV6;
- #endif
- #ifdef AF_IRDA
- case AF_IRDA:
- return kAddressFamilyIrda;
- #endif
-
- default:
- break;
- }
-
- return kAddressFamilyError;
- }
-
- static bool socketaddr_to_endpoint_info(const struct sockaddr *address, socklen_t address_len, EndPointInfo &info)
- {
- info.family = convert_define_to_address_family(address->sa_family);
-
- if (info.family == os::kAddressFamilyInterNetwork)
- {
- const struct sockaddr_in *address_in = (const struct sockaddr_in *)address;
-
- info.data.inet.port = ntohs(address_in->sin_port);
- info.data.inet.address = ntohl(address_in->sin_addr.s_addr);
-
- return true;
- }
-
- // NOTE(gab): kAddressFamilyUnix not supported on Windows
- //if (info.family == os::kAddressFamilyUnix)
- //{
- // for (int32_t i = 0; i < address_len; i++)
- // info.data.path[i] = address->sa_data[i];
-
- // return true;
- //}
-
- #if IL2CPP_SUPPORT_IPV6
- if (info.family == os::kAddressFamilyInterNetworkV6)
- {
- const struct sockaddr_in6 *address_in = (const struct sockaddr_in6 *)address;
-
- uint16_t port = ntohs(address_in->sin6_port);
-
- info.data.raw[2] = (port >> 8) & 0xff;
- info.data.raw[3] = port & 0xff;
-
- for (int i = 0; i < 16; i++)
- info.data.raw[i + 8] = address_in->sin6_addr.s6_addr[i];
-
- info.data.raw[24] = address_in->sin6_scope_id & 0xff;
- info.data.raw[25] = (address_in->sin6_scope_id >> 8) & 0xff;
- info.data.raw[26] = (address_in->sin6_scope_id >> 16) & 0xff;
- info.data.raw[27] = (address_in->sin6_scope_id >> 24) & 0xff;
-
- return true;
- }
- #endif
-
- return false;
- }
-
- WaitStatus SocketImpl::Bind(const char *path)
- {
- // NOTE(gab): not supported on Windows.
- return kWaitStatusFailure;
- }
-
- WaitStatus SocketImpl::Bind(const char *address, uint16_t port)
- {
- struct sockaddr sa = {0};
- socklen_t sa_size = 0;
-
- sockaddr_from_address(inet_addr(address), htons(port), &sa, &sa_size);
-
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- int bindResult = -1;
-
- __try
- {
- bindResult = bind(fd, &sa, sa_size);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- }
-
- if (bindResult == -1)
- {
- StoreLastError();
- return kWaitStatusFailure;
- }
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::Bind(uint32_t address, uint16_t port)
- {
- struct sockaddr sa = {0};
- socklen_t sa_size = 0;
-
- sockaddr_from_address(htonl(address), htons(port), &sa, &sa_size);
-
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- int bindResult = -1;
-
- __try
- {
- bindResult = bind(fd, &sa, sa_size);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- }
-
- if (bindResult == -1)
- {
- StoreLastError();
- return kWaitStatusFailure;
- }
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::Bind(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port)
- {
- struct sockaddr_in6 sa = { 0 };
- socklen_t sa_size = 0;
-
- sockaddr_from_address(address, scope, htons(port), &sa, &sa_size);
-
- if (bind((SOCKET)_fd, (sockaddr*)&sa, sa_size) == -1)
- {
- StoreLastError();
- return kWaitStatusFailure;
- }
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::ConnectInternal(struct sockaddr *sa, int32_t sa_size)
- {
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- __try
- {
- if (connect(fd, sa, (socklen_t)sa_size) != -1)
- return kWaitStatusSuccess;
-
- if (errno != EINTR)
- {
- StoreLastError();
- return kWaitStatusFailure;
- }
-
- struct pollfd fds = { 0 };
-
- fds.fd = fd;
- fds.events = POLLOUT;
-
- while (WSAPoll(&fds, 1, -1) == -1)
- {
- if (errno != EINTR)
- {
- StoreLastError();
- return kWaitStatusFailure;
- }
- }
-
- int32_t so_error = 0;
- socklen_t len = sizeof(so_error);
-
- if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*)&so_error, &len) == -1)
- {
- StoreLastError();
- return kWaitStatusFailure;
- }
-
- if (so_error != 0)
- {
- StoreLastError(so_error);
- return kWaitStatusFailure;
- }
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- SetLastError(kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::Connect(const char *path)
- {
- // NOTE(gab): not supported on Windows.
- return kWaitStatusFailure;
- }
-
- WaitStatus SocketImpl::Connect(uint32_t address, uint16_t port)
- {
- struct sockaddr sa = {0};
- socklen_t sa_size = 0;
-
- sockaddr_from_address(htonl(address), htons(port), &sa, &sa_size);
-
- return ConnectInternal((struct sockaddr *)&sa, sa_size);
- }
-
- WaitStatus SocketImpl::Connect(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port)
- {
- struct sockaddr_in6 sa = { 0 };
- socklen_t sa_size = 0;
-
- sockaddr_from_address(address, scope, htons(port), &sa, &sa_size);
-
- return ConnectInternal((struct sockaddr *)&sa, sa_size);
- }
-
- WaitStatus SocketImpl::GetLocalEndPointInfo(EndPointInfo &info)
- {
- // Note: the size here could probably be smaller
- uint8_t buffer[END_POINT_MAX_PATH_LEN + 3] = {0};
- socklen_t address_len = sizeof(buffer);
-
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- int getsocknameResult = -1;
-
- __try
- {
- getsocknameResult = getsockname(fd, (struct sockaddr *)buffer, &address_len);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- SetLastError(kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- if (getsocknameResult == -1)
- {
- StoreLastError();
- return kWaitStatusFailure;
- }
-
- if (!socketaddr_to_endpoint_info((struct sockaddr *)buffer, address_len, info))
- {
- _saved_error = kWSAeafnosupport;
- return kWaitStatusFailure;
- }
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::GetRemoteEndPointInfo(EndPointInfo &info)
- {
- // Note: the size here could probably be smaller
- uint8_t buffer[END_POINT_MAX_PATH_LEN + 3] = {0};
- socklen_t address_len = sizeof(buffer);
-
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- int getpeernameResult = -1;
-
- __try
- {
- getpeernameResult = getpeername(fd, (struct sockaddr *)buffer, &address_len);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- }
-
- if (getpeernameResult == -1)
- {
- StoreLastError();
- return kWaitStatusFailure;
- }
-
- if (!socketaddr_to_endpoint_info((struct sockaddr *)buffer, address_len, info))
- {
- _saved_error = kWSAeafnosupport;
- return kWaitStatusFailure;
- }
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::Listen(int32_t backlog)
- {
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- int listenResult = -1;
-
- __try
- {
- listenResult = listen(fd, backlog);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- }
-
- if (listenResult == -1)
- {
- StoreLastError();
- return kWaitStatusFailure;
- }
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::Shutdown(int32_t how)
- {
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- int shutdownResult = -1;
-
- __try
- {
- shutdownResult = shutdown(fd, how);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- }
-
- if (shutdownResult == -1)
- {
- StoreLastError();
- return kWaitStatusFailure;
- }
-
- if (how == SD_RECEIVE || how == SD_BOTH)
- _still_readable = 0;
-
- return kWaitStatusSuccess;
- }
-
- static SOCKET AcceptProtected(SOCKET fd)
- {
- SOCKET new_fd;
-
- do
- {
- __try
- {
- new_fd = accept(fd, NULL, 0);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- new_fd = -1;
- break;
- }
- }
- while (new_fd == -1 && errno == EINTR);
-
- return new_fd;
- }
-
- WaitStatus SocketImpl::Accept(os::Socket **socket)
- {
- SocketDescriptor new_fd = 0;
-
- *socket = NULL;
-
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- new_fd = AcceptProtected(fd);
-
- if (new_fd == -1)
- {
- StoreLastError();
-
- return kWaitStatusFailure;
- }
-
- *socket = new os::Socket(_thread_status_callback);
-
- const WaitStatus status = (*socket)->Create(new_fd, _domain, _type, _protocol);
-
- if (status != kWaitStatusSuccess)
- {
- delete *socket;
- *socket = NULL;
- return status;
- }
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::Disconnect(bool reuse)
- {
- GUID GuidDisconnectEx = WSAID_DISCONNECTEX;
- LPFN_DISCONNECTEX disconnectEx;
- DWORD copied;
-
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- __try
- {
- int32_t ret = WSAIoctl(fd, SIO_GET_EXTENSION_FUNCTION_POINTER, &GuidDisconnectEx, sizeof(GuidDisconnectEx), &disconnectEx, sizeof(disconnectEx), &copied, 0, 0);
-
- if (ret == SOCKET_ERROR)
- {
- StoreLastError();
- return kWaitStatusFailure;
- }
-
- if (!disconnectEx(fd, NULL, 0, NULL))
- {
- StoreLastError();
- return kWaitStatusFailure;
- }
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::Receive(const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len)
- {
- *len = 0;
-
- const int32_t c_flags = convert_socket_flags(flags);
-
- if (c_flags == -1)
- {
- _saved_error = kWSAeopnotsupp;
- return kWaitStatusFailure;
- }
-
- return ReceiveFromInternal(data, count, c_flags, len, NULL, 0);
- }
-
- WaitStatus SocketImpl::ReceiveFromInternal(const uint8_t *data, size_t count, int32_t flags, int32_t *len, struct sockaddr *from, int32_t *fromlen)
- {
- int32_t ret = 0;
- IL2CPP_ASSERT(count < static_cast<size_t>(std::numeric_limits<int>::max()));
-
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- do
- {
- __try
- {
- ret = recvfrom(fd, (char*)data, static_cast<int>(count), flags, from, (socklen_t*)fromlen);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- ret = -1;
- break;
- }
- }
- while (ret == -1 && errno == EINTR);
-
- if (ret == 0 && count > 0)
- {
- // According to the Linux man page, recvfrom only
- // returns 0 when the socket has been shut down
- // cleanly. Turn this into an EINTR to simulate win32
- // behaviour of returning EINTR when a socket is
- // closed while the recvfrom is blocking (we use a
- // shutdown() in socket_close() to trigger this.) See
- // bug 75705.
-
- // Distinguish between the socket being shut down at
- // the local or remote ends, and reads that request 0
- // bytes to be read
-
- // If this returns FALSE, it means the socket has been
- // closed locally. If it returns TRUE, but
- // still_readable != 1 then shutdown
- // (SHUT_RD|SHUT_RDWR) has been called locally.
-
- if (_still_readable != 1)
- {
- ret = -1;
- errno = EINTR;
- }
- }
-
- if (ret == -1)
- {
- StoreLastError();
- return kWaitStatusFailure;
- }
-
- *len = ret;
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::Send(const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len)
- {
- *len = 0;
-
- const int32_t c_flags = convert_socket_flags(flags);
-
- if (c_flags == -1)
- {
- _saved_error = kWSAeopnotsupp;
- return kWaitStatusFailure;
- }
-
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- int32_t ret = 0;
-
- do
- {
- __try
- {
- ret = send(fd, (char*)data, count, flags);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- ret = -1;
- break;
- }
- }
- while (ret == -1 && errno == EINTR);
-
- if (ret == -1)
- {
- StoreLastError();
- return kWaitStatusFailure;
- }
-
- *len = ret;
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::SendArray(WSABuf *wsabufs, int32_t count, int32_t *sent, SocketFlags flags)
- {
- DWORD c_flags = convert_socket_flags(flags);
-
- if (c_flags == -1)
- {
- _saved_error = kWSAeopnotsupp;
- return kWaitStatusFailure;
- }
-
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- DWORD bytes_sent;
-
- int32_t ret = -1;
-
- __try
- {
- ret = WSASend(fd, (WSABUF*)wsabufs, count, &bytes_sent, c_flags, NULL, NULL);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- }
-
- if (ret == SOCKET_ERROR)
- {
- *sent = 0;
-
- StoreLastError();
-
- return kWaitStatusFailure;
- }
-
- *sent = bytes_sent;
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::ReceiveArray(WSABuf *wsabufs, int32_t count, int32_t *len, SocketFlags flags)
- {
- DWORD recv;
- DWORD c_flags = convert_socket_flags(flags);
-
- if (c_flags == -1)
- {
- _saved_error = kWSAeopnotsupp;
- return kWaitStatusFailure;
- }
-
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- int32_t ret = -1;
-
- __try
- {
- // NOTE(gab): this cast is possible as long as WSABuf is binary compatible with WSABUF
- ret = WSARecv(fd, (WSABUF*)wsabufs, count, &recv, &c_flags, NULL, NULL);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- }
-
- if (ret == SOCKET_ERROR)
- {
- *len = 0;
-
- StoreLastError();
-
- return kWaitStatusFailure;
- }
-
- *len = recv;
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::SendToInternal(struct sockaddr *sa, int32_t sa_size, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len)
- {
- const int32_t c_flags = convert_socket_flags(flags);
-
- if (c_flags == -1)
- {
- _saved_error = kWSAeopnotsupp;
- return kWaitStatusFailure;
- }
-
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- int32_t ret = 0;
-
- do
- {
- __try
- {
- ret = sendto(fd, (char*)data, count, c_flags, sa, sa_size);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- ret = -1;
- break;
- }
- }
- while (ret == -1 && errno == EINTR);
-
- if (ret == -1)
- {
- StoreLastError();
- return kWaitStatusFailure;
- }
-
- *len = ret;
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::SendTo(uint32_t address, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len)
- {
- *len = 0;
-
- struct sockaddr sa = {0};
- socklen_t sa_size = 0;
-
- sockaddr_from_address(htonl(address), htons(port), &sa, &sa_size);
-
- return SendToInternal(&sa, sa_size, data, count, flags, len);
- }
-
- utils::Expected<WaitStatus> SocketImpl::SendTo(const char *path, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len)
- {
- return utils::Il2CppError(utils::NotSupported, "Unix sockets are not supported on this platform.");
- }
-
- WaitStatus SocketImpl::SendTo(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len)
- {
- *len = 0;
-
- struct sockaddr_in6 sa = { 0 };
- socklen_t sa_size = 0;
-
- sockaddr_from_address(address, scope, htons(port), &sa, &sa_size);
-
- return SendToInternal((sockaddr*)&sa, sa_size, data, count, flags, len);
- }
-
- WaitStatus SocketImpl::RecvFromInternal(struct sockaddr* sa, int32_t sa_size, const uint8_t* data, int32_t count, os::SocketFlags flags, int32_t* len, os::EndPointInfo& ep)
- {
- const int32_t c_flags = convert_socket_flags(flags);
-
- if (c_flags == -1)
- {
- _saved_error = kWSAeopnotsupp;
- return kWaitStatusFailure;
- }
-
- int32_t ret = 0;
-
- const WaitStatus status = ReceiveFromInternal(data, count, c_flags, len, sa, &sa_size);
-
- if (status != kWaitStatusSuccess)
- {
- ep.family = os::kAddressFamilyError;
- return status;
- }
-
- if (sa_size == 0)
- return kWaitStatusSuccess;
-
- if (!socketaddr_to_endpoint_info(sa, sa_size, ep))
- {
- ep.family = os::kAddressFamilyError;
- _saved_error = kWSAeafnosupport;
- return kWaitStatusFailure;
- }
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::RecvFrom(uint32_t address, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len, os::EndPointInfo &ep)
- {
- *len = 0;
-
- struct sockaddr sa = {0};
- socklen_t sa_size = 0;
-
- sockaddr_from_address(htonl(address), htons(port), &sa, &sa_size);
-
- return RecvFromInternal(&sa, sa_size, data, count, flags, len, ep);
- }
-
- utils::Expected<WaitStatus> SocketImpl::RecvFrom(const char *path, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len, os::EndPointInfo &ep)
- {
- return utils::Il2CppError(utils::NotSupported, "Unix sockets are not supported on this platform.");
- }
-
- WaitStatus SocketImpl::RecvFrom(uint8_t address[ipv6AddressSize], uint32_t scope, uint16_t port, const uint8_t *data, int32_t count, os::SocketFlags flags, int32_t *len, os::EndPointInfo &ep)
- {
- *len = 0;
-
- struct sockaddr_in6 sa = { 0 };
- socklen_t sa_size = 0;
-
- sockaddr_from_address(address, scope, htons(port), &sa, &sa_size);
-
- return RecvFromInternal((sockaddr*)&sa, sa_size, data, count, flags, len, ep);
- }
-
- WaitStatus SocketImpl::Available(int32_t *amount)
- {
- *amount = 0;
-
- u_long a = 0;
-
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- int ioctlsocketResult = -1;
-
- __try
- {
- ioctlsocketResult = ioctlsocket(fd, FIONREAD, &a);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- }
-
- if (ioctlsocketResult == -1)
- {
- StoreLastError();
- return kWaitStatusFailure;
- }
-
- *amount = a;
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::Ioctl(int32_t command, const uint8_t *in_data, int32_t in_len, uint8_t *out_data, int32_t out_len, int32_t *written)
- {
- IL2CPP_ASSERT(command != 0xC8000006 /* SIO_GET_EXTENSION_FUNCTION_POINTER */ && "SIO_GET_EXTENSION_FUNCTION_POINTER ioctl command not supported");
-
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- DWORD len = 0;
- int32_t ret = -1;
-
- __try
- {
- ret = WSAIoctl(fd, command, (void*)in_data, in_len, out_data, out_len, &len, NULL, NULL);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- }
-
- if (ret == SOCKET_ERROR)
- {
- StoreLastError();
-
- return kWaitStatusFailure;
- }
-
- *written = len;
-
- return kWaitStatusSuccess;
- }
-
- #define SKIP_OPTION -2
- #define INVALID_OPTION_NAME -1
-
- static int32_t level_and_name_to_system(SocketOptionLevel level, SocketOptionName name, int32_t *system_level, int32_t *system_name)
- {
- switch (level)
- {
- case kSocketOptionLevelSocket:
- *system_level = SOL_SOCKET;
-
- switch (name)
- {
- // This is SO_LINGER, because the setsockopt
- // internal call maps DontLinger to SO_LINGER
- // with l_onoff=0
- case kSocketOptionNameDontLinger:
- *system_name = SO_LINGER;
- break;
-
- case kSocketOptionNameDebug:
- *system_name = SO_DEBUG;
- break;
-
- // #ifdef SO_ACCEPTCONN
- case kSocketOptionNameAcceptConnection:
- *system_name = SO_ACCEPTCONN;
- break;
- // #endif
- case kSocketOptionNameReuseAddress:
- *system_name = SO_REUSEADDR;
- break;
-
- case kSocketOptionNameKeepAlive:
- *system_name = SO_KEEPALIVE;
- break;
-
- case kSocketOptionNameDontRoute:
- *system_name = SO_DONTROUTE;
- break;
-
- case kSocketOptionNameBroadcast:
- *system_name = SO_BROADCAST;
- break;
-
- case kSocketOptionNameLinger:
- *system_name = SO_LINGER;
- break;
-
- case kSocketOptionNameOutOfBandInline:
- *system_name = SO_OOBINLINE;
- break;
-
- case kSocketOptionNameSendBuffer:
- *system_name = SO_SNDBUF;
- break;
-
- case kSocketOptionNameReceiveBuffer:
- *system_name = SO_RCVBUF;
- break;
-
- case kSocketOptionNameSendLowWater:
- *system_name = SO_SNDLOWAT;
- break;
-
- case kSocketOptionNameReceiveLowWater:
- *system_name = SO_RCVLOWAT;
- break;
-
- case kSocketOptionNameSendTimeout:
- *system_name = SO_SNDTIMEO;
- break;
-
- case kSocketOptionNameReceiveTimeout:
- *system_name = SO_RCVTIMEO;
- break;
-
- case kSocketOptionNameError:
- *system_name = SO_ERROR;
- break;
-
- case kSocketOptionNameType:
- *system_name = SO_TYPE;
- break;
-
- case kSocketOptionNameExclusiveAddressUse:
- #ifdef SO_EXCLUSIVEADDRUSE
- *system_name = SO_EXCLUSIVEADDRUSE;
- break;
- #elif SO_REUSEADDR
- *system_name = SO_REUSEADDR;
- break;
- #endif
- case kSocketOptionNameUseLoopback:
- #ifdef SO_USELOOPBACK
- *system_name = SO_USELOOPBACK;
- break;
- #endif
- case kSocketOptionNameMaxConnections:
- #ifdef SO_MAXCONN
- *system_name = SO_MAXCONN;
- break;
- #elif defined(SOMAXCONN)
- *system_name = SOMAXCONN;
- break;
- #endif
- default:
- return INVALID_OPTION_NAME;
- }
- break;
-
- case kSocketOptionLevelIP:
- *system_level = IPPROTO_IP;
-
- switch (name)
- {
- case kSocketOptionNameIPOptions:
- *system_name = IP_OPTIONS;
- break;
-
- #ifdef IP_HDRINCL
- case kSocketOptionNameHeaderIncluded:
- *system_name = IP_HDRINCL;
- break;
- #endif
- #ifdef IP_TOS
- case kSocketOptionNameTypeOfService:
- *system_name = IP_TOS;
- break;
- #endif
- #ifdef IP_TTL
- case kSocketOptionNameIpTimeToLive:
- *system_name = IP_TTL;
- break;
- #endif
- case kSocketOptionNameMulticastInterface:
- *system_name = IP_MULTICAST_IF;
- break;
-
- case kSocketOptionNameMulticastTimeToLive:
- *system_name = IP_MULTICAST_TTL;
- break;
-
- case kSocketOptionNameMulticastLoopback:
- *system_name = IP_MULTICAST_LOOP;
- break;
-
- case kSocketOptionNameAddMembership:
- *system_name = IP_ADD_MEMBERSHIP;
- break;
-
- case kSocketOptionNameDropMembership:
- *system_name = IP_DROP_MEMBERSHIP;
- break;
-
- #ifdef HAVE_IP_PKTINFO
- case kSocketOptionNamePacketInformation:
- *system_name = IP_PKTINFO;
- break;
- #endif
-
- case kSocketOptionNameDontFragment:
- #ifdef IP_DONTFRAGMENT
- *system_name = IP_DONTFRAGMENT;
- #elif IP_MTU_DISCOVER
- *system_name = IP_MTU_DISCOVER;
- #elif IP_DONTFRAG
- *system_name = IP_DONTFRAG;
- #else
- return SKIP_OPTION;
- #endif
- break;
-
- case kSocketOptionNameAddSourceMembership:
- case kSocketOptionNameDropSourceMembership:
- case kSocketOptionNameBlockSource:
- case kSocketOptionNameUnblockSource:
- // Can't figure out how to map these, so fall
- // through
- default:
- return INVALID_OPTION_NAME;
- }
- break;
- #if IL2CPP_SUPPORT_IPV6
- case kSocketOptionLevelIPv6:
- *system_level = IPPROTO_IPV6;
-
- switch (name)
- {
- case kSocketOptionNameMulticastInterface:
- *system_name = IPV6_MULTICAST_IF;
- break;
- case kSocketOptionNameMulticastTimeToLive:
- *system_name = IPV6_MULTICAST_HOPS;
- break;
- case kSocketOptionNameMulticastLoopback:
- *system_name = IPV6_MULTICAST_LOOP;
- break;
- case kSocketOptionNameAddMembership:
- *system_name = IPV6_JOIN_GROUP;
- break;
- case kSocketOptionNameDropMembership:
- *system_name = IPV6_LEAVE_GROUP;
- break;
- case kSocketOptionNamePacketInformation:
- #ifdef HAVE_IPV6_PKTINFO
- *system_name = IPV6_PKTINFO;
- break;
- #endif
- case kSocketOptionNameIPv6Only:
- #ifdef IPV6_V6ONLY
- *system_name = IPV6_V6ONLY;
- break;
- #endif
- case kSocketOptionNameHeaderIncluded:
- case kSocketOptionNameIPOptions:
- case kSocketOptionNameTypeOfService:
- case kSocketOptionNameDontFragment:
- case kSocketOptionNameAddSourceMembership:
- case kSocketOptionNameDropSourceMembership:
- case kSocketOptionNameBlockSource:
- case kSocketOptionNameUnblockSource:
- // Can't figure out how to map these, so fall
- // through
- default:
- return INVALID_OPTION_NAME;
- }
- break;
- #endif // IL2CPP_SUPPORT_IPV6
- case kSocketOptionLevelTcp:
- #ifdef SOL_TCP
- *system_level = SOL_TCP;
- #else
- *system_level = IPPROTO_TCP;
- #endif
-
- switch (name)
- {
- case kSocketOptionNameNoDelay:
- *system_name = TCP_NODELAY;
- break;
- default:
- return INVALID_OPTION_NAME;
- }
- break;
-
- case kSocketOptionLevelUdp:
- default:
- return INVALID_OPTION_NAME;
- }
-
- return 0;
- }
-
- WaitStatus SocketImpl::GetSocketOption(SocketOptionLevel level, SocketOptionName name, uint8_t *buffer, int32_t *length)
- {
- int32_t system_level = 0;
- int32_t system_name = 0;
-
- const int32_t o_res = level_and_name_to_system(level, name, &system_level, &system_name);
-
- if (o_res == SKIP_OPTION)
- {
- *((int32_t*)buffer) = 0;
- *length = sizeof(int32_t);
-
- return kWaitStatusSuccess;
- }
-
- if (o_res == INVALID_OPTION_NAME)
- {
- _saved_error = kWSAenoprotoopt;
-
- return kWaitStatusFailure;
- }
-
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- uint8_t *tmp_val = buffer;
-
- int32_t ret = -1;
-
- __try
- {
- ret = getsockopt(fd, system_level, system_name, (char*)tmp_val, (socklen_t*)length);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- }
-
- if (ret == -1)
- {
- StoreLastError();
-
- return kWaitStatusFailure;
- }
-
- if (system_name == SO_ERROR)
- {
- if (*((int32_t*)buffer) != 0)
- {
- StoreLastError(*((int32_t*)buffer));
- }
- else
- {
- *((int32_t*)buffer) = _saved_error;
- }
- }
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::GetSocketOptionFull(SocketOptionLevel level, SocketOptionName name, int32_t *first, int32_t *second)
- {
- int32_t system_level = 0;
- int32_t system_name = 0;
-
- #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
- if (level == kSocketOptionLevelSocket && name == kSocketOptionNameExclusiveAddressUse)
- {
- system_level = SOL_SOCKET;
- system_name = SO_REUSEADDR;
- }
- else
- #endif
- {
- const int32_t o_res = level_and_name_to_system(level, name, &system_level, &system_name);
-
- if (o_res == SKIP_OPTION)
- {
- *first = 0;
- *second = 0;
-
- return kWaitStatusSuccess;
- }
-
- if (o_res == INVALID_OPTION_NAME)
- {
- _saved_error = kWSAenoprotoopt;
-
- return kWaitStatusFailure;
- }
- }
-
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- int32_t ret = -1;
-
- switch (name)
- {
- case kSocketOptionNameLinger:
- {
- struct linger linger;
- socklen_t lingersize = sizeof(linger);
-
- __try
- {
- ret = getsockopt(fd, system_level, system_name, (char*)&linger, &lingersize);
-
- *first = linger.l_onoff;
- *second = linger.l_linger;
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- ret = -1;
- }
- }
- break;
-
- case kSocketOptionNameDontLinger:
- {
- struct linger linger;
- socklen_t lingersize = sizeof(linger);
-
- __try
- {
- ret = getsockopt(fd, system_level, system_name, (char*)&linger, &lingersize);
-
- *first = !linger.l_onoff;
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- ret = -1;
- }
- }
- break;
-
- case kSocketOptionNameSendTimeout:
- case kSocketOptionNameReceiveTimeout:
- {
- socklen_t time_ms_size = sizeof(*first);
-
- __try
- {
- ret = getsockopt(fd, system_level, system_name, (char*)first, &time_ms_size);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- ret = -1;
- }
- }
- break;
-
- default:
- {
- socklen_t valsize = sizeof(*first);
-
- __try
- {
- ret = getsockopt(fd, system_level, system_name, (char*)first, &valsize);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- ret = -1;
- }
- }
- break;
- }
-
- if (ret == -1)
- {
- StoreLastError();
-
- return kWaitStatusFailure;
- }
-
- #if !defined(SO_EXCLUSIVEADDRUSE) && defined(SO_REUSEADDR)
- if (level == kSocketOptionLevelSocket && name == kSocketOptionNameExclusiveAddressUse)
- *first = *first ? 0 : 1;
- #endif
-
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::Poll(std::vector<PollRequest> &requests, int32_t count, int32_t timeout, int32_t *result, int32_t *error)
- {
- const size_t nfds = (size_t)count;
- fd_set rfds, wfds, efds;
-
- FD_ZERO(&rfds);
- FD_ZERO(&wfds);
- FD_ZERO(&efds);
-
- for (size_t i = 0; i < nfds; i++)
- {
- SOCKET fd = static_cast<SOCKET>(requests[i].fd);
- requests[i].revents = kPollFlagsNone;
- if (fd == -1)
- continue;
-
- if ((requests[i].events & kPollFlagsIn) != 0)
- FD_SET(fd, &rfds);
-
- if ((requests[i].events & kPollFlagsOut) != 0)
- FD_SET(fd, &wfds);
-
- FD_SET(fd, &efds);
- }
-
- timeval timevalTimeout;
- timeval* timeoutPtr = NULL;
- if (timeout != -1)
- {
- timevalTimeout.tv_sec = timeout / 1000;
- timevalTimeout.tv_usec = (timeout % 1000) * 1000;
- timeoutPtr = &timevalTimeout;
- }
-
- int32_t affected = -1;
-
- __try
- {
- affected = select(0, &rfds, &wfds, &efds, timeoutPtr);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- }
-
- if (affected == -1)
- {
- *error = WSAGetLastError();
-
- // Mono does this as well and the threadpool-ms-io-poll code depends on this behavior
- if (*error == WSAENOTSOCK)
- {
- *error = os::kInvalidHandle;
- }
-
- return kWaitStatusFailure;
- }
-
- int32_t resultCount = 0;
- for (size_t i = 0; i < nfds && affected > 0; i++)
- {
- SOCKET fd = static_cast<SOCKET>(requests[i].fd);
- if (fd == -1)
- continue;
-
- if ((requests[i].events & kPollFlagsIn) != 0 && FD_ISSET(fd, &rfds))
- {
- requests[i].revents |= kPollFlagsIn;
- affected--;
- }
-
- if ((requests[i].events & kPollFlagsOut) != 0 && FD_ISSET(fd, &wfds))
- {
- requests[i].revents |= kPollFlagsOut;
- affected--;
- }
-
- if (FD_ISSET(fd, &efds))
- {
- requests[i].revents |= kPollFlagsErr;
- affected--;
- }
-
- if (requests[i].revents != kPollFlagsNone)
- resultCount++;
- }
-
- *result = resultCount;
- return kWaitStatusSuccess;
- }
-
- WaitStatus SocketImpl::Poll(std::vector<PollRequest>& requests, int32_t timeout, int32_t *result, int32_t *error)
- {
- return Poll(requests, (int32_t)requests.size(), timeout, result, error);
- }
-
- WaitStatus SocketImpl::Poll(PollRequest& request, int32_t timeout, int32_t *result, int32_t *error)
- {
- std::vector<PollRequest> requests;
- requests.push_back(request);
- return Poll(requests, 1, timeout, result, error);
- }
-
- WaitStatus SocketImpl::SetSocketOption(SocketOptionLevel level, SocketOptionName name, int32_t value)
- {
- int32_t system_level = 0;
- int32_t system_name = 0;
-
- const int32_t o_res = level_and_name_to_system(level, name, &system_level, &system_name);
-
- if (o_res == SKIP_OPTION)
- {
- return kWaitStatusSuccess;
- }
-
- if (o_res == INVALID_OPTION_NAME)
- {
- _saved_error = kWSAenoprotoopt;
-
- return kWaitStatusFailure;
- }
-
- struct linger linger;
-
- WaitStatus ret = kWaitStatusFailure;
-
- switch (name)
- {
- case kSocketOptionNameDontLinger:
- linger.l_onoff = !value;
- linger.l_linger = 0;
- ret = SetSocketOptionInternal(system_level, system_name, &linger, sizeof(linger));
- break;
-
- case kSocketOptionNameDontFragment:
- #ifdef IP_PMTUDISC_DO
- // Fiddle with the value slightly if we're turning DF on
- if (value == 1)
- value = IP_PMTUDISC_DO;
- #endif
- ret = SetSocketOptionInternal(system_level, system_name, (char*)&value, sizeof(value));
- break;
-
- default:
- ret = SetSocketOptionInternal(system_level, system_name, (char*)&value, sizeof(value));
- break;
- }
-
- return ret;
- }
-
- WaitStatus SocketImpl::SetSocketOptionLinger(SocketOptionLevel level, SocketOptionName name, bool enabled, int32_t seconds)
- {
- int32_t system_level = 0;
- int32_t system_name = 0;
-
- const int32_t o_res = level_and_name_to_system(level, name, &system_level, &system_name);
-
- if (o_res == SKIP_OPTION)
- {
- return kWaitStatusSuccess;
- }
-
- if (o_res == INVALID_OPTION_NAME)
- {
- _saved_error = kWSAenoprotoopt;
-
- return kWaitStatusFailure;
- }
-
- struct linger linger;
-
- linger.l_onoff = enabled;
- linger.l_linger = seconds;
-
- return SetSocketOptionInternal(system_level, system_name, &linger, sizeof(linger));
- }
-
- WaitStatus SocketImpl::SetSocketOptionArray(SocketOptionLevel level, SocketOptionName name, const uint8_t *buffer, int32_t length)
- {
- int32_t system_level = 0;
- int32_t system_name = 0;
-
- const int32_t o_res = level_and_name_to_system(level, name, &system_level, &system_name);
-
- if (o_res == SKIP_OPTION)
- {
- return kWaitStatusSuccess;
- }
-
- if (o_res == INVALID_OPTION_NAME)
- {
- _saved_error = kWSAenoprotoopt;
-
- return kWaitStatusFailure;
- }
-
- struct linger linger;
-
- WaitStatus ret = kWaitStatusFailure;
-
- switch (name)
- {
- case kSocketOptionNameDontLinger:
- if (length == 1)
- {
- linger.l_linger = 0;
- linger.l_onoff = (*((char*)buffer)) ? 0 : 1;
-
- ret = SetSocketOptionInternal(system_level, system_name, &linger, sizeof(linger));
- }
- else
- {
- _saved_error = kWSAeinval;
-
- return kWaitStatusFailure;
- }
- break;
-
- default:
- ret = SetSocketOptionInternal(system_level, system_name, buffer, length);
- break;
- }
-
- return ret;
- }
-
- WaitStatus SocketImpl::SetSocketOptionMembership(SocketOptionLevel level, SocketOptionName name, uint32_t group_address, uint32_t local_address)
- {
- int32_t system_level = 0;
- int32_t system_name = 0;
-
- const int32_t o_res = level_and_name_to_system(level, name, &system_level, &system_name);
-
- if (o_res == SKIP_OPTION)
- {
- return kWaitStatusSuccess;
- }
-
- if (o_res == INVALID_OPTION_NAME)
- {
- _saved_error = kWSAenoprotoopt;
-
- return kWaitStatusFailure;
- }
-
- IP_MREQ mreq = {{0}};
-
- mreq.imr_multiaddr.s_addr = group_address;
- mreq.imr_interface.s_addr = local_address;
-
- return SetSocketOptionInternal(system_level, system_name, &mreq, sizeof(mreq));
-
- return kWaitStatusFailure;
- }
-
- #if IL2CPP_SUPPORT_IPV6
- WaitStatus SocketImpl::SetSocketOptionMembership(SocketOptionLevel level, SocketOptionName name, IPv6Address ipv6, uint64_t interfaceOffset)
- {
- int32_t system_level = 0;
- int32_t system_name = 0;
-
- const int32_t o_res = level_and_name_to_system(level, name, &system_level, &system_name);
- if (o_res == SKIP_OPTION)
- {
- return kWaitStatusSuccess;
- }
-
- if (o_res == INVALID_OPTION_NAME)
- {
- _saved_error = kWSAenoprotoopt;
-
- return kWaitStatusFailure;
- }
-
- struct ipv6_mreq mreq6 = { { 0 } };
- struct in6_addr in6addr;
- for (int i = 0; i < 16; ++i)
- in6addr.s6_addr[i] = ipv6.addr[i];
- mreq6.ipv6mr_multiaddr = in6addr;
- mreq6.ipv6mr_interface = (ULONG)interfaceOffset;
-
- return SetSocketOptionInternal(system_level, system_name, &mreq6, sizeof(mreq6));
- }
-
- #endif
-
- WaitStatus SocketImpl::SetSocketOptionInternal(int32_t level, int32_t name, const void *value, int32_t len)
- {
- const void *real_val = value;
-
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- int32_t ret = -1;
-
- __try
- {
- ret = setsockopt(fd, level, name, (const char*)real_val, (socklen_t)len);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- }
-
- if (ret == -1)
- {
- StoreLastError();
-
- return kWaitStatusFailure;
- }
-
- return kWaitStatusSuccess;
- }
-
- int32_t WSAIoctlProtected(SOCKET s, DWORD dwIoControlCode, LPVOID lpvInBuffer, DWORD cbInBuffer, LPVOID lpvOutBuffer, DWORD cbOutBuffer, LPDWORD lpcbBytesReturned, LPWSAOVERLAPPED lpOverlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
- {
- __try
- {
- return WSAIoctl(s, dwIoControlCode, lpvInBuffer, cbInBuffer, lpvOutBuffer, cbOutBuffer, lpcbBytesReturned, lpOverlapped, lpCompletionRoutine);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- }
-
- return -1;
- }
-
- BOOL transmitFileProtected(LPFN_TRANSMITFILE transmitFile, SOCKET hSocket, HANDLE hFile, DWORD nNumberOfBytesToWrite, DWORD nNumberOfBytesPerSend, LPOVERLAPPED lpOverlapped, LPTRANSMIT_FILE_BUFFERS lpTransmitBuffers, DWORD dwReserved)
- {
- __try
- {
- return transmitFile(hSocket, hFile, nNumberOfBytesToWrite, nNumberOfBytesPerSend, lpOverlapped, lpTransmitBuffers, dwReserved);
- }
- __except (SocketExceptionFilter(GetExceptionCode()))
- {
- }
-
- return FALSE;
- }
-
- WaitStatus SocketImpl::SendFile(const char *filename, TransmitFileBuffers *buffers, TransmitFileOptions options)
- {
- if (filename == NULL)
- return kWaitStatusFailure;
-
- const UTF16String wideFilename = utils::StringUtils::Utf8ToUtf16(filename);
- HANDLE file = CreateFileW((LPCWSTR)wideFilename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
-
- if (file == INVALID_HANDLE_VALUE)
- {
- StoreLastError();
-
- return kWaitStatusFailure;
- }
-
- GUID transmitFileGuid = WSAID_TRANSMITFILE;
- LPFN_TRANSMITFILE transmitFile;
- DWORD copied;
-
- SOCKET fd = (SOCKET)_fd;
- if (fd == -1)
- {
- Error::SetLastError(il2cpp::os::kWSAeshutdown);
- return kWaitStatusFailure;
- }
-
- int32_t ret = WSAIoctlProtected(fd, SIO_GET_EXTENSION_FUNCTION_POINTER, &transmitFileGuid, sizeof(transmitFileGuid), &transmitFile, sizeof(transmitFile), &copied, 0, 0);
-
- if (ret == SOCKET_ERROR)
- {
- StoreLastError();
-
- return kWaitStatusFailure;
- }
-
- if (!transmitFileProtected(transmitFile, fd, file, 0, 0, NULL, (TRANSMIT_FILE_BUFFERS*)buffers, options))
- {
- StoreLastError();
-
- CloseHandle(file);
- return kWaitStatusFailure;
- }
-
- CloseHandle(file);
-
- return kWaitStatusSuccess;
- }
- }
- }
- #endif
|