From 6419602e1412067315c2906744ce96baa0467590 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Thu, 20 Mar 2014 13:49:10 +0100 Subject: [PATCH] miniupnpd/portinuse: port_in_use() returns -1 in case of error --- miniupnpd/natpmp.c | 5 +++-- miniupnpd/portinuse.c | 7 ++++--- miniupnpd/portinuse.h | 5 +++-- miniupnpd/upnpredirect.c | 5 +++-- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/miniupnpd/natpmp.c b/miniupnpd/natpmp.c index 2bacb8f..be37e29 100644 --- a/miniupnpd/natpmp.c +++ b/miniupnpd/natpmp.c @@ -293,8 +293,9 @@ void ProcessIncomingNATPMPPacket(int s, unsigned char *msg_buff, int len, } any_eport_allowed = 1; /* at lease one eport is allowed */ #ifdef CHECK_PORTINUSE - if (port_in_use(ext_if_name, eport, proto, senderaddrstr, iport)) { - syslog(LOG_INFO, "port %hu protocol %s already in use", eport, (proto==IPPROTO_TCP)?"tcp":"udp"); + if (port_in_use(ext_if_name, eport, proto, senderaddrstr, iport) > 0) { + syslog(LOG_INFO, "port %hu protocol %s already in use", + eport, (proto==IPPROTO_TCP)?"tcp":"udp"); eport++; if(eport == 0) eport++; /* skip port zero */ continue; diff --git a/miniupnpd/portinuse.c b/miniupnpd/portinuse.c index c9b4849..278faa7 100644 --- a/miniupnpd/portinuse.c +++ b/miniupnpd/portinuse.c @@ -1,4 +1,4 @@ -/* $Id $ */ +/* $Id: $ */ /* MiniUPnP project * (c) 2007-2014 Thomas Bernard * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ @@ -83,13 +83,14 @@ port_in_use(const char *if_name, ip_addr.s_addr = 0; syslog(LOG_DEBUG, "Check protocol %s for port %d on ext_if %s %s, %08X", - (proto==IPPROTO_TCP)?"tcp":"udp", eport, if_name, ip_addr_str, (unsigned)ip_addr.s_addr); + (proto==IPPROTO_TCP)?"tcp":"udp", eport, if_name, + ip_addr_str, (unsigned)ip_addr.s_addr); #ifdef __linux__ f = fopen((proto==IPPROTO_TCP)?tcpfile:udpfile, "r"); if (!f) { syslog(LOG_ERR, "cannot open %s", (proto==IPPROTO_TCP)?tcpfile:udpfile); - return 0; + return -1; } while (fgets(line, 255, f)) { diff --git a/miniupnpd/portinuse.h b/miniupnpd/portinuse.h index fddc7c8..01842f6 100644 --- a/miniupnpd/portinuse.h +++ b/miniupnpd/portinuse.h @@ -1,4 +1,4 @@ -/* $Id $ */ +/* $Id: $ */ /* MiniUPnP project * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * (c) 2006-2014 Thomas Bernard @@ -12,7 +12,8 @@ /* portinuse() * determine wether a port is already in use * on a given interface. - * returns: 0 not in use, 1 in use */ + * returns: 0 not in use, > 0 in use + * -1 in case of error */ int port_in_use(const char *if_name, unsigned port, int proto, diff --git a/miniupnpd/upnpredirect.c b/miniupnpd/upnpredirect.c index 0139a7d..5a50c6d 100644 --- a/miniupnpd/upnpredirect.c +++ b/miniupnpd/upnpredirect.c @@ -296,8 +296,9 @@ upnp_redirect(const char * rhost, unsigned short eport, return -2; } #ifdef CHECK_PORTINUSE - } else if (port_in_use(ext_if_name, eport, proto, iaddr, iport)) { - syslog(LOG_INFO, "port %hu protocol %s already in use", eport, protocol); + } else if (port_in_use(ext_if_name, eport, proto, iaddr, iport) > 0) { + syslog(LOG_INFO, "port %hu protocol %s already in use", + eport, protocol); return -2; #endif /* CHECK_PORTINUSE */ } else {