]> git.sur5r.net Git - ngadmin/blobdiff - lib/src/network.c
Factorize string related functions
[ngadmin] / lib / src / network.c
index b48461c1fdb903165f318a98f96478676a390900..f53b4756d5914a8c05706ffd2eef1cd948ff6db5 100644 (file)
@@ -10,7 +10,7 @@
 #include <sys/ioctl.h>
 
 #include <nsdp/attr.h>
-#include <nsdp/misc.h>
+#include <nsdp/str.h>
 #include <nsdp/net.h>
 #include <nsdp/protocol.h>
 
@@ -149,6 +149,10 @@ static int checkErrorCode (const struct nsdp_cmd *nc)
        case 0:
                return ERR_OK;
        
+       case ERROR_READONLY:
+       case ERROR_WRITEONLY:
+               return ERR_INVOP;
+
        case ERROR_DENIED:
                return (nc->attr_error == ATTR_PASSWORD) ? ERR_BADPASS : ERR_DENIED;
        
@@ -237,12 +241,14 @@ int readRequest (struct ngadmin *nga, List *attr)
        if (i == -EINVAL) {
                ret = ERR_INVARG;
                goto end;
+       } else if (i == -ETIMEDOUT) {
+               ret = ERR_TIMEOUT;
+               goto end;
        } else if (i < 0) {
-               ret = (errno == EAGAIN || errno == EWOULDBLOCK) ? ERR_TIMEOUT : ERR_NET;
+               ret = ERR_NET;
                goto end;
        }
        
-       
        /* check the switch error code */
        ret = checkErrorCode(&nc);
        
@@ -296,8 +302,10 @@ int writeRequest (struct ngadmin *nga, List *attr)
        if (i == -EINVAL) {
                ret = ERR_INVARG;
                goto end;
+       } else if (i == -ETIMEDOUT) {
+               ret = ERR_TIMEOUT;
        } else if (i < 0) {
-               ret = (errno == EAGAIN || errno == EWOULDBLOCK) ? ERR_TIMEOUT : ERR_NET;
+               ret = ERR_NET;
                goto end;
        }
        
@@ -314,7 +322,7 @@ end:
 }
 
 
-void extractSwitchAttributes (struct swi_attr *sa, const List *l)
+int extractSwitchAttributes (struct swi_attr *sa, const List *l)
 {
        const ListNode *ln;
        const struct attr *at;
@@ -325,6 +333,12 @@ void extractSwitchAttributes (struct swi_attr *sa, const List *l)
        
        for (ln = l->first; ln != NULL; ln = ln->next) {
                at = ln->data;
+               if (at->size == 0) {
+                       if (at->attr == ATTR_NAME)
+                               continue;
+                       else
+                               return -EMSGSIZE;
+               }
                
                switch (at->attr) {
                
@@ -357,7 +371,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:
@@ -371,9 +388,12 @@ void extractSwitchAttributes (struct swi_attr *sa, const List *l)
                        break;
                
                case ATTR_END:
-                       return;
+                       return 0;
                }
        }
+       
+       
+       return 0;
 }