summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Messenger.c65
-rw-r--r--toxcore/network.c76
-rw-r--r--toxcore/network.h1
-rw-r--r--toxcore/tox.h17
-rw-r--r--toxcore/util.c32
-rw-r--r--toxcore/util.h17
6 files changed, 170 insertions, 38 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index cf744e23..b955ad5a 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -965,19 +965,23 @@ void Messenger_save(Messenger *m, uint8_t *data)
965{ 965{
966 save_keys(m->net_crypto, data); 966 save_keys(m->net_crypto, data);
967 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; 967 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
968
968 uint32_t nospam = get_nospam(&(m->fr)); 969 uint32_t nospam = get_nospam(&(m->fr));
969 memcpy(data, &nospam, sizeof(nospam)); 970 memcpy(data, &nospam, sizeof(nospam));
970 data += sizeof(nospam); 971 data += sizeof(nospam);
972
971 uint32_t size = DHT_size(m->dht); 973 uint32_t size = DHT_size(m->dht);
972 memcpy(data, &size, sizeof(size)); 974 memcpy(data, &size, sizeof(size));
973 data += sizeof(size); 975 data += sizeof(size);
974 DHT_save(m->dht, data); 976 DHT_save(m->dht, data);
975 data += size; 977 data += size;
978
976 size = sizeof(Friend) * m->numfriends; 979 size = sizeof(Friend) * m->numfriends;
977 memcpy(data, &size, sizeof(size)); 980 memcpy(data, &size, sizeof(size));
978 data += sizeof(size); 981 data += sizeof(size);
979 memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends); 982 memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends);
980 data += size; 983 data += size;
984
981 uint16_t small_size = m->name_length; 985 uint16_t small_size = m->name_length;
982 memcpy(data, &small_size, sizeof(small_size)); 986 memcpy(data, &small_size, sizeof(small_size));
983 data += sizeof(small_size); 987 data += sizeof(small_size);
@@ -990,59 +994,64 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
990 if (length == ~((uint32_t)0)) 994 if (length == ~((uint32_t)0))
991 return -1; 995 return -1;
992 996
993 if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3) 997 /* BLOCK1: PUBKEY, SECKEY, NOSPAM, SIZE */
998 if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2)
994 return -1; 999 return -1;
995 1000
996 length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3;
997 load_keys(m->net_crypto, data); 1001 load_keys(m->net_crypto, data);
998 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; 1002 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
1003 length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
1004
999 uint32_t nospam; 1005 uint32_t nospam;
1000 memcpy(&nospam, data, sizeof(nospam)); 1006 memcpy(&nospam, data, sizeof(nospam));
1001 set_nospam(&(m->fr), nospam); 1007 set_nospam(&(m->fr), nospam);
1002 data += sizeof(nospam); 1008 data += sizeof(nospam);
1009 length -= sizeof(nospam);
1010
1003 uint32_t size; 1011 uint32_t size;
1004 memcpy(&size, data, sizeof(size)); 1012 memcpy(&size, data, sizeof(size));
1005 data += sizeof(size); 1013 data += sizeof(size);
1014 length -= sizeof(size);
1006 1015
1007 if (length < size) 1016 if (length < size)
1008 return -1; 1017 return -1;
1009 1018
1010 length -= size;
1011
1012 if (DHT_load(m->dht, data, size) == -1) 1019 if (DHT_load(m->dht, data, size) == -1)
1013 return -1; 1020 fprintf(stderr, "Data file: Something wicked happened to the stored connections.\n");
1021
1022 /* go on, friends still might be intact */
1014 1023
1015 data += size; 1024 data += size;
1025 length -= size;
1026
1016 memcpy(&size, data, sizeof(size)); 1027 memcpy(&size, data, sizeof(size));
1017 data += sizeof(size); 1028 data += sizeof(size);
1018 1029 if (length < size)
1019 if (length < size || size % sizeof(Friend) != 0)
1020 return -1; 1030 return -1;
1021 1031
1022 Friend *temp = malloc(size); 1032 if (!(size % sizeof(Friend))) {
1023 memcpy(temp, data, size); 1033 uint16_t num = size / sizeof(Friend);
1024 1034 Friend temp[num];
1025 uint16_t num = size / sizeof(Friend); 1035 memcpy(temp, data, size);
1026 1036
1027 uint32_t i; 1037 uint32_t i;
1028 1038 for (i = 0; i < num; ++i) {
1029 for (i = 0; i < num; ++i) { 1039 if (temp[i].status >= 3) {
1030 if (temp[i].status >= 3) { 1040 int fnum = m_addfriend_norequest(m, temp[i].client_id);
1031 int fnum = m_addfriend_norequest(m, temp[i].client_id); 1041 setfriendname(m, fnum, temp[i].name, temp[i].name_length);
1032 setfriendname(m, fnum, temp[i].name, temp[i].name_length); 1042 /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */
1033 /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ 1043 } else if (temp[i].status != 0) {
1034 } else if (temp[i].status != 0) { 1044 /* TODO: This is not a good way to do this. */
1035 /* TODO: This is not a good way to do this. */ 1045 uint8_t address[FRIEND_ADDRESS_SIZE];
1036 uint8_t address[FRIEND_ADDRESS_SIZE]; 1046 memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES);
1037 memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES); 1047 memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t));
1038 memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t)); 1048 uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
1039 uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); 1049 memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum));
1040 memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); 1050 m_addfriend(m, address, temp[i].info, temp[i].info_size);
1041 m_addfriend(m, address, temp[i].info, temp[i].info_size); 1051 }
1042 } 1052 }
1043 } 1053 }
1044 1054
1045 free(temp);
1046 data += size; 1055 data += size;
1047 length -= size; 1056 length -= size;
1048 1057
diff --git a/toxcore/network.c b/toxcore/network.c
index 2969f3ac..0b5eba61 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -26,6 +26,7 @@
26#endif 26#endif
27 27
28#include "network.h" 28#include "network.h"
29#include "util.h"
29 30
30/* return current UNIX time in microseconds (us). */ 31/* return current UNIX time in microseconds (us). */
31uint64_t current_time(void) 32uint64_t current_time(void)
@@ -61,6 +62,10 @@ uint32_t random_int(void)
61#endif 62#endif
62} 63}
63 64
65#ifdef LOGGING
66static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res);
67#endif
68
64/* Basic network functions: 69/* Basic network functions:
65 * Function to send packet(data) of length length to ip_port. 70 * Function to send packet(data) of length length to ip_port.
66 */ 71 */
@@ -122,7 +127,11 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t le
122 } 127 }
123#endif 128#endif
124 129
125 return sendto(net->sock, (char *) data, length, 0, (struct sockaddr *)&addr, addrsize); 130 int res = sendto(net->sock, (char *) data, length, 0, (struct sockaddr *)&addr, addrsize);
131#ifdef LOGGING
132 loglogdata("O=>", data, length, &ip_port, res);
133#endif
134 return res;
126} 135}
127 136
128/* Function to receive data 137/* Function to receive data
@@ -131,11 +140,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t le
131 * Packet length is put into length. 140 * Packet length is put into length.
132 * Dump all empty packets. 141 * Dump all empty packets.
133 */ 142 */
134#ifdef WIN32 143static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t *length)
135static int receivepacket(unsigned int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length)
136#else
137static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length)
138#endif
139{ 144{
140 struct sockaddr_storage addr; 145 struct sockaddr_storage addr;
141#ifdef WIN32 146#ifdef WIN32
@@ -145,8 +150,13 @@ static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *le
145#endif 150#endif
146 (*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); 151 (*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
147 152
148 if (*(int32_t *)length <= 0) 153 if (*(int32_t *)length <= 0) {
154#ifdef LOGGING
155 if ((length < 0) && (errno != EWOULDBLOCK))
156 sprintf(logbuffer, "Unexpected error reading from socket: %u, %s\n", errno, strerror(errno));
157#endif
149 return -1; /* Nothing received or empty packet. */ 158 return -1; /* Nothing received or empty packet. */
159 }
150 160
151#ifdef TOX_ENABLE_IPV6 161#ifdef TOX_ENABLE_IPV6
152 if (addr.ss_family == AF_INET) { 162 if (addr.ss_family == AF_INET) {
@@ -173,6 +183,10 @@ static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *le
173 return -1; 183 return -1;
174#endif 184#endif
175 185
186#ifdef LOGGING
187 loglogdata("=>O", data, *length, ip_port, 0);
188#endif
189
176 return 0; 190 return 0;
177} 191}
178 192
@@ -307,6 +321,10 @@ Networking_Core *new_networking(IP ip, uint16_t port)
307 fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1); 321 fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1);
308#endif 322#endif
309 323
324#ifdef LOGGING
325 loginit(ntohs(port));
326#endif
327
310 /* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */ 328 /* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */
311 uint16_t *portptr = NULL; 329 uint16_t *portptr = NULL;
312 struct sockaddr_storage addr; 330 struct sockaddr_storage addr;
@@ -346,9 +364,33 @@ Networking_Core *new_networking(IP ip, uint16_t port)
346 if (ip.family == AF_INET6) { 364 if (ip.family == AF_INET6) {
347 char ipv6only = 0; 365 char ipv6only = 0;
348 int res = setsockopt(temp->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only)); 366 int res = setsockopt(temp->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only));
367#ifdef LOGGING
368 if (res < 0) {
369 sprintf(logbuffer, "Failed to enable dual-stack on IPv6 socket, won't be able to receive from/send to IPv4 addresses. (%u, %s)\n",
370 errno, strerror(errno));
371 loglog(logbuffer);
372 }
373 else
374 loglog("Embedded IPv4 addresses enabled successfully.\n");
375#endif
376
377 /* multicast local nodes */
378 struct ipv6_mreq mreq;
379 memset(&mreq, 0, sizeof(mreq));
380 mreq.ipv6mr_multiaddr.s6_addr[ 0] = 0xFF;
381 mreq.ipv6mr_multiaddr.s6_addr[ 1] = 0x02;
382 mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01;
383 mreq.ipv6mr_interface = 0;
384 res = setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
385#ifdef LOGGING
349 if (res < 0) { 386 if (res < 0) {
350 /* add log message*/ 387 sprintf(logbuffer, "Failed to activate local multicast membership. (%u, %s)\n",
388 errno, strerror(errno));
389 loglog(logbuffer);
351 } 390 }
391 else
392 loglog("Local multicast group FF02::1 joined successfully.\n");
393#endif
352 } 394 }
353#endif 395#endif
354 396
@@ -363,6 +405,10 @@ Networking_Core *new_networking(IP ip, uint16_t port)
363 if (!res) 405 if (!res)
364 { 406 {
365 temp->port = *portptr; 407 temp->port = *portptr;
408#ifdef LOGGING
409 sprintf(logbuffer, "Bound successfully to %s:%u.\n", ip_ntoa(&ip), ntohs(temp->port));
410 loglog(logbuffer);
411#endif
366 return temp; 412 return temp;
367 } 413 }
368 414
@@ -733,3 +779,17 @@ int addr_resolve_or_parse_ip(const char *address, IP *to)
733 779
734 return 1; 780 return 1;
735}; 781};
782
783#ifdef LOGGING
784static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res)
785{
786 snprintf(logbuffer, sizeof(logbuffer), "[%2u] %3u%c %s %s:%u (%u: %s) | %04x%04x\n",
787 buffer[0], res < 0 ? (buflen & 0xFF) : res,
788 res < 0 ? '-' : (res == buflen ? '=' : '+'),
789 message, ip_ntoa(&ip_port->ip), ntohs(ip_port->port), res < 0 ? errno : 0,
790 res < 0 ? strerror(errno) : "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0,
791 buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0);
792 logbuffer[sizeof(logbuffer) - 1] = 0;
793 loglog(logbuffer);
794}
795#endif
diff --git a/toxcore/network.h b/toxcore/network.h
index 0d31ffac..e804379d 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -119,6 +119,7 @@ typedef struct {
119} IPAny_Port; 119} IPAny_Port;
120 120
121#undef TOX_ENABLE_IPV6 121#undef TOX_ENABLE_IPV6
122/* #define TOX_ENABLE_IPV6 */
122#ifdef TOX_ENABLE_IPV6 123#ifdef TOX_ENABLE_IPV6
123#define TOX_ENABLE_IPV6_DEFAULT 1 124#define TOX_ENABLE_IPV6_DEFAULT 1
124typedef IPAny IP; 125typedef IPAny IP;
diff --git a/toxcore/tox.h b/toxcore/tox.h
index 44eba081..15bef94c 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -26,6 +26,22 @@
26 26
27#include <stdint.h> 27#include <stdint.h>
28 28
29#ifdef WIN32
30#ifndef WINVER
31//Windows XP
32#define WINVER 0x0501
33#endif
34
35#include <winsock2.h>
36#include <windows.h>
37#include <ws2tcpip.h>
38
39#else
40
41#include <netinet/ip.h>
42
43#endif
44
29#ifdef __cplusplus 45#ifdef __cplusplus
30extern "C" { 46extern "C" {
31#endif 47#endif
@@ -71,6 +87,7 @@ typedef struct {
71} tox_IPAny_Port; 87} tox_IPAny_Port;
72 88
73#undef TOX_ENABLE_IPV6 89#undef TOX_ENABLE_IPV6
90/* #define TOX_ENABLE_IPV6 */
74#ifdef TOX_ENABLE_IPV6 91#ifdef TOX_ENABLE_IPV6
75#define TOX_ENABLE_IPV6_DEFAULT 1 92#define TOX_ENABLE_IPV6_DEFAULT 1
76typedef tox_IPAny tox_IP; 93typedef tox_IPAny tox_IP;
diff --git a/toxcore/util.c b/toxcore/util.c
index ff366a07..19d464d4 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -10,11 +10,12 @@
10#endif 10#endif
11 11
12#include <time.h> 12#include <time.h>
13#include <stdint.h>
14#include <stdbool.h>
15 13
14/* for CLIENT_ID_SIZE */
16#include "DHT.h" 15#include "DHT.h"
17 16
17#include "util.h"
18
18uint64_t now() 19uint64_t now()
19{ 20{
20 return time(NULL); 21 return time(NULL);
@@ -41,3 +42,30 @@ void id_cpy(uint8_t *dest, uint8_t *src)
41{ 42{
42 memcpy(dest, src, CLIENT_ID_SIZE); 43 memcpy(dest, src, CLIENT_ID_SIZE);
43} 44}
45
46#ifdef LOGGING
47char logbuffer[512];
48static FILE *logfile = NULL;
49void loginit(uint16_t port)
50{
51 if (logfile)
52 fclose(logfile);
53
54 sprintf(logbuffer, "%u-%u.log", ntohs(port), now);
55 logfile = fopen(logbuffer, "w");
56};
57void loglog(char *text)
58{
59 if (logfile) {
60 fprintf(logfile, text);
61 fflush(logfile);
62 }
63};
64void logexit()
65{
66 if (logfile) {
67 fclose(logfile);
68 logfile = NULL;
69 }
70};
71#endif
diff --git a/toxcore/util.h b/toxcore/util.h
index 7dea3eac..20731a05 100644
--- a/toxcore/util.h
+++ b/toxcore/util.h
@@ -5,7 +5,24 @@
5 * Copyright 2013 plutooo 5 * Copyright 2013 plutooo
6 */ 6 */
7 7
8#ifndef __UTIL_H__
9#define __UTIL_H__
10
11#include <stdbool.h>
12#include <stdint.h>
13
8uint64_t now(); 14uint64_t now();
9uint64_t random_64b(); 15uint64_t random_64b();
10bool id_eq(uint8_t *dest, uint8_t *src); 16bool id_eq(uint8_t *dest, uint8_t *src);
11void id_cpy(uint8_t *dest, uint8_t *src); 17void id_cpy(uint8_t *dest, uint8_t *src);
18
19#undef LOGGING
20// #define LOGGING
21#ifdef LOGGING
22extern char logbuffer[512];
23void loginit(uint16_t port);
24void loglog(char *text);
25void logexit();
26#endif
27
28#endif /* __UTIL_H__ */