]> git.sur5r.net Git - ngadmin/blob - lib/src/netconf.c
Fix network configuration modification
[ngadmin] / lib / src / netconf.c
1
2 #include <ngadmin.h>
3
4 #include <nsdp/attr.h>
5 #include <nsdp/protocol.h>
6
7 #include "lib.h"
8 #include "network.h"
9
10
11 int ngadmin_getIGMPConf (struct ngadmin *nga, struct igmp_conf *ic)
12 {
13         List *attr;
14         struct attr *at;
15         int ret = ERR_OK;
16         struct attr_igmp_vlan *aiv;
17         
18         
19         if (nga == NULL || ic == NULL)
20                 return ERR_INVARG;
21         else if (nga->current == NULL)
22                 return ERR_NOTLOG;
23         
24         /* ATTR_IGMP_ENABLE_VLAN
25          * ATTR_IGMP_BLOCK_UNK
26          * ATTR_IGMP_VALID_V3
27          *
28          * apparently, read-querying these attributes at the same time causes
29          * the switch to reply garbage
30          * here we are forced to do like the official win app and send a
31          * separate request for each attribute
32          */
33         
34         attr = createEmptyList();
35         memset(ic, 0, sizeof(struct igmp_conf));
36         
37         
38         pushBackList(attr, newEmptyAttr(ATTR_IGMP_ENABLE_VLAN));
39         ret = readRequest(nga, attr);
40         if (ret < 0)
41                 goto end;
42         
43         filterAttributes(attr, ATTR_IGMP_ENABLE_VLAN, ATTR_END);
44         
45         if (attr->first != NULL) {
46                 at = attr->first->data;
47                 aiv = at->data;
48                 ic->enable = aiv->enable;
49                 ic->vlan = aiv->vlan;
50         }
51         
52         clearList(attr, (void(*)(void*))freeAttr);
53         
54         
55         pushBackList(attr, newEmptyAttr(ATTR_IGMP_BLOCK_UNK));
56         ret = readRequest(nga, attr);
57         if (ret < 0)
58                 goto end;
59         
60         filterAttributes(attr, ATTR_IGMP_BLOCK_UNK, ATTR_END);
61         
62         if (attr->first != NULL) {
63                 at = attr->first->data;
64                 ic->block = *(char*)at->data;
65         }
66         
67         clearList(attr, (void(*)(void*))freeAttr);
68         
69         
70         pushBackList(attr, newEmptyAttr(ATTR_IGMP_VALID_V3));
71         ret = readRequest(nga, attr);
72         if (ret < 0)
73                 goto end;
74         
75         filterAttributes(attr, ATTR_IGMP_VALID_V3, ATTR_END);
76         
77         if (attr->first != NULL) {
78                 at = attr->first->data;
79                 ic->validate = *(char*)at->data;
80         }
81         
82         
83 end:
84         destroyList(attr, (void(*)(void*))freeAttr);
85         
86         
87         return ret;
88 }
89
90
91 int ngadmin_setIGMPConf (struct ngadmin *nga, const struct igmp_conf *ic)
92 {
93         List *attr;
94         struct attr_igmp_vlan *aiv;
95         
96         
97         if (nga == NULL || ic == NULL)
98                 return ERR_INVARG;
99         else if (nga->current == NULL)
100                 return ERR_NOTLOG;
101         
102         
103         aiv = malloc(sizeof(struct attr_igmp_vlan));
104         if (aiv == NULL)
105                 return ERR_MEM;
106         aiv->enable = ic->enable;
107         aiv->vlan = ic->vlan;
108         
109         
110         attr = createEmptyList();
111         pushBackList(attr, newAttr(ATTR_IGMP_ENABLE_VLAN, sizeof(struct attr_igmp_vlan), aiv));
112         pushBackList(attr, newByteAttr(ATTR_IGMP_BLOCK_UNK, ic->block != false));
113         pushBackList(attr, newByteAttr(ATTR_IGMP_VALID_V3, ic->validate != false));
114         
115         
116         return writeRequest(nga, attr);
117 }
118
119
120 int ngadmin_setNetConf (struct ngadmin *nga, const struct net_conf *nc)
121 {
122         List *attr;
123         int ret = ERR_OK;
124         struct swi_attr *sa;
125         struct net_conf nc_new;
126         
127         
128         if (nga == NULL || nc == NULL)
129                 return ERR_INVARG;
130         
131         sa = nga->current;
132         if (sa == NULL)
133                 return ERR_NOTLOG;
134         
135         memcpy(&nc_new, &sa->nc, sizeof(struct net_conf));
136         if (nc->ip.s_addr != 0)
137                 nc_new.ip = nc->ip;
138         if (nc->netmask.s_addr != 0)
139                 nc_new.netmask = nc->netmask;
140         if (nc->gw.s_addr != 0)
141                 nc_new.gw = nc->gw;
142         nc_new.dhcp = nc->dhcp;
143         
144         /* gateway must be in the network range */
145         if ((nc_new.ip.s_addr & nc_new.netmask.s_addr) != (nc_new.gw.s_addr & nc_new.netmask.s_addr))
146                 return ERR_INVARG;
147         
148         /* no need to send anything if old and new configurations are the same */
149         if (memcmp(&nc_new, &sa->nc, sizeof(struct net_conf)) == 0)
150                 return ERR_OK;
151         
152         
153         attr = createEmptyList();
154         
155         /* Note: DHCP attribute is special, it is 2 two bytes long when sent
156          * by the switch but only 1 byte long when sent by the client
157          */
158         if (nc_new.dhcp) {
159                 pushBackList(attr, newByteAttr(ATTR_DHCP, 1));
160         } else {
161                 pushBackList(attr, newAddrAttr(ATTR_IP, nc_new.ip));
162                 pushBackList(attr, newAddrAttr(ATTR_NETMASK, nc_new.netmask));
163                 pushBackList(attr, newAddrAttr(ATTR_GATEWAY, nc_new.gw));
164                 pushBackList(attr, newByteAttr(ATTR_DHCP, 0));
165         }
166         
167         ret = writeRequest(nga, attr);
168         if (ret != ERR_OK)
169                 goto end;
170         
171         
172         /* update local values */
173         memcpy(&sa->nc, &nc_new, sizeof(struct net_conf));
174         
175 end:
176         
177         return ret;
178 }
179
180