9 #include <netinet/ether.h>
10 #include <sys/ioctl.h>
12 #include <nsdp/attr.h>
13 #include <nsdp/misc.h>
15 #include <nsdp/protocol.h>
20 int startNetwork (struct ngadmin *nga)
27 nga->sock = socket(AF_INET, SOCK_DGRAM, 0);
33 memset(&ifr, 0, sizeof(struct ifreq));
34 strncpy(ifr.ifr_name, nga->iface, IFNAMSIZ - 1);
36 /* get the interface MAC address */
37 ret = ioctl(nga->sock, SIOCGIFHWADDR, &ifr);
39 perror("ioctl(SIOCGIFHWADDR)");
43 memcpy(&nga->localmac, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
46 memset(&nga->local, 0, sizeof(struct sockaddr_in));
47 nga->local.sin_family = AF_INET;
48 nga->local.sin_port = htons(CLIENT_PORT);
50 ret = bind(nga->sock, (struct sockaddr*)&nga->local, sizeof(struct sockaddr_in));
57 /* allow broadcasting */
59 ret = setsockopt(nga->sock, SOL_SOCKET, SO_BROADCAST, &ret, sizeof(ret));
61 perror("setsockopt(SO_BROADCAST)");
65 /* prevent unicast packets from being routed by setting the TTL to 1 */
67 ret = setsockopt(nga->sock, IPPROTO_IP, IP_TTL, &ret, sizeof(ret));
69 perror("setsockopt(IP_TTL)");
78 int setBroadcastType (struct ngadmin *nga, bool value)
84 nga->globalbroad = value;
86 nga->brd.s_addr = (in_addr_t)0;
90 memset(&ifr, 0, sizeof(struct ifreq));
91 strncpy(ifr.ifr_name, nga->iface, IFNAMSIZ - 1);
93 /* get the interface broadcast address */
94 ret = ioctl(nga->sock, SIOCGIFBRDADDR, &ifr);
96 perror("ioctl(SIOCGIFBRDADDR)");
97 nga->brd.s_addr = (in_addr_t)0;
98 nga->globalbroad = true;
102 nga->brd = (*(struct sockaddr_in*)&ifr.ifr_addr).sin_addr;
108 int stopNetwork (struct ngadmin *nga)
110 return close(nga->sock);
114 int forceInterface (struct ngadmin *nga)
119 /* as described bellow, when you have multiple interfaces, this
120 * forces the packet to go to a particular interface
122 ret = setsockopt(nga->sock, SOL_SOCKET, SO_BINDTODEVICE, nga->iface, strlen(nga->iface) + 1);
124 perror("setsockopt(SO_BINDTODEVICE)");
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
134 ret = setsockopt(nga->sock, SOL_SOCKET, SO_DONTROUTE, &ret, sizeof(ret));
136 perror("setsockopt(SO_DONTROUTE)");
145 static int checkErrorCode (const struct nsdp_cmd *nc)
153 case ERROR_WRITEONLY:
157 return (nc->attr_error == ATTR_PASSWORD) ? ERR_BADPASS : ERR_DENIED;
159 case ERROR_INVALID_VALUE:
168 void prepareSend (struct ngadmin *nga, struct nsdp_cmd *nc, unsigned char code)
170 struct swi_attr *sa = nga->current;
173 memset(nc, 0, sizeof(struct nsdp_cmd));
174 memcpy(&nc->client_mac, &nga->localmac, ETH_ALEN);
175 nc->remote_addr.sin_family = AF_INET;
176 nc->remote_addr.sin_port = htons(SWITCH_PORT);
178 memcpy(&nc->switch_mac, &sa->mac, ETH_ALEN);
180 /* destination address selection */
181 if (sa != NULL && !nga->keepbroad)
182 nc->remote_addr.sin_addr = sa->nc.ip;
183 else if (nga->globalbroad)
184 nc->remote_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
186 nc->remote_addr.sin_addr = nga->brd;
188 nc->seqnum = ++nga->seq;
193 void prepareRecv (struct ngadmin *nga, struct nsdp_cmd *nc, unsigned char code)
195 struct swi_attr *sa = nga->current;
198 memset(nc, 0, sizeof(struct nsdp_cmd));
199 memcpy(&nc->client_mac, &nga->localmac, ETH_ALEN);
200 nc->remote_addr.sin_family = AF_INET;
201 nc->remote_addr.sin_port = htons(SWITCH_PORT);
203 memcpy(&nc->switch_mac, &sa->mac, ETH_ALEN);
205 /* set filter on switch IP */
207 nc->remote_addr.sin_addr.s_addr = htonl(INADDR_ANY);
209 nc->remote_addr.sin_addr = sa->nc.ip;
211 nc->seqnum = nga->seq;
216 int readRequest (struct ngadmin *nga, List *attr)
227 /* add end attribute to end */
228 pushBackList(attr, newEmptyAttr(ATTR_END));
230 prepareSend(nga, &nc, CODE_READ_REQ);
231 i = sendNsdpPacket(nga->sock, &nc, attr);
233 /* do not destroy the list, it will be filled again later by recvNsdpPacket */
234 clearList(attr, (void(*)(void*))freeAttr);
237 prepareRecv(nga, &nc, CODE_READ_REP);
238 i = recvNsdpPacket(nga->sock, &nc, attr, &nga->timeout);
244 } else if (i == -ETIMEDOUT) {
252 /* check the switch error code */
253 ret = checkErrorCode(&nc);
261 int writeRequest (struct ngadmin *nga, List *attr)
271 } else if (nga->current == NULL) {
278 attr = createEmptyList();
280 /* add password attribute to start */
281 at = newAttr(ATTR_PASSWORD, strlen(nga->password), strdup(nga->password));
282 if (nga->encrypt_pass)
283 passwordEndecode(at->data, at->size);
284 pushFrontList(attr, at);
286 /* add end attribute to end */
287 pushBackList(attr, newEmptyAttr(ATTR_END));
289 prepareSend(nga, &nc, CODE_WRITE_REQ);
290 i = sendNsdpPacket(nga->sock, &nc, attr);
292 /* the list will be filled again by recvNgPacket
293 * but normally it will be still empty
295 clearList(attr, (void(*)(void*))freeAttr);
298 prepareRecv(nga, &nc, CODE_WRITE_REP);
299 i = recvNsdpPacket(nga->sock, &nc, attr, &nga->timeout);
305 } else if (i == -ETIMEDOUT) {
312 /* check the switch error code */
313 ret = checkErrorCode(&nc);
317 /* the switch replies to write request by just a header (no attributes), so the list can be destroyed */
318 destroyList(attr, (void(*)(void*))freeAttr);
325 int extractSwitchAttributes (struct swi_attr *sa, const List *l)
328 const struct attr *at;
332 memset(sa, 0, sizeof(struct swi_attr));
334 for (ln = l->first; ln != NULL; ln = ln->next) {
337 if (at->attr == ATTR_NAME)
346 len = min(at->size, PRODUCT_SIZE);
347 memcpy(sa->product, at->data, len);
348 trim(sa->product, len);
352 len = min(at->size, NAME_SIZE);
353 memcpy(sa->name, at->data, len);
358 memcpy(&sa->mac, at->data, ETH_ALEN);
362 sa->nc.ip = *(struct in_addr*)at->data;
366 sa->nc.netmask = *(struct in_addr*)at->data;
370 sa->nc.gw = *(struct in_addr*)at->data;
374 /* Note: DHCP attribute is special, it is 2 two bytes long when sent
375 * by the switch but only 1 byte long when sent by the client
377 sa->nc.dhcp = (at->size == 2) && ((*(unsigned short*)at->data) == 1);
381 len = min(at->size, FIRMWARE_SIZE - 1);
382 memcpy(sa->firmware, at->data, len);
383 sa->firmware[len] = '\0';
386 case ATTR_PORTS_COUNT:
387 sa->ports = *(unsigned char*)at->data;