diff options
author | irungentoo <irungentoo@gmail.com> | 2013-09-04 15:14:11 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-09-04 15:14:11 -0400 |
commit | efea6b9e822aa69377727d9dbe75458eb2b8e2d7 (patch) | |
tree | 4394381fac81a2259c5120715952682251fe831d /toxcore/LAN_discovery.c | |
parent | 31f43799e11ea824db79de2458a4ac29e6f22c3d (diff) |
Possibly fixed LAN discovery on some configurations.
Diffstat (limited to 'toxcore/LAN_discovery.c')
-rw-r--r-- | toxcore/LAN_discovery.c | 36 |
1 files changed, 13 insertions, 23 deletions
diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index 880593d2..06e5a677 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c | |||
@@ -26,12 +26,12 @@ | |||
26 | #define MAX_INTERFACES 16 | 26 | #define MAX_INTERFACES 16 |
27 | 27 | ||
28 | #ifdef __linux | 28 | #ifdef __linux |
29 | /* Get the first working broadcast address that's not from "lo". | 29 | /* Send packet to all broadcast addresses |
30 | * | 30 | * |
31 | * return higher than 0 on success. | 31 | * return higher than 0 on success. |
32 | * return 0 on error. | 32 | * return 0 on error. |
33 | */ | 33 | */ |
34 | static uint32_t get_broadcast(void) | 34 | static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t * data, uint16_t length) |
35 | { | 35 | { |
36 | /* Not sure how many platforms this will run on, | 36 | /* Not sure how many platforms this will run on, |
37 | * so it's wrapped in __linux for now. | 37 | * so it's wrapped in __linux for now. |
@@ -45,7 +45,7 @@ static uint32_t get_broadcast(void) | |||
45 | 45 | ||
46 | /* Configure ifconf for the ioctl call. */ | 46 | /* Configure ifconf for the ioctl call. */ |
47 | if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { | 47 | if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
48 | return 0; | 48 | return 1; |
49 | } | 49 | } |
50 | 50 | ||
51 | memset(i_faces, 0, sizeof(struct ifreq) * MAX_INTERFACES); | 51 | memset(i_faces, 0, sizeof(struct ifreq) * MAX_INTERFACES); |
@@ -55,29 +55,24 @@ static uint32_t get_broadcast(void) | |||
55 | count = ifconf.ifc_len / sizeof(struct ifreq); | 55 | count = ifconf.ifc_len / sizeof(struct ifreq); |
56 | 56 | ||
57 | if (ioctl(sock, SIOCGIFCONF, &ifconf) < 0) { | 57 | if (ioctl(sock, SIOCGIFCONF, &ifconf) < 0) { |
58 | return 0; | 58 | return 1; |
59 | } | 59 | } |
60 | 60 | ||
61 | for (i = 0; i < count; i++) { | 61 | for (i = 0; i < count; i++) { |
62 | /* Skip the loopback interface, as it's useless. */ | ||
63 | if (strcmp(i_faces[i].ifr_name, "lo") != 0) { | ||
64 | if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) { | 62 | if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) { |
65 | return 0; | 63 | return 1; |
66 | } | 64 | } |
67 | 65 | ||
68 | /* Just to clarify where we're getting the values from. */ | 66 | /* Just to clarify where we're getting the values from. */ |
69 | sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr; | 67 | sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr; |
70 | break; | 68 | if (sock_holder != NULL) { |
71 | } | 69 | IP_Port ip_port = {{{{sock_holder->sin_addr.s_addr}}, port, 0}}; |
70 | sendpacket(net->sock, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); | ||
71 | } | ||
72 | } | 72 | } |
73 | 73 | ||
74 | close(sock); | 74 | close(sock); |
75 | 75 | return 0; | |
76 | if (sock_holder == NULL) { | ||
77 | return 0; | ||
78 | } | ||
79 | |||
80 | return sock_holder->sin_addr.s_addr; | ||
81 | } | 76 | } |
82 | #endif | 77 | #endif |
83 | 78 | ||
@@ -85,15 +80,7 @@ static uint32_t get_broadcast(void) | |||
85 | static IP broadcast_ip(void) | 80 | static IP broadcast_ip(void) |
86 | { | 81 | { |
87 | IP ip; | 82 | IP ip; |
88 | #ifdef __linux | ||
89 | ip.uint32 = get_broadcast(); | ||
90 | |||
91 | if (ip.uint32 == 0) | ||
92 | ip.uint32 = ~0; /* Error occured, but try anyway? */ | ||
93 | |||
94 | #else | ||
95 | ip.uint32 = ~0; | 83 | ip.uint32 = ~0; |
96 | #endif | ||
97 | return ip; | 84 | return ip; |
98 | } | 85 | } |
99 | 86 | ||
@@ -141,6 +128,9 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c) | |||
141 | uint8_t data[crypto_box_PUBLICKEYBYTES + 1]; | 128 | uint8_t data[crypto_box_PUBLICKEYBYTES + 1]; |
142 | data[0] = NET_PACKET_LAN_DISCOVERY; | 129 | data[0] = NET_PACKET_LAN_DISCOVERY; |
143 | memcpy(data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES); | 130 | memcpy(data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES); |
131 | #ifdef __linux | ||
132 | send_broadcasts(c->lossless_udp->net, port, data, 1 + crypto_box_PUBLICKEYBYTES); | ||
133 | #endif | ||
144 | IP_Port ip_port = {{broadcast_ip(), port, 0}}; | 134 | IP_Port ip_port = {{broadcast_ip(), port, 0}}; |
145 | return sendpacket(c->lossless_udp->net->sock, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); | 135 | return sendpacket(c->lossless_udp->net->sock, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); |
146 | } | 136 | } |