9 #include <ngadmin.h> /* FIXME */
10 #include <nsdp/protocol.h>
11 #include <nsdp/attr.h>
16 static void print_password (const char *pass, unsigned int len)
22 if (isStringPrintable(pass, len)) {
23 /* all characters are printable, cleartext password assumed */
24 printf("(clear) \"%s\"", pass);
34 passwordEndecode(s, len);
36 if (isStringPrintable(s, len)) {
37 /* all characters are printable, encrypted password assumed */
38 printf("(encrypted) \"%s\"", s);
45 /* unknown encoding, fallback to hex display */
47 for (i = 0; i < len; i++)
48 printf("%02x ", (unsigned char)pass[i]);
52 static void print_attr (const struct attr *at)
54 unsigned char p, ports, *byte = at->data;
55 unsigned short *word = at->data;
57 struct ether_addr *eth = at->data;
58 struct in_addr *ip = at->data;
59 struct attr_port_status *apsu = at->data;
60 struct attr_port_stat *apsi = at->data;
61 struct attr_cabletest_do *acd = at->data;
62 struct attr_cabletest_result *acr = at->data;
63 struct attr_vlan_conf *avc = at->data;
64 struct attr_pvid *ap = at->data;
65 struct attr_qos *aq = at->data;
66 struct attr_bitrate *ab = at->data;
67 struct attr_mirror *am = at->data;
68 struct attr_igmp_vlan *aiv = at->data;
71 printf("code = %04X, length = %d\n", at->attr, at->size);
74 printf("\tempty\n\n");
82 printf("\tproduct = %s\n", s);
87 printf("\tname = %s\n", s);
91 printf("\tMAC = %s\n", ether_ntoa(eth));
95 printf("\tIP = %s\n", inet_ntoa(*ip));
99 printf("\tnetmask = %s\n", inet_ntoa(*ip));
103 printf("\tgateway = %s\n", inet_ntoa(*ip));
106 case ATTR_NEW_PASSWORD:
107 printf("\tnew password = ");
108 print_password(s, at->size);
113 printf("\tpassword = ");
114 print_password(s, at->size);
119 printf("\tDHCP = %s\n", (at->size == 1 ? *byte : *word) ? "yes" : "no");
124 printf("\tfirmware = %s\n", s);
127 case ATTR_FIRM_UPGRADE:
128 printf("\tfirmware upgrade requested\n");
132 printf("\trestart requested\n");
136 printf("\tpassword must be encrypted\n");
140 printf("\treset parameters requested\n");
143 case ATTR_PORT_STATUS:
144 printf("\tport status\n");
145 printf("\tport = %u\n", apsu->port);
146 printf("\tstate = %s\n", safeStr(getSpeedStr(apsu->status)));
149 case ATTR_PORT_STATISTICS:
150 printf("\tport statistics\n");
151 printf("\tport = %u\n", apsi->port);
152 printf("\treceived = %llu\n", apsi->recv);
153 printf("\tsend = %llu\n", apsi->sent);
154 printf("\tCRC = %llu\n", apsi->crc);
157 case ATTR_STATS_RESET:
158 printf("\tport statistics reset requested\n");
162 case ATTR_CABLETEST_DO:
163 printf("\tstart cabletest\n");
164 printf("\tport = %u\n", acd->port);
165 printf("\taction = %u\n", acd->action);
168 case ATTR_CABLETEST_RESULT:
169 printf("\tcabletest result\n");
170 printf("\tport = %u\n", acr->port);
172 printf("\tv1 = %u\n", acr->v1);
173 printf("\tv2 = %u\n", acr->v2);
178 printf("\tVLAN type = %s\n", safeStr(getVlanTypeStr(*byte)));
181 case ATTR_VLAN_PORT_CONF:
182 printf("\tport based VLAN configuration\n");
183 printf("\tVLAN = %u\n", avc->vlan);
184 ports = at->size - sizeof(struct attr_vlan_conf);
185 for (p = 0; p < ports; p++)
186 printf("\tport %d = %s\n", p + 1, safeStr(getVlanCodeStr(avc->ports[p])));
189 case ATTR_VLAN_DOT_CONF:
190 printf("\t802.1Q based VLAN configuration\n");
191 printf("\tVLAN = %u\n", avc->vlan);
192 ports = at->size - sizeof(struct attr_vlan_conf);
193 for (p = 0; p < ports; p++)
194 printf("\tport %d = %s\n", p + 1, safeStr(getVlanCodeStr(avc->ports[p])));
197 case ATTR_VLAN_DESTROY:
198 printf("\tdestroy VLAN = %u\n", *word);
202 printf("\tVLAN PVID\n");
203 printf("\tport = %u\n", ap->port);
204 printf("\tVLAN = %u\n", ap->vlan);
208 printf("\tQoS type = %s\n", safeStr(getQosTypeStr(*byte)));
211 case ATTR_QOS_CONFIG:
212 printf("\tQoS configuration\n");
213 printf("\tport = %u\n", aq->port);
214 printf("\tpriority = %s\n", safeStr(getQosPrioStr(aq->prio)));
217 case ATTR_BITRATE_INPUT:
218 printf("\tinput bitrate\n");
219 printf("\tport = %u\n", ab->port);
220 printf("\tbitrate = %s\n", safeStr(getBitrateStr(ab->bitrate)));
223 case ATTR_BITRATE_OUTPUT:
224 printf("\toutput bitrate\n");
225 printf("\tport = %u\n", ab->port);
226 printf("\tbitrate = %s\n", safeStr(getBitrateStr(ab->bitrate)));
229 case ATTR_STORM_ENABLE:
230 printf("\tstorm filtering = %s\n", *byte ? "yes" : "no");
233 case ATTR_STORM_BITRATE:
234 printf("\tstorm filtering bitrate\n");
235 printf("\tport = %u\n", ab->port);
236 printf("\tbitrate = %s\n", safeStr(getBitrateStr(ab->bitrate)));
240 printf("\tport mirroring\n");
241 if (am->outport == 0) {
242 printf("\tdisabled\n");
246 printf("\toutput port = %u\n", am->outport);
247 ports = at->size - sizeof(struct attr_mirror);
248 for (p = 0; p < ports; p++)
249 printf("\tport %u = %s\n", p + 1, am->ports[p] ? "yes" : "no");
252 case ATTR_PORTS_COUNT:
253 printf("\tports count = %u\n", *byte);
256 case ATTR_IGMP_ENABLE_VLAN:
257 printf("\tIGMP filtering\n");
258 printf("\tenable = %s\n", aiv->enable ? "yes" : "no");
259 printf("\tVLAN = %u\n", aiv->vlan);
262 case ATTR_IGMP_BLOCK_UNK:
263 printf("\tblock unknown IGMP = %s\n", *byte ? "yes" : "no");
266 case ATTR_IGMP_VALID_V3:
267 printf("\tvalidate IGMPv3 = %s\n", *byte ? "yes" : "no");
270 case ATTR_LOOP_DETECT:
271 printf("\tloop detection = %s\n", *byte ? "yes" : "no");
275 printf("\tunknown\n");
282 static void print_packet (const List *attr, const struct nsdp_cmd *nc)
285 const struct attr *at;
288 printf("---------------------------------\n");
289 printf("code = %s (%u)\n", safeStr(getCodeStr(nc->code)), nc->code);
290 printf("error = %s (%u)\n", safeStr(getErrorStr(nc->error)), nc->error);
291 if (nc->attr_error != 0)
292 printf("erroneous attribute = %04X\n", nc->attr_error);
293 printf("source address = %s:%u\n", inet_ntoa(nc->remote_addr.sin_addr), ntohs(nc->remote_addr.sin_port));
294 printf("client MAC = %s\n", ether_ntoa(&nc->client_mac));
295 printf("switch MAC = %s\n", ether_ntoa(&nc->switch_mac));
296 printf("sequence number = %u\n\n", nc->seqnum);
297 printf("received %d attribute(s)\n\n", attr->count);
299 for (ln = attr->first; ln != NULL; ln = ln->next) {
304 printf("---------------------------------\n\n");
308 static void handler (int sig)
311 printf("interrupt\n");
317 int err = 0, sw_sock = -1, cl_sock = -1;
320 struct sockaddr_in sw_local, cl_local;
321 struct pollfd fds[2];
325 sw_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
332 cl_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
340 memset(&sw_local, 0, sizeof(struct sockaddr_in));
341 sw_local.sin_family = AF_INET;
342 sw_local.sin_addr.s_addr = htonl(INADDR_ANY);
343 sw_local.sin_port = htons(SWITCH_PORT);
346 cl_local.sin_port = htons(CLIENT_PORT);
348 if (bind(sw_sock, (struct sockaddr*)&sw_local, sizeof(struct sockaddr_in)) < 0) {
354 if (bind(cl_sock, (struct sockaddr*)&cl_local, sizeof(struct sockaddr_in)) < 0) {
361 fds[0].events = POLLIN;
364 fds[1].events = POLLIN;
367 memset(&sa, 0, sizeof(struct sigaction));
368 sa.sa_handler = handler;
369 sigaction(SIGINT, &sa, NULL);
370 sigaction(SIGTERM, &sa, NULL);
372 attr = createEmptyList();
375 err = poll(fds, 2, -1);
379 } else if (err == 0) {
383 memset(&nc, 0, sizeof(struct nsdp_cmd));
384 nc.remote_addr.sin_family = AF_INET;
386 if (fds[0].revents & POLLIN) {
387 nc.remote_addr.sin_port = htons(CLIENT_PORT);
388 err = recvNsdpPacket(sw_sock, &nc, attr, NULL);
390 nc.remote_addr.sin_port = htons(SWITCH_PORT);
391 err = recvNsdpPacket(cl_sock, &nc, attr, NULL);
397 print_packet(attr, &nc);
399 clearList(attr, (void(*)(void*))freeAttr);
402 destroyList(attr, (void(*)(void*))freeAttr);