]> git.sur5r.net Git - ngadmin/blob - lib/src/network.c
Raw: refactor attribute encoding and decoding
[ngadmin] / lib / src / network.c
1
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <unistd.h>
6 #include <arpa/inet.h>
7
8 #include <net/if.h>
9 #include <netinet/ether.h>
10 #include <sys/ioctl.h>
11
12 #include <nsdp/attr.h>
13 #include <nsdp/misc.h>
14 #include <nsdp/net.h>
15 #include <nsdp/protocol.h>
16
17 #include "network.h"
18
19
20 int startNetwork (struct ngadmin *nga)
21 {
22         struct ifreq ifr;
23         int ret;
24         
25         
26         /* create socket */
27         nga->sock = socket(AF_INET, SOCK_DGRAM, 0);
28         if (nga->sock < 0) {
29                 perror("socket");
30                 return nga->sock;
31         }
32         
33         memset(&ifr, 0, sizeof(struct ifreq));
34         strncpy(ifr.ifr_name, nga->iface, IFNAMSIZ - 1);
35
36         /* get the interface MAC address */
37         ret = ioctl(nga->sock, SIOCGIFHWADDR, &ifr);
38         if (ret < 0) {
39                 perror("ioctl(SIOCGIFHWADDR)");
40                 close(nga->sock);
41                 return ret;
42         }
43         memcpy(&nga->localmac, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
44         
45         /* bind */
46         memset(&nga->local, 0, sizeof(struct sockaddr_in));
47         nga->local.sin_family = AF_INET;
48         nga->local.sin_port = htons(CLIENT_PORT);
49         
50         ret = bind(nga->sock, (struct sockaddr*)&nga->local, sizeof(struct sockaddr_in));
51         if (ret < 0) {
52                 perror("bind");
53                 close(nga->sock);
54                 return ret;
55         }
56         
57         /* allow broadcasting */
58         ret = 1;
59         ret = setsockopt(nga->sock, SOL_SOCKET, SO_BROADCAST, &ret, sizeof(ret));
60         if (ret < 0) {
61                 perror("setsockopt(SO_BROADCAST)");
62                 return ret;
63         }
64         
65         /* prevent unicast packets from being routed by setting the TTL to 1 */
66         ret = 1;
67         ret = setsockopt(nga->sock, IPPROTO_IP, IP_TTL, &ret, sizeof(ret));
68         if (ret < 0) {
69                 perror("setsockopt(IP_TTL)");
70                 return ret;
71         }
72         
73         
74         return 0;
75 }
76
77
78 int setBroadcastType (struct ngadmin *nga, bool value)
79 {
80         int ret;
81         struct ifreq ifr;
82         
83         
84         nga->globalbroad = value;
85         if (value) {
86                 nga->brd.s_addr = (in_addr_t)0;
87                 return 0;
88         }
89         
90         memset(&ifr, 0, sizeof(struct ifreq));
91         strncpy(ifr.ifr_name, nga->iface, IFNAMSIZ - 1);
92         
93         /* get the interface broadcast address */
94         ret = ioctl(nga->sock, SIOCGIFBRDADDR, &ifr);
95         if (ret < 0) {
96                 perror("ioctl(SIOCGIFBRDADDR)");
97                 nga->brd.s_addr = (in_addr_t)0;
98                 nga->globalbroad = true;
99                 return ret;
100         }
101         
102         nga->brd = (*(struct sockaddr_in*)&ifr.ifr_addr).sin_addr;
103         
104         return 0;
105 }
106
107
108 int stopNetwork (struct ngadmin *nga)
109 {
110         return close(nga->sock);
111 }
112
113
114 int forceInterface (struct ngadmin *nga)
115 {
116         int ret;
117         
118         
119         /* as described bellow, when you have multiple interfaces, this
120          * forces the packet to go to a particular interface
121          */
122         ret = setsockopt(nga->sock, SOL_SOCKET, SO_BINDTODEVICE, nga->iface, strlen(nga->iface) + 1);
123         if (ret < 0) {
124                 perror("setsockopt(SO_BINDTODEVICE)");
125                 return ret;
126         }
127         
128         /* if the switch's IP is not in your network range, for instance
129          * because you do not have DHCP  enabled or you started the switch
130          * after your DHCP server, this allows to bypass the routing tables
131          * and consider every address is directly reachable on the interface
132          */
133         ret = 1;
134         ret = setsockopt(nga->sock, SOL_SOCKET, SO_DONTROUTE, &ret, sizeof(ret));
135         if (ret <0) {
136                 perror("setsockopt(SO_DONTROUTE)");
137                 return ret;
138         }
139         
140         
141         return 0;
142 }
143
144
145 static int checkErrorCode (const struct nsdp_cmd *nc)
146 {
147         switch (nc->error) {
148         
149         case ERROR_DENIED:
150                 return (nc->attr_error == ATTR_PASSWORD) ? ERR_BADPASS : ERR_DENIED;
151         
152         case ERROR_INVALID_VALUE:
153                 return ERR_INVARG;
154         
155         default:
156                 return ERR_OK;
157         }
158 }
159
160
161 void prepareSend (struct ngadmin *nga, struct nsdp_cmd *nc, unsigned char code)
162 {
163         struct swi_attr *sa = nga->current;
164         
165         
166         memset(nc, 0, sizeof(struct nsdp_cmd));
167         memcpy(&nc->client_mac, &nga->localmac, ETH_ALEN);
168         nc->remote_addr.sin_family = AF_INET;
169         nc->remote_addr.sin_port = htons(SWITCH_PORT);
170         if (sa != NULL)
171                 memcpy(&nc->switch_mac, &sa->mac, ETH_ALEN);
172         
173         /* destination address selection */
174         if (sa != NULL && !nga->keepbroad)
175                 nc->remote_addr.sin_addr = sa->nc.ip;
176         else if (nga->globalbroad)
177                 nc->remote_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
178         else
179                 nc->remote_addr.sin_addr = nga->brd;
180         
181         nc->seqnum = ++nga->seq;
182         nc->code = code;
183 }
184
185
186 void prepareRecv (struct ngadmin *nga, struct nsdp_cmd *nc, unsigned char code)
187 {
188         struct swi_attr *sa = nga->current;
189         
190         
191         memset(nc, 0, sizeof(struct nsdp_cmd));
192         memcpy(&nc->client_mac, &nga->localmac, ETH_ALEN);
193         nc->remote_addr.sin_family = AF_INET;
194         nc->remote_addr.sin_port = htons(SWITCH_PORT);
195         if (sa != NULL)
196                 memcpy(&nc->switch_mac, &sa->mac, ETH_ALEN);
197         
198         /* set filter on switch IP */
199         if (sa == NULL)
200                 nc->remote_addr.sin_addr.s_addr = htonl(INADDR_ANY);
201         else
202                 nc->remote_addr.sin_addr = sa->nc.ip;
203         
204         nc->seqnum = nga->seq;
205         nc->code = code;
206 }
207
208
209 int readRequest (struct ngadmin *nga, List *attr)
210 {
211         int i, ret = ERR_OK;
212         struct nsdp_cmd nc;
213         
214         
215         if (nga == NULL) {
216                 ret = ERR_INVARG;
217                 goto end;
218         }
219         
220         /* add end attribute to end */
221         pushBackList(attr, newEmptyAttr(ATTR_END));
222         
223         prepareSend(nga, &nc, CODE_READ_REQ);
224         i = sendNsdpPacket(nga->sock, &nc, attr);
225         
226         /* do not destroy the list, it will be filled again later by recvNsdpPacket */
227         clearList(attr, (void(*)(void*))freeAttr);
228         
229         if (i >= 0) {
230                 prepareRecv(nga, &nc, CODE_READ_REP);
231                 i = recvNsdpPacket(nga->sock, &nc, attr, &nga->timeout);
232         }
233         
234         if (i == -EINVAL) {
235                 ret = ERR_INVARG;
236                 goto end;
237         } else if (i < 0) {
238                 ret = (errno == EAGAIN || errno == EWOULDBLOCK) ? ERR_TIMEOUT : ERR_NET;
239                 goto end;
240         }
241         
242         
243         /* check the switch error code */
244         ret = checkErrorCode(&nc);
245         
246         
247 end:
248         return ret;
249 }
250
251
252 int writeRequest (struct ngadmin *nga, List *attr)
253 {
254         int i, ret = ERR_OK;
255         struct attr *at;
256         struct nsdp_cmd nc;
257         
258         
259         if (nga == NULL) {
260                 ret = ERR_INVARG;
261                 goto end;
262         } else if (nga->current == NULL) {
263                 ret = ERR_NOTLOG;
264                 goto end;
265         }
266         
267         
268         if (attr == NULL)
269                 attr = createEmptyList();
270         
271         /* add password attribute to start */
272         at = newAttr(ATTR_PASSWORD, strlen(nga->password), strdup(nga->password));
273         if (nga->encrypt_pass)
274                 passwordEndecode(at->data, at->size);
275         pushFrontList(attr, at);
276         
277         /* add end attribute to end */
278         pushBackList(attr, newEmptyAttr(ATTR_END));
279         
280         prepareSend(nga, &nc, CODE_WRITE_REQ);
281         i = sendNsdpPacket(nga->sock, &nc, attr);
282         
283         /* the list will be filled again by recvNgPacket
284          * but normally it will be still empty
285          */
286         clearList(attr, (void(*)(void*))freeAttr);
287         
288         if (i >= 0) {
289                 prepareRecv(nga, &nc, CODE_WRITE_REP);
290                 i = recvNsdpPacket(nga->sock, &nc, attr, &nga->timeout);
291         }
292         
293         if (i == -EINVAL) {
294                 ret = ERR_INVARG;
295                 goto end;
296         } else if (i < 0) {
297                 ret = (errno == EAGAIN || errno == EWOULDBLOCK) ? ERR_TIMEOUT : ERR_NET;
298                 goto end;
299         }
300         
301         /* check the switch error code */
302         ret = checkErrorCode(&nc);
303         
304         
305 end:
306         /* the switch replies to write request by just a header (no attributes), so the list can be destroyed */
307         destroyList(attr, (void(*)(void*))freeAttr);
308         
309         
310         return ret;
311 }
312
313
314 void extractSwitchAttributes (struct swi_attr *sa, const List *l)
315 {
316         const ListNode *ln;
317         const struct attr *at;
318         int len;
319         
320         
321         memset(sa, 0, sizeof(struct swi_attr));
322         
323         for (ln = l->first; ln != NULL; ln = ln->next) {
324                 at = ln->data;
325                 
326                 switch (at->attr) {
327                 
328                 case ATTR_PRODUCT:
329                         len = min(at->size, PRODUCT_SIZE);
330                         memcpy(sa->product, at->data, len);
331                         trim(sa->product, len);
332                         break;
333                 
334                 case ATTR_NAME:
335                         len = min(at->size, NAME_SIZE);
336                         memcpy(sa->name, at->data, len);
337                         trim(sa->name, len);
338                         break;
339                 
340                 case ATTR_MAC:
341                         memcpy(&sa->mac, at->data, ETH_ALEN);
342                         break;
343                 
344                 case ATTR_IP:
345                         sa->nc.ip = *(struct in_addr*)at->data;
346                         break;
347                 
348                 case ATTR_NETMASK:
349                         sa->nc.netmask = *(struct in_addr*)at->data;
350                         break;
351                 
352                 case ATTR_GATEWAY:
353                         sa->nc.gw = *(struct in_addr*)at->data;
354                         break;
355                 
356                 case ATTR_DHCP:
357                         sa->nc.dhcp = (ntohs(*(unsigned short*)at->data) == 1);
358                         break;
359                 
360                 case ATTR_FIRM_VER:
361                         len = min(at->size, FIRMWARE_SIZE - 1);
362                         memcpy(sa->firmware, at->data, len);
363                         sa->firmware[len] = '\0';
364                         break;
365                 
366                 case ATTR_PORTS_COUNT:
367                         sa->ports = *(unsigned char*)at->data;
368                         break;
369                 
370                 case ATTR_END:
371                         return;
372                 }
373         }
374 }
375
376