From: darkcoven Date: Wed, 30 Nov 2011 11:00:00 +0000 (+0100) Subject: Added support for changing VLAN 802.1q and PVID configuration. X-Git-Url: https://git.sur5r.net/?p=ngadmin;a=commitdiff_plain;h=602baa14950a0a9144df0893a3db2cb711a52272 Added support for changing VLAN 802.1q and PVID configuration. Lib: fixed the issue when changing only a part of network configuration. --- diff --git a/cli/com_qos.c b/cli/com_qos.c index cd61741..b690424 100644 --- a/cli/com_qos.c +++ b/cli/com_qos.c @@ -117,7 +117,7 @@ bool do_qos_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { } - printf("QOS mode: "); + printf("QoS mode: "); if ( s==QOS_DOT ) { printf("802.1p\n"); diff --git a/cli/com_vlan.c b/cli/com_vlan.c index 14be784..813bc18 100644 --- a/cli/com_vlan.c +++ b/cli/com_vlan.c @@ -94,6 +94,41 @@ bool do_vlan_8021q_show (int nb, const char **com, struct ngadmin *nga) { +bool do_vlan_8021q_vlan_del (int nb, const char **com, struct ngadmin *nga) { + + const struct swi_attr *sa; + unsigned short vlan; + int i; + + + if ( nb!=1 ) { + printf("Usage: vlan 8021q vlan del \n"); + return false; + } + + if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { + printf("must be logged\n"); + return false; + } + + vlan=strtoul(com[0], NULL, 0); + + if ( vlan<1 || vlan>VLAN_MAX ) { + printf("vlan out of range\n"); + return false; + } + + + i=ngadmin_VLANDestroy(nga, vlan); + printErrCode(i); + + + return true; + +} + + + bool do_vlan_mode_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { int i, t, ret=true; @@ -130,6 +165,48 @@ bool do_vlan_mode_show (int nb UNUSED, const char **com UNUSED, struct ngadmin * +bool do_vlan_pvid_set (int nb, const char **com, struct ngadmin *nga) { + + const struct swi_attr *sa; + unsigned char port; + unsigned short vlan; + int i; + + + if ( nb!=2 ) { + printf("Usage: vlan pvid set \n"); + return false; + } + + if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { + printf("must be logged\n"); + return false; + } + + port=strtoul(com[0], NULL, 0); + vlan=strtoul(com[1], NULL, 0); + + if ( port<1 || port>sa->ports ) { + printf("port out of range\n"); + return false; + } + + if ( vlan<1 || vlan>VLAN_MAX ) { + printf("vlan out of range\n"); + return false; + } + + + i=ngadmin_setPVID(nga, port, vlan); + printErrCode(i); + + + return true; + +} + + + bool do_vlan_pvid_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { unsigned short *ports=NULL; @@ -145,7 +222,7 @@ bool do_vlan_pvid_show (int nb UNUSED, const char **com UNUSED, struct ngadmin * } ports=malloc(sa->ports*sizeof(unsigned short)); - i=ngadmin_getPVID(nga, ports); + i=ngadmin_getAllPVID(nga, ports); if ( i!=ERR_OK ) { printErrCode(i); ret=false; diff --git a/cli/commands.c b/cli/commands.c index 70740b2..552d18e 100644 --- a/cli/commands.c +++ b/cli/commands.c @@ -93,7 +93,9 @@ bool do_tree (int nb, const char **com, struct ngadmin *nga); // vlan bool do_vlan_8021q_show (int nb, const char **com, struct ngadmin *nga); +bool do_vlan_8021q_vlan_del (int nb, const char **com, struct ngadmin *nga); bool do_vlan_mode_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga); +bool do_vlan_pvid_set (int nb, const char **com, struct ngadmin *nga); bool do_vlan_pvid_show (int nb, const char **com, struct ngadmin *nga); @@ -183,7 +185,7 @@ COM_ROOT_START(coms) COM_TERM(show, do_vlan_8021q_show, true) COM_START(vlan) COM_TERM(add, NULL, true) - COM_TERM(del, NULL, true) + COM_TERM(del, do_vlan_8021q_vlan_del, true) COM_TERM(flush, NULL, true) COM_END COM_END @@ -194,7 +196,7 @@ COM_ROOT_START(coms) COM_START(port) COM_END COM_START(pvid) - COM_TERM(set, NULL, true) + COM_TERM(set, do_vlan_pvid_set, true) COM_TERM(show, do_vlan_pvid_show, false) COM_END COM_END diff --git a/cli/common.c b/cli/common.c index 59f7058..0911a1a 100644 --- a/cli/common.c +++ b/cli/common.c @@ -96,7 +96,7 @@ void displaySwitchTab (const struct swi_attr *sa, int nb) { printf("Num\tMac\t\t\tProduct\t\tName\t\t\tIP/mask\t\t\tDHCP\tPorts\tFirmware\n"); for (i=0; iip.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)); @@ -1158,10 +1159,12 @@ int ngadmin_setNetConf (struct ngadmin *nga, const struct net_conf *nc) { } - if ( nc->dhcp ) { - sa->nc.dhcp=true; - } else { - memcpy(&sa->nc, nc, sizeof(struct net_conf)); + // 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; } @@ -1214,6 +1217,30 @@ int ngadmin_getVLANType (struct ngadmin *nga, int *t) { +// ------------------------------------------------- +int ngadmin_setVLANType (struct ngadmin *nga, int t) { + + List *attr; + struct swi_attr *sa; + + + if ( nga==NULL || t<1 || t>4 ) { + return ERR_INVARG; + } else if ( (sa=nga->current)==NULL ) { + return ERR_NOTLOG; + } + + + attr=createEmptyList(); + pushBackList(attr, newByteAttr(ATTR_VLAN_TYPE, t)); + + + return writeRequest(nga, attr); + +} + + + // ------------------------------------------------------------------------------------------------------ int ngadmin_getVLANDotAllConf (struct ngadmin *nga, unsigned short *vlans, unsigned char *ports, int *nb) { @@ -1317,8 +1344,74 @@ int ngadmin_getVLANDotConf (struct ngadmin *nga, unsigned short vlan, unsigned c -// ------------------------------------------------------------- -int ngadmin_getPVID (struct ngadmin *nga, unsigned short *ports) { +// ---------------------------------------------------------------------------------------------- +int ngadmin_setVLANDotConf (struct ngadmin *nga, unsigned short vlan, const unsigned char *ports) { + + List *attr; + struct swi_attr *sa; + int i; + char *p; + + + if ( nga==NULL || ports==NULL || vlan<1 || vlan>VLAN_MAX ) { + return ERR_INVARG; + } else if ( (sa=nga->current)==NULL ) { + return ERR_NOTLOG; + } + + + + attr=createEmptyList(); + p=malloc(4); + *(unsigned short*)p=htons(vlan); + + for (i=1; i<=sa->ports; ++i) { + if ( ports[i-1]==VLAN_TAGGED ) { // tagged + p[3]|=(1<<(sa->ports-i)); + } else if ( ports[i-1]==VLAN_UNTAGGED ) { // untagged + p[2]|=(1<<(sa->ports-i)); + } + } + + // tagged ports must be also present in untagged ports + p[2]|=ports[3]; + + + pushBackList(attr, newAttr(ATTR_VLAN_DOT_CONF, 4, p)); + + + return writeRequest(nga, attr); + +} + + + +// --------------------------------------------------------------- +int ngadmin_VLANDestroy (struct ngadmin *nga, unsigned short vlan) { + + List *attr; + struct swi_attr *sa; + + + if ( nga==NULL || vlan<1 || vlan>VLAN_MAX ) { + return ERR_INVARG; + } else if ( (sa=nga->current)==NULL ) { + return ERR_NOTLOG; + } + + + attr=createEmptyList(); + pushBackList(attr, newShortAttr(ATTR_VLAN_DESTROY, vlan)); + + + return writeRequest(nga, attr); + +} + + + +// ---------------------------------------------------------------- +int ngadmin_getAllPVID (struct ngadmin *nga, unsigned short *ports) { List *attr; ListNode *ln; @@ -1360,4 +1453,34 @@ int ngadmin_getPVID (struct ngadmin *nga, unsigned short *ports) { +// ------------------------------------------------------------------------------- +int ngadmin_setPVID (struct ngadmin *nga, unsigned char port, unsigned short vlan) { + + List *attr; + struct swi_attr *sa; + char *p; + + + if ( nga==NULL || port<1 || vlan<1 || vlan>VLAN_MAX ) { + return ERR_INVARG; + } else if ( (sa=nga->current)==NULL ) { + return ERR_NOTLOG; + } else if ( port>sa->ports ) { + return ERR_INVARG; + } + + + attr=createEmptyList(); + p=malloc(3); + p[0]=port; + *(unsigned short*)&p[1]=htons(vlan); + + pushBackList(attr, newAttr(ATTR_VLAN_PVID, 3, p)); + + + return writeRequest(nga, attr);; + +} + + diff --git a/lib/src/protocol.c b/lib/src/protocol.c index d1be292..61dd6fa 100644 --- a/lib/src/protocol.c +++ b/lib/src/protocol.c @@ -9,6 +9,28 @@ 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; + +} + + + // ------------------- int min (int a, int b) { return afirst; ln!=NULL; ln=ln->next) { at=ln->data; @@ -297,13 +317,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: diff --git a/lib/src/protocol.h b/lib/src/protocol.h index 3b26f1d..f535aba 100644 --- a/lib/src/protocol.h +++ b/lib/src/protocol.h @@ -60,6 +60,10 @@ extern const struct ether_addr nullMac; + +// +int trim (char *txt, int start); + // int min (int a, int b);