]> git.sur5r.net Git - ngadmin/blob - lib/src/netconf.c
93497726f8591e7fcb6c050100193f69b1e99566
[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         /*
25         ATTR_IGMP_ENABLE_VLAN
26         ATTR_IGMP_BLOCK_UNK
27         ATTR_IGMP_VALID_V3
28         
29         Apparently, read-querying these attributes at the same time causes the switch to reply garbage. 
30         Here we are forced to do like the official win app and send a separate request for each attribute. 
31         */
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         
126         
127         if (nga == NULL || nc == NULL)
128                 return ERR_INVARG;
129         
130         sa = nga->current;
131         if (sa == NULL)
132                 return ERR_NOTLOG;
133         
134         
135         attr = createEmptyList();
136         
137         if (nc->dhcp) {
138                 pushBackList(attr, newShortAttr(ATTR_DHCP, 1));
139         } else {
140                 pushBackList(attr, newShortAttr(ATTR_DHCP, 0));
141                 /* only add non-null values */
142                 if (nc->ip.s_addr != 0)
143                         pushBackList(attr, newAddrAttr(ATTR_IP, nc->ip));
144                 if (nc->netmask.s_addr != 0)
145                         pushBackList(attr, newAddrAttr(ATTR_NETMASK, nc->netmask));
146                 if (nc->gw.s_addr != 0)
147                         pushBackList(attr, newAddrAttr(ATTR_GATEWAY, nc->gw));
148         }
149         
150         ret = writeRequest(nga, attr);
151         if (ret != ERR_OK)
152                 goto end;
153         
154         
155         /* update local values */
156         sa->nc.dhcp = nc->dhcp;
157         if (!nc->dhcp) {
158                 if (nc->ip.s_addr !=0)
159                         sa->nc.ip = nc->ip;
160                 if (nc->netmask.s_addr != 0)
161                         sa->nc.netmask = nc->netmask;
162                 if (nc->gw.s_addr != 0)
163                         sa->nc.gw = nc->gw;
164         }
165         
166         
167 end:
168         
169         return ret;
170 }
171
172