summaryrefslogtreecommitdiff
path: root/toxcore/LAN_discovery.c
diff options
context:
space:
mode:
authorCoren[m] <Break@Ocean>2013-09-10 16:36:20 +0200
committerCoren[m] <Break@Ocean>2013-09-10 16:36:20 +0200
commitbcb283cf4512f36b189ce2d49eb040c205cbc778 (patch)
treeb0678f9acf245e5620d21c046265a7b3ad98c561 /toxcore/LAN_discovery.c
parentbcf251ac3190db4230d42be97b539e4df3af736f (diff)
big push, putting all the infrastructure in place behind TOX_ENABLE_IPV6
Diffstat (limited to 'toxcore/LAN_discovery.c')
-rw-r--r--toxcore/LAN_discovery.c85
1 files changed, 65 insertions, 20 deletions
diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c
index bc73a6a1..4cbe3177 100644
--- a/toxcore/LAN_discovery.c
+++ b/toxcore/LAN_discovery.c
@@ -81,33 +81,76 @@ static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t * d
81#endif 81#endif
82 82
83/* Return the broadcast ip. */ 83/* Return the broadcast ip. */
84static IP4 broadcast_ip(void) 84static IP broadcast_ip(sa_family_t sa_family)
85{ 85{
86 IP4 ip; 86 IP ip;
87 ip.uint32 = ~0; 87 ip_reset(&ip);
88
89#ifdef TOX_ENABLE_IPV6
90 if (sa_family == AF_INET)
91 {
92 ip.family = AF_INET;
93 ip.ip4.uint32 = INADDR_BROADCAST;
94 }
95
96 if (sa_family == AF_INET6)
97 {
98 ip.family = AF_INET6;
99 /* FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */
100 /* FE80::*: MUST be exact, for that we would need to look over all
101 * interfaces and check in which status they are */
102 ip.ip6.s6_addr[ 0] = 0xFF;
103 ip.ip6.s6_addr[ 1] = 0x02;
104 ip.ip6.s6_addr[15] = 0x01;
105 }
106#else
107 ip.uint32 = INADDR_BROADCAST;
108#endif
109
88 return ip; 110 return ip;
89} 111}
90 112
91/* return 0 if ip is a LAN ip. 113/* return 0 if ip is a LAN ip.
92 * return -1 if it is not. 114 * return -1 if it is not.
93 */ 115 */
94static int LAN_ip(IP4 ip) 116static int LAN_ip(IP ip)
95{ 117{
96 if (ip.uint8[0] == 127) /* Loopback. */ 118#ifdef TOX_ENABLE_IPV6
97 return 0; 119 if (ip.family == AF_INET) {
98 120 IP4 ip4 = ip.ip4;
99 if (ip.uint8[0] == 10) /* 10.0.0.0 to 10.255.255.255 range. */ 121#else
100 return 0; 122 IP4 ip4 = ip;
101 123#endif
102 if (ip.uint8[0] == 172 && ip.uint8[1] >= 16 && ip.uint8[1] <= 31) /* 172.16.0.0 to 172.31.255.255 range. */ 124 /* Loopback. */
103 return 0; 125 if (ip4.uint8[0] == 127)
104 126 return 0;
105 if (ip.uint8[0] == 192 && ip.uint8[1] == 168) /* 192.168.0.0 to 192.168.255.255 range. */ 127
106 return 0; 128 /* 10.0.0.0 to 10.255.255.255 range. */
107 129 if (ip4.uint8[0] == 10)
108 if (ip.uint8[0] == 169 && ip.uint8[1] == 254 && ip.uint8[2] != 0 130 return 0;
109 && ip.uint8[2] != 255)/* 169.254.1.0 to 169.254.254.255 range. */ 131
110 return 0; 132 /* 172.16.0.0 to 172.31.255.255 range. */
133 if (ip4.uint8[0] == 172 && ip4.uint8[1] >= 16 && ip4.uint8[1] <= 31)
134 return 0;
135
136 /* 192.168.0.0 to 192.168.255.255 range. */
137 if (ip4.uint8[0] == 192 && ip4.uint8[1] == 168)
138 return 0;
139
140 /* 169.254.1.0 to 169.254.254.255 range. */
141 if (ip4.uint8[0] == 169 && ip4.uint8[1] == 254 && ip4.uint8[2] != 0
142 && ip4.uint8[2] != 255)
143 return 0;
144#ifdef TOX_ENABLE_IPV6
145 }
146 else if (ip.family == AF_INET6) {
147 /* autogenerated for each interface: FE80::* (up to FEBF::*)
148 /* FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */
149 if (((ip.ip6.s6_addr[0] == 0xFF) && (ip.ip6.s6_addr[1] < 3) && (ip.ip6.s6_addr[15] == 1)) ||
150 ((ip.ip6.s6_addr[0] == 0xFE) && ((ip.ip6.s6_addr[1] & 0xC0) == 0x80)))
151 return 0;
152 }
153#endif
111 154
112 return -1; 155 return -1;
113} 156}
@@ -135,7 +178,9 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c)
135#ifdef __linux 178#ifdef __linux
136 send_broadcasts(c->lossless_udp->net, port, data, 1 + crypto_box_PUBLICKEYBYTES); 179 send_broadcasts(c->lossless_udp->net, port, data, 1 + crypto_box_PUBLICKEYBYTES);
137#endif 180#endif
138 IP_Port ip_port = {{broadcast_ip(), port, 0}}; 181 IP_Port ip_port;
182 ip_port.ip = broadcast_ip(c->lossless_udp->net->family);
183 ip_port.port = port;
139 return sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); 184 return sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES);
140} 185}
141 186