From: John Melton g0orx/n6lyt Date: Mon, 2 May 2016 13:26:53 +0000 (+0000) Subject: check discovered device on same subnet X-Git-Url: https://git.rkrishnan.org/frontends//%22%22.?a=commitdiff_plain;h=671ee9010996d9f56de9a4773e5acae7727d66d0;p=pihpsdr.git check discovered device on same subnet --- diff --git a/discovered.h b/discovered.h index 417d742..429b523 100644 --- a/discovered.h +++ b/discovered.h @@ -53,6 +53,7 @@ struct _DISCOVERED { struct sockaddr_in address; int interface_length; struct sockaddr_in interface_address; + struct sockaddr_in interface_netmask; char interface_name[64]; }; diff --git a/main.c b/main.c index 4bf80d9..db3d116 100644 --- a/main.c +++ b/main.c @@ -810,7 +810,11 @@ gint init(void* arg) { gtk_widget_set_sensitive(start_button, FALSE); } - // check subnet to see if can access + // if not on the same subnet then cannot start it + if((d->interface_address.sin_addr.s_addr&d->interface_netmask.sin_addr.s_addr) != (d->address.sin_addr.s_addr&d->interface_netmask.sin_addr.s_addr)) { + gtk_button_set_label(GTK_BUTTON(start_button),"Subnet!"); + gtk_widget_set_sensitive(start_button, FALSE); + } GtkWidget *configure_button=gtk_button_new_with_label("Configure"); gtk_widget_override_font(configure_button, pango_font_description_from_string("Arial 18")); @@ -827,8 +831,8 @@ gint init(void* arg) { _exit(0); } -#ifdef INCLUDE_GPIO gtk_widget_destroy(discovery_dialog); +#ifdef INCLUDE_GPIO if(result==GTK_RESPONSE_YES) { configure_gpio(); } diff --git a/new_discovery.c b/new_discovery.c index 8d23da4..dc4a8f3 100644 --- a/new_discovery.c +++ b/new_discovery.c @@ -39,6 +39,7 @@ static char interface_name[64]; static struct sockaddr_in interface_addr={0}; +static struct sockaddr_in interface_netmask={0}; static int interface_length; #define DISCOVERY_PORT 1024 @@ -95,7 +96,9 @@ void new_discovery() { void new_discover(struct ifaddrs* iface) { int rc; struct sockaddr_in *sa; - //char *addr; + struct sockaddr_in *mask; + char addr[16]; + char net_mask[16]; strcpy(interface_name,iface->ifa_name); fprintf(stderr,"new_discover: looking for HPSDR devices on %s\n",interface_name); @@ -111,7 +114,9 @@ void new_discover(struct ifaddrs* iface) { setsockopt(discovery_socket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); sa = (struct sockaddr_in *) iface->ifa_addr; - //addr = inet_ntoa(sa->sin_addr); + mask = (struct sockaddr_in *) iface->ifa_netmask; + + interface_netmask.sin_addr.s_addr = mask->sin_addr.s_addr; // bind to this interface and the discovery port interface_addr.sin_family = AF_INET; @@ -122,7 +127,10 @@ void new_discover(struct ifaddrs* iface) { exit(-1); } - fprintf(stderr,"new_discover: bound to %s\n",interface_name); + strcpy(addr,inet_ntoa(sa->sin_addr)); + strcpy(net_mask,inet_ntoa(mask->sin_addr)); + + fprintf(stderr,"new_discover: bound to %s %s %s\n",interface_name,addr,net_mask); // allow broadcast on the socket int on=1; @@ -145,7 +153,6 @@ void new_discover(struct ifaddrs* iface) { exit(-1); } - // send discovery packet unsigned char buffer[60]; buffer[0]=0x00; @@ -239,6 +246,7 @@ void* new_discover_receive_thread(void* arg) { memcpy((void*)&discovered[devices].address,(void*)&addr,sizeof(addr)); discovered[devices].address_length=sizeof(addr); memcpy((void*)&discovered[devices].interface_address,(void*)&interface_addr,sizeof(interface_addr)); + memcpy((void*)&discovered[devices].interface_netmask,(void*)&interface_netmask,sizeof(interface_netmask)); discovered[devices].interface_length=sizeof(interface_addr); strcpy(discovered[devices].interface_name,interface_name); fprintf(stderr,"new_discover: found protocol=%d device=%d software_version=%d status=%d address=%s (%02X:%02X:%02X:%02X:%02X:%02X) on %s\n", diff --git a/old_discovery.c b/old_discovery.c index 5b9835b..7867f6e 100644 --- a/old_discovery.c +++ b/old_discovery.c @@ -37,6 +37,7 @@ static char interface_name[64]; static struct sockaddr_in interface_addr={0}; +static struct sockaddr_in interface_netmask={0}; static int interface_length; #define DISCOVERY_PORT 1024 @@ -49,10 +50,10 @@ static void* discover_receive_thread(void* arg); static void discover(struct ifaddrs* iface) { int rc; struct sockaddr_in *sa; - //char *addr; + struct sockaddr_in *mask; strcpy(interface_name,iface->ifa_name); - fprintf(stderr,"discover: looking for HPSDR devices on %s\n",interface_name); + fprintf(stderr,"discover: looking for HPSDR devices on %s\n", interface_name); // send a broadcast to locate hpsdr boards on the network discovery_socket=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP); @@ -65,7 +66,8 @@ static void discover(struct ifaddrs* iface) { setsockopt(discovery_socket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); sa = (struct sockaddr_in *) iface->ifa_addr; - //addr = inet_ntoa(sa->sin_addr); + mask = (struct sockaddr_in *) iface->ifa_netmask; + interface_netmask.sin_addr.s_addr = mask->sin_addr.s_addr; // bind to this interface and the discovery port interface_addr.sin_family = AF_INET; @@ -186,6 +188,7 @@ fprintf(stderr,"discover_receive_thread\n"); memcpy((void*)&discovered[devices].address,(void*)&addr,sizeof(addr)); discovered[devices].address_length=sizeof(addr); memcpy((void*)&discovered[devices].interface_address,(void*)&interface_addr,sizeof(interface_addr)); + memcpy((void*)&discovered[devices].interface_netmask,(void*)&interface_netmask,sizeof(interface_netmask)); discovered[devices].interface_length=sizeof(interface_addr); strcpy(discovered[devices].interface_name,interface_name); fprintf(stderr,"discovery: found device=%d software_version=%d status=%d address=%s (%02X:%02X:%02X:%02X:%02X:%02X) on %s\n", diff --git a/pihpsdr b/pihpsdr index 604655f..8cbefd6 100755 Binary files a/pihpsdr and b/pihpsdr differ diff --git a/release/pihpsdr.tar b/release/pihpsdr.tar index 23adfaf..d3ea57a 100644 Binary files a/release/pihpsdr.tar and b/release/pihpsdr.tar differ diff --git a/release/pihpsdr/pihpsdr b/release/pihpsdr/pihpsdr index 604655f..8cbefd6 100755 Binary files a/release/pihpsdr/pihpsdr and b/release/pihpsdr/pihpsdr differ