}
-bool validateNsdpHeader (const struct nsdp_header *nh, const struct nsdp_cmd *nc)
+bool extractNsdpHeader (const struct nsdp_header *nh, struct nsdp_cmd *nc)
{
unsigned int i;
if (nc->code > 0 && nh->code != nc->code)
return false;
+ nc->code = nh->code;
+
+ nc->error = nh->error;
+ nc->attr_error = ntohs(nh->attr);
if (nh->unk1 != 0)
return false;
for (i = 0; i < ETH_ALEN && nc->client_mac.ether_addr_octet[i] == 0; i++);
if (i < ETH_ALEN && memcmp(nh->client_mac, &nc->client_mac, ETH_ALEN) != 0)
return false;
+ memcpy(&nc->client_mac, nh->client_mac, ETH_ALEN);
for (i = 0; i < ETH_ALEN && nc->switch_mac.ether_addr_octet[i] == 0; i++);
if (i < ETH_ALEN && memcmp(nh->switch_mac, &nc->switch_mac, ETH_ALEN) != 0)
return false;
+ memcpy(&nc->switch_mac, nh->switch_mac, ETH_ALEN);
if (nc->seqnum > 0 && ntohl(nh->seqnum) != nc->seqnum)
return false;
+ nc->seqnum = ntohl(nh->seqnum);
if (memcmp(nh->proto_id, NSDP_PROTOID, 4) != 0)
return false;
if ((nc->remote_addr.sin_addr.s_addr != 0 && remote.sin_addr.s_addr != nc->remote_addr.sin_addr.s_addr) ||
(nc->remote_addr.sin_port != 0 && remote.sin_port != nc->remote_addr.sin_port) ||
len < (int)sizeof(struct nsdp_header) ||
- !validateNsdpHeader(np.nh, nc) ||
+ !extractNsdpHeader(np.nh, nc) ||
extractPacketAttributes(&np, attr, nc->ports) < 0)
continue;
nc->remote_addr = remote;
- nc->code = np.nh->code;
- nc->error = np.nh->error;
- nc->attr_error = ntohs(np.nh->attr);
-
len = 0;
break;
}
#include <nsdp/net.h>
+static const char* code_str (unsigned char code)
+{
+ switch (code) {
+
+ case CODE_READ_REQ:
+ return "read request";
+
+ case CODE_READ_REP:
+ return "read reply";
+
+ case CODE_WRITE_REQ:
+ return "write request";
+
+ case CODE_WRITE_REP:
+ return "write reply";
+
+ default:
+ return "unknown";
+ }
+}
+
+
+static const char* error_str (unsigned char err)
+{
+ switch (err) {
+
+ case 0:
+ return "none";
+
+ case ERROR_READONLY:
+ return "read only";
+
+ case ERROR_INVALID_VALUE:
+ return "invalid value";
+
+ case ERROR_DENIED:
+ return "access denied";
+
+ default:
+ return "unknown";
+ }
+}
+
+
+static void print_packet (const List *attr, const struct nsdp_cmd *nc)
+{
+ const ListNode *ln;
+ const struct attr *at;
+
+
+ printf("---------------------------------\n");
+ printf("code = %s (%u)\n", code_str(nc->code), nc->code);
+ printf("error = %s (%u)\n", error_str(nc->error), nc->error);
+ if (nc->attr_error != 0)
+ printf("erroneous attribute = %04X\n", nc->attr_error);
+ printf("source address = %s:%u\n", inet_ntoa(nc->remote_addr.sin_addr), ntohs(nc->remote_addr.sin_port));
+ printf("client MAC = %s\n", ether_ntoa(&nc->client_mac));
+ printf("switch MAC = %s\n", ether_ntoa(&nc->switch_mac));
+ printf("sequence number = %u\n\n", nc->seqnum);
+ printf("received %d attribute(s)\n", attr->count);
+
+ for (ln = attr->first; ln != NULL; ln = ln->next) {
+ at = ln->data;
+ printf("received attribute code = %04X, length = %d\n", at->attr, at->size);
+ }
+
+ printf("---------------------------------\n\n");
+}
+
+
static void handler (int sig)
{
(void)sig;
{
int err = 0, sw_sock = -1, cl_sock = -1;
List *attr;
- ListNode *ln;
- struct attr *at;
struct nsdp_cmd nc;
struct sockaddr_in sw_local, cl_local;
struct pollfd fds[2];
if (err < 0)
continue;
- printf("---------------------------------\n");
-
- printf("received %d attribute(s)\n", attr->count);
-
- for (ln = attr->first; ln != NULL; ln = ln->next) {
- at = ln->data;
- printf("received attribute code = %04X, length = %d\n", at->attr, at->size);
- }
+ print_packet(attr, &nc);
clearList(attr, (void(*)(void*))freeAttr);
-
- printf("---------------------------------\n\n");
}
destroyList(attr, (void(*)(void*))freeAttr);