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