]> git.sur5r.net Git - ngadmin/commitdiff
Fix network configuration modification
authordarkcoven <admin@darkcoven.tk>
Sat, 19 Oct 2013 19:55:23 +0000 (21:55 +0200)
committerdarkcoven <admin@darkcoven.tk>
Sat, 19 Oct 2013 19:55:23 +0000 (21:55 +0200)
cli/src/com_netconf.c
lib/src/netconf.c
lib/src/network.c
raw/src/attr.c
spy/src/spy.c

index 31b09efba03bd740dd372f1271b764ea4622ff39..277d750c5fb1e561fb06811687a07c5de23d0d52 100644 (file)
@@ -48,9 +48,9 @@ int do_netconf_set (int argc, const char **argv, struct ngadmin *nga)
                return 1;
        }
        
-       memset(&nc, 0, sizeof(struct net_conf));
+       memcpy(&nc, &sa->nc, sizeof(struct net_conf));
        
-       for (k = 0; k < argc; k += 2) {
+       for (k = 0; k < argc - 1; k += 2) {
                if (strcasecmp(argv[k], "dhcp") == 0) {
                        if (strcasecmp(argv[k + 1], "yes") == 0) {
                                nc.dhcp = true;
index 15d0c908e77ab3c097b3c5db0b1458f5dbd39236..70498c3d83fe6c783cab9b697d0edf04705b5a80 100644 (file)
@@ -122,6 +122,7 @@ int ngadmin_setNetConf (struct ngadmin *nga, const struct net_conf *nc)
        List *attr;
        int ret = ERR_OK;
        struct swi_attr *sa;
+       struct net_conf nc_new;
        
        
        if (nga == NULL || nc == NULL)
@@ -131,20 +132,36 @@ int ngadmin_setNetConf (struct ngadmin *nga, const struct net_conf *nc)
        if (sa == NULL)
                return ERR_NOTLOG;
        
+       memcpy(&nc_new, &sa->nc, sizeof(struct net_conf));
+       if (nc->ip.s_addr != 0)
+               nc_new.ip = nc->ip;
+       if (nc->netmask.s_addr != 0)
+               nc_new.netmask = nc->netmask;
+       if (nc->gw.s_addr != 0)
+               nc_new.gw = nc->gw;
+       nc_new.dhcp = nc->dhcp;
+       
+       /* gateway must be in the network range */
+       if ((nc_new.ip.s_addr & nc_new.netmask.s_addr) != (nc_new.gw.s_addr & nc_new.netmask.s_addr))
+               return ERR_INVARG;
+       
+       /* no need to send anything if old and new configurations are the same */
+       if (memcmp(&nc_new, &sa->nc, sizeof(struct net_conf)) == 0)
+               return ERR_OK;
+       
        
        attr = createEmptyList();
        
-       if (nc->dhcp) {
-               pushBackList(attr, newShortAttr(ATTR_DHCP, 1));
+       /* Note: DHCP attribute is special, it is 2 two bytes long when sent
+        * by the switch but only 1 byte long when sent by the client
+        */
+       if (nc_new.dhcp) {
+               pushBackList(attr, newByteAttr(ATTR_DHCP, 1));
        } else {
-               pushBackList(attr, newShortAttr(ATTR_DHCP, 0));
-               /* only add non-null values */
-               if (nc->ip.s_addr != 0)
-                       pushBackList(attr, newAddrAttr(ATTR_IP, nc->ip));
-               if (nc->netmask.s_addr != 0)
-                       pushBackList(attr, newAddrAttr(ATTR_NETMASK, nc->netmask));
-               if (nc->gw.s_addr != 0)
-                       pushBackList(attr, newAddrAttr(ATTR_GATEWAY, nc->gw));
+               pushBackList(attr, newAddrAttr(ATTR_IP, nc_new.ip));
+               pushBackList(attr, newAddrAttr(ATTR_NETMASK, nc_new.netmask));
+               pushBackList(attr, newAddrAttr(ATTR_GATEWAY, nc_new.gw));
+               pushBackList(attr, newByteAttr(ATTR_DHCP, 0));
        }
        
        ret = writeRequest(nga, attr);
@@ -153,16 +170,7 @@ int ngadmin_setNetConf (struct ngadmin *nga, const struct net_conf *nc)
        
        
        /* update local values */
-       sa->nc.dhcp = nc->dhcp;
-       if (!nc->dhcp) {
-               if (nc->ip.s_addr !=0)
-                       sa->nc.ip = nc->ip;
-               if (nc->netmask.s_addr != 0)
-                       sa->nc.netmask = nc->netmask;
-               if (nc->gw.s_addr != 0)
-                       sa->nc.gw = nc->gw;
-       }
-       
+       memcpy(&sa->nc, &nc_new, sizeof(struct net_conf));
        
 end:
        
index b48461c1fdb903165f318a98f96478676a390900..8f26fc921a010e71dbeddce811b00d2c52fcfd44 100644 (file)
@@ -357,7 +357,10 @@ void extractSwitchAttributes (struct swi_attr *sa, const List *l)
                        break;
                
                case ATTR_DHCP:
-                       sa->nc.dhcp = (ntohs(*(unsigned short*)at->data) == 1);
+                       /* Note: DHCP attribute is special, it is 2 two bytes long when sent
+                        * by the switch but only 1 byte long when sent by the client
+                        */
+                       sa->nc.dhcp = (at->size == 2) && ((*(unsigned short*)at->data) == 1);
                        break;
                
                case ATTR_FIRM_VER:
index 70f3a18760d21cd434dd5f27da82cb59daa10926..aec75843bd1ad4af4e04a4283614924e918bcc62 100644 (file)
@@ -556,8 +556,16 @@ static int processAttr (struct attr *at, bool encode)
                return 0;
        
        case ATTR_DHCP:
-               if (at->size != 2)
+               /* Note: DHCP attribute is special, it is 2 two bytes long
+                * when sent by the switch but only 1 byte long when sent
+                * by the client
+                */
+               if (at->size == 1) {
+                       *byte = (*byte != 0);
+                       return 0;
+               } else if (at->size > 2) {
                        return -EMSGSIZE;
+               }
                
                if (!encode)
                        *word = ntohs(*word);
index ead684dfb163649de4f1c6c11d7091092a5b278e..20068906348a4ceee75ac5885c34c24bbd9c3b51 100644 (file)
@@ -276,7 +276,7 @@ static void print_attr (const struct attr *at)
                break;
        
        case ATTR_DHCP:
-               printf("\tDHCP = %s\n", *byte ? "yes" : "no");
+               printf("\tDHCP = %s\n", (at->size == 1 ? *byte : *word) ? "yes" : "no");
                break;
        
        case ATTR_FIRM_VER: