Skip to content

Commit

Permalink
try using getaddrinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidan63 committed Jun 16, 2024
1 parent 5137631 commit 2c28157
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions src/hx/libs/std/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,32 +353,26 @@ Array<unsigned char> _hx_std_socket_read( Dynamic o )
**/
int _hx_std_host_resolve( String host )
{
unsigned int ip;
hx::EnterGCFreeZone();

hx::EnterGCFreeZone();
hx::strbuf buf;
ip = inet_addr(host.utf8_str(&buf));
if( ip == INADDR_NONE )
{
struct hostent *h = 0;
hx::strbuf hostBuf;

# if defined(NEKO_WINDOWS) || defined(NEKO_MAC) || defined(BLACKBERRY) || defined(EMSCRIPTEN)
h = gethostbyname(host.utf8_str(&hostBuf));
# else
struct hostent hbase;
char buf[1024];
int errcode;
gethostbyname_r(host.utf8_str(&hostBuf),&hbase,buf,1024,&h,&errcode);
# endif
if( !h ) {
hx::ExitGCFreeZone();
return hx::Throw( HX_CSTRING("Unknown host:") + host );
}
ip = *((unsigned int*)h->h_addr);
}
hx::ExitGCFreeZone();
return ip;
auto result = (addrinfo*)nullptr;
auto hints = addrinfo();
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;

if (0 != getaddrinfo(host.utf8_str(), nullptr, &hints, &result))
{
hx::ExitGCFreeZone();

return hx::Throw(HX_CSTRING("Unknown host:") + host);
}

auto ipv4 = reinterpret_cast<sockaddr_in*>(result->ai_addr);
auto ip = ipv4->sin_addr.S_un;

hx::ExitGCFreeZone();

return ip.S_addr;
}

#ifdef DYNAMIC_INET_FUNCS
Expand Down

0 comments on commit 2c28157

Please sign in to comment.