mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
core: getaddrinfo() lookup error handling
This commit is contained in:
parent
d534a05264
commit
3d916b35f2
|
@ -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.
|
||||
|
||||
int resolved = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr;
|
||||
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 resolved;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
addr = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr;
|
||||
|
||||
freeaddrinfo(server);
|
||||
return addr;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user