summaryrefslogtreecommitdiff
path: root/toxcore/LAN_discovery.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-09-14 12:42:17 -0400
committerirungentoo <irungentoo@gmail.com>2013-09-14 12:42:17 -0400
commit12d1c5199bd2feecd93424ff57f44fab4bbb02ad (patch)
treecd5a20e0c995199a383efd0a77ca0efb0033cfb6 /toxcore/LAN_discovery.c
parent64d000cdfaed710d3e22fa5444b8c2883b9f09d6 (diff)
astyled everything.
Diffstat (limited to 'toxcore/LAN_discovery.c')
-rw-r--r--toxcore/LAN_discovery.c48
1 files changed, 29 insertions, 19 deletions
diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c
index 92e42e0f..ed8dd229 100644
--- a/toxcore/LAN_discovery.c
+++ b/toxcore/LAN_discovery.c
@@ -35,7 +35,7 @@
35 * return higher than 0 on success. 35 * return higher than 0 on success.
36 * return 0 on error. 36 * return 0 on error.
37 */ 37 */
38static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t * data, uint16_t length) 38static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t *data, uint16_t length)
39{ 39{
40 /* Not sure how many platforms this will run on, 40 /* Not sure how many platforms this will run on,
41 * so it's wrapped in __linux for now. 41 * so it's wrapped in __linux for now.
@@ -63,16 +63,17 @@ static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t * d
63 } 63 }
64 64
65 for (i = 0; i < count; i++) { 65 for (i = 0; i < count; i++) {
66 if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) { 66 if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) {
67 return 1; 67 return 1;
68 } 68 }
69 69
70 /* Just to clarify where we're getting the values from. */ 70 /* Just to clarify where we're getting the values from. */
71 sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr; 71 sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr;
72 if (sock_holder != NULL) { 72
73 IP_Port ip_port = {{{{sock_holder->sin_addr.s_addr}}, port, 0}}; 73 if (sock_holder != NULL) {
74 sendpacket(net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); 74 IP_Port ip_port = {{{{sock_holder->sin_addr.s_addr}}, port, 0}};
75 } 75 sendpacket(net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES);
76 }
76 } 77 }
77 78
78 close(sock); 79 close(sock);
@@ -87,6 +88,7 @@ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast)
87 ip_reset(&ip); 88 ip_reset(&ip);
88 89
89#ifdef TOX_ENABLE_IPV6 90#ifdef TOX_ENABLE_IPV6
91
90 if (family_socket == AF_INET6) { 92 if (family_socket == AF_INET6) {
91 if (family_broadcast == AF_INET6) { 93 if (family_broadcast == AF_INET6) {
92 ip.family = AF_INET6; 94 ip.family = AF_INET6;
@@ -96,25 +98,26 @@ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast)
96 ip.ip6.s6_addr[ 0] = 0xFF; 98 ip.ip6.s6_addr[ 0] = 0xFF;
97 ip.ip6.s6_addr[ 1] = 0x02; 99 ip.ip6.s6_addr[ 1] = 0x02;
98 ip.ip6.s6_addr[15] = 0x01; 100 ip.ip6.s6_addr[15] = 0x01;
99 } 101 } else if (family_broadcast == AF_INET) {
100 else if (family_broadcast == AF_INET) {
101 ip.family = AF_INET6; 102 ip.family = AF_INET6;
102 ip.ip6.s6_addr32[0] = 0; 103 ip.ip6.s6_addr32[0] = 0;
103 ip.ip6.s6_addr32[1] = 0; 104 ip.ip6.s6_addr32[1] = 0;
104 ip.ip6.s6_addr32[2] = htonl(0xFFFF); 105 ip.ip6.s6_addr32[2] = htonl(0xFFFF);
105 ip.ip6.s6_addr32[3] = INADDR_BROADCAST; 106 ip.ip6.s6_addr32[3] = INADDR_BROADCAST;
106 } 107 }
107 } 108 } else if (family_socket == AF_INET) {
108 else if (family_socket == AF_INET) {
109 if (family_broadcast == AF_INET) { 109 if (family_broadcast == AF_INET) {
110 ip.family = AF_INET; 110 ip.family = AF_INET;
111 ip.ip4.uint32 = INADDR_BROADCAST; 111 ip.ip4.uint32 = INADDR_BROADCAST;
112 } 112 }
113 } 113 }
114
114#else 115#else
116
115 if (family_socket == AF_INET) 117 if (family_socket == AF_INET)
116 if (family_broadcast == AF_INET) 118 if (family_broadcast == AF_INET)
117 ip.uint32 = INADDR_BROADCAST; 119 ip.uint32 = INADDR_BROADCAST;
120
118#endif 121#endif
119 122
120 return ip; 123 return ip;
@@ -126,11 +129,13 @@ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast)
126static int LAN_ip(IP ip) 129static int LAN_ip(IP ip)
127{ 130{
128#ifdef TOX_ENABLE_IPV6 131#ifdef TOX_ENABLE_IPV6
132
129 if (ip.family == AF_INET) { 133 if (ip.family == AF_INET) {
130 IP4 ip4 = ip.ip4; 134 IP4 ip4 = ip.ip4;
131#else 135#else
132 IP4 ip4 = ip; 136 IP4 ip4 = ip;
133#endif 137#endif
138
134 /* Loopback. */ 139 /* Loopback. */
135 if (ip4.uint8[0] == 127) 140 if (ip4.uint8[0] == 127)
136 return 0; 141 return 0;
@@ -151,13 +156,14 @@ static int LAN_ip(IP ip)
151 if (ip4.uint8[0] == 169 && ip4.uint8[1] == 254 && ip4.uint8[2] != 0 156 if (ip4.uint8[0] == 169 && ip4.uint8[1] == 254 && ip4.uint8[2] != 0
152 && ip4.uint8[2] != 255) 157 && ip4.uint8[2] != 255)
153 return 0; 158 return 0;
159
154#ifdef TOX_ENABLE_IPV6 160#ifdef TOX_ENABLE_IPV6
155 } 161 } else if (ip.family == AF_INET6)
156 else if (ip.family == AF_INET6) { 162 {
157 /* autogenerated for each interface: FE80::* (up to FEBF::*) 163 /* autogenerated for each interface: FE80::* (up to FEBF::*)
158 /* FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ 164 /* FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */
159 if (((ip.ip6.s6_addr[0] == 0xFF) && (ip.ip6.s6_addr[1] < 3) && (ip.ip6.s6_addr[15] == 1)) || 165 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))) 166 ((ip.ip6.s6_addr[0] == 0xFE) && ((ip.ip6.s6_addr[1] & 0xC0) == 0x80)))
161 return 0; 167 return 0;
162 168
163 /* embedded IPv4-in-IPv6 */ 169 /* embedded IPv4-in-IPv6 */
@@ -168,6 +174,7 @@ static int LAN_ip(IP ip)
168 return LAN_ip(ip4); 174 return LAN_ip(ip4);
169 } 175 }
170 } 176 }
177
171#endif 178#endif
172 179
173 return -1; 180 return -1;
@@ -203,9 +210,11 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c)
203 ip_port.port = port; 210 ip_port.port = port;
204 211
205#ifdef TOX_ENABLE_IPV6 212#ifdef TOX_ENABLE_IPV6
213
206 /* IPv6 multicast */ 214 /* IPv6 multicast */
207 if (c->lossless_udp->net->family == AF_INET6) { 215 if (c->lossless_udp->net->family == AF_INET6) {
208 ip_port.ip = broadcast_ip(AF_INET6, AF_INET6); 216 ip_port.ip = broadcast_ip(AF_INET6, AF_INET6);
217
209 if (ip_isset(&ip_port.ip)) 218 if (ip_isset(&ip_port.ip))
210 if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES) > 0) 219 if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES) > 0)
211 res = 1; 220 res = 1;
@@ -216,6 +225,7 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c)
216#else 225#else
217 ip_port.ip = broadcast_ip(AF_INET, AF_INET); 226 ip_port.ip = broadcast_ip(AF_INET, AF_INET);
218#endif 227#endif
228
219 if (ip_isset(&ip_port.ip)) 229 if (ip_isset(&ip_port.ip))
220 if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES)) 230 if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES))
221 res = 1; 231 res = 1;