11 int ngadmin_getVLANType (struct ngadmin *nga, int *t)
18 if (nga == NULL || t == NULL)
20 else if (nga->current == NULL)
24 attr = createEmptyList();
25 pushBackList(attr, newEmptyAttr(ATTR_VLAN_TYPE));
26 ret=readRequest(nga, attr);
30 filterAttributes(attr, ATTR_VLAN_TYPE, ATTR_END);
34 if (attr->first != NULL) {
35 at = attr->first->data;
36 *t =(int)*(char*)at->data;
41 destroyList(attr, (void(*)(void*))freeAttr);
48 int ngadmin_setVLANType (struct ngadmin *nga, int t)
53 if (nga == NULL || t < 1 || t > 4)
55 else if (nga->current == NULL)
59 attr = createEmptyList();
60 pushBackList(attr, newByteAttr(ATTR_VLAN_TYPE, t));
63 return writeRequest(nga, attr);
67 int ngadmin_getVLANPortConf (struct ngadmin *nga, unsigned char *ports)
73 struct attr_vlan_conf *avc;
78 if (nga == NULL || ports== NULL)
86 attr = createEmptyList();
87 pushBackList(attr, newEmptyAttr(ATTR_VLAN_PORT_CONF));
88 ret = readRequest(nga, attr);
92 filterAttributes(attr, ATTR_VLAN_PORT_CONF, ATTR_END);
94 if (attr->first == NULL) {
99 memset(ports, 0, sa->ports);
101 for (ln = attr->first; ln != NULL; ln = ln->next) {
105 if (at->size != sizeof(struct attr_vlan_conf) + sa->ports) {
110 for (port = 0; port < sa->ports; port++) {
111 if (avc->ports[port] == VLAN_UNTAGGED)
112 ports[port] = avc->vlan;
118 destroyList(attr, (void(*)(void*))freeAttr);
125 int ngadmin_setVLANPortConf (struct ngadmin *nga, const unsigned char *ports)
127 List *conf_old = NULL, *conf_new = NULL;
131 struct attr_vlan_conf *avc_old, *avc_new;
132 int ret = ERR_OK, port;
135 if (nga == NULL || ports == NULL)
142 /* if nothing is to be changed, do nothing */
143 for (port = 0; port < sa->ports && ports[port] == 0; port++);
144 if (port == sa->ports)
147 /* read old config */
148 conf_old = createEmptyList();
149 pushBackList(conf_old, newEmptyAttr(ATTR_VLAN_PORT_CONF));
150 ret = readRequest(nga, conf_old);
154 filterAttributes(conf_old, ATTR_VLAN_PORT_CONF, ATTR_END);
156 if (conf_old->first == NULL) {
161 /* merge old config with requested config */
162 conf_new = createEmptyList();
163 avc_new = malloc(sizeof(struct attr_vlan_conf) + sa->ports);
165 for (ln = conf_old->first; ln != NULL; ln = ln->next) {
169 if (at->size != sizeof(struct attr_vlan_conf) + sa->ports) {
175 /* compute new VLAN configuration */
176 avc_new->vlan = avc_old->vlan;
178 for (port = 0; port < sa->ports; port++) {
179 if (ports[port] == 0)
180 avc_new->ports[port] = avc_old->ports[port];
181 else if (ports[port] == avc_new->vlan)
182 avc_new->ports[port] = VLAN_UNTAGGED;
184 avc_new->ports[port] = VLAN_NO;
187 /* only add it if it is different from old config */
188 if (memcmp(avc_old->ports, avc_new->ports, sa->ports) != 0) {
189 pushBackList(conf_new, newAttr(ATTR_VLAN_PORT_CONF, sizeof(struct attr_vlan_conf) + sa->ports, avc_new));
190 avc_new = malloc(sizeof(struct attr_vlan_conf) + sa->ports);
196 /* if no VLAN is changed, no need to send anything to the switch */
197 if (conf_new->first == NULL)
200 /* send new configuration to the switch */
201 ret = writeRequest(nga, conf_new);
205 destroyList(conf_old, (void(*)(void*))freeAttr);
206 destroyList(conf_new, (void(*)(void*))freeAttr);
213 int ngadmin_getVLANDotAllConf (struct ngadmin *nga, unsigned short *vlans, unsigned char *ports, int *nb)
218 int ret = ERR_OK, total;
219 struct attr_vlan_conf *avc;
223 if (nga == NULL || vlans == NULL || ports== NULL || nb == NULL || *nb <= 0)
234 attr = createEmptyList();
235 pushBackList(attr, newEmptyAttr(ATTR_VLAN_DOT_CONF));
236 ret = readRequest(nga, attr);
240 filterAttributes(attr, ATTR_VLAN_DOT_CONF, ATTR_END);
242 if (attr->first == NULL) {
247 memset(vlans, 0, total * sizeof(unsigned short));
248 memset(ports, 0, total * sa->ports);
250 for (ln = attr->first; ln != NULL; ln = ln->next) {
254 if (at->size != sizeof(struct attr_vlan_conf) + sa->ports) {
260 memcpy(ports, avc->ports, sa->ports);
267 break; /* no more room */
272 destroyList(attr, (void(*)(void*))freeAttr);
279 int ngadmin_getVLANDotConf (struct ngadmin *nga, unsigned short vlan, unsigned char *ports)
286 struct attr_vlan_conf *avc;
289 if (nga == NULL || vlan < VLAN_MIN || vlan > VLAN_DOT_MAX || ports == NULL)
296 attr = createEmptyList();
297 pushBackList(attr, newShortAttr(ATTR_VLAN_DOT_CONF, vlan));
298 ret = readRequest(nga, attr);
302 filterAttributes(attr, ATTR_VLAN_DOT_CONF, ATTR_END);
304 if (attr->first == NULL) {
309 memset(ports, 0, sa->ports);
311 for (ln = attr->first; ln != NULL; ln = ln->next) {
315 if (at->size != sizeof(struct attr_vlan_conf) + sa->ports) {
320 if (avc->vlan == vlan) {
321 memcpy(ports, avc->ports, sa->ports);
328 destroyList(attr, (void(*)(void*))freeAttr);
335 int ngadmin_setVLANDotConf (struct ngadmin *nga, unsigned short vlan, const unsigned char *ports)
340 struct attr_vlan_conf *avc;
341 int ret = ERR_OK, port;
344 if (nga == NULL || vlan < VLAN_MIN || vlan > VLAN_DOT_MAX || ports == NULL)
352 /* if nothing is to be changed, do nothing */
353 for (port = 0; port < sa->ports && ports[port] == VLAN_UNSPEC; port++);
354 if (port == sa->ports )
358 attr = createEmptyList();
359 avc = malloc(sizeof(struct attr_vlan_conf) + sa->ports);
365 /* if all is to be changed, we do not need to read old config */
366 if (memchr(ports, VLAN_UNSPEC, sa->ports) != NULL) {
368 pushBackList(attr, newShortAttr(ATTR_VLAN_DOT_CONF, vlan));
369 ret = readRequest(nga, attr);
373 filterAttributes(attr, ATTR_VLAN_DOT_CONF, ATTR_END);
375 /* check if the switch is in 802.1Q mode */
376 if (attr->first == NULL) {
380 at = attr->first->data;
381 if (at->size != sizeof(struct attr_vlan_conf) + sa->ports) {
387 memcpy(avc, at->data, sizeof(struct attr_vlan_conf) + sa->ports);
388 clearList(attr, (void(*)(void*))freeAttr);
393 for (port = 0; port < sa->ports; port++) {
394 if (ports[port] != VLAN_UNSPEC)
395 avc->ports[port] = ports[port];
399 pushBackList(attr, newAttr(ATTR_VLAN_DOT_CONF, sizeof(struct attr_vlan_conf) + sa->ports, avc));
400 ret = writeRequest(nga, attr);
405 destroyList(attr, (void(*)(void*))freeAttr);
412 int ngadmin_VLANDestroy (struct ngadmin *nga, unsigned short vlan)
417 if (nga == NULL || vlan < VLAN_MIN || vlan > VLAN_DOT_MAX)
419 else if (nga->current == NULL)
423 attr = createEmptyList();
424 pushBackList(attr, newShortAttr(ATTR_VLAN_DESTROY, vlan));
427 return writeRequest(nga, attr);
431 int ngadmin_getAllPVID (struct ngadmin *nga, unsigned short *ports)
437 struct attr_pvid *ap;
440 if (nga == NULL || ports == NULL)
442 else if (nga->current == NULL)
446 attr = createEmptyList();
447 pushBackList(attr, newEmptyAttr(ATTR_VLAN_PVID));
448 ret = readRequest(nga, attr);
452 filterAttributes(attr, ATTR_VLAN_PVID, ATTR_END);
454 memset(ports, 0, nga->current->ports * sizeof(unsigned short));
456 for (ln = attr->first; ln != NULL; ln = ln->next) {
459 ports[ap->port - 1] = ap->vlan;
464 destroyList(attr, (void(*)(void*))freeAttr);
471 int ngadmin_setPVID (struct ngadmin *nga, unsigned char port, unsigned short vlan)
474 struct attr_pvid *ap;
477 if (nga == NULL || port < 1 || vlan < VLAN_MIN || vlan > VLAN_DOT_MAX)
479 else if (nga->current == NULL)
481 else if (port > nga->current->ports)
485 attr = createEmptyList();
486 ap = malloc(sizeof(struct attr_pvid));
492 pushBackList(attr, newAttr(ATTR_VLAN_PVID, sizeof(struct attr_pvid), ap));
495 return writeRequest(nga, attr);