]> git.sur5r.net Git - ngadmin/blob - lib/src/network.c
Add error codes to handle bad replies and unknown errors
[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 0:
150                 return ERR_OK;
151         
152         case ERROR_DENIED:
153                 return (nc->attr_error == ATTR_PASSWORD) ? ERR_BADPASS : ERR_DENIED;
154         
155         case ERROR_INVALID_VALUE:
156                 return ERR_INVARG;
157         
158         default:
159                 return ERR_UNKNOWN;
160         }
161 }
162
163
164 void prepareSend (struct ngadmin *nga, struct nsdp_cmd *nc, unsigned char code)
165 {
166         struct swi_attr *sa = nga->current;
167         
168         
169         memset(nc, 0, sizeof(struct nsdp_cmd));
170         memcpy(&nc->client_mac, &nga->localmac, ETH_ALEN);
171         nc->remote_addr.sin_family = AF_INET;
172         nc->remote_addr.sin_port = htons(SWITCH_PORT);
173         if (sa != NULL)
174                 memcpy(&nc->switch_mac, &sa->mac, ETH_ALEN);
175         
176         /* destination address selection */
177         if (sa != NULL && !nga->keepbroad)
178                 nc->remote_addr.sin_addr = sa->nc.ip;
179         else if (nga->globalbroad)
180                 nc->remote_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
181         else
182                 nc->remote_addr.sin_addr = nga->brd;
183         
184         nc->seqnum = ++nga->seq;
185         nc->code = code;
186 }
187
188
189 void prepareRecv (struct ngadmin *nga, struct nsdp_cmd *nc, unsigned char code)
190 {
191         struct swi_attr *sa = nga->current;
192         
193         
194         memset(nc, 0, sizeof(struct nsdp_cmd));
195         memcpy(&nc->client_mac, &nga->localmac, ETH_ALEN);
196         nc->remote_addr.sin_family = AF_INET;
197         nc->remote_addr.sin_port = htons(SWITCH_PORT);
198         if (sa != NULL)
199                 memcpy(&nc->switch_mac, &sa->mac, ETH_ALEN);
200         
201         /* set filter on switch IP */
202         if (sa == NULL)
203                 nc->remote_addr.sin_addr.s_addr = htonl(INADDR_ANY);
204         else
205                 nc->remote_addr.sin_addr = sa->nc.ip;
206         
207         nc->seqnum = nga->seq;
208         nc->code = code;
209 }
210
211
212 int readRequest (struct ngadmin *nga, List *attr)
213 {
214         int i, ret = ERR_OK;
215         struct nsdp_cmd nc;
216         
217         
218         if (nga == NULL) {
219                 ret = ERR_INVARG;
220                 goto end;
221         }
222         
223         /* add end attribute to end */
224         pushBackList(attr, newEmptyAttr(ATTR_END));
225         
226         prepareSend(nga, &nc, CODE_READ_REQ);
227         i = sendNsdpPacket(nga->sock, &nc, attr);
228         
229         /* do not destroy the list, it will be filled again later by recvNsdpPacket */
230         clearList(attr, (void(*)(void*))freeAttr);
231         
232         if (i >= 0) {
233                 prepareRecv(nga, &nc, CODE_READ_REP);
234                 i = recvNsdpPacket(nga->sock, &nc, attr, &nga->timeout);
235         }
236         
237         if (i == -EINVAL) {
238                 ret = ERR_INVARG;
239                 goto end;
240         } else if (i < 0) {
241                 ret = (errno == EAGAIN || errno == EWOULDBLOCK) ? ERR_TIMEOUT : ERR_NET;
242                 goto end;
243         }
244         
245         
246         /* check the switch error code */
247         ret = checkErrorCode(&nc);
248         
249         
250 end:
251         return ret;
252 }
253
254
255 int writeRequest (struct ngadmin *nga, List *attr)
256 {
257         int i, ret = ERR_OK;
258         struct attr *at;
259         struct nsdp_cmd nc;
260         
261         
262         if (nga == NULL) {
263                 ret = ERR_INVARG;
264                 goto end;
265         } else if (nga->current == NULL) {
266                 ret = ERR_NOTLOG;
267                 goto end;
268         }
269         
270         
271         if (attr == NULL)
272                 attr = createEmptyList();
273         
274         /* add password attribute to start */
275         at = newAttr(ATTR_PASSWORD, strlen(nga->password), strdup(nga->password));
276         if (nga->encrypt_pass)
277                 passwordEndecode(at->data, at->size);
278         pushFrontList(attr, at);
279         
280         /* add end attribute to end */
281         pushBackList(attr, newEmptyAttr(ATTR_END));
282         
283         prepareSend(nga, &nc, CODE_WRITE_REQ);
284         i = sendNsdpPacket(nga->sock, &nc, attr);
285         
286         /* the list will be filled again by recvNgPacket
287          * but normally it will be still empty
288          */
289         clearList(attr, (void(*)(void*))freeAttr);
290         
291         if (i >= 0) {
292                 prepareRecv(nga, &nc, CODE_WRITE_REP);
293                 i = recvNsdpPacket(nga->sock, &nc, attr, &nga->timeout);
294         }
295         
296         if (i == -EINVAL) {
297                 ret = ERR_INVARG;
298                 goto end;
299         } else if (i < 0) {
300                 ret = (errno == EAGAIN || errno == EWOULDBLOCK) ? ERR_TIMEOUT : ERR_NET;
301                 goto end;
302         }
303         
304         /* check the switch error code */
305         ret = checkErrorCode(&nc);
306         
307         
308 end:
309         /* the switch replies to write request by just a header (no attributes), so the list can be destroyed */
310         destroyList(attr, (void(*)(void*))freeAttr);
311         
312         
313         return ret;
314 }
315
316
317 void extractSwitchAttributes (struct swi_attr *sa, const List *l)
318 {
319         const ListNode *ln;
320         const struct attr *at;
321         int len;
322         
323         
324         memset(sa, 0, sizeof(struct swi_attr));
325         
326         for (ln = l->first; ln != NULL; ln = ln->next) {
327                 at = ln->data;
328                 
329                 switch (at->attr) {
330                 
331                 case ATTR_PRODUCT:
332                         len = min(at->size, PRODUCT_SIZE);
333                         memcpy(sa->product, at->data, len);
334                         trim(sa->product, len);
335                         break;
336                 
337                 case ATTR_NAME:
338                         len = min(at->size, NAME_SIZE);
339                         memcpy(sa->name, at->data, len);
340                         trim(sa->name, len);
341                         break;
342                 
343                 case ATTR_MAC:
344                         memcpy(&sa->mac, at->data, ETH_ALEN);
345                         break;
346                 
347                 case ATTR_IP:
348                         sa->nc.ip = *(struct in_addr*)at->data;
349                         break;
350                 
351                 case ATTR_NETMASK:
352                         sa->nc.netmask = *(struct in_addr*)at->data;
353                         break;
354                 
355                 case ATTR_GATEWAY:
356                         sa->nc.gw = *(struct in_addr*)at->data;
357                         break;
358                 
359                 case ATTR_DHCP:
360                         sa->nc.dhcp = (ntohs(*(unsigned short*)at->data) == 1);
361                         break;
362                 
363                 case ATTR_FIRM_VER:
364                         len = min(at->size, FIRMWARE_SIZE - 1);
365                         memcpy(sa->firmware, at->data, len);
366                         sa->firmware[len] = '\0';
367                         break;
368                 
369                 case ATTR_PORTS_COUNT:
370                         sa->ports = *(unsigned char*)at->data;
371                         break;
372                 
373                 case ATTR_END:
374                         return;
375                 }
376         }
377 }
378
379