From ad38e32921045bbcb192eb22b830f9a2733b7c84 Mon Sep 17 00:00:00 2001 From: darkcoven Date: Sun, 6 Oct 2013 12:56:28 +0200 Subject: [PATCH] Set global broadcast by default --- cli/admin.c | 18 +++++++++--------- lib/src/libconf.c | 8 +++++--- lib/src/network.c | 41 +++++++++++++++++++++++++++++++---------- lib/src/network.h | 3 +++ 4 files changed, 48 insertions(+), 22 deletions(-) diff --git a/cli/admin.c b/cli/admin.c index bb4b310..b40bbd9 100644 --- a/cli/admin.c +++ b/cli/admin.c @@ -191,9 +191,9 @@ int main (int argc, char **argv) {"batch", no_argument, NULL, 'a'}, {"keep-broadcasting", no_argument, NULL, 'b'}, {"force-interface", no_argument, NULL, 'f'}, - {"global-broadcast", no_argument, NULL, 'g'}, {"help", no_argument, NULL, 'h'}, {"interface", required_argument, NULL, 'i'}, + {"local-broadcast", no_argument, NULL, 'l'}, {"mac", required_argument, NULL, 'm'}, {"password", required_argument, NULL, 'p'}, {"retries", required_argument, NULL, 'r'}, @@ -203,7 +203,7 @@ int main (int argc, char **argv) char *line, *com[MAXCOM]; const char *iface = "eth0", *password = NULL; float timeout = 0.f; - bool kb = false, force = false, global = false; + bool kb = false, force = false, global = true; struct timeval tv; const struct TreeNode *cur, *next; struct ether_addr *mac = NULL; @@ -216,7 +216,7 @@ int main (int argc, char **argv) opterr = 0; - while ((n = getopt_long(argc, argv, "abfghi:m:p:r:t:", opts, NULL)) != -1) { + while ((n = getopt_long(argc, argv, "abfhi:lm:p:r:t:", opts, NULL)) != -1) { switch (n) { case 'a': @@ -231,10 +231,6 @@ int main (int argc, char **argv) force = true; break; - case 'g': - global = true; - break; - case 'h': printf("usage: %s [-a] [-b] [-f] [-g] [-i ] [-m ] [-p ]\n", argv[0]); goto end; @@ -243,6 +239,10 @@ int main (int argc, char **argv) iface = optarg; break; + case 'l': + global = false; + break; + case 'm': mac = ether_aton(optarg); if (mac == NULL) { @@ -294,13 +294,13 @@ int main (int argc, char **argv) } - if (kb && ngadmin_setKeepBroadcasting(nga, true) != ERR_OK) + if (ngadmin_setKeepBroadcasting(nga, kb) != ERR_OK) goto end; if (force && ngadmin_forceInterface(nga) != ERR_OK) goto end; - if (global && ngadmin_useGlobalBroadcast(nga, true) != ERR_OK) + if (ngadmin_useGlobalBroadcast(nga, global) != ERR_OK) goto end; /* non-TTY inputs are automatically set to batch mode */ diff --git a/lib/src/libconf.c b/lib/src/libconf.c index e26a722..62cb6ff 100644 --- a/lib/src/libconf.c +++ b/lib/src/libconf.c @@ -19,6 +19,7 @@ struct ngadmin* ngadmin_init (const char *iface) /* allocate main structure */ nga = malloc(sizeof(struct ngadmin)); memset(nga, 0, sizeof(struct ngadmin)); + nga->globalbroad = true; strncpy(nga->iface, iface, IFNAMSIZ - 1); @@ -76,9 +77,10 @@ int ngadmin_useGlobalBroadcast (struct ngadmin *nga, bool value) if (nga == NULL) return ERR_INVARG; - nga->globalbroad = value; - - return ERR_OK; + if (setBroadcastType(nga, value) == 0) + return ERR_OK; + else + return ERR_NET; } diff --git a/lib/src/network.c b/lib/src/network.c index 0306d0a..1e0211b 100644 --- a/lib/src/network.c +++ b/lib/src/network.c @@ -30,16 +30,7 @@ int startNetwork (struct ngadmin *nga) memset(&ifr, 0, sizeof(struct ifreq)); strncpy(ifr.ifr_name, nga->iface, IFNAMSIZ - 1); - - /* get the interface broadcast address */ - ret = ioctl(nga->sock, SIOCGIFBRDADDR, &ifr); - if (ret < 0) { - perror("ioctl(SIOCGIFBRDADDR)"); - close(nga->sock); - return ret; - } - nga->brd = (*(struct sockaddr_in*)&ifr.ifr_addr).sin_addr; - + /* get the interface MAC address */ ret = ioctl(nga->sock, SIOCGIFHWADDR, &ifr); if (ret < 0) { @@ -82,6 +73,36 @@ int startNetwork (struct ngadmin *nga) } +int setBroadcastType (struct ngadmin *nga, bool value) +{ + int ret; + struct ifreq ifr; + + + nga->globalbroad = value; + if (value) { + nga->brd.s_addr = (in_addr_t)0; + return 0; + } + + memset(&ifr, 0, sizeof(struct ifreq)); + strncpy(ifr.ifr_name, nga->iface, IFNAMSIZ - 1); + + /* get the interface broadcast address */ + ret = ioctl(nga->sock, SIOCGIFBRDADDR, &ifr); + if (ret < 0) { + perror("ioctl(SIOCGIFBRDADDR)"); + nga->brd.s_addr = (in_addr_t)0; + nga->globalbroad = true; + return ret; + } + + nga->brd = (*(struct sockaddr_in*)&ifr.ifr_addr).sin_addr; + + return 0; +} + + int stopNetwork (struct ngadmin *nga) { return close(nga->sock); diff --git a/lib/src/network.h b/lib/src/network.h index c071f10..5137c98 100644 --- a/lib/src/network.h +++ b/lib/src/network.h @@ -10,6 +10,9 @@ int startNetwork (struct ngadmin *nga); +int setBroadcastType (struct ngadmin *nga, bool value); + + int stopNetwork (struct ngadmin *nga); -- 2.39.5