]> git.sur5r.net Git - ngadmin/blobdiff - lib/src/protocol.c
CLI: added the possibility to interrupt hanging commands with CTRL+C.
[ngadmin] / lib / src / protocol.c
index 51e06118efa560fe156d6c6f72224e38efa3b7de..05677a848f2d360fcb0ae81124681d0da0e955ba 100644 (file)
@@ -4,8 +4,24 @@
 
 
 
-
-const struct ether_addr nullMac={.ether_addr_octet={0, 0, 0, 0, 0, 0}};
+// ----------------------------
+int trim (char *txt, int start) {
+ char *p, c;
+ if ( txt==NULL ) {
+  return 0;
+ }
+ //for (p=txt; *p!=0; p++);
+ p=txt+start;
+ for (p--; p>=txt && ( (c=*p)==' ' || c=='\n' ); *p--=0);
+ return p-txt+1;
+}
 
 
 
@@ -21,7 +37,7 @@ void initNgHeader (struct ng_header *nh, char code, const struct ether_addr *cli
  
  
  memset(nh, 0, sizeof(struct ng_header));
- nh->unk1=1;
+ nh->version=1;
  nh->code=code;
  
  memcpy(nh->client_mac, client_mac, ETH_ALEN);
@@ -42,7 +58,7 @@ void initNgHeader (struct ng_header *nh, char code, const struct ether_addr *cli
 bool validateNgHeader (const struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum) {
  
  
- if ( nh->unk1!=1 ) {
+ if ( nh->version!=1 ) {
   return false;
  }
  
@@ -50,11 +66,11 @@ bool validateNgHeader (const struct ng_header *nh, char code, const struct ether
   return false;
  }
  
- if ( nh->unk2!=0 ) {
+ if ( nh->unk1!=0 ) {
   return false;
  }
  
- if ( *(unsigned short*)nh->unk3!=0 ) {
+ if ( *(unsigned short*)nh->unk2!=0 ) {
   return false;
  }
  
@@ -70,7 +86,11 @@ bool validateNgHeader (const struct ng_header *nh, char code, const struct ether
   return false;
  }
  
- if ( *(unsigned int*)nh->unk4!=0 ) {
+ if ( memcmp(nh->proto_id, "NSDP", 4)!=0 ) {
+  return false;
+ }
+ if ( *(unsigned int*)nh->unk3!=0 ) {
   return false;
  }
  
@@ -178,7 +198,46 @@ struct attr* newByteAttr (unsigned short attr, unsigned char value) {
  
  *v=value;
  
- return newAttr(attr, 1, v);
+ return newAttr(attr, sizeof(char), v);
+}
+
+
+
+// ---------------------------------------------------------
+struct attr* newShortAttr (unsigned short attr, short value) {
+ short *v=malloc(sizeof(short));
+ *v=htons(value);
+ return newAttr(attr, sizeof(short), v);
+}
+
+
+
+// -----------------------------------------------------
+struct attr* newIntAttr (unsigned short attr, int value) {
+ int *v=malloc(sizeof(int));
+ *v=htonl(value);
+ return newAttr(attr, sizeof(int), v);
+}
+
+
+
+// -----------------------------------------------------------------
+struct attr* newAddrAttr (unsigned short attr, struct in_addr value) {
+ struct in_addr *v=malloc(sizeof(struct in_addr));
+ *v=value;
+ return newAttr(attr, sizeof(struct in_addr), v);
  
 }
 
@@ -196,31 +255,32 @@ void freeAttr (struct attr *at) {
 
 
 
-// ------------------------------------------------------------------------------------------
-List* extractPacketAttributes (struct ng_packet *np, char *error, unsigned short *attr_error) {
+// --------------------------------------------------------------------------------------------------------------
+int extractPacketAttributes (struct ng_packet *np, unsigned char *error, unsigned short *attr_error, List *attr) {
  
- List *l;
  struct attr *at;
+ int ret=0;
  
  
- if ( error!=NULL ) {
-  *error=np->nh->error;
- }
- if ( attr_error!=NULL ) {
-  *attr_error=ntohs(np->nh->attr);
- }
- l=createEmptyList();
+ if ( error!=NULL ) *error=np->nh->error;
+ if ( attr_error!=NULL ) *attr_error=ntohs(np->nh->attr);
  
  while ( getPacketTotalSize(np)<np->maxlen ) {
   
+  // no room for an attribute header: error
+  if ( getPacketTotalSize(np)+(int)sizeof(struct attr_header)>np->maxlen ) {
+   ret=-1;
+   break;
+  }
+  
   at=malloc(sizeof(struct attr));
   at->attr=ntohs(np->ah->attr);
   at->size=ntohs(np->ah->size);
   
-  if ( getPacketTotalSize(np)+at->size>np->maxlen ) {
+  // attribute data bigger than the remaining size: error
+  if ( getPacketTotalSize(np)+(int)sizeof(struct attr_header)+at->size>np->maxlen ) {
    free(at);
+   ret=-1;
    break;
   }
   
@@ -231,18 +291,18 @@ List* extractPacketAttributes (struct ng_packet *np, char *error, unsigned short
    memcpy(at->data, np->ah->data, at->size);
   }
   
-  pushBackList(l, at);
+  pushBackList(attr, at);
   
-  if ( at->attr==ATTR_END ) {
-   break;
-  }
+  // stop on an END attribute
+  if ( at->attr==ATTR_END ) break;
   
+  // move to next attribute
   np->ah=(struct attr_header*)(np->ah->data+at->size);
   
  }
  
  
- return l;
+ return ret;
  
 }
 
@@ -258,8 +318,6 @@ void extractSwitchAttributes (struct swi_attr *sa, const List *l) {
  
  memset(sa, 0, sizeof(struct swi_attr));
  
- // FIXME: mutex lock ?
  for (ln=l->first; ln!=NULL; ln=ln->next) {
   at=ln->data;
   
@@ -268,13 +326,13 @@ void extractSwitchAttributes (struct swi_attr *sa, const List *l) {
    case ATTR_PRODUCT:
     len=min(at->size, PRODUCT_SIZE);
     memcpy(sa->product, at->data, len);
-    //trim(sa->product, len); // FIXME
+    trim(sa->product, len);
    break;
    
    case ATTR_NAME:
     len=min(at->size, NAME_SIZE);
     memcpy(sa->name, at->data, len);
-    //trim(sa->name, len); // FIXME
+    trim(sa->name, len);
    break;
    
    case ATTR_MAC: