minissdpd/testminissdpd.c: Better HexDump

also improved comments and debug output
This commit is contained in:
Thomas Bernard 2015-06-16 14:35:08 +02:00
parent 8a180b1cac
commit edd7c7bbdc
1 changed files with 22 additions and 10 deletions

View File

@ -1,4 +1,4 @@
/* $Id: testminissdpd.c,v 1.8 2014/02/28 18:38:21 nanard Exp $ */ /* $Id: testminissdpd.c,v 1.11 2015/05/27 20:03:21 nanard Exp $ */
/* Project : miniupnp /* Project : miniupnp
* website : http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * website : http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* Author : Thomas BERNARD * Author : Thomas BERNARD
@ -25,10 +25,22 @@ void printresponse(const unsigned char * resp, int n)
const unsigned char * p; const unsigned char * p;
if(n == 0) if(n == 0)
return; return;
for(i=0; i<n; i++) /* first, hexdump the response : */
printf("%02x ", resp[i]); for(i = 0; i < n; i += 16) {
printf("\n"); printf("%06x | ", i);
nresp = resp[0]; for(l = i; l < n && l < (i + 16); l++)
printf("%02x ", resp[l]);
while(l < (i + 16)) {
printf(" ");
l++;
}
printf("| ");
for(l = i; l < n && l < (i + 16); l++)
putchar((resp[l] >= ' ' && resp[l] < 128) ? resp[l] : '.');
putchar('\n');
}
/* now parse and display all devices of response */
nresp = resp[0]; /* 1st byte : number of devices in response */
p = resp + 1; p = resp + 1;
for(i = 0; i < (int)nresp; i++) { for(i = 0; i < (int)nresp; i++) {
if(p >= resp + n) if(p >= resp + n)
@ -37,7 +49,7 @@ void printresponse(const unsigned char * resp, int n)
DECODELENGTH(l, p); DECODELENGTH(l, p);
if(p + l > resp + n) if(p + l > resp + n)
goto error; goto error;
printf("%d - %.*s\n", i, l, p); printf("%d - %.*s\n", i, l, p); /* URL */
p += l; p += l;
if(p >= resp + n) if(p >= resp + n)
goto error; goto error;
@ -45,7 +57,7 @@ void printresponse(const unsigned char * resp, int n)
DECODELENGTH(l, p); DECODELENGTH(l, p);
if(p + l > resp + n) if(p + l > resp + n)
goto error; goto error;
printf(" %.*s\n", l, p); printf(" %.*s\n", l, p); /* ST */
p += l; p += l;
if(p >= resp + n) if(p >= resp + n)
goto error; goto error;
@ -53,7 +65,7 @@ void printresponse(const unsigned char * resp, int n)
DECODELENGTH(l, p); DECODELENGTH(l, p);
if(p + l > resp + n) if(p + l > resp + n)
goto error; goto error;
printf(" %.*s\n", l, p); printf(" %.*s\n", l, p); /* USN */
p += l; p += l;
} }
return; return;
@ -62,7 +74,7 @@ error:
} }
#define SENDCOMMAND(command, size) write(s, command, size); \ #define SENDCOMMAND(command, size) write(s, command, size); \
printf("Command written type=%u\n", (unsigned)command[0]); printf("Command written type=%u\n", (unsigned char)command[0]);
int connect_unix_socket(const char * sockpath) int connect_unix_socket(const char * sockpath)
{ {
@ -73,7 +85,7 @@ int connect_unix_socket(const char * sockpath)
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, sockpath, sizeof(addr.sun_path)); strncpy(addr.sun_path, sockpath, sizeof(addr.sun_path));
if(connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) { if(connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
fprintf(stderr, "connecting to %s\n", addr.sun_path); fprintf(stderr, "connecting to %s : ", addr.sun_path);
perror("connect"); perror("connect");
exit(1); exit(1);
} }