7 #include <nsdp/protocol.h>
13 int ngadmin_scan (struct ngadmin *nga)
19 /* sent by official win client:
35 * one may be tempted to add ATTR_ENCPASS so we can now early if the
36 * switch uses password encryption, but this would cause (at least)
37 * switches that do not support this feature not to reply to the
38 * discovery request at all
40 static const unsigned short hello[] = {
63 /* create attributes for an "hello" request */
64 attr = createEmptyList();
66 pushBackList(attr, newEmptyAttr(hello[i]));
67 if (hello[i] == ATTR_END)
71 /* send request to all potential switches */
72 prepareSend(nga, &nc, CODE_READ_REQ);
73 i = sendNsdpPacket(nga->sock, &nc, attr);
74 clearList(attr, (void(*)(void*))freeAttr);
80 /* try to receive any packets until timeout */
81 swiList = createEmptyList();
82 /* FIXME: end after timeout whatever received packet is good or not */
84 prepareRecv(nga, &nc, CODE_READ_REP);
85 if (recvNsdpPacket(nga->sock, &nc, attr, &nga->timeout) < 0)
88 sa = malloc(sizeof(struct swi_attr));
92 extractSwitchAttributes(sa, attr);
93 clearList(attr, (void(*)(void*))freeAttr);
94 pushBackList(swiList, sa);
97 nga->swi_count = swiList->count;
98 nga->swi_tab = convertToArray(swiList, sizeof(struct swi_attr));
99 destroyList(swiList, free);
100 destroyList(attr, (void(*)(void*))freeAttr);
107 const struct swi_attr* ngadmin_getSwitchTab (struct ngadmin *nga, int *nb)
109 if (nga == NULL || nb == NULL)
112 *nb = nga->swi_count;
118 const struct swi_attr* ngadmin_getCurrentSwitch (struct ngadmin *nga)
127 int ngadmin_login (struct ngadmin *nga, int id)
137 else if (id < 0 || id >= nga->swi_count)
140 sa = &nga->swi_tab[id];
142 nga->encrypt_pass = false;
144 /* determine if the switch uses password encryption
145 * as explained in ngadmin_scan, it cannot be done at discovery
148 attr = createEmptyList();
149 pushBackList(attr, newEmptyAttr(ATTR_ENCPASS));
150 ret = readRequest(nga, attr);
154 filterAttributes(attr, ATTR_ENCPASS, ATTR_END);
155 if (attr->first != NULL) {
156 at = attr->first->data;
157 nga->encrypt_pass = (at->size == 4 && *(unsigned int*)at->data == 1);
159 clearList(attr, (void(*)(void*))freeAttr);
161 /* strangely, passwords must never be encrypted inside a read request,
162 * or it will be rejected. Seems more to be a firmware bug
164 pushBackList(attr, newAttr(ATTR_PASSWORD, strlen(nga->password), strdup(nga->password)));
165 ret = readRequest(nga, attr);
166 if (ret == ERR_OK ) {
167 /* login succeeded */
168 /* TODO: if keep broadcasting is disabled, connect() the UDP
169 * socket so icmp errors messages (port unreachable, TTL exceeded
170 * in transit, ...) can be received
178 destroyList(attr, (void(*)(void*))freeAttr);