From 300a87389d19832b33c9e6a2c12577de6ef3e19d Mon Sep 17 00:00:00 2001 From: darkcoven Date: Sat, 12 Oct 2013 13:10:58 +0200 Subject: [PATCH] Spy: print more header details --- raw/src/encoding.c | 9 ++++- raw/src/encoding.h | 2 +- raw/src/net.c | 6 +--- spy/src/spy.c | 83 +++++++++++++++++++++++++++++++++++++++------- 4 files changed, 81 insertions(+), 19 deletions(-) diff --git a/raw/src/encoding.c b/raw/src/encoding.c index 5ac97d8..a21c372 100644 --- a/raw/src/encoding.c +++ b/raw/src/encoding.c @@ -24,7 +24,7 @@ void initNsdpHeader (struct nsdp_header *nh, const struct nsdp_cmd *nc) } -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; @@ -34,6 +34,10 @@ bool validateNsdpHeader (const struct nsdp_header *nh, const struct nsdp_cmd *nc 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; @@ -44,13 +48,16 @@ bool validateNsdpHeader (const struct nsdp_header *nh, const struct nsdp_cmd *nc 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; diff --git a/raw/src/encoding.h b/raw/src/encoding.h index 5c42e60..27ec812 100644 --- a/raw/src/encoding.h +++ b/raw/src/encoding.h @@ -26,7 +26,7 @@ struct nsdp_packet { void initNsdpHeader (struct nsdp_header *nh, const struct nsdp_cmd *nc); -bool validateNsdpHeader (const struct nsdp_header *nh, const struct nsdp_cmd *nc); +bool extractNsdpHeader (const struct nsdp_header *nh, struct nsdp_cmd *nc); static inline void initNsdpPacket (struct nsdp_packet *np) diff --git a/raw/src/net.c b/raw/src/net.c index f92b48e..c94f4eb 100644 --- a/raw/src/net.c +++ b/raw/src/net.c @@ -138,16 +138,12 @@ int recvNsdpPacket (int sock, struct nsdp_cmd *nc, List *attr, const struct time 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; } diff --git a/spy/src/spy.c b/spy/src/spy.c index 1d291f2..1e90bda 100644 --- a/spy/src/spy.c +++ b/spy/src/spy.c @@ -10,6 +10,76 @@ #include +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; @@ -21,8 +91,6 @@ int main (void) { 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]; @@ -101,18 +169,9 @@ int main (void) 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); -- 2.39.5