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 memset(ports, 0, sa->ports);
96 for (ln = attr->first; ln != NULL; ln = ln->next) {
100 if (at->size != sizeof(struct attr_vlan_conf) + sa->ports)
103 for (port = 0; port < sa->ports; port++) {
104 if (avc->ports[port] == VLAN_UNTAGGED)
105 ports[port] = avc->vlan;
111 destroyList(attr, (void(*)(void*))freeAttr);
118 int ngadmin_setVLANPortConf (struct ngadmin *nga, const unsigned char *ports)
120 List *conf_old = NULL, *conf_new = NULL;
124 struct attr_vlan_conf *avc_old, *avc_new;
125 int ret = ERR_OK, port;
129 if (nga == NULL || ports == NULL)
136 /* if nothing is to be changed, do nothing */
137 for (port = 0; port < sa->ports && ports[port] == 0; port++);
138 if (port == sa->ports)
141 /* read old config */
142 conf_old = createEmptyList();
143 pushBackList(conf_old, newEmptyAttr(ATTR_VLAN_PORT_CONF));
144 ret = readRequest(nga, conf_old);
148 filterAttributes(conf_old, ATTR_VLAN_PORT_CONF, ATTR_END);
150 /* check if the switch is in port mode */
151 if (conf_old->first == NULL) {
155 at = conf_old->first->data;
156 if (at->size != sizeof(struct attr_vlan_conf) + sa->ports) {
162 /* merge old config with requested config */
163 conf_new = createEmptyList();
165 for (ln = conf_old->first; ln != NULL; ln = ln->next) {
169 /* check if there is a change on this VLAN */
171 for (port = 0; !change && port < sa->ports; port++) {
172 if (ports[port] == 0)
174 if (ports[port] == avc_old->vlan && avc_old->ports[port] == VLAN_NO)
176 if (ports[port] != avc_old->vlan && avc_old->ports[port] == VLAN_UNTAGGED)
180 /* if the VLAN is not changed, no need to send it to the switch */
184 /* compute new VLAN configuration */
185 avc_new = malloc(sizeof(struct attr_vlan_conf) + sa->ports);
186 avc_new->vlan = avc_old->vlan;
188 for (port = 0; port < sa->ports; port++) {
189 if (ports[port] == 0)
190 avc_new->ports[port] = avc_old->ports[port];
191 else if (ports[port] == avc_new->vlan)
192 avc_new->ports[port] = VLAN_UNTAGGED;
194 avc_new->ports[port] = VLAN_NO;
197 pushBackList(conf_new, newAttr(ATTR_VLAN_PORT_CONF, sizeof(struct attr_vlan_conf) + sa->ports, avc_new));
200 /* if no VLAN is changed, no need to send anything to the switch */
201 if (conf_new->first == NULL)
204 /* send new configuration to the switch */
205 ret = writeRequest(nga, conf_new);
209 destroyList(conf_old, (void(*)(void*))freeAttr);
210 destroyList(conf_new, (void(*)(void*))freeAttr);
217 int ngadmin_getVLANDotAllConf (struct ngadmin *nga, unsigned short *vlans, unsigned char *ports, int *nb)
222 int ret = ERR_OK, total;
223 struct attr_vlan_conf *avc;
227 if (nga == NULL || vlans == NULL || ports== NULL || nb == NULL || *nb <= 0)
238 attr = createEmptyList();
239 pushBackList(attr, newEmptyAttr(ATTR_VLAN_DOT_CONF));
240 ret = readRequest(nga, attr);
244 filterAttributes(attr, ATTR_VLAN_DOT_CONF, ATTR_END);
246 memset(vlans, 0, total * sizeof(unsigned short));
247 memset(ports, 0, total * sa->ports);
249 for (ln = attr->first; ln != NULL; ln = ln->next) {
253 if (at->size != sizeof(struct attr_vlan_conf) + sa->ports)
257 memcpy(ports, avc->ports, sa->ports);
264 break; /* no more room */
269 destroyList(attr, (void(*)(void*))freeAttr);
276 int ngadmin_getVLANDotConf (struct ngadmin *nga, unsigned short vlan, unsigned char *ports)
282 struct attr_vlan_conf *avc;
285 if (nga == NULL || vlan < VLAN_MIN || vlan > VLAN_DOT_MAX || ports == NULL)
287 else if (nga->current == NULL)
291 attr = createEmptyList();
292 pushBackList(attr, newShortAttr(ATTR_VLAN_DOT_CONF, vlan));
293 ret = readRequest(nga, attr);
297 filterAttributes(attr, ATTR_VLAN_DOT_CONF, ATTR_END);
299 memset(ports, 0, nga->current->ports);
301 for (ln = attr->first; ln != NULL; ln = ln->next) {
304 if (avc->vlan == vlan) {
305 memcpy(ports, avc->ports, nga->current->ports);
312 destroyList(attr, (void(*)(void*))freeAttr);
319 int ngadmin_setVLANDotConf (struct ngadmin *nga, unsigned short vlan, const unsigned char *ports)
324 struct attr_vlan_conf *avc;
325 int ret = ERR_OK, port;
328 if (nga == NULL || vlan < VLAN_MIN || vlan > VLAN_DOT_MAX || ports == NULL)
336 /* if nothing is to be changed, do nothing */
337 for (port = 0; port < sa->ports && ports[port] == VLAN_UNSPEC; port++);
338 if (port == sa->ports )
342 attr = createEmptyList();
343 avc = malloc(sizeof(struct attr_vlan_conf) + sa->ports);
349 /* if all is to be changed, we do not need to read old config */
350 if (memchr(ports, VLAN_UNSPEC, sa->ports) != NULL) {
352 pushBackList(attr, newShortAttr(ATTR_VLAN_DOT_CONF, vlan));
353 ret = readRequest(nga, attr);
357 filterAttributes(attr, ATTR_VLAN_DOT_CONF, ATTR_END);
359 if (attr->first != NULL) {
360 at = attr->first->data;
361 memcpy(avc, at->data, sizeof(struct attr_vlan_conf) + sa->ports);
364 clearList(attr, (void(*)(void*))freeAttr);
369 for (port = 0; port < sa->ports; port++) {
370 if (ports[port] != VLAN_UNSPEC)
371 avc->ports[port] = ports[port];
375 pushBackList(attr, newAttr(ATTR_VLAN_DOT_CONF, sizeof(struct attr_vlan_conf) + sa->ports, avc));
376 ret = writeRequest(nga, attr);
381 destroyList(attr, (void(*)(void*))freeAttr);
388 int ngadmin_VLANDestroy (struct ngadmin *nga, unsigned short vlan)
393 if (nga == NULL || vlan < VLAN_MIN || vlan > VLAN_DOT_MAX)
395 else if (nga->current == NULL)
399 attr = createEmptyList();
400 pushBackList(attr, newShortAttr(ATTR_VLAN_DESTROY, vlan));
403 return writeRequest(nga, attr);
407 int ngadmin_getAllPVID (struct ngadmin *nga, unsigned short *ports)
413 struct attr_pvid *ap;
416 if (nga == NULL || ports == NULL)
418 else if (nga->current == NULL)
422 attr = createEmptyList();
423 pushBackList(attr, newEmptyAttr(ATTR_VLAN_PVID));
424 ret = readRequest(nga, attr);
428 filterAttributes(attr, ATTR_VLAN_PVID, ATTR_END);
430 memset(ports, 0, nga->current->ports * sizeof(unsigned short));
432 for (ln = attr->first; ln != NULL; ln = ln->next) {
435 ports[ap->port - 1] = ap->vlan;
440 destroyList(attr, (void(*)(void*))freeAttr);
447 int ngadmin_setPVID (struct ngadmin *nga, unsigned char port, unsigned short vlan)
450 struct attr_pvid *ap;
453 if (nga == NULL || port < 1 || vlan < VLAN_MIN || vlan > VLAN_DOT_MAX)
455 else if (nga->current == NULL)
457 else if (port > nga->current->ports)
461 attr = createEmptyList();
462 ap = malloc(sizeof(struct attr_pvid));
468 pushBackList(attr, newAttr(ATTR_VLAN_PVID, sizeof(struct attr_pvid), ap));
471 return writeRequest(nga, attr);