Added address resolving function (Thank you stal).

This commit is contained in:
Maxim Biro 2013-07-20 17:31:36 -04:00
parent 292ccc54e0
commit e4469b5130
2 changed files with 35 additions and 1 deletions

View File

@ -159,3 +159,28 @@ void shutdown_networking()
#endif #endif
return; return;
} }
/* resolves provided address to a binary data in network byte order
address is ASCII null terminated string
address should represent IPv4, IPv6 or a hostname
on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
on failure returns -1 */
int resolve_addr(char *address)
{
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; //support both IPv4 and IPv6
hints.ai_socktype = SOCK_DGRAM; //type of socket Tox uses
struct addrinfo *server = NULL;
int success = getaddrinfo(address, "7", &hints, &server);
if(success != 0)
{
return -1;
}
int resolved = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr;
freeaddrinfo(server);
return resolved;
}

View File

@ -38,6 +38,7 @@
#include <winsock2.h> #include <winsock2.h>
#include <windows.h> #include <windows.h>
#include <wspiapi.h>
#undef VANILLA_NACL /* make sure on windows we use libsodium */ #undef VANILLA_NACL /* make sure on windows we use libsodium */
@ -48,7 +49,8 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <errno.h> #include <errno.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h>
#include <netdb.h>
#endif #endif
@ -121,4 +123,11 @@ int init_networking(IP ip ,uint16_t port);
/* function to cleanup networking stuff(doesn't do much right now) */ /* function to cleanup networking stuff(doesn't do much right now) */
void shutdown_networking(); void shutdown_networking();
/* resolves provided address to a binary data in network byte order
address is ASCII null terminated string
address should represent IPv4, IPv6 or a hostname
on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
on failure returns -1 */
int resolve_addr(char *address);
#endif #endif