summaryrefslogtreecommitdiff
path: root/core/LAN_discovery.c
diff options
context:
space:
mode:
authorcharmlesscoin <charmlesscoin@gmail.com>2013-08-01 16:25:22 -0400
committercharmlesscoin <charmlesscoin@gmail.com>2013-08-01 16:25:22 -0400
commit8a0800da4e2c3850ea3e440e0dffe842ea672511 (patch)
tree171e963a829680f1265ebd8e3a0f14d1c805c661 /core/LAN_discovery.c
parent75daa1aec9c908205ebc8e085b4c5cdb048b6017 (diff)
redid get_broadcast(), looks like it should work now
Diffstat (limited to 'core/LAN_discovery.c')
-rw-r--r--core/LAN_discovery.c65
1 files changed, 63 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.*/ 30uint32_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 */
29IP broadcast_ip() 83IP 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