Merge pull request #244 from plutooo/master

core: getaddrinfo() lookup error handling
This commit is contained in:
irungentoo 2013-08-01 12:21:45 -07:00
commit f6c510b3cc
6 changed files with 49 additions and 28 deletions

View File

@ -164,25 +164,42 @@ void shutdown_networking()
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(const char *address)
/*
resolve_addr():
address should represent IPv4 or a hostname with A record
returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
returns 0 on failure
TODO: Fix ipv6 support
*/
uint32_t resolve_addr(const 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;
struct addrinfo hints;
int rc;
uint32_t addr;
int success = getaddrinfo(address, "echo", &hints, &server);
if(success != 0)
return -1;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; // IPv4 only right now.
hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses.
rc = getaddrinfo(address, "echo", &hints, &server);
// Lookup failed.
if(rc != 0) {
return 0;
}
// IPv4 records only..
if(server->ai_family != AF_INET) {
freeaddrinfo(server);
return 0;
}
addr = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr;
int resolved = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr;
freeaddrinfo(server);
return resolved;
return addr;
}

View File

@ -116,12 +116,16 @@ int init_networking(IP ip, uint16_t port);
/* function to cleanup networking stuff(doesn't do much right now) */
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(const char *address);
/*
resolve_addr():
address should represent IPv4 or a hostname with A record
returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
returns 0 on failure
TODO: Fix ipv6 support
*/
uint32_t resolve_addr(const char *address);
#ifdef __cplusplus
}

View File

@ -245,7 +245,7 @@ struct server_conf_s configure_server(char *cfg_file)
it away in the server_info struct */
server_conf.info[i].valid = 1;
if(resolve_addr(strcpy(tmp_ip, bs_ip)) == -1) {
if(resolve_addr(strcpy(tmp_ip, bs_ip)) == 0) {
server_conf.info[i].valid = 0;
printf("bootstrap_server %d: Invalid IP\n", i);
}

View File

@ -423,7 +423,7 @@ int main(int argc, char *argv[])
IP_Port bootstrap_ip_port;
bootstrap_ip_port.port = htons(atoi(argv[2]));
int resolved_address = resolve_addr(argv[1]);
if (resolved_address != -1)
if (resolved_address != 0)
bootstrap_ip_port.ip.i = resolved_address;
else
exit(1);

View File

@ -346,7 +346,7 @@ int main(int argc, char *argv[])
IP_Port bootstrap_ip_port;
bootstrap_ip_port.port = htons(atoi(argv[2]));
int resolved_address = resolve_addr(argv[1]);
if (resolved_address != -1)
if (resolved_address != 0)
bootstrap_ip_port.ip.i = resolved_address;
else
exit(1);
@ -384,4 +384,4 @@ int main(int argc, char *argv[])
Sleep(1);
}
return 0;
}
}

View File

@ -83,8 +83,8 @@ static void execute(ToxWindow* self, char* cmd) {
dht.port = htons(atoi(port));
int resolved_address = resolve_addr(ip);
if (resolved_address == -1) {
uint32_t resolved_address = resolve_addr(ip);
if (resolved_address == 0) {
return;
}