summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/DHT.h4
-rw-r--r--toxcore/LAN_discovery.c48
-rw-r--r--toxcore/Messenger.c159
-rw-r--r--toxcore/ping.c2
-rw-r--r--toxcore/tox.c2
-rw-r--r--toxcore/tox.h4
6 files changed, 122 insertions, 97 deletions
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index 255074b0..ff20c892 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -169,12 +169,12 @@ void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key);
169 * if ipv6enabled is 0 (zero), the resolving sticks STRICTLY to IPv4 addresses 169 * if ipv6enabled is 0 (zero), the resolving sticks STRICTLY to IPv4 addresses
170 * if ipv6enabled is not 0 (zero), the resolving looks for IPv6 addresses first, 170 * if ipv6enabled is not 0 (zero), the resolving looks for IPv6 addresses first,
171 * then IPv4 addresses. 171 * then IPv4 addresses.
172 * 172 *
173 * returns 1 if the address could be converted into an IP address 173 * returns 1 if the address could be converted into an IP address
174 * returns 0 otherwise 174 * returns 0 otherwise
175 */ 175 */
176int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled, 176int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled,
177 uint16_t port, uint8_t *public_key); 177 uint16_t port, uint8_t *public_key);
178 178
179/* Add nodes to the toping list. 179/* Add nodes to the toping list.
180 * All nodes in this list are pinged every TIME_TOPING seconds 180 * All nodes in this list are pinged every TIME_TOPING seconds
diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c
index 92e42e0f..ed8dd229 100644
--- a/toxcore/LAN_discovery.c
+++ b/toxcore/LAN_discovery.c
@@ -35,7 +35,7 @@
35 * return higher than 0 on success. 35 * return higher than 0 on success.
36 * return 0 on error. 36 * return 0 on error.
37 */ 37 */
38static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t * data, uint16_t length) 38static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t *data, uint16_t length)
39{ 39{
40 /* Not sure how many platforms this will run on, 40 /* Not sure how many platforms this will run on,
41 * so it's wrapped in __linux for now. 41 * so it's wrapped in __linux for now.
@@ -63,16 +63,17 @@ static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t * d
63 } 63 }
64 64
65 for (i = 0; i < count; i++) { 65 for (i = 0; i < count; i++) {
66 if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) { 66 if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) {
67 return 1; 67 return 1;
68 } 68 }
69 69
70 /* Just to clarify where we're getting the values from. */ 70 /* Just to clarify where we're getting the values from. */
71 sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr; 71 sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr;
72 if (sock_holder != NULL) { 72
73 IP_Port ip_port = {{{{sock_holder->sin_addr.s_addr}}, port, 0}}; 73 if (sock_holder != NULL) {
74 sendpacket(net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); 74 IP_Port ip_port = {{{{sock_holder->sin_addr.s_addr}}, port, 0}};
75 } 75 sendpacket(net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES);
76 }
76 } 77 }
77 78
78 close(sock); 79 close(sock);
@@ -87,6 +88,7 @@ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast)
87 ip_reset(&ip); 88 ip_reset(&ip);
88 89
89#ifdef TOX_ENABLE_IPV6 90#ifdef TOX_ENABLE_IPV6
91
90 if (family_socket == AF_INET6) { 92 if (family_socket == AF_INET6) {
91 if (family_broadcast == AF_INET6) { 93 if (family_broadcast == AF_INET6) {
92 ip.family = AF_INET6; 94 ip.family = AF_INET6;
@@ -96,25 +98,26 @@ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast)
96 ip.ip6.s6_addr[ 0] = 0xFF; 98 ip.ip6.s6_addr[ 0] = 0xFF;
97 ip.ip6.s6_addr[ 1] = 0x02; 99 ip.ip6.s6_addr[ 1] = 0x02;
98 ip.ip6.s6_addr[15] = 0x01; 100 ip.ip6.s6_addr[15] = 0x01;
99 } 101 } else if (family_broadcast == AF_INET) {
100 else if (family_broadcast == AF_INET) {
101 ip.family = AF_INET6; 102 ip.family = AF_INET6;
102 ip.ip6.s6_addr32[0] = 0; 103 ip.ip6.s6_addr32[0] = 0;
103 ip.ip6.s6_addr32[1] = 0; 104 ip.ip6.s6_addr32[1] = 0;
104 ip.ip6.s6_addr32[2] = htonl(0xFFFF); 105 ip.ip6.s6_addr32[2] = htonl(0xFFFF);
105 ip.ip6.s6_addr32[3] = INADDR_BROADCAST; 106 ip.ip6.s6_addr32[3] = INADDR_BROADCAST;
106 } 107 }
107 } 108 } else if (family_socket == AF_INET) {
108 else if (family_socket == AF_INET) {
109 if (family_broadcast == AF_INET) { 109 if (family_broadcast == AF_INET) {
110 ip.family = AF_INET; 110 ip.family = AF_INET;
111 ip.ip4.uint32 = INADDR_BROADCAST; 111 ip.ip4.uint32 = INADDR_BROADCAST;
112 } 112 }
113 } 113 }
114
114#else 115#else
116
115 if (family_socket == AF_INET) 117 if (family_socket == AF_INET)
116 if (family_broadcast == AF_INET) 118 if (family_broadcast == AF_INET)
117 ip.uint32 = INADDR_BROADCAST; 119 ip.uint32 = INADDR_BROADCAST;
120
118#endif 121#endif
119 122
120 return ip; 123 return ip;
@@ -126,11 +129,13 @@ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast)
126static int LAN_ip(IP ip) 129static int LAN_ip(IP ip)
127{ 130{
128#ifdef TOX_ENABLE_IPV6 131#ifdef TOX_ENABLE_IPV6
132
129 if (ip.family == AF_INET) { 133 if (ip.family == AF_INET) {
130 IP4 ip4 = ip.ip4; 134 IP4 ip4 = ip.ip4;
131#else 135#else
132 IP4 ip4 = ip; 136 IP4 ip4 = ip;
133#endif 137#endif
138
134 /* Loopback. */ 139 /* Loopback. */
135 if (ip4.uint8[0] == 127) 140 if (ip4.uint8[0] == 127)
136 return 0; 141 return 0;
@@ -151,13 +156,14 @@ static int LAN_ip(IP ip)
151 if (ip4.uint8[0] == 169 && ip4.uint8[1] == 254 && ip4.uint8[2] != 0 156 if (ip4.uint8[0] == 169 && ip4.uint8[1] == 254 && ip4.uint8[2] != 0
152 && ip4.uint8[2] != 255) 157 && ip4.uint8[2] != 255)
153 return 0; 158 return 0;
159
154#ifdef TOX_ENABLE_IPV6 160#ifdef TOX_ENABLE_IPV6
155 } 161 } else if (ip.family == AF_INET6)
156 else if (ip.family == AF_INET6) { 162 {
157 /* autogenerated for each interface: FE80::* (up to FEBF::*) 163 /* autogenerated for each interface: FE80::* (up to FEBF::*)
158 /* FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ 164 /* FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */
159 if (((ip.ip6.s6_addr[0] == 0xFF) && (ip.ip6.s6_addr[1] < 3) && (ip.ip6.s6_addr[15] == 1)) || 165 if (((ip.ip6.s6_addr[0] == 0xFF) && (ip.ip6.s6_addr[1] < 3) && (ip.ip6.s6_addr[15] == 1)) ||
160 ((ip.ip6.s6_addr[0] == 0xFE) && ((ip.ip6.s6_addr[1] & 0xC0) == 0x80))) 166 ((ip.ip6.s6_addr[0] == 0xFE) && ((ip.ip6.s6_addr[1] & 0xC0) == 0x80)))
161 return 0; 167 return 0;
162 168
163 /* embedded IPv4-in-IPv6 */ 169 /* embedded IPv4-in-IPv6 */
@@ -168,6 +174,7 @@ static int LAN_ip(IP ip)
168 return LAN_ip(ip4); 174 return LAN_ip(ip4);
169 } 175 }
170 } 176 }
177
171#endif 178#endif
172 179
173 return -1; 180 return -1;
@@ -203,9 +210,11 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c)
203 ip_port.port = port; 210 ip_port.port = port;
204 211
205#ifdef TOX_ENABLE_IPV6 212#ifdef TOX_ENABLE_IPV6
213
206 /* IPv6 multicast */ 214 /* IPv6 multicast */
207 if (c->lossless_udp->net->family == AF_INET6) { 215 if (c->lossless_udp->net->family == AF_INET6) {
208 ip_port.ip = broadcast_ip(AF_INET6, AF_INET6); 216 ip_port.ip = broadcast_ip(AF_INET6, AF_INET6);
217
209 if (ip_isset(&ip_port.ip)) 218 if (ip_isset(&ip_port.ip))
210 if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES) > 0) 219 if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES) > 0)
211 res = 1; 220 res = 1;
@@ -216,6 +225,7 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c)
216#else 225#else
217 ip_port.ip = broadcast_ip(AF_INET, AF_INET); 226 ip_port.ip = broadcast_ip(AF_INET, AF_INET);
218#endif 227#endif
228
219 if (ip_isset(&ip_port.ip)) 229 if (ip_isset(&ip_port.ip))
220 if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES)) 230 if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES))
221 res = 1; 231 res = 1;
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 55b27353..eb18e3a3 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1225,11 +1225,13 @@ static time_t lastdump = 0;
1225static char IDString[CLIENT_ID_SIZE * 2 + 1]; 1225static char IDString[CLIENT_ID_SIZE * 2 + 1];
1226static char *ID2String(uint8_t *client_id) 1226static char *ID2String(uint8_t *client_id)
1227{ 1227{
1228 uint32_t i; 1228 uint32_t i;
1229 for(i = 0; i < CLIENT_ID_SIZE; i++) 1229
1230 sprintf(&IDString[i], "%02X", client_id[i]); 1230 for (i = 0; i < CLIENT_ID_SIZE; i++)
1231 IDString[CLIENT_ID_SIZE * 2] = 0; 1231 sprintf(&IDString[i], "%02X", client_id[i]);
1232 return IDString; 1232
1233 IDString[CLIENT_ID_SIZE * 2] = 0;
1234 return IDString;
1233} 1235}
1234#endif 1236#endif
1235 1237
@@ -1246,73 +1248,85 @@ void doMessenger(Messenger *m)
1246 LANdiscovery(m); 1248 LANdiscovery(m);
1247 1249
1248#ifdef LOGGING 1250#ifdef LOGGING
1249 if (now() > lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) { 1251
1250 loglog(" = = = = = = = = \n"); 1252 if (now() > lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) {
1251 1253 loglog(" = = = = = = = = \n");
1252 lastdump = now(); 1254
1253 uint32_t client, last_pinged; 1255 lastdump = now();
1254 for(client = 0; client < LCLIENT_LIST; client++) { 1256 uint32_t client, last_pinged;
1255 Client_data *cptr = &m->dht->close_clientlist[client]; 1257
1256 if (ip_isset(&cptr->ip_port.ip)) { 1258 for (client = 0; client < LCLIENT_LIST; client++) {
1257 last_pinged = lastdump - cptr->last_pinged; 1259 Client_data *cptr = &m->dht->close_clientlist[client];
1258 if (last_pinged > 999) 1260
1259 last_pinged = 999; 1261 if (ip_isset(&cptr->ip_port.ip)) {
1260 snprintf(logbuffer, sizeof(logbuffer), "C[%2u] %s:%u [%3u] %s\n", 1262 last_pinged = lastdump - cptr->last_pinged;
1261 client, ip_ntoa(&cptr->ip_port.ip), ntohs(cptr->ip_port.port), 1263
1262 last_pinged, ID2String(cptr->client_id)); 1264 if (last_pinged > 999)
1263 loglog(logbuffer); 1265 last_pinged = 999;
1264 } 1266
1265 } 1267 snprintf(logbuffer, sizeof(logbuffer), "C[%2u] %s:%u [%3u] %s\n",
1266 1268 client, ip_ntoa(&cptr->ip_port.ip), ntohs(cptr->ip_port.port),
1267 loglog(" = = = = = = = = \n"); 1269 last_pinged, ID2String(cptr->client_id));
1268 1270 loglog(logbuffer);
1269 uint32_t num_friends = MIN(m->numfriends, m->dht->num_friends); 1271 }
1270 if (m->numfriends != m->dht->num_friends) { 1272 }
1271 sprintf(logbuffer, "Friend num in DHT %u != friend num in msger %u\n", 1273
1272 m->dht->num_friends, m->numfriends); 1274 loglog(" = = = = = = = = \n");
1273 loglog(logbuffer); 1275
1274 } 1276 uint32_t num_friends = MIN(m->numfriends, m->dht->num_friends);
1275 1277
1276 uint32_t friend, ping_lastrecv; 1278 if (m->numfriends != m->dht->num_friends) {
1277 for(friend = 0; friend < num_friends; friend++) { 1279 sprintf(logbuffer, "Friend num in DHT %u != friend num in msger %u\n",
1278 Friend *msgfptr = &m->friendlist[friend]; 1280 m->dht->num_friends, m->numfriends);
1279 DHT_Friend *dhtfptr = &m->dht->friends_list[friend]; 1281 loglog(logbuffer);
1280 if (memcmp(msgfptr->client_id, dhtfptr->client_id, CLIENT_ID_SIZE)) { 1282 }
1281 if (sizeof(logbuffer) > 2 * CLIENT_ID_SIZE + 64) { 1283
1282 sprintf(logbuffer, "F[%2u] ID(m) %s != ID(d) ", friend, 1284 uint32_t friend, ping_lastrecv;
1283 ID2String(msgfptr->client_id)); 1285
1284 strcat(logbuffer + strlen(logbuffer), ID2String(dhtfptr->client_id)); 1286 for (friend = 0; friend < num_friends; friend++) {
1285 strcat(logbuffer + strlen(logbuffer), "\n"); 1287 Friend *msgfptr = &m->friendlist[friend];
1286 } 1288 DHT_Friend *dhtfptr = &m->dht->friends_list[friend];
1287 else 1289
1288 sprintf(logbuffer, "F[%2u] ID(m) != ID(d) ", friend); 1290 if (memcmp(msgfptr->client_id, dhtfptr->client_id, CLIENT_ID_SIZE)) {
1289 1291 if (sizeof(logbuffer) > 2 * CLIENT_ID_SIZE + 64) {
1290 loglog(logbuffer); 1292 sprintf(logbuffer, "F[%2u] ID(m) %s != ID(d) ", friend,
1291 } 1293 ID2String(msgfptr->client_id));
1292 1294 strcat(logbuffer + strlen(logbuffer), ID2String(dhtfptr->client_id));
1293 ping_lastrecv = lastdump - msgfptr->ping_lastrecv; 1295 strcat(logbuffer + strlen(logbuffer), "\n");
1294 if (ping_lastrecv > 999) 1296 } else
1295 ping_lastrecv = 999; 1297 sprintf(logbuffer, "F[%2u] ID(m) != ID(d) ", friend);
1296 snprintf(logbuffer, sizeof(logbuffer), "F[%2u] <%s> %02u [%03u] %s\n", 1298
1297 friend, msgfptr->name, msgfptr->crypt_connection_id, 1299 loglog(logbuffer);
1298 ping_lastrecv, ID2String(msgfptr->client_id)); 1300 }
1299 loglog(logbuffer); 1301
1300 1302 ping_lastrecv = lastdump - msgfptr->ping_lastrecv;
1301 for(client = 0; client < MAX_FRIEND_CLIENTS; client++) { 1303
1302 Client_data *cptr = &dhtfptr->client_list[client]; 1304 if (ping_lastrecv > 999)
1303 last_pinged = lastdump - cptr->last_pinged; 1305 ping_lastrecv = 999;
1304 if (last_pinged > 999) 1306
1305 last_pinged = 999; 1307 snprintf(logbuffer, sizeof(logbuffer), "F[%2u] <%s> %02u [%03u] %s\n",
1306 snprintf(logbuffer, sizeof(logbuffer), "F[%2u] => C[%2u] %s:%u [%3u] %s\n", 1308 friend, msgfptr->name, msgfptr->crypt_connection_id,
1307 friend, client, ip_ntoa(&cptr->ip_port.ip), 1309 ping_lastrecv, ID2String(msgfptr->client_id));
1308 ntohs(cptr->ip_port.port), last_pinged, 1310 loglog(logbuffer);
1309 ID2String(cptr->client_id)); 1311
1310 loglog(logbuffer); 1312 for (client = 0; client < MAX_FRIEND_CLIENTS; client++) {
1311 } 1313 Client_data *cptr = &dhtfptr->client_list[client];
1312 } 1314 last_pinged = lastdump - cptr->last_pinged;
1313 1315
1314 loglog(" = = = = = = = = \n"); 1316 if (last_pinged > 999)
1315 } 1317 last_pinged = 999;
1318
1319 snprintf(logbuffer, sizeof(logbuffer), "F[%2u] => C[%2u] %s:%u [%3u] %s\n",
1320 friend, client, ip_ntoa(&cptr->ip_port.ip),
1321 ntohs(cptr->ip_port.port), last_pinged,
1322 ID2String(cptr->client_id));
1323 loglog(logbuffer);
1324 }
1325 }
1326
1327 loglog(" = = = = = = = = \n");
1328 }
1329
1316#endif 1330#endif
1317} 1331}
1318 1332
@@ -1410,6 +1424,7 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
1410 memcpy(temp, data, size); 1424 memcpy(temp, data, size);
1411 1425
1412 uint32_t i; 1426 uint32_t i;
1427
1413 for (i = 0; i < num; ++i) { 1428 for (i = 0; i < num; ++i) {
1414 if (temp[i].status >= 3) { 1429 if (temp[i].status >= 3) {
1415 int fnum = m_addfriend_norequest(m, temp[i].client_id); 1430 int fnum = m_addfriend_norequest(m, temp[i].client_id);
diff --git a/toxcore/ping.c b/toxcore/ping.c
index 113702bf..56ce2f59 100644
--- a/toxcore/ping.c
+++ b/toxcore/ping.c
@@ -113,7 +113,7 @@ bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: Repl
113 113
114 /* ping_id = 0 means match any id. */ 114 /* ping_id = 0 means match any id. */
115 if ((!ip_isset(&ipp.ip) || ipport_equal(&png->pings[id].ipp, &ipp)) && 115 if ((!ip_isset(&ipp.ip) || ipport_equal(&png->pings[id].ipp, &ipp)) &&
116 (png->pings[id].id == ping_id || ping_id == 0)) { 116 (png->pings[id].id == ping_id || ping_id == 0)) {
117 return true; 117 return true;
118 } 118 }
119 } 119 }
diff --git a/toxcore/tox.c b/toxcore/tox.c
index 68b1e6e9..80d64626 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -450,7 +450,7 @@ void tox_bootstrap_from_ip(void *tox, IP_Port ip_port, uint8_t *public_key)
450 DHT_bootstrap(m->dht, ip_port, public_key); 450 DHT_bootstrap(m->dht, ip_port, public_key);
451} 451}
452int tox_bootstrap_from_address(void *tox, const char *address, 452int tox_bootstrap_from_address(void *tox, const char *address,
453 uint8_t ipv6enabled, uint16_t port, uint8_t *public_key) 453 uint8_t ipv6enabled, uint16_t port, uint8_t *public_key)
454{ 454{
455 Messenger *m = tox; 455 Messenger *m = tox;
456 return DHT_bootstrap_from_address(m->dht, address, ipv6enabled, port, public_key); 456 return DHT_bootstrap_from_address(m->dht, address, ipv6enabled, port, public_key);
diff --git a/toxcore/tox.h b/toxcore/tox.h
index c52a644f..a15d7ea0 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -409,12 +409,12 @@ void tox_bootstrap_from_ip(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key);
409 * if ipv6enabled is 0 (zero), the resolving sticks STRICTLY to IPv4 addresses 409 * if ipv6enabled is 0 (zero), the resolving sticks STRICTLY to IPv4 addresses
410 * if ipv6enabled is not 0 (zero), the resolving looks for IPv6 addresses first, 410 * if ipv6enabled is not 0 (zero), the resolving looks for IPv6 addresses first,
411 * then IPv4 addresses. 411 * then IPv4 addresses.
412 * 412 *
413 * returns 1 if the address could be converted into an IP address 413 * returns 1 if the address could be converted into an IP address
414 * returns 0 otherwise 414 * returns 0 otherwise
415 */ 415 */
416int tox_bootstrap_from_address(Tox *tox, const char *address, uint8_t ipv6enabled, 416int tox_bootstrap_from_address(Tox *tox, const char *address, uint8_t ipv6enabled,
417 uint16_t port, uint8_t *public_key); 417 uint16_t port, uint8_t *public_key);
418 418
419/* return 0 if we are not connected to the DHT. 419/* return 0 if we are not connected to the DHT.
420 * return 1 if we are. 420 * return 1 if we are.