diff options
Diffstat (limited to 'toxcore/tox.c')
-rw-r--r-- | toxcore/tox.c | 132 |
1 files changed, 86 insertions, 46 deletions
diff --git a/toxcore/tox.c b/toxcore/tox.c index 848f81c7..de615768 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c | |||
@@ -152,6 +152,7 @@ Tox *tox_new(const struct Tox_Options *options, const uint8_t *data, size_t leng | |||
152 | m_options.udp_disabled = !options->udp_enabled; | 152 | m_options.udp_disabled = !options->udp_enabled; |
153 | m_options.port_range[0] = options->start_port; | 153 | m_options.port_range[0] = options->start_port; |
154 | m_options.port_range[1] = options->end_port; | 154 | m_options.port_range[1] = options->end_port; |
155 | m_options.tcp_server_port = options->tcp_port; | ||
155 | 156 | ||
156 | switch (options->proxy_type) { | 157 | switch (options->proxy_type) { |
157 | case TOX_PROXY_TYPE_HTTP: | 158 | case TOX_PROXY_TYPE_HTTP: |
@@ -238,31 +239,6 @@ void tox_get_savedata(const Tox *tox, uint8_t *data) | |||
238 | } | 239 | } |
239 | } | 240 | } |
240 | 241 | ||
241 | static int address_to_ip(Messenger *m, const char *address, IP_Port *ip_port, IP_Port *ip_port_v4) | ||
242 | { | ||
243 | if (!addr_parse_ip(address, &ip_port->ip)) { | ||
244 | if (m->options.udp_disabled) { /* Disable DNS when udp is disabled. */ | ||
245 | return -1; | ||
246 | } | ||
247 | |||
248 | IP *ip_extra = NULL; | ||
249 | ip_init(&ip_port->ip, m->options.ipv6enabled); | ||
250 | |||
251 | if (m->options.ipv6enabled && ip_port_v4) { | ||
252 | /* setup for getting BOTH: an IPv6 AND an IPv4 address */ | ||
253 | ip_port->ip.family = AF_UNSPEC; | ||
254 | ip_reset(&ip_port_v4->ip); | ||
255 | ip_extra = &ip_port_v4->ip; | ||
256 | } | ||
257 | |||
258 | if (!addr_resolve(address, &ip_port->ip, ip_extra)) { | ||
259 | return -1; | ||
260 | } | ||
261 | } | ||
262 | |||
263 | return 0; | ||
264 | } | ||
265 | |||
266 | bool tox_bootstrap(Tox *tox, const char *address, uint16_t port, const uint8_t *public_key, TOX_ERR_BOOTSTRAP *error) | 242 | bool tox_bootstrap(Tox *tox, const char *address, uint16_t port, const uint8_t *public_key, TOX_ERR_BOOTSTRAP *error) |
267 | { | 243 | { |
268 | if (!address || !public_key) { | 244 | if (!address || !public_key) { |
@@ -270,23 +246,53 @@ bool tox_bootstrap(Tox *tox, const char *address, uint16_t port, const uint8_t * | |||
270 | return 0; | 246 | return 0; |
271 | } | 247 | } |
272 | 248 | ||
273 | Messenger *m = tox; | 249 | if (port == 0) { |
274 | bool ret = tox_add_tcp_relay(tox, address, port, public_key, error); | 250 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT); |
251 | return 0; | ||
252 | } | ||
275 | 253 | ||
276 | if (!ret) { | 254 | struct addrinfo *root, *info; |
255 | |||
256 | if (getaddrinfo(address, NULL, NULL, &root) != 0) { | ||
257 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); | ||
277 | return 0; | 258 | return 0; |
278 | } | 259 | } |
279 | 260 | ||
280 | if (m->options.udp_disabled) { | 261 | info = root; |
281 | return ret; | 262 | |
282 | } else { /* DHT only works on UDP. */ | 263 | unsigned int count = 0; |
283 | if (DHT_bootstrap_from_address(m->dht, address, m->options.ipv6enabled, htons(port), public_key) == 0) { | 264 | |
284 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); | 265 | do { |
285 | return 0; | 266 | IP_Port ip_port; |
267 | ip_port.port = htons(port); | ||
268 | ip_port.ip.family = info->ai_family; | ||
269 | |||
270 | if (info->ai_socktype && info->ai_socktype != SOCK_DGRAM) { | ||
271 | continue; | ||
272 | } | ||
273 | |||
274 | if (info->ai_family == AF_INET) { | ||
275 | ip_port.ip.ip4.in_addr = ((struct sockaddr_in *)info->ai_addr)->sin_addr; | ||
276 | } else if (info->ai_family == AF_INET6) { | ||
277 | ip_port.ip.ip6.in6_addr = ((struct sockaddr_in6 *)info->ai_addr)->sin6_addr; | ||
278 | } else { | ||
279 | continue; | ||
286 | } | 280 | } |
287 | 281 | ||
282 | Messenger *m = tox; | ||
283 | onion_add_bs_path_node(m->onion_c, ip_port, public_key); | ||
284 | DHT_bootstrap(m->dht, ip_port, public_key); | ||
285 | ++count; | ||
286 | } while ((info = info->ai_next)); | ||
287 | |||
288 | freeaddrinfo(root); | ||
289 | |||
290 | if (count) { | ||
288 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); | 291 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); |
289 | return 1; | 292 | return 1; |
293 | } else { | ||
294 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); | ||
295 | return 0; | ||
290 | } | 296 | } |
291 | } | 297 | } |
292 | 298 | ||
@@ -298,25 +304,53 @@ bool tox_add_tcp_relay(Tox *tox, const char *address, uint16_t port, const uint8 | |||
298 | return 0; | 304 | return 0; |
299 | } | 305 | } |
300 | 306 | ||
301 | Messenger *m = tox; | ||
302 | IP_Port ip_port, ip_port_v4; | ||
303 | |||
304 | if (port == 0) { | 307 | if (port == 0) { |
305 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT); | 308 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT); |
306 | return 0; | 309 | return 0; |
307 | } | 310 | } |
308 | 311 | ||
309 | if (address_to_ip(m, address, &ip_port, &ip_port_v4) == -1) { | 312 | struct addrinfo *root, *info; |
313 | |||
314 | if (getaddrinfo(address, NULL, NULL, &root) != 0) { | ||
310 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); | 315 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); |
311 | return 0; | 316 | return 0; |
312 | } | 317 | } |
313 | 318 | ||
314 | ip_port.port = htons(port); | 319 | info = root; |
315 | add_tcp_relay(m->net_crypto, ip_port, public_key); | ||
316 | onion_add_bs_path_node(m->onion_c, ip_port, public_key); //TODO: move this | ||
317 | 320 | ||
318 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); | 321 | unsigned int count = 0; |
319 | return 1; | 322 | |
323 | do { | ||
324 | IP_Port ip_port; | ||
325 | ip_port.port = htons(port); | ||
326 | ip_port.ip.family = info->ai_family; | ||
327 | |||
328 | if (info->ai_socktype && info->ai_socktype != SOCK_STREAM) { | ||
329 | continue; | ||
330 | } | ||
331 | |||
332 | if (info->ai_family == AF_INET) { | ||
333 | ip_port.ip.ip4.in_addr = ((struct sockaddr_in *)info->ai_addr)->sin_addr; | ||
334 | } else if (info->ai_family == AF_INET6) { | ||
335 | ip_port.ip.ip6.in6_addr = ((struct sockaddr_in6 *)info->ai_addr)->sin6_addr; | ||
336 | } else { | ||
337 | continue; | ||
338 | } | ||
339 | |||
340 | Messenger *m = tox; | ||
341 | add_tcp_relay(m->net_crypto, ip_port, public_key); | ||
342 | ++count; | ||
343 | } while ((info = info->ai_next)); | ||
344 | |||
345 | freeaddrinfo(root); | ||
346 | |||
347 | if (count) { | ||
348 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); | ||
349 | return 1; | ||
350 | } else { | ||
351 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); | ||
352 | return 0; | ||
353 | } | ||
320 | } | 354 | } |
321 | 355 | ||
322 | TOX_CONNECTION tox_self_get_connection_status(const Tox *tox) | 356 | TOX_CONNECTION tox_self_get_connection_status(const Tox *tox) |
@@ -1205,9 +1239,15 @@ uint16_t tox_self_get_udp_port(const Tox *tox, TOX_ERR_GET_PORT *error) | |||
1205 | 1239 | ||
1206 | uint16_t tox_self_get_tcp_port(const Tox *tox, TOX_ERR_GET_PORT *error) | 1240 | uint16_t tox_self_get_tcp_port(const Tox *tox, TOX_ERR_GET_PORT *error) |
1207 | { | 1241 | { |
1208 | /* TCP server not yet implemented in clients. */ | 1242 | const Messenger *m = tox; |
1209 | SET_ERROR_PARAMETER(error, TOX_ERR_GET_PORT_NOT_BOUND); | 1243 | |
1210 | return 0; | 1244 | if (m->tcp_server) { |
1245 | SET_ERROR_PARAMETER(error, TOX_ERR_GET_PORT_OK); | ||
1246 | return m->options.tcp_server_port; | ||
1247 | } else { | ||
1248 | SET_ERROR_PARAMETER(error, TOX_ERR_GET_PORT_NOT_BOUND); | ||
1249 | return 0; | ||
1250 | } | ||
1211 | } | 1251 | } |
1212 | 1252 | ||
1213 | #include "tox_old_code.h" | 1253 | #include "tox_old_code.h" |