2015-08-06 13:20:30 +00:00
|
|
|
/* $Id: testminissdpd.c,v 1.12 2015/08/06 13:16:59 nanard Exp $ */
|
2011-09-28 19:14:08 +00:00
|
|
|
/* Project : miniupnp
|
|
|
|
* website : http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
|
|
|
|
* Author : Thomas BERNARD
|
2016-01-13 15:07:09 +00:00
|
|
|
* copyright (c) 2005-2016 Thomas Bernard
|
2011-09-28 19:14:08 +00:00
|
|
|
* This software is subjet to the conditions detailed in the
|
|
|
|
* provided LICENCE file. */
|
2015-05-26 19:32:39 +00:00
|
|
|
|
2011-09-28 19:14:08 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
2014-02-28 19:24:19 +00:00
|
|
|
#include <stdlib.h>
|
2011-09-28 19:14:08 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
2016-01-13 15:07:09 +00:00
|
|
|
#include "codelength.h"
|
|
|
|
#include "printresponse.h"
|
2011-09-28 19:14:08 +00:00
|
|
|
|
2015-08-06 13:20:30 +00:00
|
|
|
void printversion(const unsigned char * resp, int n)
|
|
|
|
{
|
|
|
|
int l;
|
|
|
|
const unsigned char * p;
|
|
|
|
|
|
|
|
p = resp;
|
|
|
|
DECODELENGTH(l, p);
|
|
|
|
if(resp + n < p + l) {
|
|
|
|
printf("get version error\n");
|
|
|
|
}
|
|
|
|
printf("MiniSSDPd version : %.*s\n", l, p);
|
|
|
|
}
|
|
|
|
|
2011-09-28 19:14:08 +00:00
|
|
|
#define SENDCOMMAND(command, size) write(s, command, size); \
|
2015-06-16 12:35:08 +00:00
|
|
|
printf("Command written type=%u\n", (unsigned char)command[0]);
|
2011-09-28 19:14:08 +00:00
|
|
|
|
2014-02-28 19:24:19 +00:00
|
|
|
int connect_unix_socket(const char * sockpath)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
|
|
|
|
s = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
strncpy(addr.sun_path, sockpath, sizeof(addr.sun_path));
|
|
|
|
if(connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
|
2015-06-16 12:35:08 +00:00
|
|
|
fprintf(stderr, "connecting to %s : ", addr.sun_path);
|
2014-02-28 19:24:19 +00:00
|
|
|
perror("connect");
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-05-27 08:41:37 +00:00
|
|
|
printf("Connected to %s\n", addr.sun_path);
|
2014-02-28 19:24:19 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-09-28 19:14:08 +00:00
|
|
|
/* test program for minissdpd */
|
|
|
|
int
|
|
|
|
main(int argc, char * * argv)
|
|
|
|
{
|
2016-01-13 15:07:09 +00:00
|
|
|
const char command0[] = { 0x00, 0x00 };
|
2011-09-28 19:14:08 +00:00
|
|
|
char command1[] = "\x01\x00urn:schemas-upnp-org:device:InternetGatewayDevice";
|
|
|
|
char command2[] = "\x02\x00uuid:fc4ec57e-b051-11db-88f8-0060085db3f6::upnp:rootdevice";
|
2016-01-13 15:07:09 +00:00
|
|
|
const char command3[] = { 0x03, 0x00 };
|
2015-05-27 08:49:06 +00:00
|
|
|
/* old versions of minissdpd would reject a command with
|
|
|
|
* a zero length string argument */
|
|
|
|
char command3compat[] = "\x03\x00ssdp:all";
|
2014-02-28 19:24:19 +00:00
|
|
|
char command4[] = "\x04\x00test:test:test";
|
2016-01-13 15:07:09 +00:00
|
|
|
const char bad_command[] = { 0xff, 0xff };
|
|
|
|
const char overflow[] = { 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
|
|
|
|
const char command5[] = { 0x05, 0x00 };
|
2011-09-28 19:14:08 +00:00
|
|
|
int s;
|
|
|
|
int i;
|
2015-05-27 08:48:05 +00:00
|
|
|
void * tmp;
|
|
|
|
unsigned char * resp = NULL;
|
|
|
|
size_t respsize = 0;
|
|
|
|
unsigned char buf[4096];
|
2011-09-28 19:14:08 +00:00
|
|
|
ssize_t n;
|
2015-05-27 08:48:05 +00:00
|
|
|
int total = 0;
|
2011-09-28 19:14:08 +00:00
|
|
|
const char * sockpath = "/var/run/minissdpd.sock";
|
|
|
|
|
|
|
|
for(i=0; i<argc-1; i++) {
|
|
|
|
if(0==strcmp(argv[i], "-s"))
|
|
|
|
sockpath = argv[++i];
|
|
|
|
}
|
|
|
|
command1[1] = sizeof(command1) - 3;
|
|
|
|
command2[1] = sizeof(command2) - 3;
|
2015-05-27 08:49:06 +00:00
|
|
|
command3compat[1] = sizeof(command3compat) - 3;
|
2014-02-28 19:24:19 +00:00
|
|
|
command4[1] = sizeof(command4) - 3;
|
|
|
|
s = connect_unix_socket(sockpath);
|
2011-09-28 19:14:08 +00:00
|
|
|
|
2015-08-06 13:20:30 +00:00
|
|
|
n = SENDCOMMAND(command0, sizeof(command0));
|
|
|
|
n = read(s, buf, sizeof(buf));
|
|
|
|
printf("Response received %d bytes\n", (int)n);
|
|
|
|
if(n > 0) {
|
|
|
|
printversion(buf, n);
|
|
|
|
} else {
|
|
|
|
printf("Command 0 (get version) not supported\n");
|
|
|
|
close(s);
|
|
|
|
s = connect_unix_socket(sockpath);
|
|
|
|
}
|
|
|
|
|
2015-07-29 21:50:08 +00:00
|
|
|
n = SENDCOMMAND(command1, sizeof(command1) - 1);
|
2011-09-28 19:14:08 +00:00
|
|
|
n = read(s, buf, sizeof(buf));
|
|
|
|
printf("Response received %d bytes\n", (int)n);
|
|
|
|
printresponse(buf, n);
|
2014-02-28 19:24:19 +00:00
|
|
|
if(n == 0) {
|
|
|
|
close(s);
|
|
|
|
s = connect_unix_socket(sockpath);
|
|
|
|
}
|
2011-09-28 19:14:08 +00:00
|
|
|
|
2015-07-29 21:50:08 +00:00
|
|
|
n = SENDCOMMAND(command2, sizeof(command2) - 1);
|
2011-09-28 19:14:08 +00:00
|
|
|
n = read(s, buf, sizeof(buf));
|
|
|
|
printf("Response received %d bytes\n", (int)n);
|
|
|
|
printresponse(buf, n);
|
2014-02-28 19:24:19 +00:00
|
|
|
if(n == 0) {
|
|
|
|
close(s);
|
|
|
|
s = connect_unix_socket(sockpath);
|
|
|
|
}
|
2011-09-28 19:14:08 +00:00
|
|
|
|
2015-05-27 08:48:05 +00:00
|
|
|
buf[0] = 0; /* Slight hack for printing num devices when 0 */
|
2015-07-29 21:50:08 +00:00
|
|
|
n = SENDCOMMAND(command3, sizeof(command3));
|
2015-05-27 08:48:05 +00:00
|
|
|
n = read(s, buf, sizeof(buf));
|
2015-05-27 08:49:06 +00:00
|
|
|
if(n == 0) {
|
|
|
|
printf("command3 failed, testing compatible one\n");
|
|
|
|
close(s);
|
|
|
|
s = connect_unix_socket(sockpath);
|
2015-07-29 21:50:08 +00:00
|
|
|
n = SENDCOMMAND(command3compat, sizeof(command3compat) - 1);
|
2015-05-27 08:49:06 +00:00
|
|
|
n = read(s, buf, sizeof(buf));
|
|
|
|
}
|
2011-09-28 19:14:08 +00:00
|
|
|
printf("Response received %d bytes\n", (int)n);
|
2015-05-27 08:48:05 +00:00
|
|
|
printf("Number of devices %d\n", (int)buf[0]);
|
|
|
|
while(n > 0) {
|
|
|
|
tmp = realloc(resp, respsize + n);
|
|
|
|
if(tmp == NULL) {
|
|
|
|
fprintf(stderr, "memory allocation error\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
resp = tmp;
|
|
|
|
respsize += n;
|
|
|
|
if (n > 0) {
|
|
|
|
memcpy(resp + total, buf, n);
|
|
|
|
total += n;
|
|
|
|
}
|
|
|
|
if (n < (ssize_t)sizeof(buf)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = read(s, buf, sizeof(buf));
|
|
|
|
printf("response received %d bytes\n", (int)n);
|
|
|
|
}
|
|
|
|
if(resp != NULL) {
|
|
|
|
printresponse(resp, total);
|
|
|
|
free(resp);
|
|
|
|
resp = NULL;
|
|
|
|
}
|
2014-02-28 19:24:19 +00:00
|
|
|
if(n == 0) {
|
|
|
|
close(s);
|
|
|
|
s = connect_unix_socket(sockpath);
|
|
|
|
}
|
|
|
|
|
2015-07-29 21:50:08 +00:00
|
|
|
n = SENDCOMMAND(command4, sizeof(command4));
|
2015-05-27 08:41:37 +00:00
|
|
|
/* no response for request type 4 */
|
2014-02-28 19:24:19 +00:00
|
|
|
|
2015-07-29 21:50:08 +00:00
|
|
|
n = SENDCOMMAND(bad_command, sizeof(bad_command));
|
2014-02-28 19:24:19 +00:00
|
|
|
n = read(s, buf, sizeof(buf));
|
|
|
|
printf("Response received %d bytes\n", (int)n);
|
|
|
|
printresponse(buf, n);
|
|
|
|
if(n == 0) {
|
|
|
|
close(s);
|
|
|
|
s = connect_unix_socket(sockpath);
|
|
|
|
}
|
|
|
|
|
2015-07-29 21:50:08 +00:00
|
|
|
n = SENDCOMMAND(overflow, sizeof(overflow));
|
2014-02-28 19:24:19 +00:00
|
|
|
n = read(s, buf, sizeof(buf));
|
|
|
|
printf("Response received %d bytes\n", (int)n);
|
|
|
|
printresponse(buf, n);
|
2016-01-13 15:07:09 +00:00
|
|
|
if(n == 0) {
|
|
|
|
close(s);
|
|
|
|
s = connect_unix_socket(sockpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
n = SENDCOMMAND(command5, sizeof(command5));
|
|
|
|
n = read(s, buf, sizeof(buf));
|
|
|
|
printf("Response received %d bytes\n", (int)n);
|
|
|
|
printresponse(buf, n);
|
2011-09-28 19:14:08 +00:00
|
|
|
|
|
|
|
close(s);
|
|
|
|
return 0;
|
|
|
|
}
|