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;
128 if (nga == NULL || ports == NULL)
135 /* if nothing is to be changed, do nothing */
136 for (port = 0; port < sa->ports && ports[port] == 0; port++);
137 if (port == sa->ports)
140 /* read old config */
141 conf_old = createEmptyList();
142 pushBackList(conf_old, newEmptyAttr(ATTR_VLAN_PORT_CONF));
143 ret = readRequest(nga, conf_old);
147 filterAttributes(conf_old, ATTR_VLAN_PORT_CONF, ATTR_END);
149 if (conf_old->first == NULL) {
154 /* merge old config with requested config */
155 conf_new = createEmptyList();
156 avc_new = malloc(sizeof(struct attr_vlan_conf) + sa->ports);
158 for (ln = conf_old->first; ln != NULL; ln = ln->next) {
162 if (at->size != sizeof(struct attr_vlan_conf) + sa->ports) {
168 /* compute new VLAN configuration */
169 avc_new->vlan = avc_old->vlan;
171 for (port = 0; port < sa->ports; port++) {
172 if (ports[port] == 0)
173 avc_new->ports[port] = avc_old->ports[port];
174 else if (ports[port] == avc_new->vlan)
175 avc_new->ports[port] = VLAN_UNTAGGED;
177 avc_new->ports[port] = VLAN_NO;
180 /* only add it if it is different from old config */
181 if (memcmp(avc_old->ports, avc_new->ports, sa->ports) != 0) {
182 pushBackList(conf_new, newAttr(ATTR_VLAN_PORT_CONF, sizeof(struct attr_vlan_conf) + sa->ports, avc_new));
183 avc_new = malloc(sizeof(struct attr_vlan_conf) + sa->ports);
189 /* if no VLAN is changed, no need to send anything to the switch */
190 if (conf_new->first == NULL)
193 /* send new configuration to the switch */
194 ret = writeRequest(nga, conf_new);
198 destroyList(conf_old, (void(*)(void*))freeAttr);
199 destroyList(conf_new, (void(*)(void*))freeAttr);
206 int ngadmin_getVLANDotAllConf (struct ngadmin *nga, unsigned short *vlans, unsigned char *ports, int *nb)
211 int ret = ERR_OK, total;
212 struct attr_vlan_conf *avc;
216 if (nga == NULL || vlans == NULL || ports== NULL || nb == NULL || *nb <= 0)
227 attr = createEmptyList();
228 pushBackList(attr, newEmptyAttr(ATTR_VLAN_DOT_CONF));
229 ret = readRequest(nga, attr);
233 filterAttributes(attr, ATTR_VLAN_DOT_CONF, ATTR_END);
235 memset(vlans, 0, total * sizeof(unsigned short));
236 memset(ports, 0, total * sa->ports);
238 for (ln = attr->first; ln != NULL; ln = ln->next) {
242 if (at->size != sizeof(struct attr_vlan_conf) + sa->ports)
246 memcpy(ports, avc->ports, sa->ports);
253 break; /* no more room */
258 destroyList(attr, (void(*)(void*))freeAttr);
265 int ngadmin_getVLANDotConf (struct ngadmin *nga, unsigned short vlan, unsigned char *ports)
271 struct attr_vlan_conf *avc;
274 if (nga == NULL || vlan < VLAN_MIN || vlan > VLAN_DOT_MAX || ports == NULL)
276 else if (nga->current == NULL)
280 attr = createEmptyList();
281 pushBackList(attr, newShortAttr(ATTR_VLAN_DOT_CONF, vlan));
282 ret = readRequest(nga, attr);
286 filterAttributes(attr, ATTR_VLAN_DOT_CONF, ATTR_END);
288 memset(ports, 0, nga->current->ports);
290 for (ln = attr->first; ln != NULL; ln = ln->next) {
293 if (avc->vlan == vlan) {
294 memcpy(ports, avc->ports, nga->current->ports);
301 destroyList(attr, (void(*)(void*))freeAttr);
308 int ngadmin_setVLANDotConf (struct ngadmin *nga, unsigned short vlan, const unsigned char *ports)
313 struct attr_vlan_conf *avc;
314 int ret = ERR_OK, port;
317 if (nga == NULL || vlan < VLAN_MIN || vlan > VLAN_DOT_MAX || ports == NULL)
325 /* if nothing is to be changed, do nothing */
326 for (port = 0; port < sa->ports && ports[port] == VLAN_UNSPEC; port++);
327 if (port == sa->ports )
331 attr = createEmptyList();
332 avc = malloc(sizeof(struct attr_vlan_conf) + sa->ports);
338 /* if all is to be changed, we do not need to read old config */
339 if (memchr(ports, VLAN_UNSPEC, sa->ports) != NULL) {
341 pushBackList(attr, newShortAttr(ATTR_VLAN_DOT_CONF, vlan));
342 ret = readRequest(nga, attr);
346 filterAttributes(attr, ATTR_VLAN_DOT_CONF, ATTR_END);
348 /* check if the switch is in 802.1Q mode */
349 if (attr->first == NULL) {
353 at = attr->first->data;
354 if (at->size != sizeof(struct attr_vlan_conf) + sa->ports) {
360 memcpy(avc, at->data, sizeof(struct attr_vlan_conf) + sa->ports);
361 clearList(attr, (void(*)(void*))freeAttr);
366 for (port = 0; port < sa->ports; port++) {
367 if (ports[port] != VLAN_UNSPEC)
368 avc->ports[port] = ports[port];
372 pushBackList(attr, newAttr(ATTR_VLAN_DOT_CONF, sizeof(struct attr_vlan_conf) + sa->ports, avc));
373 ret = writeRequest(nga, attr);
378 destroyList(attr, (void(*)(void*))freeAttr);
385 int ngadmin_VLANDestroy (struct ngadmin *nga, unsigned short vlan)
390 if (nga == NULL || vlan < VLAN_MIN || vlan > VLAN_DOT_MAX)
392 else if (nga->current == NULL)
396 attr = createEmptyList();
397 pushBackList(attr, newShortAttr(ATTR_VLAN_DESTROY, vlan));
400 return writeRequest(nga, attr);
404 int ngadmin_getAllPVID (struct ngadmin *nga, unsigned short *ports)
410 struct attr_pvid *ap;
413 if (nga == NULL || ports == NULL)
415 else if (nga->current == NULL)
419 attr = createEmptyList();
420 pushBackList(attr, newEmptyAttr(ATTR_VLAN_PVID));
421 ret = readRequest(nga, attr);
425 filterAttributes(attr, ATTR_VLAN_PVID, ATTR_END);
427 memset(ports, 0, nga->current->ports * sizeof(unsigned short));
429 for (ln = attr->first; ln != NULL; ln = ln->next) {
432 ports[ap->port - 1] = ap->vlan;
437 destroyList(attr, (void(*)(void*))freeAttr);
444 int ngadmin_setPVID (struct ngadmin *nga, unsigned char port, unsigned short vlan)
447 struct attr_pvid *ap;
450 if (nga == NULL || port < 1 || vlan < VLAN_MIN || vlan > VLAN_DOT_MAX)
452 else if (nga->current == NULL)
454 else if (port > nga->current->ports)
458 attr = createEmptyList();
459 ap = malloc(sizeof(struct attr_pvid));
465 pushBackList(attr, newAttr(ATTR_VLAN_PVID, sizeof(struct attr_pvid), ap));
468 return writeRequest(nga, attr);