miniupnpd/portinuse: port_in_use() returns -1 in case of error

This commit is contained in:
Thomas Bernard 2014-03-20 13:49:10 +01:00
parent aea062a7ba
commit 6419602e14
4 changed files with 13 additions and 9 deletions

View File

@ -293,8 +293,9 @@ void ProcessIncomingNATPMPPacket(int s, unsigned char *msg_buff, int len,
} }
any_eport_allowed = 1; /* at lease one eport is allowed */ any_eport_allowed = 1; /* at lease one eport is allowed */
#ifdef CHECK_PORTINUSE #ifdef CHECK_PORTINUSE
if (port_in_use(ext_if_name, eport, proto, senderaddrstr, iport)) { 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"); syslog(LOG_INFO, "port %hu protocol %s already in use",
eport, (proto==IPPROTO_TCP)?"tcp":"udp");
eport++; eport++;
if(eport == 0) eport++; /* skip port zero */ if(eport == 0) eport++; /* skip port zero */
continue; continue;

View File

@ -1,4 +1,4 @@
/* $Id $ */ /* $Id: $ */
/* MiniUPnP project /* MiniUPnP project
* (c) 2007-2014 Thomas Bernard * (c) 2007-2014 Thomas Bernard
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * 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; ip_addr.s_addr = 0;
syslog(LOG_DEBUG, "Check protocol %s for port %d on ext_if %s %s, %08X", 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__ #ifdef __linux__
f = fopen((proto==IPPROTO_TCP)?tcpfile:udpfile, "r"); f = fopen((proto==IPPROTO_TCP)?tcpfile:udpfile, "r");
if (!f) { if (!f) {
syslog(LOG_ERR, "cannot open %s", (proto==IPPROTO_TCP)?tcpfile:udpfile); syslog(LOG_ERR, "cannot open %s", (proto==IPPROTO_TCP)?tcpfile:udpfile);
return 0; return -1;
} }
while (fgets(line, 255, f)) { while (fgets(line, 255, f)) {

View File

@ -1,4 +1,4 @@
/* $Id $ */ /* $Id: $ */
/* MiniUPnP project /* MiniUPnP project
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* (c) 2006-2014 Thomas Bernard * (c) 2006-2014 Thomas Bernard
@ -12,7 +12,8 @@
/* portinuse() /* portinuse()
* determine wether a port is already in use * determine wether a port is already in use
* on a given interface. * 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 int
port_in_use(const char *if_name, port_in_use(const char *if_name,
unsigned port, int proto, unsigned port, int proto,

View File

@ -296,8 +296,9 @@ upnp_redirect(const char * rhost, unsigned short eport,
return -2; return -2;
} }
#ifdef CHECK_PORTINUSE #ifdef CHECK_PORTINUSE
} else if (port_in_use(ext_if_name, eport, proto, iaddr, iport)) { } 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); syslog(LOG_INFO, "port %hu protocol %s already in use",
eport, protocol);
return -2; return -2;
#endif /* CHECK_PORTINUSE */ #endif /* CHECK_PORTINUSE */
} else { } else {