From 2ae481b9693d56f0372df9bf8ca968d93302fc12 Mon Sep 17 00:00:00 2001 From: Karl Hiramoto Date: Fri, 15 Nov 2013 10:42:27 +0100 Subject: [PATCH 1/7] some libc implementations don't define struct ip_mreqn define it here if CFLAG passed with -DNEED_STRUCT_IP_MREQN I found this issue cross compiling for ARM using eclibc in the https://www.yoctoproject.org/ build system. --- miniupnpc/miniupnpc.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/miniupnpc/miniupnpc.c b/miniupnpc/miniupnpc.c index 5c191c5..95504b1 100644 --- a/miniupnpc/miniupnpc.c +++ b/miniupnpc/miniupnpc.c @@ -71,6 +71,17 @@ #define TIMEVAL struct timeval #endif + +#if defined(HAS_IP_MREQN) && defined(NEED_STRUCT_IP_MREQN) +/* Several versions of glibc don't define this structure, define it here and compile with CFLAGS NEED_STRUCT_IP_MREQN */ +struct ip_mreqn +{ + struct in_addr imr_multiaddr; /* IP multicast address of group */ + struct in_addr imr_address; /* local IP address of interface */ + int imr_ifindex; /* Interface index */ +}; +#endif + #include "miniupnpc.h" #include "minissdpc.h" #include "miniwget.h" From b0c66c01e2a8cfc781f14abc711d418ff6d6667b Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Mon, 9 Dec 2013 09:24:28 +0100 Subject: [PATCH 2/7] miniupnpc: define MAXHOSTNAMELEN if not already done --- miniupnpc/Changelog.txt | 3 +++ miniupnpc/connecthostport.c | 8 ++++++-- miniupnpc/miniupnpc.c | 8 ++++++-- miniupnpc/miniwget.c | 8 ++++++-- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/miniupnpc/Changelog.txt b/miniupnpc/Changelog.txt index 4dc1aa9..50b8221 100644 --- a/miniupnpc/Changelog.txt +++ b/miniupnpc/Changelog.txt @@ -1,6 +1,9 @@ $Id: Changelog.txt,v 1.187 2013/06/06 21:36:39 nanard Exp $ miniUPnP client Changelog. +2013/08/01: + define MAXHOSTNAMELEN if not already done + 2013/06/06: update upnpreplyparse to allow larger values (128 chars instead of 64) diff --git a/miniupnpc/connecthostport.c b/miniupnpc/connecthostport.c index 98a00c4..aabc7a6 100644 --- a/miniupnpc/connecthostport.c +++ b/miniupnpc/connecthostport.c @@ -1,7 +1,7 @@ -/* $Id: connecthostport.c,v 1.9 2012/06/26 00:00:27 nanard Exp $ */ +/* $Id: connecthostport.c,v 1.11 2013/08/01 21:21:25 nanard Exp $ */ /* Project : miniupnp * Author : Thomas Bernard - * Copyright (c) 2010-2012 Thomas Bernard + * Copyright (c) 2010-2013 Thomas Bernard * This software is subject to the conditions detailed in the * LICENCE file provided in this distribution. */ @@ -51,6 +51,10 @@ #include "connecthostport.h" +#ifndef MAXHOSTNAMELEN +#define MAXHOSTNAMELEN 64 +#endif + /* connecthostport() * return a socket connected (TCP) to the host and port * or -1 in case of error */ diff --git a/miniupnpc/miniupnpc.c b/miniupnpc/miniupnpc.c index 95504b1..5405e23 100644 --- a/miniupnpc/miniupnpc.c +++ b/miniupnpc/miniupnpc.c @@ -1,8 +1,8 @@ -/* $Id: miniupnpc.c,v 1.111 2012/10/09 17:53:14 nanard Exp $ */ +/* $Id: miniupnpc.c,v 1.113 2013/10/07 10:04:56 nanard Exp $ */ /* Project : miniupnp * Web : http://miniupnp.free.fr/ * Author : Thomas BERNARD - * copyright (c) 2005-2012 Thomas Bernard + * copyright (c) 2005-2013 Thomas Bernard * This software is subjet to the conditions detailed in the * provided LICENSE file. */ #define __EXTENSIONS__ 1 @@ -97,6 +97,10 @@ struct ip_mreqn #define PRINT_SOCKET_ERROR(x) perror(x) #endif +#ifndef MAXHOSTNAMELEN +#define MAXHOSTNAMELEN 64 +#endif + #define SOAPPREFIX "s" #define SERVICEPREFIX "u" #define SERVICEPREFIX2 'u' diff --git a/miniupnpc/miniwget.c b/miniupnpc/miniwget.c index 31f3af0..a420d57 100644 --- a/miniupnpc/miniwget.c +++ b/miniupnpc/miniwget.c @@ -1,8 +1,8 @@ -/* $Id: miniwget.c,v 1.58 2012/08/11 05:52:49 nanard Exp $ */ +/* $Id: miniwget.c,v 1.60 2013/10/07 10:03:16 nanard Exp $ */ /* Project : miniupnp * Website : http://miniupnp.free.fr/ * Author : Thomas Bernard - * Copyright (c) 2005-2012 Thomas Bernard + * Copyright (c) 2005-2013 Thomas Bernard * This software is subject to the conditions detailed in the * LICENCE file provided in this distribution. */ @@ -52,6 +52,10 @@ #include "connecthostport.h" #include "receivedata.h" +#ifndef MAXHOSTNAMELEN +#define MAXHOSTNAMELEN 64 +#endif + /* * Read a HTTP response from a socket. * Process Content-Length and Transfer-encoding headers. From 3a87aa2f10bd7f1408e1849bdb59c41dd63a9fe9 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Mon, 9 Dec 2013 09:25:39 +0100 Subject: [PATCH 3/7] miniwget.c: fixed potential buffer overrun --- miniupnpc/Changelog.txt | 3 +++ miniupnpc/miniwget.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/miniupnpc/Changelog.txt b/miniupnpc/Changelog.txt index 50b8221..1aded29 100644 --- a/miniupnpc/Changelog.txt +++ b/miniupnpc/Changelog.txt @@ -1,6 +1,9 @@ $Id: Changelog.txt,v 1.187 2013/06/06 21:36:39 nanard Exp $ miniUPnP client Changelog. +2013/10/07: + fixed potential buffer overrun in miniwget.c + 2013/08/01: define MAXHOSTNAMELEN if not already done diff --git a/miniupnpc/miniwget.c b/miniupnpc/miniwget.c index a420d57..a75f55b 100644 --- a/miniupnpc/miniwget.c +++ b/miniupnpc/miniwget.c @@ -162,7 +162,7 @@ getHTTPResponse(int s, int * size) chunked = 1; } } - while(header_buf[i]=='\r' || header_buf[i] == '\n') + while((i < (int)header_buf_used) && (header_buf[i]=='\r' || header_buf[i] == '\n')) i++; linestart = i; colon = linestart; From 6e20b2af614b27b14c2f20553f195041edc45a55 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Mon, 9 Dec 2013 09:29:08 +0100 Subject: [PATCH 4/7] miniupnpc.c: UPNP_GetValidIGD() now checks for ExternalIpAddress --- miniupnpc/Changelog.txt | 3 +- miniupnpc/miniupnpc.c | 62 ++++++++++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/miniupnpc/Changelog.txt b/miniupnpc/Changelog.txt index 1aded29..4fa6cee 100644 --- a/miniupnpc/Changelog.txt +++ b/miniupnpc/Changelog.txt @@ -1,8 +1,9 @@ -$Id: Changelog.txt,v 1.187 2013/06/06 21:36:39 nanard Exp $ +$Id: Changelog.txt,v 1.189 2013/10/07 10:04:55 nanard Exp $ miniUPnP client Changelog. 2013/10/07: fixed potential buffer overrun in miniwget.c + Modified UPNP_GetValidIGD() to check for ExternalIpAddress 2013/08/01: define MAXHOSTNAMELEN if not already done diff --git a/miniupnpc/miniupnpc.c b/miniupnpc/miniupnpc.c index 5405e23..d9debde 100644 --- a/miniupnpc/miniupnpc.c +++ b/miniupnpc/miniupnpc.c @@ -882,7 +882,7 @@ UPNPIGD_IsConnected(struct UPNPUrls * urls, struct IGDdatas * data) * not connected * 3 = an UPnP device has been found but was not recognized as an IGD * - * In any non zero return case, the urls and data structures + * In any positive non zero return case, the urls and data structures * passed as parameters are set. Donc forget to call FreeUPNPUrls(urls) to * free allocated memory. */ @@ -895,11 +895,14 @@ UPNP_GetValidIGD(struct UPNPDev * devlist, struct xml_desc { char * xml; int size; + int is_igd; } * desc = NULL; struct UPNPDev * dev; int ndev = 0; int i; int state = -1; /* state 1 : IGD connected. State 2 : IGD. State 3 : anything */ + int n_igd = 0; + char extIpAddr[16]; if(!devlist) { #ifdef DEBUG @@ -907,6 +910,7 @@ UPNP_GetValidIGD(struct UPNPDev * devlist, #endif return 0; } + /* counting total number of devices in the list */ for(dev = devlist; dev; dev = dev->pNext) ndev++; if(ndev > 0) @@ -915,41 +919,58 @@ UPNP_GetValidIGD(struct UPNPDev * devlist, if(!desc) return -1; /* memory allocation error */ } + /* Step 1 : downloading descriptions and testing type */ + for(dev = devlist, i = 0; dev; dev = dev->pNext, i++) + { + /* we should choose an internet gateway device. + * with st == urn:schemas-upnp-org:device:InternetGatewayDevice:1 */ + desc[i].xml = miniwget_getaddr(dev->descURL, &(desc[i].size), + lanaddr, lanaddrlen, + dev->scope_id); +#ifdef DEBUG + if(!desc[i].xml) + { + printf("error getting XML description %s\n", dev->descURL); + } +#endif + if(desc[i].xml) + { + memset(data, 0, sizeof(struct IGDdatas)); + memset(urls, 0, sizeof(struct UPNPUrls)); + parserootdesc(desc[i].xml, desc[i].size, data); + if(0==strcmp(data->CIF.servicetype, + "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1")) + { + desc[i].is_igd = 1; + n_igd++; + } + } + } + /* iterate the list to find a device depending on state */ for(state = 1; state <= 3; state++) { for(dev = devlist, i = 0; dev; dev = dev->pNext, i++) { - /* we should choose an internet gateway device. - * with st == urn:schemas-upnp-org:device:InternetGatewayDevice:1 */ - if(state == 1) - { - desc[i].xml = miniwget_getaddr(dev->descURL, &(desc[i].size), - lanaddr, lanaddrlen, - dev->scope_id); -#ifdef DEBUG - if(!desc[i].xml) - { - printf("error getting XML description %s\n", dev->descURL); - } -#endif - } if(desc[i].xml) { memset(data, 0, sizeof(struct IGDdatas)); memset(urls, 0, sizeof(struct UPNPUrls)); parserootdesc(desc[i].xml, desc[i].size, data); - if(0==strcmp(data->CIF.servicetype, - "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1") - || state >= 3 ) + if(desc[i].is_igd || state >= 3 ) { GetUPNPUrls(urls, data, dev->descURL, dev->scope_id); + /* in state 2 and 3 we dont test if device is connected ! */ + if(state >= 2) + goto free_and_return; #ifdef DEBUG printf("UPNPIGD_IsConnected(%s) = %d\n", urls->controlURL, UPNPIGD_IsConnected(urls, data)); #endif - if((state >= 2) || UPNPIGD_IsConnected(urls, data)) + /* checks that status is connected AND there is a external IP address assigned */ + if(UPNPIGD_IsConnected(urls, data) + && (UPNP_GetExternalIPAddress(urls->controlURL, data->first.servicetype, extIpAddr) == 0)) goto free_and_return; FreeUPNPUrls(urls); if(data->second.servicetype[0] != '\0') { @@ -967,7 +988,8 @@ UPNP_GetValidIGD(struct UPNPDev * devlist, urls->controlURL, UPNPIGD_IsConnected(urls, data)); #endif - if((state >= 2) || UPNPIGD_IsConnected(urls, data)) + if(UPNPIGD_IsConnected(urls, data) + && (UPNP_GetExternalIPAddress(urls->controlURL, data->first.servicetype, extIpAddr) == 0)) goto free_and_return; FreeUPNPUrls(urls); } From 3ae6b969a31ffbd15689b95fc73e01aa93ceddf7 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Mon, 9 Dec 2013 09:30:18 +0100 Subject: [PATCH 5/7] testminiwget.sh: fix comment (it now works with dash) --- miniupnpc/testminiwget.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/miniupnpc/testminiwget.sh b/miniupnpc/testminiwget.sh index 4f08803..80e153b 100755 --- a/miniupnpc/testminiwget.sh +++ b/miniupnpc/testminiwget.sh @@ -1,5 +1,5 @@ #!/bin/sh -# $Id: testminiwget.sh,v 1.9 2013/04/27 15:47:27 nanard Exp $ +# $Id: testminiwget.sh,v 1.10 2013/11/13 15:08:08 nanard Exp $ # project miniupnp : http://miniupnp.free.fr/ # (c) 2011-2012 Thomas Bernard # @@ -13,7 +13,7 @@ # 4 - kills the local HTTP server and exits # # The script was tested and works with ksh, bash -# It fails to run with dash 0.5.5.1 because of "kill %1" +# it should now also run with dash TMPD=`mktemp -d miniwgetXXXXXXXXXX` HTTPSERVEROUT="${TMPD}/httpserverout" From 81f57226eb76c1ab03c25e8f762539fb03ea9f5b Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Mon, 9 Dec 2013 09:30:53 +0100 Subject: [PATCH 6/7] upnpcommands.c: fixes potential null dereference. Fix #046 --- miniupnpc/upnpcommands.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/miniupnpc/upnpcommands.c b/miniupnpc/upnpcommands.c index 662c10b..76d2d8a 100644 --- a/miniupnpc/upnpcommands.c +++ b/miniupnpc/upnpcommands.c @@ -1,4 +1,4 @@ -/* $Id: upnpcommands.c,v 1.40 2012/06/23 22:36:35 nanard Exp $ */ +/* $Id: upnpcommands.c,v 1.41 2013/12/09 08:18:23 nanard Exp $ */ /* Project : miniupnp * Author : Thomas Bernard * Copyright (c) 2005-2012 Thomas Bernard @@ -759,7 +759,7 @@ UPNP_GetFirewallStatus(const char * controlURL, char * fe, *ipa, *p; int ret = UPNPCOMMAND_UNKNOWN_ERROR; - if(!firewallEnabled && !inboundPinholeAllowed) + if(!firewallEnabled || !inboundPinholeAllowed) return UPNPCOMMAND_INVALID_ARGS; buffer = simpleUPnPcommand(-1, controlURL, servicetype, From b6ecae893ca159f2116e8ca480aeb462feb23817 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Mon, 9 Dec 2013 10:25:27 +0100 Subject: [PATCH 7/7] upnpc.c: --help and -h arguments fixes #037 --- miniupnpc/Changelog.txt | 3 +++ miniupnpc/upnpc.c | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/miniupnpc/Changelog.txt b/miniupnpc/Changelog.txt index 4fa6cee..d09ba0a 100644 --- a/miniupnpc/Changelog.txt +++ b/miniupnpc/Changelog.txt @@ -1,6 +1,9 @@ $Id: Changelog.txt,v 1.189 2013/10/07 10:04:55 nanard Exp $ miniUPnP client Changelog. +2013/12/09: + --help and -h arguments in upnpc.c + 2013/10/07: fixed potential buffer overrun in miniwget.c Modified UPNP_GetValidIGD() to check for ExternalIpAddress diff --git a/miniupnpc/upnpc.c b/miniupnpc/upnpc.c index 54cddf9..ff132aa 100644 --- a/miniupnpc/upnpc.c +++ b/miniupnpc/upnpc.c @@ -506,6 +506,11 @@ int main(int argc, char ** argv) /* command line processing */ for(i=1; i