minissdpd/testminissdpd.c: improve large buffer support

update 122617e5a6
This commit is contained in:
Thomas Bernard 2015-05-27 10:48:05 +02:00
parent 210851edc2
commit a48fbe86f2
2 changed files with 34 additions and 23 deletions

View File

@ -1,5 +1,8 @@
$Id: Changelog.txt,v 1.39 2014/12/05 13:42:59 nanard Exp $ $Id: Changelog.txt,v 1.39 2014/12/05 13:42:59 nanard Exp $
2015/05/27:
support larger buffer size (useful for type 3 requests)
VERSION 1.3: VERSION 1.3:
2014/12/05: 2014/12/05:

View File

@ -5,7 +5,6 @@
* copyright (c) 2005-2015 Thomas Bernard * copyright (c) 2005-2015 Thomas Bernard
* This software is subjet to the conditions detailed in the * This software is subjet to the conditions detailed in the
* provided LICENCE file. */ * provided LICENCE file. */
#include "config.h"
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
@ -94,14 +93,14 @@ main(int argc, char * * argv)
char overflow[] = { 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; char overflow[] = { 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
int s; int s;
int i; int i;
unsigned char buf[RESPONSE_BUFFER_SIZE]; void * tmp;
unsigned char chunk[4096]; unsigned char * resp = NULL;
size_t respsize = 0;
unsigned char buf[4096];
ssize_t n; ssize_t n;
int total = 0; int total = 0;
const char * sockpath = "/var/run/minissdpd.sock"; const char * sockpath = "/var/run/minissdpd.sock";
printf("Response buffer size %d bytes\n", (int)RESPONSE_BUFFER_SIZE);
for(i=0; i<argc-1; i++) { for(i=0; i<argc-1; i++) {
if(0==strcmp(argv[i], "-s")) if(0==strcmp(argv[i], "-s"))
sockpath = argv[++i]; sockpath = argv[++i];
@ -129,26 +128,35 @@ main(int argc, char * * argv)
s = connect_unix_socket(sockpath); s = connect_unix_socket(sockpath);
} }
chunk[0] = 0; /* Slight hack for printing num devices when 0 */ buf[0] = 0; /* Slight hack for printing num devices when 0 */
SENDCOMMAND(command3, sizeof(command3)); SENDCOMMAND(command3, sizeof(command3));
n = read(s, chunk, sizeof(chunk)); n = read(s, buf, sizeof(buf));
printf("Response received %d bytes\n", (int)n); printf("Response received %d bytes\n", (int)n);
printf("Number of devices %d\n", (int)chunk[0]); printf("Number of devices %d\n", (int)buf[0]);
while (1) { while(n > 0) {
if (n < (int)sizeof(chunk)) { tmp = realloc(resp, respsize + n);
if (n > 0) { if(tmp == NULL) {
memcpy(buf + total, chunk, n); fprintf(stderr, "memory allocation error\n");
total += n; break;
} }
break; resp = tmp;
} respsize += n;
if (n > 0) {
memcpy(resp + total, buf, n);
total += n;
}
if (n < (ssize_t)sizeof(buf)) {
break;
}
memcpy(buf + total, chunk, n); n = read(s, buf, sizeof(buf));
total += n; printf("response received %d bytes\n", (int)n);
n = read(s, chunk, sizeof(chunk)); }
printf("response received %d bytes\n", (int)n); if(resp != NULL) {
} printresponse(resp, total);
printresponse(buf, total); free(resp);
resp = NULL;
}
if(n == 0) { if(n == 0) {
close(s); close(s);
s = connect_unix_socket(sockpath); s = connect_unix_socket(sockpath);