From a4fd83e9689ca07929a8a3a6cb8b7bde2b872649 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Mon, 19 Aug 2013 18:49:09 +0200 Subject: [PATCH] minissdpd: update upnputils.c/.h --- minissdpd/upnputils.c | 82 +++++++++++++++++++++++++++++++++++++++++-- minissdpd/upnputils.h | 10 ++++-- 2 files changed, 88 insertions(+), 4 deletions(-) diff --git a/minissdpd/upnputils.c b/minissdpd/upnputils.c index ec32d29..23ad242 100644 --- a/minissdpd/upnputils.c +++ b/minissdpd/upnputils.c @@ -1,13 +1,15 @@ -/* $Id: upnputils.c,v 1.5 2012/05/24 16:51:09 nanard Exp $ */ +/* $Id: upnputils.c,v 1.7 2013/04/20 09:03:18 nanard Exp $ */ /* MiniUPnP project * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ - * (c) 2006-2012 Thomas Bernard + * (c) 2006-2013 Thomas Bernard * This software is subject to the conditions detailed * in the LICENCE file provided within the distribution */ #include "config.h" #include +#include +#include #include #include #include @@ -19,6 +21,10 @@ #endif #include "upnputils.h" +#include "upnpglobalvars.h" +#ifdef ENABLE_IPV6 +#include "getroute.h" +#endif int sockaddr_to_string(const struct sockaddr * addr, char * str, size_t size) @@ -83,3 +89,75 @@ set_non_blocking(int fd) return 1; } +struct lan_addr_s * +get_lan_for_peer(const struct sockaddr * peer) +{ + struct lan_addr_s * lan_addr = NULL; + +#ifdef ENABLE_IPV6 + if(peer->sa_family == AF_INET6) + { + struct sockaddr_in6 * peer6 = (struct sockaddr_in6 *)peer; + if(IN6_IS_ADDR_V4MAPPED(&peer6->sin6_addr)) + { + struct in_addr peer_addr; + memcpy(&peer_addr, &peer6->sin6_addr.s6_addr[12], 4); + for(lan_addr = lan_addrs.lh_first; + lan_addr != NULL; + lan_addr = lan_addr->list.le_next) + { + if( (peer_addr.s_addr & lan_addr->mask.s_addr) + == (lan_addr->addr.s_addr & lan_addr->mask.s_addr)) + break; + } + } + else + { + int index = -1; + if(peer6->sin6_scope_id > 0) + index = (int)peer6->sin6_scope_id; + else + { + if(get_src_for_route_to(peer, NULL, NULL, &index) < 0) + return NULL; + } + syslog(LOG_DEBUG, "%s looking for LAN interface index=%d", + "get_lan_for_peer()", index); + for(lan_addr = lan_addrs.lh_first; + lan_addr != NULL; + lan_addr = lan_addr->list.le_next) + { + syslog(LOG_DEBUG, + "ifname=%s index=%u str=%s addr=%08x mask=%08x", + lan_addr->ifname, lan_addr->index, + lan_addr->str, + ntohl(lan_addr->addr.s_addr), + ntohl(lan_addr->mask.s_addr)); + if(index == (int)lan_addr->index) + break; + } + } + } + else if(peer->sa_family == AF_INET) + { +#endif + for(lan_addr = lan_addrs.lh_first; + lan_addr != NULL; + lan_addr = lan_addr->list.le_next) + { + if( (((const struct sockaddr_in *)peer)->sin_addr.s_addr & lan_addr->mask.s_addr) + == (lan_addr->addr.s_addr & lan_addr->mask.s_addr)) + break; + } +#ifdef ENABLE_IPV6 + } +#endif + + if(lan_addr) + syslog(LOG_DEBUG, "%s: found in LAN %s %s", + "get_lan_for_peer()", lan_addr->ifname, lan_addr->str); + else + syslog(LOG_DEBUG, "%s: not found !", "get_lan_for_peer()"); + return lan_addr; +} + diff --git a/minissdpd/upnputils.h b/minissdpd/upnputils.h index 9652cc0..26e2c0b 100644 --- a/minissdpd/upnputils.h +++ b/minissdpd/upnputils.h @@ -1,7 +1,7 @@ -/* $Id: upnputils.h,v 1.2 2012/02/06 16:21:24 nanard Exp $ */ +/* $Id: upnputils.h,v 1.4 2013/02/06 10:50:04 nanard Exp $ */ /* MiniUPnP project * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ - * (c) 2011-2012 Thomas Bernard + * (c) 2011-2013 Thomas Bernard * This software is subject to the conditions detailed * in the LICENCE file provided within the distribution */ @@ -23,5 +23,11 @@ sockaddr_to_string(const struct sockaddr * addr, char * str, size_t size); int set_non_blocking(int fd); +/** + * get the LAN which the peer belongs to + */ +struct lan_addr_s * +get_lan_for_peer(const struct sockaddr * peer); + #endif