diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/LAN_discovery.c | 58 | ||||
-rw-r--r-- | core/LAN_discovery.h | 8 |
2 files changed, 64 insertions, 2 deletions
diff --git a/core/LAN_discovery.c b/core/LAN_discovery.c index 0a23914d..6170f2fb 100644 --- a/core/LAN_discovery.c +++ b/core/LAN_discovery.c | |||
@@ -23,13 +23,67 @@ | |||
23 | 23 | ||
24 | #include "LAN_discovery.h" | 24 | #include "LAN_discovery.h" |
25 | 25 | ||
26 | #define MAX_INTERFACES 16 | ||
27 | #define ERR_INTERFACES 0 | ||
28 | #define ERR_IOCTL 1 | ||
26 | 29 | ||
27 | /*Return the broadcast ip | 30 | #ifdef __linux |
28 | TODO: make it return the real one, not the 255.255.255.255 one.*/ | 31 | /* get the first running interface's broadcast IP, |
32 | * return is higher than 0 on success | ||
33 | * ERR_IOCTL on an ioctl error | ||
34 | * ERR_INTERFACES on general error */ | ||
35 | uint32_t get_broadcast(void) | ||
36 | { | ||
37 | /* not sure how many platforms this will | ||
38 | * run on, so it's wrapped in __linux for now */ | ||
39 | struct ifconf ifconf; | ||
40 | struct ifreq i_faces[MAX_INTERFACES]; | ||
41 | int count = 0; | ||
42 | int sock = 0; | ||
43 | int i = 0; | ||
44 | struct sockaddr_in *sock_holder = NULL; | ||
45 | |||
46 | /* configure ifconf for the ioctl call */ | ||
47 | sock = socket(AF_INET, SOCK_STREAM, 0); | ||
48 | ifconf.ifc_buf = (char *)i_faces; | ||
49 | ifconf.ifc_len = sizeof(i_faces); | ||
50 | count = ifconf.ifc_len / sizeof(i_faces[0]); | ||
51 | |||
52 | for(i = 0; i < count; i++) { | ||
53 | /* skip the loopback interface, as it's useless */ | ||
54 | if(strcmp(i_faces[i].ifr_name, "lo") != 0) { | ||
55 | if(ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) { | ||
56 | perror("[!] get_broadcast: ioctl error"); | ||
57 | return 0; | ||
58 | } | ||
59 | |||
60 | /* just to clarify where we're getting the values from */ | ||
61 | sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr; | ||
62 | break; | ||
63 | } | ||
64 | } | ||
65 | close(sock); | ||
66 | |||
67 | if(sock_holder == NULL) | ||
68 | return ERR_INTERFACES; | ||
69 | |||
70 | return sock_holder->sin_addr.s_addr; | ||
71 | } | ||
72 | #endif | ||
73 | |||
74 | /* Return the broadcast ip */ | ||
29 | IP broadcast_ip() | 75 | IP broadcast_ip() |
30 | { | 76 | { |
31 | IP ip; | 77 | IP ip; |
78 | #ifdef __linux | ||
79 | ip.i = get_broadcast(); | ||
80 | if(ip.i == ERR_INTERFACES) | ||
81 | ip.i = ~0; | ||
82 | else if(ip.i == ERR_IOCTL) | ||
83 | ip.i = 0; | ||
84 | #else | ||
32 | ip.i = ~0; | 85 | ip.i = ~0; |
86 | #endif | ||
33 | return ip; | 87 | return ip; |
34 | } | 88 | } |
35 | 89 | ||
diff --git a/core/LAN_discovery.h b/core/LAN_discovery.h index 4ca65c03..23391b90 100644 --- a/core/LAN_discovery.h +++ b/core/LAN_discovery.h | |||
@@ -28,6 +28,14 @@ | |||
28 | 28 | ||
29 | #include "DHT.h" | 29 | #include "DHT.h" |
30 | 30 | ||
31 | /* used for get_broadcast() */ | ||
32 | #ifdef __linux | ||
33 | #include <sys/ioctl.h> | ||
34 | #include <arpa/inet.h> | ||
35 | #include <linux/netdevice.h> | ||
36 | #endif | ||
37 | |||
38 | |||
31 | #ifdef __cplusplus | 39 | #ifdef __cplusplus |
32 | extern "C" { | 40 | extern "C" { |
33 | #endif | 41 | #endif |