From aefeada189c7f08167d281094af69ac809d4f95f Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Thu, 6 Aug 2015 15:20:30 +0200 Subject: [PATCH] minissdpd: added command 0 (version) --- minissdpd/Changelog.txt | 5 ++++- minissdpd/README | 15 +++++++++++---- minissdpd/README.fr | 1 + minissdpd/config.h | 6 ++++-- minissdpd/minissdpd.c | 22 ++++++++++++++++++---- minissdpd/testminissdpd.c | 27 ++++++++++++++++++++++++++- 6 files changed, 64 insertions(+), 12 deletions(-) diff --git a/minissdpd/Changelog.txt b/minissdpd/Changelog.txt index 72e12e0..315fcaa 100644 --- a/minissdpd/Changelog.txt +++ b/minissdpd/Changelog.txt @@ -1,4 +1,7 @@ -$Id: Changelog.txt,v 1.41 2015/07/21 15:39:36 nanard Exp $ +$Id: Changelog.txt,v 1.42 2015/08/06 13:16:58 nanard Exp $ + +2015/08/06: + added command 0 (version) 2015/07/21: set multicast TTL to 2 by default and configurable diff --git a/minissdpd/README b/minissdpd/README index 4b44f1e..eb46def 100644 --- a/minissdpd/README +++ b/minissdpd/README @@ -24,15 +24,22 @@ close unix socket connection. * Request format : 1st byte : request type - 1 - type - 2 - USN (unique id) - 3 - everything - 4 - submit service (see below) + 0 - version + 1 - type + 2 - USN (unique id) + 3 - everything + 4 - submit service (see below) n bytes : string length : 1 byte if < 128 else the upper bit indicate that one additional byte should be read, etc. (see codelength.h) n bytes = string Response format : + +request type 0 (version) : +n bytes string length +n bytes = version string + +request type 1 / 2 / 3 : 1st byte : number of services/devices For each service/device : URL : diff --git a/minissdpd/README.fr b/minissdpd/README.fr index ec7486b..71ceac3 100644 --- a/minissdpd/README.fr +++ b/minissdpd/README.fr @@ -6,6 +6,7 @@ fermeture de la connexion. format de requete : 1 octet : type de la requete + 0 - version 1 - type 2 - USN (id unique) 3 - tout diff --git a/minissdpd/config.h b/minissdpd/config.h index 201ef23..7fc477c 100644 --- a/minissdpd/config.h +++ b/minissdpd/config.h @@ -1,12 +1,14 @@ -/* $Id: config.h,v 1.6 2012/09/27 15:40:29 nanard Exp $ */ +/* $Id: config.h,v 1.9 2015/08/06 13:16:58 nanard Exp $ */ /* MiniUPnP project * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ - * (c) 2006-2011 Thomas Bernard + * (c) 2006-2015 Thomas Bernard * This software is subject to the conditions detailed * in the LICENCE file provided within the distribution */ #ifndef CONFIG_H_INCLUDED #define CONFIG_H_INCLUDED +#define MINISSDPD_VERSION "1.3" + /* use BSD daemon() ? */ #define USE_DAEMON diff --git a/minissdpd/minissdpd.c b/minissdpd/minissdpd.c index af23696..6295af9 100644 --- a/minissdpd/minissdpd.c +++ b/minissdpd/minissdpd.c @@ -1,4 +1,4 @@ -/* $Id: minissdpd.c,v 1.48 2015/07/21 15:39:36 nanard Exp $ */ +/* $Id: minissdpd.c,v 1.49 2015/08/06 13:16:58 nanard Exp $ */ /* MiniUPnP project * (c) 2007-2015 Thomas Bernard * website : http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ @@ -27,8 +27,10 @@ /* unix sockets */ #include /* for getpwnam() and getgrnam() */ +#if 0 #include #include +#endif #include "getifaddr.h" #include "upnputils.h" @@ -727,7 +729,7 @@ void processRequest(struct reqelem * req) int type; struct device * d = devlist; unsigned char rbuf[RESPONSE_BUFFER_SIZE]; - unsigned char * rp = rbuf+1; + unsigned char * rp; unsigned char nrep = 0; time_t t; struct service * newserv = NULL; @@ -749,19 +751,31 @@ void processRequest(struct reqelem * req) p = buf + 1; DECODELENGTH_CHECKLIMIT(l, p, buf + n); if(p+l > buf+n) { - syslog(LOG_WARNING, "bad request (length encoding)"); + syslog(LOG_WARNING, "bad request (length encoding l=%u n=%u)", + l, (unsigned)n); goto error; } - if(l == 0 && type != 3) { + if(l == 0 && type != 3 && type != 0) { syslog(LOG_WARNING, "bad request (length=0)"); goto error; } syslog(LOG_INFO, "(s=%d) request type=%d str='%.*s'", req->socket, type, l, p); switch(type) { + case 0: /* version */ + rp = rbuf; + CODELENGTH((sizeof(MINISSDPD_VERSION) - 1), rp); + memcpy(rp, MINISSDPD_VERSION, sizeof(MINISSDPD_VERSION) - 1); + rp += (sizeof(MINISSDPD_VERSION) - 1); + if(write_or_buffer(req, rbuf, rp - rbuf) < 0) { + syslog(LOG_ERR, "(s=%d) write: %m", req->socket); + goto error; + } + break; case 1: /* request by type */ case 2: /* request by USN (unique id) */ case 3: /* everything */ + rp = rbuf+1; while(d && (nrep < 255)) { if(d->t < t) { syslog(LOG_INFO, "outdated device"); diff --git a/minissdpd/testminissdpd.c b/minissdpd/testminissdpd.c index 60ef8f9..6de2554 100644 --- a/minissdpd/testminissdpd.c +++ b/minissdpd/testminissdpd.c @@ -1,4 +1,4 @@ -/* $Id: testminissdpd.c,v 1.11 2015/05/27 20:03:21 nanard Exp $ */ +/* $Id: testminissdpd.c,v 1.12 2015/08/06 13:16:59 nanard Exp $ */ /* Project : miniupnp * website : http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * Author : Thomas BERNARD @@ -18,6 +18,19 @@ do { n = (n << 7) | (*p & 0x7f); } \ while(*(p++)&0x80); +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); +} + void printresponse(const unsigned char * resp, int n) { int i, l; @@ -97,6 +110,7 @@ int connect_unix_socket(const char * sockpath) int main(int argc, char * * argv) { + char command0[] = { 0x00, 0x00 }; char command1[] = "\x01\x00urn:schemas-upnp-org:device:InternetGatewayDevice"; char command2[] = "\x02\x00uuid:fc4ec57e-b051-11db-88f8-0060085db3f6::upnp:rootdevice"; char command3[] = { 0x03, 0x00 }; @@ -126,6 +140,17 @@ main(int argc, char * * argv) command4[1] = sizeof(command4) - 3; s = connect_unix_socket(sockpath); + 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); + } + n = SENDCOMMAND(command1, sizeof(command1) - 1); n = read(s, buf, sizeof(buf)); printf("Response received %d bytes\n", (int)n);