]> git.sur5r.net Git - ngadmin/blobdiff - lib/src/network.c
Fix network configuration modification
[ngadmin] / lib / src / network.c
index 09dcd6043142eea70b46c326cab5917c6a7d8891..8f26fc921a010e71dbeddce811b00d2c52fcfd44 100644 (file)
@@ -116,22 +116,20 @@ int forceInterface (struct ngadmin *nga)
        int ret;
        
        
-       /*
-       As described bellow, when you have multiple interfaces, this forces the packet 
-       to go to a particular interface. 
-       */
+       /* as described bellow, when you have multiple interfaces, this
+        * forces the packet to go to a particular interface
+        */
        ret = setsockopt(nga->sock, SOL_SOCKET, SO_BINDTODEVICE, nga->iface, strlen(nga->iface) + 1);
        if (ret < 0) {
                perror("setsockopt(SO_BINDTODEVICE)");
                return ret;
        }
        
-       /*
-       If the switch's IP is not in your network range, for instance because you do 
-       not have DHCP  enabled or you started the switch after it, this allows to 
-       bypass the routing tables and consider every address is directly reachable on 
-       the interface. 
-       */
+       /* if the switch's IP is not in your network range, for instance
+        * because you do not have DHCP  enabled or you started the switch
+        * after your DHCP server, this allows to bypass the routing tables
+        * and consider every address is directly reachable on the interface
+        */
        ret = 1;
        ret = setsockopt(nga->sock, SOL_SOCKET, SO_DONTROUTE, &ret, sizeof(ret));
        if (ret <0) {
@@ -144,27 +142,13 @@ int forceInterface (struct ngadmin *nga)
 }
 
 
-int updateTimeout (struct ngadmin *nga)
-{
-       int ret;
-       
-       
-       /* specify receive timeout */
-       ret = setsockopt(nga->sock, SOL_SOCKET, SO_RCVTIMEO, &nga->timeout, sizeof(struct timeval));
-       if (ret < 0) {
-               perror("setsockopt(SO_RCVTIMEO)");
-               return ret;
-       }
-       
-       
-       return 0;
-}
-
-
 static int checkErrorCode (const struct nsdp_cmd *nc)
 {
        switch (nc->error) {
        
+       case 0:
+               return ERR_OK;
+       
        case ERROR_DENIED:
                return (nc->attr_error == ATTR_PASSWORD) ? ERR_BADPASS : ERR_DENIED;
        
@@ -172,7 +156,7 @@ static int checkErrorCode (const struct nsdp_cmd *nc)
                return ERR_INVARG;
        
        default:
-               return ERR_OK;
+               return ERR_UNKNOWN;
        }
 }
 
@@ -186,10 +170,8 @@ void prepareSend (struct ngadmin *nga, struct nsdp_cmd *nc, unsigned char code)
        memcpy(&nc->client_mac, &nga->localmac, ETH_ALEN);
        nc->remote_addr.sin_family = AF_INET;
        nc->remote_addr.sin_port = htons(SWITCH_PORT);
-       if (sa != NULL) {
+       if (sa != NULL)
                memcpy(&nc->switch_mac, &sa->mac, ETH_ALEN);
-               nc->ports = sa->ports;
-       }
        
        /* destination address selection */
        if (sa != NULL && !nga->keepbroad)
@@ -213,10 +195,8 @@ void prepareRecv (struct ngadmin *nga, struct nsdp_cmd *nc, unsigned char code)
        memcpy(&nc->client_mac, &nga->localmac, ETH_ALEN);
        nc->remote_addr.sin_family = AF_INET;
        nc->remote_addr.sin_port = htons(SWITCH_PORT);
-       if (sa != NULL) {
+       if (sa != NULL)
                memcpy(&nc->switch_mac, &sa->mac, ETH_ALEN);
-               nc->ports = sa->ports;
-       }
        
        /* set filter on switch IP */
        if (sa == NULL)
@@ -304,7 +284,8 @@ int writeRequest (struct ngadmin *nga, List *attr)
        i = sendNsdpPacket(nga->sock, &nc, attr);
        
        /* the list will be filled again by recvNgPacket
-       but normally it will be still empty */
+        * but normally it will be still empty
+        */
        clearList(attr, (void(*)(void*))freeAttr);
        
        if (i >= 0) {
@@ -376,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: