7 #include <nsdp/protocol.h>
13 int ngadmin_scan (struct ngadmin *nga)
20 sent by official win client:
36 static const unsigned short hello[] = {
59 /* create attributes for an "hello" request */
60 attr = createEmptyList();
62 pushBackList(attr, newEmptyAttr(hello[i]));
63 if (hello[i] == ATTR_END)
67 /* send request to all potential switches */
68 prepareSend(nga, &nc, CODE_READ_REQ);
69 i = sendNsdpPacket(nga->sock, &nc, attr);
70 clearList(attr, (void(*)(void*))freeAttr);
76 /* try to receive any packets until timeout */
77 swiList = createEmptyList();
78 /* FIXME: end after timeout whatever received packet is good or not */
80 prepareRecv(nga, &nc, CODE_READ_REP);
81 if (recvNsdpPacket(nga->sock, &nc, attr, &nga->timeout) < 0)
84 sa = malloc(sizeof(struct swi_attr));
88 extractSwitchAttributes(sa, attr);
89 clearList(attr, (void(*)(void*))freeAttr);
90 pushBackList(swiList, sa);
93 nga->swi_count = swiList->count;
94 nga->swi_tab = convertToArray(swiList, sizeof(struct swi_attr));
95 destroyList(swiList, free);
96 destroyList(attr, (void(*)(void*))freeAttr);
103 const struct swi_attr* ngadmin_getSwitchTab (struct ngadmin *nga, int *nb)
105 if (nga == NULL || nb == NULL)
108 *nb = nga->swi_count;
114 const struct swi_attr* ngadmin_getCurrentSwitch (struct ngadmin *nga)
123 int ngadmin_login (struct ngadmin *nga, int id)
133 else if (id < 0 || id >= nga->swi_count)
136 sa = &nga->swi_tab[id];
138 nga->encrypt_pass = false;
140 attr = createEmptyList();
141 pushBackList(attr, newEmptyAttr(ATTR_ENCPASS));
142 ret = readRequest(nga, attr);
146 filterAttributes(attr, ATTR_ENCPASS, ATTR_END);
147 if (attr->first != NULL) {
148 at = attr->first->data;
149 nga->encrypt_pass = (at->size == 4 && ntohl(*(unsigned int*)at->data) == 1);
151 clearList(attr, (void(*)(void*))freeAttr);
153 /* Strangely, passwords must never be encrypted inside a read request,
154 * or it will be rejected. Seems more to be a firmware bug. */
155 pushBackList(attr, newAttr(ATTR_PASSWORD, strlen(nga->password), strdup(nga->password)));
156 ret = readRequest(nga, attr);
157 if (ret == ERR_OK ) {
158 /* login succeeded */
159 /* TODO: if keep broadcasting is disabled, connect() the UDP
160 socket so icmp errors messages (port unreachable, TTL exceeded
161 in transit, ...) can be received */
168 destroyList(attr, (void(*)(void*))freeAttr);