diff options
author | irungentoo <irungentoo@gmail.com> | 2013-08-01 04:19:17 -0700 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-08-01 04:19:17 -0700 |
commit | b553043b17ba0d39b2fd72ed09ceb4dfac948b84 (patch) | |
tree | 959be821e686db6f4ae06d8094f9b35bf00c2f67 /core/LAN_discovery.c | |
parent | c3b5fe3fa154e60d46d647f7928cab48ec4012a0 (diff) | |
parent | 5ceb601fcd78874a5e7c483f0610938362de90c7 (diff) |
Merge pull request #236 from CharmlessCoin/network
Add get_broadcast()
Diffstat (limited to 'core/LAN_discovery.c')
-rw-r--r-- | core/LAN_discovery.c | 58 |
1 files changed, 56 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 | ||