miniupnpd: bind to device using SO_BINDTODEVICE

This commit is contained in:
Thomas Bernard 2015-08-26 09:46:05 +02:00
parent 0cf182e51e
commit f8f5f2eb97
3 changed files with 25 additions and 15 deletions

View File

@ -1,4 +1,8 @@
$Id: Changelog.txt,v 1.398 2015/04/26 14:43:27 nanard Exp $ $Id: Changelog.txt,v 1.400 2015/08/26 07:32:31 nanard Exp $
2015/08/25:
better bind socket to right interface(s),
using struct ip_mreqn, SO_BINDTODEVICE
2015/04/30: 2015/04/30:
Adding linux/nftables support Adding linux/nftables support

View File

@ -1,4 +1,4 @@
/* $Id: minissdp.c,v 1.73 2015/01/17 11:26:05 nanard Exp $ */ /* $Id: minissdp.c,v 1.77 2015/08/26 07:36:52 nanard Exp $ */
/* MiniUPnP project /* MiniUPnP project
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* (c) 2006-2015 Thomas Bernard * (c) 2006-2015 Thomas Bernard
@ -177,14 +177,17 @@ OpenAndConfSSDPReceiveSocket(int ipv6)
"OpenAndConfSSDPReceiveSocket"); "OpenAndConfSSDPReceiveSocket");
} }
/* TODO: Bind to device only if one LAN interface #if defined(SO_BINDTODEVICE) && !defined(MULTIPLE_EXTERNAL_IP)
#ifndef MULTIPLE_EXTERNAL_IP /* One and only one LAN interface */
if(setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, int_if_name, strlen(int_if_name)) < 0) if(lan_addrs.lh_first != NULL && lan_addrs.lh_first->list.le_next == NULL
&& strlen(lan_addrs.lh_first->ifname) > 0)
{ {
if(setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE,
lan_addrs.lh_first->ifname,
strlen(lan_addrs.lh_first->ifname)) < 0)
syslog(LOG_WARNING, "setsockopt(udp, SO_BINDTODEVICE): %m"); syslog(LOG_WARNING, "setsockopt(udp, SO_BINDTODEVICE): %m");
} }
#endif #endif /* defined(SO_BINDTODEVICE) && !defined(MULTIPLE_EXTERNAL_IP) */
*/
if(bind(s, (struct sockaddr *)&sockname, sockname_len) < 0) if(bind(s, (struct sockaddr *)&sockname, sockname_len) < 0)
{ {

View File

@ -1,4 +1,4 @@
/* $Id: miniupnpd.c,v 1.206 2015/01/17 11:26:04 nanard Exp $ */ /* $Id: miniupnpd.c,v 1.210 2015/08/26 07:32:32 nanard Exp $ */
/* MiniUPnP project /* MiniUPnP project
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* (c) 2006-2015 Thomas Bernard * (c) 2006-2015 Thomas Bernard
@ -370,14 +370,17 @@ OpenAndConfHTTPSocket(unsigned short * port)
listenname_len = sizeof(struct sockaddr_in); listenname_len = sizeof(struct sockaddr_in);
#endif #endif
/* TODO: Bind to device only if one LAN interface #if defined(SO_BINDTODEVICE) && !defined(MULTIPLE_EXTERNAL_IP)
#ifndef MULTIPLE_EXTERNAL_IP /* One and only one LAN interface */
if(setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, int_if_name, strlen(int_if_name)) < 0) if(lan_addrs.lh_first != NULL && lan_addrs.lh_first->list.le_next == NULL
&& strlen(lan_addrs.lh_first->ifname) > 0)
{ {
if(setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE,
lan_addrs.lh_first->ifname,
strlen(lan_addrs.lh_first->ifname)) < 0)
syslog(LOG_WARNING, "setsockopt(udp, SO_BINDTODEVICE): %m"); syslog(LOG_WARNING, "setsockopt(udp, SO_BINDTODEVICE): %m");
} }
#endif #endif /* defined(SO_BINDTODEVICE) && !defined(MULTIPLE_EXTERNAL_IP) */
*/
#ifdef ENABLE_IPV6 #ifdef ENABLE_IPV6
if(bind(s, if(bind(s,