summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--testing/dns3_test.c1
-rw-r--r--toxcore/network.c55
-rw-r--r--toxcore/network.h6
-rw-r--r--toxcore/tox.c6
4 files changed, 42 insertions, 26 deletions
diff --git a/testing/dns3_test.c b/testing/dns3_test.c
index 37bbfadb..821f3b4b 100644
--- a/testing/dns3_test.c
+++ b/testing/dns3_test.c
@@ -92,6 +92,7 @@ int main(int argc, char *argv[])
92 } 92 }
93 93
94 unsigned int i; 94 unsigned int i;
95
95 for (i = r_len - 1; i != 0 && buffer[i] != '='; --i) { 96 for (i = r_len - 1; i != 0 && buffer[i] != '='; --i) {
96 ; 97 ;
97 } 98 }
diff --git a/toxcore/network.c b/toxcore/network.c
index 92a04276..69dacee4 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -1209,16 +1209,19 @@ int net_connect(Socket sock, IP_Port ip_port)
1209 return connect(sock, (struct sockaddr *)&addr, addrsize); 1209 return connect(sock, (struct sockaddr *)&addr, addrsize);
1210} 1210}
1211 1211
1212int32_t net_getipport(const char* node, IP_Port** res, int type) 1212int32_t net_getipport(const char *node, IP_Port **res, int type)
1213{ 1213{
1214 struct addrinfo *infos; 1214 struct addrinfo *infos;
1215 int ret = getaddrinfo(node, NULL, NULL, &infos); 1215 int ret = getaddrinfo(node, NULL, NULL, &infos);
1216
1216 if (ret != 0) { 1217 if (ret != 0) {
1217 return -1; 1218 return -1;
1218 } 1219 }
1219 1220
1220 struct addrinfo *cur; 1221 struct addrinfo *cur;
1222
1221 int count = 0; 1223 int count = 0;
1224
1222 for (cur = infos; count < INT32_MAX && cur != NULL; cur = cur->ai_next) { 1225 for (cur = infos; count < INT32_MAX && cur != NULL; cur = cur->ai_next) {
1223 if (cur->ai_socktype && type > 0 && cur->ai_socktype != type) { 1226 if (cur->ai_socktype && type > 0 && cur->ai_socktype != type) {
1224 continue; 1227 continue;
@@ -1235,12 +1238,14 @@ int32_t net_getipport(const char* node, IP_Port** res, int type)
1235 return -1; 1238 return -1;
1236 } 1239 }
1237 1240
1238 *res = (IP_Port*)malloc(sizeof(IP_Port) * count); 1241 *res = (IP_Port *)malloc(sizeof(IP_Port) * count);
1242
1239 if (*res == NULL) { 1243 if (*res == NULL) {
1240 return -1; 1244 return -1;
1241 } 1245 }
1242 1246
1243 IP_Port *ip_port = *res; 1247 IP_Port *ip_port = *res;
1248
1244 for (cur = infos; cur != NULL; cur = cur->ai_next) { 1249 for (cur = infos; cur != NULL; cur = cur->ai_next) {
1245 if (cur->ai_socktype && type > 0 && cur->ai_socktype != type) { 1250 if (cur->ai_socktype && type > 0 && cur->ai_socktype != type) {
1246 continue; 1251 continue;
@@ -1266,7 +1271,7 @@ int32_t net_getipport(const char* node, IP_Port** res, int type)
1266 return count; 1271 return count;
1267} 1272}
1268 1273
1269void net_freeipport(IP_Port* ip_ports) 1274void net_freeipport(IP_Port *ip_ports)
1270{ 1275{
1271 free(ip_ports); 1276 free(ip_ports);
1272} 1277}
@@ -1301,36 +1306,42 @@ int bind_to_port(Socket sock, int family, uint16_t port)
1301static int make_family(int family) 1306static int make_family(int family)
1302{ 1307{
1303 switch (family) { 1308 switch (family) {
1304 case TOX_AF_INET: 1309 case TOX_AF_INET:
1305 return AF_INET; 1310 return AF_INET;
1306 case TOX_AF_INET6: 1311
1307 return AF_INET6; 1312 case TOX_AF_INET6:
1308 default: 1313 return AF_INET6;
1309 return family; 1314
1315 default:
1316 return family;
1310 } 1317 }
1311} 1318}
1312 1319
1313static int make_socktype(int type) 1320static int make_socktype(int type)
1314{ 1321{
1315 switch (type) { 1322 switch (type) {
1316 case TOX_SOCK_STREAM: 1323 case TOX_SOCK_STREAM:
1317 return SOCK_STREAM; 1324 return SOCK_STREAM;
1318 case TOX_SOCK_DGRAM: 1325
1319 return SOCK_DGRAM; 1326 case TOX_SOCK_DGRAM:
1320 default: 1327 return SOCK_DGRAM;
1321 return type; 1328
1329 default:
1330 return type;
1322 } 1331 }
1323} 1332}
1324 1333
1325static int make_proto(int proto) 1334static int make_proto(int proto)
1326{ 1335{
1327 switch (proto) { 1336 switch (proto) {
1328 case TOX_PROTO_TCP: 1337 case TOX_PROTO_TCP:
1329 return IPPROTO_TCP; 1338 return IPPROTO_TCP;
1330 case TOX_PROTO_UDP: 1339
1331 return IPPROTO_UDP; 1340 case TOX_PROTO_UDP:
1332 default: 1341 return IPPROTO_UDP;
1333 return proto; 1342
1343 default:
1344 return proto;
1334 } 1345 }
1335} 1346}
1336 1347
@@ -1345,7 +1356,7 @@ Socket net_socket(int domain, int type, int protocol)
1345/* TODO: Remove, when tox DNS support will be removed. 1356/* TODO: Remove, when tox DNS support will be removed.
1346 * Used only by dns3_test.c 1357 * Used only by dns3_test.c
1347 */ 1358 */
1348size_t net_sendto_ip4(Socket sock, const char* buf, size_t n, IP_Port ip_port) 1359size_t net_sendto_ip4(Socket sock, const char *buf, size_t n, IP_Port ip_port)
1349{ 1360{
1350 struct sockaddr_in target; 1361 struct sockaddr_in target;
1351 size_t addrsize = sizeof(target); 1362 size_t addrsize = sizeof(target);
diff --git a/toxcore/network.h b/toxcore/network.h
index e6d7bbef..92e5bbcf 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -391,18 +391,18 @@ int net_connect(Socket sock, IP_Port ip_port);
391 * 391 *
392 * return number of elements in res array. 392 * return number of elements in res array.
393 */ 393 */
394int32_t net_getipport(const char* node, IP_Port** res, int type); 394int32_t net_getipport(const char *node, IP_Port **res, int type);
395 395
396/* Deallocates memory allocated by net_getipport 396/* Deallocates memory allocated by net_getipport
397 */ 397 */
398void net_freeipport(IP_Port* ip_ports); 398void net_freeipport(IP_Port *ip_ports);
399 399
400/* return 1 on success 400/* return 1 on success
401 * return 0 on failure 401 * return 0 on failure
402 */ 402 */
403int bind_to_port(Socket sock, int family, uint16_t port); 403int bind_to_port(Socket sock, int family, uint16_t port);
404 404
405size_t net_sendto_ip4(Socket sock, const char* buf, size_t n, IP_Port ip_port); 405size_t net_sendto_ip4(Socket sock, const char *buf, size_t n, IP_Port ip_port);
406 406
407/* Initialize networking. 407/* Initialize networking.
408 * bind to ip and port. 408 * bind to ip and port.
diff --git a/toxcore/tox.c b/toxcore/tox.c
index ed741995..fe34c284 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -236,12 +236,14 @@ bool tox_bootstrap(Tox *tox, const char *address, uint16_t port, const uint8_t *
236 IP_Port *root; 236 IP_Port *root;
237 237
238 int32_t count = net_getipport(address, &root, SOCK_DGRAM); 238 int32_t count = net_getipport(address, &root, SOCK_DGRAM);
239
239 if (count == -1) { 240 if (count == -1) {
240 SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); 241 SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST);
241 return 0; 242 return 0;
242 } 243 }
243 244
244 unsigned int i; 245 unsigned int i;
246
245 for (i = 0; i < count; i++) { 247 for (i = 0; i < count; i++) {
246 root[i].port = htons(port); 248 root[i].port = htons(port);
247 249
@@ -277,12 +279,14 @@ bool tox_add_tcp_relay(Tox *tox, const char *address, uint16_t port, const uint8
277 IP_Port *root; 279 IP_Port *root;
278 280
279 int32_t count = net_getipport(address, &root, SOCK_STREAM); 281 int32_t count = net_getipport(address, &root, SOCK_STREAM);
282
280 if (count == -1) { 283 if (count == -1) {
281 SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); 284 SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST);
282 return 0; 285 return 0;
283 } 286 }
284 287
285 unsigned int i; 288 unsigned int i;
289
286 for (i = 0; i < count; i++) { 290 for (i = 0; i < count; i++) {
287 root[i].port = htons(port); 291 root[i].port = htons(port);
288 292
@@ -1513,7 +1517,7 @@ void tox_self_get_dht_id(const Tox *tox, uint8_t *dht_id)
1513{ 1517{
1514 if (dht_id) { 1518 if (dht_id) {
1515 const Messenger *m = tox; 1519 const Messenger *m = tox;
1516 memcpy(dht_id , m->dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE); 1520 memcpy(dht_id, m->dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE);
1517 } 1521 }
1518} 1522}
1519 1523