From 8a0800da4e2c3850ea3e440e0dffe842ea672511 Mon Sep 17 00:00:00 2001 From: charmlesscoin Date: Thu, 1 Aug 2013 16:25:22 -0400 Subject: redid get_broadcast(), looks like it should work now --- core/LAN_discovery.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++-- core/LAN_discovery.h | 7 ++++++ 2 files changed, 70 insertions(+), 2 deletions(-) (limited to 'core') 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 @@ #include "LAN_discovery.h" +#define MAX_INTERFACES 16 +#define ERR_IOCTL 0 -/*Return the broadcast ip - TODO: make it return the real one, not the 255.255.255.255 one.*/ +#ifdef __linux +uint32_t get_broadcast(void) +{ + /* not sure how many platforms this will + * run on, so it's wrapped in __linux for now */ + struct ifconf ifconf; + struct ifreq i_faces[MAX_INTERFACES]; + int count = 0; + int sock = 0; + int i = 0; + struct sockaddr_in *sock_holder = NULL; + struct in_addr result; + + /* configure ifconf for the ioctl call */ + sock = socket(AF_INET, SOCK_STREAM, 0); + memset(i_faces, 0, sizeof(struct ifreq) * MAX_INTERFACES); + + ifconf.ifc_buf = (char *)i_faces; + ifconf.ifc_len = sizeof(i_faces); + count = ifconf.ifc_len / sizeof(struct ifreq); + if(ioctl(sock, SIOCGIFCONF, &ifconf) < 0) { + perror("get_broadcast: ioctl() error"); + return ERR_IOCTL; + } + + fprintf(stderr, "count: %d\n", count); + for(i = 0; i < count; i++) { + /* skip the loopback interface, as it's useless */ + if(strcmp(i_faces[i].ifr_name, "lo") != 0) { + fprintf(stderr, "device name: %s\n", i_faces[i].ifr_name); + if(ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) { + perror("[!] get_broadcast: ioctl error"); + return ERR_IOCTL; + } + + /* just to clarify where we're getting the values from */ + sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr; + break; + } + } + close(sock); + + char test[INET_ADDRSTRLEN]; + + result.s_addr = sock_holder->sin_addr.s_addr; + inet_ntop(AF_INET, &result, test, INET_ADDRSTRLEN); + fputs(test, stderr); + getchar(); + + return sock_holder->sin_addr.s_addr; +} +#endif + +/* Return the broadcast ip */ IP broadcast_ip() { IP ip; + #ifdef __linux + ip.i = get_broadcast(); + if(ip.i == ERR_IOCTL) + /* ioctl errored, but try anyway? */ + ip.i = ~0; + #else ip.i = ~0; + #endif return ip; } 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 @@ #include "DHT.h" +/* used for get_broadcast() */ +#ifdef __linux +#include +#include +#include +#endif + #ifdef __cplusplus extern "C" { #endif -- cgit v1.2.3 From 939c4afd2cf7bb4c06e4eb69f4411baf4293bf1b Mon Sep 17 00:00:00 2001 From: charmlesscoin Date: Thu, 1 Aug 2013 16:33:03 -0400 Subject: moved some code around, changed how we print the debugging --- core/LAN_discovery.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'core') diff --git a/core/LAN_discovery.c b/core/LAN_discovery.c index 85a9e7ef..e0da2217 100644 --- a/core/LAN_discovery.c +++ b/core/LAN_discovery.c @@ -27,17 +27,20 @@ #define ERR_IOCTL 0 #ifdef __linux +/* get the first working broadcast address that's not from "lo" + * returns higher than 0 on success + * returns ERR_IOCTL on error */ uint32_t get_broadcast(void) { /* not sure how many platforms this will * run on, so it's wrapped in __linux for now */ - struct ifconf ifconf; + struct sockaddr_in *sock_holder = NULL; struct ifreq i_faces[MAX_INTERFACES]; + struct in_addr result; + struct ifconf ifconf; int count = 0; int sock = 0; int i = 0; - struct sockaddr_in *sock_holder = NULL; - struct in_addr result; /* configure ifconf for the ioctl call */ sock = socket(AF_INET, SOCK_STREAM, 0); @@ -55,7 +58,6 @@ uint32_t get_broadcast(void) for(i = 0; i < count; i++) { /* skip the loopback interface, as it's useless */ if(strcmp(i_faces[i].ifr_name, "lo") != 0) { - fprintf(stderr, "device name: %s\n", i_faces[i].ifr_name); if(ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) { perror("[!] get_broadcast: ioctl error"); return ERR_IOCTL; @@ -72,7 +74,7 @@ uint32_t get_broadcast(void) result.s_addr = sock_holder->sin_addr.s_addr; inet_ntop(AF_INET, &result, test, INET_ADDRSTRLEN); - fputs(test, stderr); + fprintf(stderr, "broadcast address for %s: %s\n", i_faces[i].ifr_name, test); getchar(); return sock_holder->sin_addr.s_addr; -- cgit v1.2.3 From 4a5bc1f0fe8546407670413c953146ed03d9278d Mon Sep 17 00:00:00 2001 From: charmlesscoin Date: Thu, 1 Aug 2013 16:54:04 -0400 Subject: removed debugging --- core/LAN_discovery.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) (limited to 'core') diff --git a/core/LAN_discovery.c b/core/LAN_discovery.c index e0da2217..72e00d32 100644 --- a/core/LAN_discovery.c +++ b/core/LAN_discovery.c @@ -24,26 +24,28 @@ #include "LAN_discovery.h" #define MAX_INTERFACES 16 -#define ERR_IOCTL 0 #ifdef __linux /* get the first working broadcast address that's not from "lo" * returns higher than 0 on success - * returns ERR_IOCTL on error */ + * returns 0 on error */ uint32_t get_broadcast(void) { /* not sure how many platforms this will * run on, so it's wrapped in __linux for now */ struct sockaddr_in *sock_holder = NULL; struct ifreq i_faces[MAX_INTERFACES]; - struct in_addr result; struct ifconf ifconf; int count = 0; int sock = 0; int i = 0; /* configure ifconf for the ioctl call */ - sock = socket(AF_INET, SOCK_STREAM, 0); + if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + perror("[!] get_broadcast: socket() error"); + return 0; + } + memset(i_faces, 0, sizeof(struct ifreq) * MAX_INTERFACES); ifconf.ifc_buf = (char *)i_faces; @@ -51,16 +53,15 @@ uint32_t get_broadcast(void) count = ifconf.ifc_len / sizeof(struct ifreq); if(ioctl(sock, SIOCGIFCONF, &ifconf) < 0) { perror("get_broadcast: ioctl() error"); - return ERR_IOCTL; + return 0; } - fprintf(stderr, "count: %d\n", count); for(i = 0; i < count; i++) { /* skip the loopback interface, as it's useless */ if(strcmp(i_faces[i].ifr_name, "lo") != 0) { if(ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) { perror("[!] get_broadcast: ioctl error"); - return ERR_IOCTL; + return 0; } /* just to clarify where we're getting the values from */ @@ -70,13 +71,6 @@ uint32_t get_broadcast(void) } close(sock); - char test[INET_ADDRSTRLEN]; - - result.s_addr = sock_holder->sin_addr.s_addr; - inet_ntop(AF_INET, &result, test, INET_ADDRSTRLEN); - fprintf(stderr, "broadcast address for %s: %s\n", i_faces[i].ifr_name, test); - getchar(); - return sock_holder->sin_addr.s_addr; } #endif @@ -87,8 +81,8 @@ IP broadcast_ip() IP ip; #ifdef __linux ip.i = get_broadcast(); - if(ip.i == ERR_IOCTL) - /* ioctl errored, but try anyway? */ + if(ip.i == 0) + /* error errored, but try anyway? */ ip.i = ~0; #else ip.i = ~0; -- cgit v1.2.3