summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/DHT.c27
-rw-r--r--toxcore/LAN_discovery.c8
-rw-r--r--toxcore/LAN_discovery.h2
-rw-r--r--toxcore/Messenger.c7
-rw-r--r--toxcore/network.c36
-rw-r--r--toxcore/network.h23
-rw-r--r--toxcore/tox.c6
7 files changed, 79 insertions, 30 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 3de7b6ae..a5f0c1ab 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -982,11 +982,28 @@ void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key)
982int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled, 982int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled,
983 uint16_t port, uint8_t *public_key) 983 uint16_t port, uint8_t *public_key)
984{ 984{
985 IP_Port ip_port; 985 IP_Port ip_port_v64, ip_port_v4;
986 ip_init(&ip_port.ip, ipv6enabled); 986 IP *ip_extra = NULL;
987 if (addr_resolve_or_parse_ip(address, &ip_port.ip)) { 987#ifdef TOX_ENABLE_IPV6
988 ip_port.port = port; 988 ip_init(&ip_port_v64.ip, ipv6enabled);
989 DHT_bootstrap(dht, ip_port, public_key); 989 if (ipv6enabled) {
990 ip_port_v64.ip.family = AF_UNSPEC;
991 ip_reset(&ip_port_v4.ip);
992 ip_extra = &ip_port_v4.ip;
993 }
994#else
995 ip_init(&ip_port_v64.ip, 0);
996#endif
997
998 if (addr_resolve_or_parse_ip(address, &ip_port_v64.ip, ip_extra)) {
999 ip_port_v64.port = port;
1000 DHT_bootstrap(dht, ip_port_v64, public_key);
1001#ifdef TOX_ENABLE_IPV6
1002 if ((ip_extra != NULL) && ip_isset(ip_extra)) {
1003 ip_port_v4.port = port;
1004 DHT_bootstrap(dht, ip_port_v4, public_key);
1005 }
1006#endif
990 return 1; 1007 return 1;
991 } 1008 }
992 else 1009 else
diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c
index b429ea6e..c50fe65d 100644
--- a/toxcore/LAN_discovery.c
+++ b/toxcore/LAN_discovery.c
@@ -159,6 +159,14 @@ static int LAN_ip(IP ip)
159 if (((ip.ip6.s6_addr[0] == 0xFF) && (ip.ip6.s6_addr[1] < 3) && (ip.ip6.s6_addr[15] == 1)) || 159 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))) 160 ((ip.ip6.s6_addr[0] == 0xFE) && ((ip.ip6.s6_addr[1] & 0xC0) == 0x80)))
161 return 0; 161 return 0;
162
163 /* embedded IPv4-in-IPv6 */
164 if (!ip.ip6.s6_addr32[0] && !ip.ip6.s6_addr32[1] && ip.ip6.s6_addr32[2] == htonl(0xFFFF)) {
165 IP ip4;
166 ip4.family = AF_INET;
167 ip4.ip4.uint32 = ip.ip6.s6_addr32[3];
168 return LAN_ip(ip4);
169 }
162 } 170 }
163#endif 171#endif
164 172
diff --git a/toxcore/LAN_discovery.h b/toxcore/LAN_discovery.h
index 78990936..a0789956 100644
--- a/toxcore/LAN_discovery.h
+++ b/toxcore/LAN_discovery.h
@@ -35,6 +35,8 @@
35#include <linux/netdevice.h> 35#include <linux/netdevice.h>
36#endif 36#endif
37 37
38/* standard interval in seconds between LAN discovery packet sending. */
39#define LAN_DISCOVERY_INTERVAL 60
38 40
39/* Send a LAN discovery pcaket to the broadcast address with port port. */ 41/* Send a LAN discovery pcaket to the broadcast address with port port. */
40int send_LANdiscovery(uint16_t port, Net_Crypto *c); 42int send_LANdiscovery(uint16_t port, Net_Crypto *c);
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 1b276602..ae13e9d6 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -637,14 +637,11 @@ int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint
637 return write_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, packet, length + 1); 637 return write_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, packet, length + 1);
638} 638}
639 639
640/* Interval in seconds between LAN discovery packet sending. */
641#define LAN_DISCOVERY_INTERVAL 60
642
643/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */ 640/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */
644static void LANdiscovery(Messenger *m) 641static void LANdiscovery(Messenger *m)
645{ 642{
646 if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { 643 if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) {
647 send_LANdiscovery(TOX_PORT_DEFAULT, m->net_crypto); 644 send_LANdiscovery(htons(TOX_PORT_DEFAULT), m->net_crypto);
648 m->last_LANdiscovery = unix_time(); 645 m->last_LANdiscovery = unix_time();
649 } 646 }
650} 647}
@@ -1016,7 +1013,7 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
1016 return -1; 1013 return -1;
1017 1014
1018 if (DHT_load(m->dht, data, size) == -1) 1015 if (DHT_load(m->dht, data, size) == -1)
1019 fprintf(stderr, "Data file: Something wicked happened to the stored connections.\n"); 1016 fprintf(stderr, "Data file: Something wicked happened to the stored connections...\n");
1020 1017
1021 /* go on, friends still might be intact */ 1018 /* go on, friends still might be intact */
1022 1019
diff --git a/toxcore/network.c b/toxcore/network.c
index c0bd366d..39483b42 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -207,7 +207,13 @@ void networking_poll(Networking_Core *net)
207 while (receivepacket(net->sock, &ip_port, data, &length) != -1) { 207 while (receivepacket(net->sock, &ip_port, data, &length) != -1) {
208 if (length < 1) continue; 208 if (length < 1) continue;
209 209
210 if (!(net->packethandlers[data[0]].function)) continue; 210 if (!(net->packethandlers[data[0]].function)) {
211#ifdef LOGGING
212 sprintf(logbuffer, "[%02u] -- Packet has no handler.\n", data[0]);
213 loglog(logbuffer);
214#endif
215 continue;
216 }
211 217
212 net->packethandlers[data[0]].function(net->packethandlers[data[0]].object, ip_port, data, length); 218 net->packethandlers[data[0]].function(net->packethandlers[data[0]].object, ip_port, data, length);
213 } 219 }
@@ -661,16 +667,18 @@ int addr_parse_ip(const char *address, IP *to)
661 * 667 *
662 * input 668 * input
663 * address: a hostname (or something parseable to an IP address) 669 * address: a hostname (or something parseable to an IP address)
664 * ip: ip.family MUST be initialized, either set to a specific IP version 670 * to: to.family MUST be initialized, either set to a specific IP version
665 * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both 671 * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both
666 * IP versions are acceptable 672 * IP versions are acceptable
673 * extra can be NULL and is only set in special circumstances, see returns
667 * 674 *
668 * returns in ip a valid IPAny (v4/v6), 675 * returns in *to a valid IPAny (v4/v6),
669 * prefers v6 if ip.family was AF_UNSPEC and both available 676 * prefers v6 if ip.family was AF_UNSPEC and both available
677 * returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is AF_INET6
670 * returns 0 on failure 678 * returns 0 on failure
671 */ 679 */
672 680
673int addr_resolve(const char *address, IP *to) 681int addr_resolve(const char *address, IP *to, IP *extra)
674{ 682{
675 if (!address || !to) 683 if (!address || !to)
676 return 0; 684 return 0;
@@ -772,6 +780,10 @@ int addr_resolve(const char *address, IP *to)
772 if (rc & 2) { 780 if (rc & 2) {
773 to->family = AF_INET6; 781 to->family = AF_INET6;
774 to->ip6 = ip6; 782 to->ip6 = ip6;
783 if ((rc & 1) && (extra != NULL)) {
784 extra->family = AF_INET;
785 extra->ip4 = ip4;
786 }
775 } 787 }
776 else if (rc & 1) { 788 else if (rc & 1) {
777 to->family = AF_INET; 789 to->family = AF_INET;
@@ -794,12 +806,20 @@ int addr_resolve(const char *address, IP *to)
794 * addr_resolve_or_parse_ip 806 * addr_resolve_or_parse_ip
795 * resolves string into an IP address 807 * resolves string into an IP address
796 * 808 *
797 * to->family MUST be set (AF_UNSPEC, AF_INET, AF_INET6) 809 * address: a hostname (or something parseable to an IP address)
798 * returns 1 on success, 0 on failure 810 * to: to.family MUST be initialized, either set to a specific IP version
811 * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both
812 * IP versions are acceptable
813 * extra can be NULL and is only set in special circumstances, see returns
814 *
815 * returns in *tro a matching address (IPv6 or IPv4)
816 * returns in *extra, if not NULL, an IPv4 address, if to->family was AF_UNSPEC
817 * returns 1 on success
818 * returns 0 on failure
799 */ 819 */
800int addr_resolve_or_parse_ip(const char *address, IP *to) 820int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra)
801{ 821{
802 if (!addr_resolve(address, to)) 822 if (!addr_resolve(address, to, extra))
803 if (!addr_parse_ip(address, to)) 823 if (!addr_parse_ip(address, to))
804 return 0; 824 return 0;
805 825
diff --git a/toxcore/network.h b/toxcore/network.h
index 5bc04632..6cdf300d 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -175,25 +175,34 @@ void ipport_copy(IP_Port *target, IP_Port *source);
175 * 175 *
176 * input 176 * input
177 * address: a hostname (or something parseable to an IP address) 177 * address: a hostname (or something parseable to an IP address)
178 * ip: ip.family MUST be initialized, either set to a specific IP version 178 * to: to.family MUST be initialized, either set to a specific IP version
179 * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both 179 * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both
180 * IP versions are acceptable 180 * IP versions are acceptable
181 * extra can be NULL and is only set in special circumstances, see returns
181 * 182 *
182 * returns in ip a valid IPAny (v4/v6), 183 * returns in *to a valid IPAny (v4/v6),
183 * prefers v6 if ip.family was AF_UNSPEC and both available 184 * prefers v6 if ip.family was AF_UNSPEC and both available
185 * returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is AF_INET6
184 * returns 0 on failure 186 * returns 0 on failure
185 */ 187 */
186 188int addr_resolve(const char *address, IP *to, IP *extra);
187int addr_resolve(const char *address, IP *to);
188 189
189/* 190/*
190 * addr_resolve_or_parse_ip 191 * addr_resolve_or_parse_ip
191 * resolves string into an IP address 192 * resolves string into an IP address
192 * 193 *
193 * to->family MUST be set (AF_UNSPEC, AF_INET, AF_INET6) 194 * address: a hostname (or something parseable to an IP address)
194 * returns 1 on success, 0 on failure 195 * to: to.family MUST be initialized, either set to a specific IP version
196 * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both
197 * IP versions are acceptable
198 * extra can be NULL and is only set in special circumstances, see returns
199 *
200 * returns in *tro a matching address (IPv6 or IPv4)
201 * returns in *extra, if not NULL, an IPv4 address, if to->family was AF_UNSPEC
202 * returns 1 on success
203 * returns 0 on failure
195 */ 204 */
196int addr_resolve_or_parse_ip(const char *address, IP *to); 205int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra);
197 206
198/* Function to receive data, ip and port of sender is put into ip_port. 207/* Function to receive data, ip and port of sender is put into ip_port.
199 * Packet data is put into data. 208 * Packet data is put into data.
diff --git a/toxcore/tox.c b/toxcore/tox.c
index f5c6c8ba..3eb80d22 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -395,14 +395,10 @@ int tox_isconnected(void *tox)
395 * return allocated instance of tox on success. 395 * return allocated instance of tox on success.
396 * return 0 if there are problems. 396 * return 0 if there are problems.
397 */ 397 */
398void *tox_new_ex(uint8_t ipv6enabled) 398void *tox_new(uint8_t ipv6enabled)
399{ 399{
400 return initMessenger(ipv6enabled); 400 return initMessenger(ipv6enabled);
401} 401}
402void *tox_new(void)
403{
404 return tox_new_ex(0);
405}
406 402
407/* Run this before closing shop. 403/* Run this before closing shop.
408 * Free all datastructures. 404 * Free all datastructures.