mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
network.*:
- addr_resolv(_or_parse_ip)(): added an optional parameter to return both an IPv6 and an IPv4 address if requested address family was AF_UNSPEC - logging of unhandled packets DHT.c: - bootstrap_from_address(): use the additional return from addr_resolv_or_parse_ip() to bootstrap in both network types at once Lossless_UDP_testclient.c: - main(): adapt to signature change of addr_resolve() Messenger.c. LAN_discovery.h: - lost a htons(), readded - moved LAN_DISCOVERY_INTERVAL #define into LAN_discovery.h LAN_discovery.c: - added IPv4-in-IPv6 local address test
This commit is contained in:
parent
0cfbe004ef
commit
591d6c70c6
|
@ -184,7 +184,7 @@ int main(int argc, char *argv[])
|
|||
|
||||
IP_Port serverip;
|
||||
ip_init(&serverip.ip, ipv6enabled);
|
||||
if (!addr_resolve(argv[argvoffset + 1], &serverip.ip)) {
|
||||
if (!addr_resolve(argv[argvoffset + 1], &serverip.ip, NULL)) {
|
||||
printf("Failed to convert \"%s\" into an IP address.\n", argv[argvoffset + 1]);
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -982,11 +982,28 @@ void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key)
|
|||
int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled,
|
||||
uint16_t port, uint8_t *public_key)
|
||||
{
|
||||
IP_Port ip_port;
|
||||
ip_init(&ip_port.ip, ipv6enabled);
|
||||
if (addr_resolve_or_parse_ip(address, &ip_port.ip)) {
|
||||
ip_port.port = port;
|
||||
DHT_bootstrap(dht, ip_port, public_key);
|
||||
IP_Port ip_port_v64, ip_port_v4;
|
||||
IP *ip_extra = NULL;
|
||||
#ifdef TOX_ENABLE_IPV6
|
||||
ip_init(&ip_port_v64.ip, ipv6enabled);
|
||||
if (ipv6enabled) {
|
||||
ip_port_v64.ip.family = AF_UNSPEC;
|
||||
ip_reset(&ip_port_v4.ip);
|
||||
ip_extra = &ip_port_v4.ip;
|
||||
}
|
||||
#else
|
||||
ip_init(&ip_port_v64.ip, 0);
|
||||
#endif
|
||||
|
||||
if (addr_resolve_or_parse_ip(address, &ip_port_v64.ip, ip_extra)) {
|
||||
ip_port_v64.port = port;
|
||||
DHT_bootstrap(dht, ip_port_v64, public_key);
|
||||
#ifdef TOX_ENABLE_IPV6
|
||||
if ((ip_extra != NULL) && ip_isset(ip_extra)) {
|
||||
ip_port_v4.port = port;
|
||||
DHT_bootstrap(dht, ip_port_v4, public_key);
|
||||
}
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
|
|
|
@ -159,6 +159,14 @@ static int LAN_ip(IP ip)
|
|||
if (((ip.ip6.s6_addr[0] == 0xFF) && (ip.ip6.s6_addr[1] < 3) && (ip.ip6.s6_addr[15] == 1)) ||
|
||||
((ip.ip6.s6_addr[0] == 0xFE) && ((ip.ip6.s6_addr[1] & 0xC0) == 0x80)))
|
||||
return 0;
|
||||
|
||||
/* embedded IPv4-in-IPv6 */
|
||||
if (!ip.ip6.s6_addr32[0] && !ip.ip6.s6_addr32[1] && ip.ip6.s6_addr32[2] == htonl(0xFFFF)) {
|
||||
IP ip4;
|
||||
ip4.family = AF_INET;
|
||||
ip4.ip4.uint32 = ip.ip6.s6_addr32[3];
|
||||
return LAN_ip(ip4);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
|
@ -35,6 +35,8 @@
|
|||
#include <linux/netdevice.h>
|
||||
#endif
|
||||
|
||||
/* standard interval in seconds between LAN discovery packet sending. */
|
||||
#define LAN_DISCOVERY_INTERVAL 60
|
||||
|
||||
/* Send a LAN discovery pcaket to the broadcast address with port port. */
|
||||
int send_LANdiscovery(uint16_t port, Net_Crypto *c);
|
||||
|
|
|
@ -637,14 +637,11 @@ int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint
|
|||
return write_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, packet, length + 1);
|
||||
}
|
||||
|
||||
/* Interval in seconds between LAN discovery packet sending. */
|
||||
#define LAN_DISCOVERY_INTERVAL 60
|
||||
|
||||
/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */
|
||||
static void LANdiscovery(Messenger *m)
|
||||
{
|
||||
if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) {
|
||||
send_LANdiscovery(TOX_PORT_DEFAULT, m->net_crypto);
|
||||
send_LANdiscovery(htons(TOX_PORT_DEFAULT), m->net_crypto);
|
||||
m->last_LANdiscovery = unix_time();
|
||||
}
|
||||
}
|
||||
|
@ -1016,7 +1013,7 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
|
|||
return -1;
|
||||
|
||||
if (DHT_load(m->dht, data, size) == -1)
|
||||
fprintf(stderr, "Data file: Something wicked happened to the stored connections.\n");
|
||||
fprintf(stderr, "Data file: Something wicked happened to the stored connections...\n");
|
||||
|
||||
/* go on, friends still might be intact */
|
||||
|
||||
|
|
|
@ -207,7 +207,13 @@ void networking_poll(Networking_Core *net)
|
|||
while (receivepacket(net->sock, &ip_port, data, &length) != -1) {
|
||||
if (length < 1) continue;
|
||||
|
||||
if (!(net->packethandlers[data[0]].function)) continue;
|
||||
if (!(net->packethandlers[data[0]].function)) {
|
||||
#ifdef LOGGING
|
||||
sprintf(logbuffer, "[%02u] -- Packet has no handler.\n", data[0]);
|
||||
loglog(logbuffer);
|
||||
#endif
|
||||
continue;
|
||||
}
|
||||
|
||||
net->packethandlers[data[0]].function(net->packethandlers[data[0]].object, ip_port, data, length);
|
||||
}
|
||||
|
@ -661,16 +667,18 @@ int addr_parse_ip(const char *address, IP *to)
|
|||
*
|
||||
* input
|
||||
* address: a hostname (or something parseable to an IP address)
|
||||
* ip: ip.family MUST be initialized, either set to a specific IP version
|
||||
* to: to.family MUST be initialized, either set to a specific IP version
|
||||
* (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both
|
||||
* IP versions are acceptable
|
||||
* extra can be NULL and is only set in special circumstances, see returns
|
||||
*
|
||||
* returns in ip a valid IPAny (v4/v6),
|
||||
* returns in *to a valid IPAny (v4/v6),
|
||||
* prefers v6 if ip.family was AF_UNSPEC and both available
|
||||
* returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is AF_INET6
|
||||
* returns 0 on failure
|
||||
*/
|
||||
|
||||
int addr_resolve(const char *address, IP *to)
|
||||
int addr_resolve(const char *address, IP *to, IP *extra)
|
||||
{
|
||||
if (!address || !to)
|
||||
return 0;
|
||||
|
@ -772,6 +780,10 @@ int addr_resolve(const char *address, IP *to)
|
|||
if (rc & 2) {
|
||||
to->family = AF_INET6;
|
||||
to->ip6 = ip6;
|
||||
if ((rc & 1) && (extra != NULL)) {
|
||||
extra->family = AF_INET;
|
||||
extra->ip4 = ip4;
|
||||
}
|
||||
}
|
||||
else if (rc & 1) {
|
||||
to->family = AF_INET;
|
||||
|
@ -794,12 +806,20 @@ int addr_resolve(const char *address, IP *to)
|
|||
* addr_resolve_or_parse_ip
|
||||
* resolves string into an IP address
|
||||
*
|
||||
* to->family MUST be set (AF_UNSPEC, AF_INET, AF_INET6)
|
||||
* returns 1 on success, 0 on failure
|
||||
* address: a hostname (or something parseable to an IP address)
|
||||
* to: to.family MUST be initialized, either set to a specific IP version
|
||||
* (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both
|
||||
* IP versions are acceptable
|
||||
* extra can be NULL and is only set in special circumstances, see returns
|
||||
*
|
||||
* returns in *tro a matching address (IPv6 or IPv4)
|
||||
* returns in *extra, if not NULL, an IPv4 address, if to->family was AF_UNSPEC
|
||||
* returns 1 on success
|
||||
* returns 0 on failure
|
||||
*/
|
||||
int addr_resolve_or_parse_ip(const char *address, IP *to)
|
||||
int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra)
|
||||
{
|
||||
if (!addr_resolve(address, to))
|
||||
if (!addr_resolve(address, to, extra))
|
||||
if (!addr_parse_ip(address, to))
|
||||
return 0;
|
||||
|
||||
|
|
|
@ -175,25 +175,34 @@ void ipport_copy(IP_Port *target, IP_Port *source);
|
|||
*
|
||||
* input
|
||||
* address: a hostname (or something parseable to an IP address)
|
||||
* ip: ip.family MUST be initialized, either set to a specific IP version
|
||||
* to: to.family MUST be initialized, either set to a specific IP version
|
||||
* (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both
|
||||
* IP versions are acceptable
|
||||
* extra can be NULL and is only set in special circumstances, see returns
|
||||
*
|
||||
* returns in ip a valid IPAny (v4/v6),
|
||||
* returns in *to a valid IPAny (v4/v6),
|
||||
* prefers v6 if ip.family was AF_UNSPEC and both available
|
||||
* returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is AF_INET6
|
||||
* returns 0 on failure
|
||||
*/
|
||||
|
||||
int addr_resolve(const char *address, IP *to);
|
||||
int addr_resolve(const char *address, IP *to, IP *extra);
|
||||
|
||||
/*
|
||||
* addr_resolve_or_parse_ip
|
||||
* resolves string into an IP address
|
||||
*
|
||||
* to->family MUST be set (AF_UNSPEC, AF_INET, AF_INET6)
|
||||
* returns 1 on success, 0 on failure
|
||||
* address: a hostname (or something parseable to an IP address)
|
||||
* to: to.family MUST be initialized, either set to a specific IP version
|
||||
* (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both
|
||||
* IP versions are acceptable
|
||||
* extra can be NULL and is only set in special circumstances, see returns
|
||||
*
|
||||
* returns in *tro a matching address (IPv6 or IPv4)
|
||||
* returns in *extra, if not NULL, an IPv4 address, if to->family was AF_UNSPEC
|
||||
* returns 1 on success
|
||||
* returns 0 on failure
|
||||
*/
|
||||
int addr_resolve_or_parse_ip(const char *address, IP *to);
|
||||
int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra);
|
||||
|
||||
/* Function to receive data, ip and port of sender is put into ip_port.
|
||||
* Packet data is put into data.
|
||||
|
|
|
@ -395,14 +395,10 @@ int tox_isconnected(void *tox)
|
|||
* return allocated instance of tox on success.
|
||||
* return 0 if there are problems.
|
||||
*/
|
||||
void *tox_new_ex(uint8_t ipv6enabled)
|
||||
void *tox_new(uint8_t ipv6enabled)
|
||||
{
|
||||
return initMessenger(ipv6enabled);
|
||||
}
|
||||
void *tox_new(void)
|
||||
{
|
||||
return tox_new_ex(0);
|
||||
}
|
||||
|
||||
/* Run this before closing shop.
|
||||
* Free all datastructures.
|
||||
|
|
Loading…
Reference in New Issue
Block a user