summaryrefslogtreecommitdiff
path: root/toxcore/Messenger.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/Messenger.c')
-rw-r--r--toxcore/Messenger.c181
1 files changed, 146 insertions, 35 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 2fbb1d3f..5736c4e5 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -26,6 +26,7 @@
26#endif 26#endif
27 27
28#include "Messenger.h" 28#include "Messenger.h"
29#include "util.h"
29 30
30#define MIN(a,b) (((a)<(b))?(a):(b)) 31#define MIN(a,b) (((a)<(b))?(a):(b))
31 32
@@ -901,19 +902,17 @@ static void do_allgroupchats(Messenger *m)
901 902
902/*********************************/ 903/*********************************/
903 904
904#define PORT 33445
905
906/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */ 905/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */
907static void LANdiscovery(Messenger *m) 906static void LANdiscovery(Messenger *m)
908{ 907{
909 if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { 908 if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) {
910 send_LANdiscovery(htons(PORT), m->net_crypto); 909 send_LANdiscovery(htons(TOX_PORT_DEFAULT), m->net_crypto);
911 m->last_LANdiscovery = unix_time(); 910 m->last_LANdiscovery = unix_time();
912 } 911 }
913} 912}
914 913
915/* Run this at startup. */ 914/* Run this at startup. */
916Messenger *initMessenger(void) 915Messenger *initMessenger(uint8_t ipv6enabled)
917{ 916{
918 Messenger *m = calloc(1, sizeof(Messenger)); 917 Messenger *m = calloc(1, sizeof(Messenger));
919 918
@@ -921,8 +920,8 @@ Messenger *initMessenger(void)
921 return NULL; 920 return NULL;
922 921
923 IP ip; 922 IP ip;
924 ip.uint32 = 0; 923 ip_init(&ip, ipv6enabled);
925 m->net = new_networking(ip, PORT); 924 m->net = new_networking(ip, TOX_PORT_DEFAULT);
926 925
927 if (m->net == NULL) { 926 if (m->net == NULL) {
928 free(m); 927 free(m);
@@ -1006,11 +1005,12 @@ void doFriends(Messenger *m)
1006 } 1005 }
1007 } 1006 }
1008 1007
1009 IP_Port friendip = DHT_getfriendip(m->dht, m->friendlist[i].client_id); 1008 IP_Port friendip;
1009 int friendok = DHT_getfriendip(m->dht, m->friendlist[i].client_id, &friendip);
1010 1010
1011 switch (is_cryptoconnected(m->net_crypto, m->friendlist[i].crypt_connection_id)) { 1011 switch (is_cryptoconnected(m->net_crypto, m->friendlist[i].crypt_connection_id)) {
1012 case 0: 1012 case 0:
1013 if (friendip.ip.uint32 > 1) 1013 if (friendok == 1)
1014 m->friendlist[i].crypt_connection_id = crypto_connect(m->net_crypto, m->friendlist[i].client_id, friendip); 1014 m->friendlist[i].crypt_connection_id = crypto_connect(m->net_crypto, m->friendlist[i].client_id, friendip);
1015 1015
1016 break; 1016 break;
@@ -1219,6 +1219,22 @@ void doInbound(Messenger *m)
1219 } 1219 }
1220} 1220}
1221 1221
1222#ifdef LOGGING
1223#define DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS 60
1224static time_t lastdump = 0;
1225static char IDString[CLIENT_ID_SIZE * 2 + 1];
1226static char *ID2String(uint8_t *client_id)
1227{
1228 uint32_t i;
1229
1230 for (i = 0; i < CLIENT_ID_SIZE; i++)
1231 sprintf(&IDString[i], "%02X", client_id[i]);
1232
1233 IDString[CLIENT_ID_SIZE * 2] = 0;
1234 return IDString;
1235}
1236#endif
1237
1222/* The main loop that needs to be run at least 20 times per second. */ 1238/* The main loop that needs to be run at least 20 times per second. */
1223void doMessenger(Messenger *m) 1239void doMessenger(Messenger *m)
1224{ 1240{
@@ -1230,6 +1246,88 @@ void doMessenger(Messenger *m)
1230 doFriends(m); 1246 doFriends(m);
1231 do_allgroupchats(m); 1247 do_allgroupchats(m);
1232 LANdiscovery(m); 1248 LANdiscovery(m);
1249
1250#ifdef LOGGING
1251
1252 if (now() > lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) {
1253 loglog(" = = = = = = = = \n");
1254
1255 lastdump = now();
1256 uint32_t client, last_pinged;
1257
1258 for (client = 0; client < LCLIENT_LIST; client++) {
1259 Client_data *cptr = &m->dht->close_clientlist[client];
1260
1261 if (ip_isset(&cptr->ip_port.ip)) {
1262 last_pinged = lastdump - cptr->last_pinged;
1263
1264 if (last_pinged > 999)
1265 last_pinged = 999;
1266
1267 snprintf(logbuffer, sizeof(logbuffer), "C[%2u] %s:%u [%3u] %s\n",
1268 client, ip_ntoa(&cptr->ip_port.ip), ntohs(cptr->ip_port.port),
1269 last_pinged, ID2String(cptr->client_id));
1270 loglog(logbuffer);
1271 }
1272 }
1273
1274 loglog(" = = = = = = = = \n");
1275
1276 uint32_t num_friends = MIN(m->numfriends, m->dht->num_friends);
1277
1278 if (m->numfriends != m->dht->num_friends) {
1279 sprintf(logbuffer, "Friend num in DHT %u != friend num in msger %u\n",
1280 m->dht->num_friends, m->numfriends);
1281 loglog(logbuffer);
1282 }
1283
1284 uint32_t friend, ping_lastrecv;
1285
1286 for (friend = 0; friend < num_friends; friend++) {
1287 Friend *msgfptr = &m->friendlist[friend];
1288 DHT_Friend *dhtfptr = &m->dht->friends_list[friend];
1289
1290 if (memcmp(msgfptr->client_id, dhtfptr->client_id, CLIENT_ID_SIZE)) {
1291 if (sizeof(logbuffer) > 2 * CLIENT_ID_SIZE + 64) {
1292 sprintf(logbuffer, "F[%2u] ID(m) %s != ID(d) ", friend,
1293 ID2String(msgfptr->client_id));
1294 strcat(logbuffer + strlen(logbuffer), ID2String(dhtfptr->client_id));
1295 strcat(logbuffer + strlen(logbuffer), "\n");
1296 } else
1297 sprintf(logbuffer, "F[%2u] ID(m) != ID(d) ", friend);
1298
1299 loglog(logbuffer);
1300 }
1301
1302 ping_lastrecv = lastdump - msgfptr->ping_lastrecv;
1303
1304 if (ping_lastrecv > 999)
1305 ping_lastrecv = 999;
1306
1307 snprintf(logbuffer, sizeof(logbuffer), "F[%2u] <%s> %02u [%03u] %s\n",
1308 friend, msgfptr->name, msgfptr->crypt_connection_id,
1309 ping_lastrecv, ID2String(msgfptr->client_id));
1310 loglog(logbuffer);
1311
1312 for (client = 0; client < MAX_FRIEND_CLIENTS; client++) {
1313 Client_data *cptr = &dhtfptr->client_list[client];
1314 last_pinged = lastdump - cptr->last_pinged;
1315
1316 if (last_pinged > 999)
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
1330#endif
1233} 1331}
1234 1332
1235/* return size of the messenger data (for saving) */ 1333/* return size of the messenger data (for saving) */
@@ -1251,19 +1349,23 @@ static void Messenger_save_old(Messenger *m, uint8_t *data)
1251{ 1349{
1252 save_keys(m->net_crypto, data); 1350 save_keys(m->net_crypto, data);
1253 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; 1351 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
1352
1254 uint32_t nospam = get_nospam(&(m->fr)); 1353 uint32_t nospam = get_nospam(&(m->fr));
1255 memcpy(data, &nospam, sizeof(nospam)); 1354 memcpy(data, &nospam, sizeof(nospam));
1256 data += sizeof(nospam); 1355 data += sizeof(nospam);
1356
1257 uint32_t size = DHT_size(m->dht); 1357 uint32_t size = DHT_size(m->dht);
1258 memcpy(data, &size, sizeof(size)); 1358 memcpy(data, &size, sizeof(size));
1259 data += sizeof(size); 1359 data += sizeof(size);
1260 DHT_save(m->dht, data); 1360 DHT_save(m->dht, data);
1261 data += size; 1361 data += size;
1362
1262 size = sizeof(Friend) * m->numfriends; 1363 size = sizeof(Friend) * m->numfriends;
1263 memcpy(data, &size, sizeof(size)); 1364 memcpy(data, &size, sizeof(size));
1264 data += sizeof(size); 1365 data += sizeof(size);
1265 memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends); 1366 memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends);
1266 data += size; 1367 data += size;
1368
1267 uint16_t small_size = m->name_length; 1369 uint16_t small_size = m->name_length;
1268 memcpy(data, &small_size, sizeof(small_size)); 1370 memcpy(data, &small_size, sizeof(small_size));
1269 data += sizeof(small_size); 1371 data += sizeof(small_size);
@@ -1276,60 +1378,69 @@ static int Messenger_load_old(Messenger *m, uint8_t *data, uint32_t length)
1276 if (length == ~((uint32_t)0)) 1378 if (length == ~((uint32_t)0))
1277 return -1; 1379 return -1;
1278 1380
1279 if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3) 1381 /* BLOCK1: PUBKEY, SECKEY, NOSPAM, SIZE */
1382 if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2)
1280 return -1; 1383 return -1;
1281 1384
1282 length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3;
1283 load_keys(m->net_crypto, data); 1385 load_keys(m->net_crypto, data);
1284 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; 1386 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
1387 length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
1388
1285 uint32_t nospam; 1389 uint32_t nospam;
1286 memcpy(&nospam, data, sizeof(nospam)); 1390 memcpy(&nospam, data, sizeof(nospam));
1287 set_nospam(&(m->fr), nospam); 1391 set_nospam(&(m->fr), nospam);
1288 data += sizeof(nospam); 1392 data += sizeof(nospam);
1393 length -= sizeof(nospam);
1394
1289 uint32_t size; 1395 uint32_t size;
1290 memcpy(&size, data, sizeof(size)); 1396 memcpy(&size, data, sizeof(size));
1291 data += sizeof(size); 1397 data += sizeof(size);
1398 length -= sizeof(size);
1292 1399
1293 if (length < size) 1400 if (length < size)
1294 return -1; 1401 return -1;
1295 1402
1296 length -= size;
1297
1298 if (DHT_load_old(m->dht, data, size) == -1) 1403 if (DHT_load_old(m->dht, data, size) == -1)
1299 fprintf(stderr, "Data file: Something wicked happened to the stored connections...\n"); 1404 fprintf(stderr, "Data file: Something wicked happened to the stored connections...\n");
1300 /* DO go on, friends/name still might be intact */ 1405 /* DO go on, friends/name still might be intact */
1301 1406
1302 data += size; 1407 data += size;
1303 memcpy(&size, data, sizeof(size)); 1408 length -= size;
1304 data += sizeof(size);
1305 1409
1306 if (length < size || size % sizeof(Friend) != 0) 1410 if (length < sizeof(size))
1307 return -1; 1411 return -1;
1308 1412
1309 Friend *temp = malloc(size); 1413 memcpy(&size, data, sizeof(size));
1310 memcpy(temp, data, size); 1414 data += sizeof(size);
1311 1415 length -= sizeof(size);
1312 uint16_t num = size / sizeof(Friend);
1313 1416
1314 uint32_t i; 1417 if (length < size)
1418 return -1;
1315 1419
1316 for (i = 0; i < num; ++i) { 1420 if (!(size % sizeof(Friend))) {
1317 if (temp[i].status >= 3) { 1421 uint16_t num = size / sizeof(Friend);
1318 int fnum = m_addfriend_norequest(m, temp[i].client_id); 1422 Friend temp[num];
1319 setfriendname(m, fnum, temp[i].name, temp[i].name_length); 1423 memcpy(temp, data, size);
1320 /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ 1424
1321 } else if (temp[i].status != 0) { 1425 uint32_t i;
1322 /* TODO: This is not a good way to do this. */ 1426
1323 uint8_t address[FRIEND_ADDRESS_SIZE]; 1427 for (i = 0; i < num; ++i) {
1324 memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES); 1428 if (temp[i].status >= 3) {
1325 memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t)); 1429 int fnum = m_addfriend_norequest(m, temp[i].client_id);
1326 uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); 1430 setfriendname(m, fnum, temp[i].name, temp[i].name_length);
1327 memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); 1431 /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */
1328 m_addfriend(m, address, temp[i].info, temp[i].info_size); 1432 } else if (temp[i].status != 0) {
1433 /* TODO: This is not a good way to do this. */
1434 uint8_t address[FRIEND_ADDRESS_SIZE];
1435 memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES);
1436 memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t));
1437 uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
1438 memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum));
1439 m_addfriend(m, address, temp[i].info, temp[i].info_size);
1440 }
1329 } 1441 }
1330 } 1442 }
1331 1443
1332 free(temp);
1333 data += size; 1444 data += size;
1334 length -= size; 1445 length -= size;
1335 1446