diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/LAN_discovery.c | 65 | ||||
-rw-r--r-- | core/LAN_discovery.h | 7 |
2 files changed, 70 insertions, 2 deletions
diff --git a/core/LAN_discovery.c b/core/LAN_discovery.c index 0a23914d..85a9e7ef 100644 --- a/core/LAN_discovery.c +++ b/core/LAN_discovery.c | |||
@@ -23,13 +23,74 @@ | |||
23 | 23 | ||
24 | #include "LAN_discovery.h" | 24 | #include "LAN_discovery.h" |
25 | 25 | ||
26 | #define MAX_INTERFACES 16 | ||
27 | #define ERR_IOCTL 0 | ||
26 | 28 | ||
27 | /*Return the broadcast ip | 29 | #ifdef __linux |
28 | TODO: make it return the real one, not the 255.255.255.255 one.*/ | 30 | uint32_t get_broadcast(void) |
31 | { | ||
32 | /* not sure how many platforms this will | ||
33 | * run on, so it's wrapped in __linux for now */ | ||
34 | struct ifconf ifconf; | ||
35 | struct ifreq i_faces[MAX_INTERFACES]; | ||
36 | int count = 0; | ||
37 | int sock = 0; | ||
38 | int i = 0; | ||
39 | struct sockaddr_in *sock_holder = NULL; | ||
40 | struct in_addr result; | ||
41 | |||
42 | /* configure ifconf for the ioctl call */ | ||
43 | sock = socket(AF_INET, SOCK_STREAM, 0); | ||
44 | memset(i_faces, 0, sizeof(struct ifreq) * MAX_INTERFACES); | ||
45 | |||
46 | ifconf.ifc_buf = (char *)i_faces; | ||
47 | ifconf.ifc_len = sizeof(i_faces); | ||
48 | count = ifconf.ifc_len / sizeof(struct ifreq); | ||
49 | if(ioctl(sock, SIOCGIFCONF, &ifconf) < 0) { | ||
50 | perror("get_broadcast: ioctl() error"); | ||
51 | return ERR_IOCTL; | ||
52 | } | ||
53 | |||
54 | fprintf(stderr, "count: %d\n", count); | ||
55 | for(i = 0; i < count; i++) { | ||
56 | /* skip the loopback interface, as it's useless */ | ||
57 | if(strcmp(i_faces[i].ifr_name, "lo") != 0) { | ||
58 | fprintf(stderr, "device name: %s\n", i_faces[i].ifr_name); | ||
59 | if(ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) { | ||
60 | perror("[!] get_broadcast: ioctl error"); | ||
61 | return ERR_IOCTL; | ||
62 | } | ||
63 | |||
64 | /* just to clarify where we're getting the values from */ | ||
65 | sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr; | ||
66 | break; | ||
67 | } | ||
68 | } | ||
69 | close(sock); | ||
70 | |||
71 | char test[INET_ADDRSTRLEN]; | ||
72 | |||
73 | result.s_addr = sock_holder->sin_addr.s_addr; | ||
74 | inet_ntop(AF_INET, &result, test, INET_ADDRSTRLEN); | ||
75 | fputs(test, stderr); | ||
76 | getchar(); | ||
77 | |||
78 | return sock_holder->sin_addr.s_addr; | ||
79 | } | ||
80 | #endif | ||
81 | |||
82 | /* Return the broadcast ip */ | ||
29 | IP broadcast_ip() | 83 | IP broadcast_ip() |
30 | { | 84 | { |
31 | IP ip; | 85 | IP ip; |
86 | #ifdef __linux | ||
87 | ip.i = get_broadcast(); | ||
88 | if(ip.i == ERR_IOCTL) | ||
89 | /* ioctl errored, but try anyway? */ | ||
90 | ip.i = ~0; | ||
91 | #else | ||
32 | ip.i = ~0; | 92 | ip.i = ~0; |
93 | #endif | ||
33 | return ip; | 94 | return ip; |
34 | } | 95 | } |
35 | 96 | ||
diff --git a/core/LAN_discovery.h b/core/LAN_discovery.h index 4ca65c03..96a6e6ad 100644 --- a/core/LAN_discovery.h +++ b/core/LAN_discovery.h | |||
@@ -28,6 +28,13 @@ | |||
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 | |||
31 | #ifdef __cplusplus | 38 | #ifdef __cplusplus |
32 | extern "C" { | 39 | extern "C" { |
33 | #endif | 40 | #endif |