improve printresponse() to consume all buffer bytes

This commit is contained in:
Thomas Bernard 2016-01-17 19:36:11 +01:00
parent 02e64e9f85
commit eea61f81c3
1 changed files with 45 additions and 42 deletions

View File

@ -1,5 +1,6 @@
/* $Id: $ */ /* $Id: $ */
/* Project : miniupnp /* vim: tabstop=4 shiftwidth=4 noexpandtab
* 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
* copyright (c) 2005-2016 Thomas Bernard * copyright (c) 2005-2016 Thomas Bernard
@ -36,12 +37,13 @@ void printresponse(const unsigned char * resp, int n)
putchar((resp[l] >= ' ' && resp[l] < 128) ? resp[l] : '.'); putchar((resp[l] >= ' ' && resp[l] < 128) ? resp[l] : '.');
putchar('\n'); putchar('\n');
} }
for(p = resp; p < resp + n; ) {
/* now parse and display all devices of response */ /* now parse and display all devices of response */
nresp = resp[0]; /* 1st byte : number of devices in response */ nresp = p[0]; /* 1st byte : number of devices in response */
if(nresp == 0xff) { if(nresp == 0xff) {
/* notification */ /* notification */
notif_type = resp[1]; notif_type = p[1];
nresp = resp[2]; nresp = p[2];
printf("Notification : "); printf("Notification : ");
switch(notif_type) { switch(notif_type) {
case NOTIF_NEW: printf("new\n"); break; case NOTIF_NEW: printf("new\n"); break;
@ -49,9 +51,9 @@ void printresponse(const unsigned char * resp, int n)
case NOTIF_REMOVE: printf("remove\n"); break; case NOTIF_REMOVE: printf("remove\n"); break;
default: printf("**UNKNOWN**\n"); default: printf("**UNKNOWN**\n");
} }
p = resp + 3; p += 3;
} else { } else {
p = resp + 1; p++;
} }
for(i = 0; i < (int)nresp; i++) { for(i = 0; i < (int)nresp; i++) {
if(p >= resp + n) if(p >= resp + n)
@ -79,6 +81,7 @@ void printresponse(const unsigned char * resp, int n)
printf(" %.*s\n", l, p); /* USN */ printf(" %.*s\n", l, p); /* USN */
p += l; p += l;
} }
}
return; return;
error: error:
printf("*** WARNING : TRUNCATED RESPONSE ***\n"); printf("*** WARNING : TRUNCATED RESPONSE ***\n");