diff options
author | irungentoo <irungentoo@gmail.com> | 2013-08-01 12:21:45 -0700 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-08-01 12:21:45 -0700 |
commit | f6c510b3ccd3b5850b714831c61d96baa58da4b5 (patch) | |
tree | 216b29a02fe7a7bf1e63455fcb84552157e933d0 | |
parent | 75daa1aec9c908205ebc8e085b4c5cdb048b6017 (diff) | |
parent | 3d916b35f2dadd17a7c9f5acd08ef396b8c8263c (diff) |
Merge pull request #244 from plutooo/master
core: getaddrinfo() lookup error handling
-rw-r--r-- | core/network.c | 47 | ||||
-rw-r--r-- | core/network.h | 16 | ||||
-rw-r--r-- | other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c | 2 | ||||
-rw-r--r-- | testing/nTox.c | 2 | ||||
-rw-r--r-- | testing/nTox_win32.c | 4 | ||||
-rw-r--r-- | testing/toxic/prompt.c | 4 |
6 files changed, 48 insertions, 27 deletions
diff --git a/core/network.c b/core/network.c index a7a4efcd..c58549bf 100644 --- a/core/network.c +++ b/core/network.c | |||
@@ -164,25 +164,42 @@ void shutdown_networking() | |||
164 | return; | 164 | return; |
165 | } | 165 | } |
166 | 166 | ||
167 | /* resolves provided address to a binary data in network byte order | 167 | /* |
168 | address is ASCII null terminated string | 168 | resolve_addr(): |
169 | address should represent IPv4, IPv6 or a hostname | 169 | address should represent IPv4 or a hostname with A record |
170 | on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i | 170 | |
171 | on failure returns -1 */ | 171 | returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i |
172 | int resolve_addr(const char *address) | 172 | returns 0 on failure |
173 | |||
174 | TODO: Fix ipv6 support | ||
175 | */ | ||
176 | uint32_t resolve_addr(const char *address) | ||
173 | { | 177 | { |
174 | struct addrinfo hints; | 178 | struct addrinfo *server = NULL; |
179 | struct addrinfo hints; | ||
180 | int rc; | ||
181 | uint32_t addr; | ||
182 | |||
175 | memset(&hints, 0, sizeof(hints)); | 183 | memset(&hints, 0, sizeof(hints)); |
176 | hints.ai_family = AF_UNSPEC; //support both IPv4 and IPv6 | 184 | hints.ai_family = AF_INET; // IPv4 only right now. |
177 | hints.ai_socktype = SOCK_DGRAM; //type of socket Tox uses | 185 | hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. |
178 | 186 | ||
179 | struct addrinfo *server = NULL; | 187 | rc = getaddrinfo(address, "echo", &hints, &server); |
180 | 188 | ||
181 | int success = getaddrinfo(address, "echo", &hints, &server); | 189 | // Lookup failed. |
182 | if(success != 0) | 190 | if(rc != 0) { |
183 | return -1; | 191 | return 0; |
192 | } | ||
193 | |||
194 | // IPv4 records only.. | ||
195 | if(server->ai_family != AF_INET) { | ||
196 | freeaddrinfo(server); | ||
197 | return 0; | ||
198 | } | ||
199 | |||
200 | |||
201 | addr = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr; | ||
184 | 202 | ||
185 | int resolved = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr; | ||
186 | freeaddrinfo(server); | 203 | freeaddrinfo(server); |
187 | return resolved; | 204 | return addr; |
188 | } | 205 | } |
diff --git a/core/network.h b/core/network.h index 3277070c..a5f7899b 100644 --- a/core/network.h +++ b/core/network.h | |||
@@ -116,12 +116,16 @@ int init_networking(IP ip, uint16_t port); | |||
116 | /* function to cleanup networking stuff(doesn't do much right now) */ | 116 | /* function to cleanup networking stuff(doesn't do much right now) */ |
117 | void shutdown_networking(); | 117 | void shutdown_networking(); |
118 | 118 | ||
119 | /* resolves provided address to a binary data in network byte order | 119 | /* |
120 | address is ASCII null terminated string | 120 | resolve_addr(): |
121 | address should represent IPv4, IPv6 or a hostname | 121 | address should represent IPv4 or a hostname with A record |
122 | on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i | 122 | |
123 | on failure returns -1 */ | 123 | returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i |
124 | int resolve_addr(const char *address); | 124 | returns 0 on failure |
125 | |||
126 | TODO: Fix ipv6 support | ||
127 | */ | ||
128 | uint32_t resolve_addr(const char *address); | ||
125 | 129 | ||
126 | #ifdef __cplusplus | 130 | #ifdef __cplusplus |
127 | } | 131 | } |
diff --git a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c index 4f28fb3c..48152744 100644 --- a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c +++ b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c | |||
@@ -245,7 +245,7 @@ struct server_conf_s configure_server(char *cfg_file) | |||
245 | it away in the server_info struct */ | 245 | it away in the server_info struct */ |
246 | server_conf.info[i].valid = 1; | 246 | server_conf.info[i].valid = 1; |
247 | 247 | ||
248 | if(resolve_addr(strcpy(tmp_ip, bs_ip)) == -1) { | 248 | if(resolve_addr(strcpy(tmp_ip, bs_ip)) == 0) { |
249 | server_conf.info[i].valid = 0; | 249 | server_conf.info[i].valid = 0; |
250 | printf("bootstrap_server %d: Invalid IP\n", i); | 250 | printf("bootstrap_server %d: Invalid IP\n", i); |
251 | } | 251 | } |
diff --git a/testing/nTox.c b/testing/nTox.c index 17f4b0e7..f87a2a25 100644 --- a/testing/nTox.c +++ b/testing/nTox.c | |||
@@ -423,7 +423,7 @@ int main(int argc, char *argv[]) | |||
423 | IP_Port bootstrap_ip_port; | 423 | IP_Port bootstrap_ip_port; |
424 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 424 | bootstrap_ip_port.port = htons(atoi(argv[2])); |
425 | int resolved_address = resolve_addr(argv[1]); | 425 | int resolved_address = resolve_addr(argv[1]); |
426 | if (resolved_address != -1) | 426 | if (resolved_address != 0) |
427 | bootstrap_ip_port.ip.i = resolved_address; | 427 | bootstrap_ip_port.ip.i = resolved_address; |
428 | else | 428 | else |
429 | exit(1); | 429 | exit(1); |
diff --git a/testing/nTox_win32.c b/testing/nTox_win32.c index b9208d4f..2394877f 100644 --- a/testing/nTox_win32.c +++ b/testing/nTox_win32.c | |||
@@ -346,7 +346,7 @@ int main(int argc, char *argv[]) | |||
346 | IP_Port bootstrap_ip_port; | 346 | IP_Port bootstrap_ip_port; |
347 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 347 | bootstrap_ip_port.port = htons(atoi(argv[2])); |
348 | int resolved_address = resolve_addr(argv[1]); | 348 | int resolved_address = resolve_addr(argv[1]); |
349 | if (resolved_address != -1) | 349 | if (resolved_address != 0) |
350 | bootstrap_ip_port.ip.i = resolved_address; | 350 | bootstrap_ip_port.ip.i = resolved_address; |
351 | else | 351 | else |
352 | exit(1); | 352 | exit(1); |
@@ -384,4 +384,4 @@ int main(int argc, char *argv[]) | |||
384 | Sleep(1); | 384 | Sleep(1); |
385 | } | 385 | } |
386 | return 0; | 386 | return 0; |
387 | } \ No newline at end of file | 387 | } |
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index 3c4a27dd..a3cf2d94 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c | |||
@@ -83,8 +83,8 @@ static void execute(ToxWindow* self, char* cmd) { | |||
83 | 83 | ||
84 | dht.port = htons(atoi(port)); | 84 | dht.port = htons(atoi(port)); |
85 | 85 | ||
86 | int resolved_address = resolve_addr(ip); | 86 | uint32_t resolved_address = resolve_addr(ip); |
87 | if (resolved_address == -1) { | 87 | if (resolved_address == 0) { |
88 | return; | 88 | return; |
89 | } | 89 | } |
90 | 90 | ||