2015-08-06 14:07:07 +00:00
|
|
|
/* $Id: openssdpsocket.c,v 1.17 2015/08/06 14:05:37 nanard Exp $ */
|
2011-09-28 19:14:08 +00:00
|
|
|
/* MiniUPnP project
|
|
|
|
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
|
2018-02-23 12:05:04 +00:00
|
|
|
* (c) 2006-2018 Thomas Bernard
|
2011-09-28 19:14:08 +00:00
|
|
|
* This software is subject to the conditions detailed
|
|
|
|
* in the LICENCE file provided within the distribution */
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
|
|
|
|
#include "openssdpsocket.h"
|
2012-05-24 16:21:24 +00:00
|
|
|
#include "upnputils.h"
|
2014-11-28 16:38:54 +00:00
|
|
|
#include "minissdpdtypes.h"
|
|
|
|
|
|
|
|
extern struct lan_addr_list lan_addrs;
|
2011-09-28 19:14:08 +00:00
|
|
|
|
|
|
|
/* SSDP ip/port */
|
|
|
|
#define SSDP_PORT (1900)
|
|
|
|
#define SSDP_MCAST_ADDR ("239.255.255.250")
|
|
|
|
/* Link Local and Site Local SSDP IPv6 multicast addresses */
|
|
|
|
#define LL_SSDP_MCAST_ADDR ("FF02::C")
|
|
|
|
#define SL_SSDP_MCAST_ADDR ("FF05::C")
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the multicast membership for SSDP on the interface
|
|
|
|
* @param s the socket
|
|
|
|
* @param ifaddr the IPv4 address or interface name
|
|
|
|
* @param ipv6 IPv6 or IPv4
|
|
|
|
* return -1 on error, 0 on success */
|
|
|
|
int
|
2014-11-28 16:38:54 +00:00
|
|
|
AddDropMulticastMembership(int s, struct lan_addr_s * lan_addr, int ipv6, int drop)
|
2011-09-28 19:14:08 +00:00
|
|
|
{
|
|
|
|
struct ip_mreq imr; /* Ip multicast membership */
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct ipv6_mreq mr;
|
2014-11-06 10:15:43 +00:00
|
|
|
#else /* ENABLE_IPV6 */
|
|
|
|
(void)ipv6;
|
|
|
|
#endif /* ENABLE_IPV6 */
|
2011-09-28 19:14:08 +00:00
|
|
|
|
2012-05-08 21:11:48 +00:00
|
|
|
if(s <= 0)
|
|
|
|
return -1; /* nothing to do */
|
2011-09-28 19:14:08 +00:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
if(ipv6)
|
|
|
|
{
|
|
|
|
memset(&mr, 0, sizeof(mr));
|
|
|
|
inet_pton(AF_INET6, LL_SSDP_MCAST_ADDR, &mr.ipv6mr_multiaddr);
|
2014-11-28 16:38:54 +00:00
|
|
|
mr.ipv6mr_interface = lan_addr->index;
|
2011-09-28 19:14:08 +00:00
|
|
|
if(setsockopt(s, IPPROTO_IPV6, drop ? IPV6_LEAVE_GROUP : IPV6_JOIN_GROUP,
|
|
|
|
&mr, sizeof(struct ipv6_mreq)) < 0)
|
|
|
|
{
|
2012-04-09 22:28:27 +00:00
|
|
|
syslog(LOG_ERR, "setsockopt(udp, %s)(%s, %s): %m",
|
2011-09-28 19:14:08 +00:00
|
|
|
drop ? "IPV6_LEAVE_GROUP" : "IPV6_JOIN_GROUP",
|
2012-04-09 22:28:27 +00:00
|
|
|
LL_SSDP_MCAST_ADDR,
|
2014-11-28 16:38:54 +00:00
|
|
|
lan_addr->ifname);
|
2011-09-28 19:14:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
inet_pton(AF_INET6, SL_SSDP_MCAST_ADDR, &mr.ipv6mr_multiaddr);
|
|
|
|
if(setsockopt(s, IPPROTO_IPV6, drop ? IPV6_LEAVE_GROUP : IPV6_JOIN_GROUP,
|
|
|
|
&mr, sizeof(struct ipv6_mreq)) < 0)
|
|
|
|
{
|
2012-04-09 22:28:27 +00:00
|
|
|
syslog(LOG_ERR, "setsockopt(udp, %s)(%s, %s): %m",
|
2011-09-28 19:14:08 +00:00
|
|
|
drop ? "IPV6_LEAVE_GROUP" : "IPV6_JOIN_GROUP",
|
2012-04-09 22:28:27 +00:00
|
|
|
SL_SSDP_MCAST_ADDR,
|
2014-11-28 16:38:54 +00:00
|
|
|
lan_addr->ifname);
|
2011-09-28 19:14:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-28 14:49:32 +00:00
|
|
|
#endif /* ENABLE_IPV6 */
|
2014-05-02 07:51:23 +00:00
|
|
|
/* setting up imr structure */
|
|
|
|
imr.imr_multiaddr.s_addr = inet_addr(SSDP_MCAST_ADDR);
|
2014-11-28 16:38:54 +00:00
|
|
|
imr.imr_interface.s_addr = lan_addr->addr.s_addr;
|
2011-09-28 19:14:08 +00:00
|
|
|
if(imr.imr_interface.s_addr == INADDR_NONE)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "no IPv4 address for interface %s",
|
2014-11-28 16:38:54 +00:00
|
|
|
lan_addr->ifname);
|
2011-09-28 19:14:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-03-01 01:52:48 +00:00
|
|
|
|
2011-09-28 19:14:08 +00:00
|
|
|
if (setsockopt(s, IPPROTO_IP, drop ? IP_DROP_MEMBERSHIP : IP_ADD_MEMBERSHIP,
|
|
|
|
(void *)&imr, sizeof(struct ip_mreq)) < 0)
|
|
|
|
{
|
2014-05-02 07:51:23 +00:00
|
|
|
syslog(LOG_ERR, "setsockopt(udp, %s)(%s): %m",
|
2011-09-28 19:14:08 +00:00
|
|
|
drop ? "IP_DROP_MEMBERSHIP" : "IP_ADD_MEMBERSHIP",
|
2014-11-28 16:38:54 +00:00
|
|
|
lan_addr->ifname);
|
2011-09-28 19:14:08 +00:00
|
|
|
return -1;
|
2014-05-02 07:51:23 +00:00
|
|
|
}
|
2011-09-28 19:14:08 +00:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
}
|
2014-11-28 14:49:32 +00:00
|
|
|
#endif /* ENABLE_IPV6 */
|
2011-09-28 19:14:08 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-07-21 15:40:51 +00:00
|
|
|
OpenAndConfSSDPReceiveSocket(int ipv6, unsigned char ttl)
|
2011-09-28 19:14:08 +00:00
|
|
|
{
|
|
|
|
int s;
|
2011-10-07 09:30:59 +00:00
|
|
|
int opt = 1;
|
2015-08-06 14:07:07 +00:00
|
|
|
unsigned char loopchar = 0;
|
2011-09-28 19:14:08 +00:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct sockaddr_storage sockname;
|
2014-11-28 14:49:32 +00:00
|
|
|
#else /* ENABLE_IPV6 */
|
2011-09-28 19:14:08 +00:00
|
|
|
struct sockaddr_in sockname;
|
2014-11-28 14:49:32 +00:00
|
|
|
#endif /* ENABLE_IPV6 */
|
2011-09-28 19:14:08 +00:00
|
|
|
socklen_t sockname_len;
|
2014-11-28 16:38:54 +00:00
|
|
|
struct lan_addr_s * lan_addr;
|
2012-03-01 01:52:48 +00:00
|
|
|
|
2014-11-28 14:49:32 +00:00
|
|
|
#ifndef ENABLE_IPV6
|
|
|
|
if(ipv6) {
|
|
|
|
syslog(LOG_ERR, "%s: please compile with ENABLE_IPV6 to allow ipv6=1", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* ENABLE_IPV6 */
|
|
|
|
|
2011-09-28 19:14:08 +00:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
if( (s = socket(ipv6 ? PF_INET6 : PF_INET, SOCK_DGRAM, 0)) < 0)
|
2014-11-28 14:49:32 +00:00
|
|
|
#else /* ENABLE_IPV6 */
|
2011-09-28 19:14:08 +00:00
|
|
|
if( (s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
|
2014-11-28 14:49:32 +00:00
|
|
|
#endif /* ENABLE_IPV6 */
|
2011-09-28 19:14:08 +00:00
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "socket(udp): %m");
|
|
|
|
return -1;
|
2012-03-01 01:52:48 +00:00
|
|
|
}
|
|
|
|
|
2012-05-24 16:21:24 +00:00
|
|
|
if(!set_non_blocking(s)) {
|
|
|
|
syslog(LOG_WARNING, "Failed to set SSDP socket non blocking : %m");
|
|
|
|
}
|
|
|
|
|
2011-09-28 19:14:08 +00:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
memset(&sockname, 0, sizeof(struct sockaddr_storage));
|
|
|
|
if(ipv6)
|
|
|
|
{
|
|
|
|
#ifdef IPV6_V6ONLY
|
|
|
|
if(setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
|
2011-10-07 09:30:59 +00:00
|
|
|
(char *)&opt, sizeof(opt)) < 0)
|
2011-09-28 19:14:08 +00:00
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, "setsockopt(IPV6_V6ONLY): %m");
|
|
|
|
}
|
2014-11-28 14:49:32 +00:00
|
|
|
#endif /* IPV6_V6ONLY */
|
2011-09-28 19:14:08 +00:00
|
|
|
struct sockaddr_in6 * sa = (struct sockaddr_in6 *)&sockname;
|
2014-05-02 07:51:23 +00:00
|
|
|
sa->sin6_family = AF_INET6;
|
|
|
|
sa->sin6_port = htons(SSDP_PORT);
|
2011-09-28 19:14:08 +00:00
|
|
|
sa->sin6_addr = in6addr_any;
|
|
|
|
sockname_len = sizeof(struct sockaddr_in6);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct sockaddr_in * sa = (struct sockaddr_in *)&sockname;
|
2014-05-02 07:51:23 +00:00
|
|
|
sa->sin_family = AF_INET;
|
|
|
|
sa->sin_port = htons(SSDP_PORT);
|
2014-11-28 14:50:06 +00:00
|
|
|
#ifdef SSDP_LISTEN_ON_SPECIFIC_ADDR
|
2018-02-23 12:05:04 +00:00
|
|
|
if(lan_addrs.lh_first != NULL && lan_addrs.lh_first->list.le_next == NULL)
|
2014-05-02 07:52:43 +00:00
|
|
|
{
|
2018-02-23 12:05:04 +00:00
|
|
|
sa->sin_addr.s_addr = lan_addrs.lh_first->addr.s_addr;
|
2014-05-02 07:52:43 +00:00
|
|
|
if(sa->sin_addr.s_addr == INADDR_NONE)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "no IPv4 address for interface %s",
|
2018-02-23 12:05:04 +00:00
|
|
|
lan_addrs.lh_first->ifname);
|
2014-05-02 07:52:43 +00:00
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2014-11-28 14:50:06 +00:00
|
|
|
#endif /* SSDP_LISTEN_ON_SPECIFIC_ADDR */
|
2014-05-02 07:52:43 +00:00
|
|
|
sa->sin_addr.s_addr = htonl(INADDR_ANY);
|
2011-09-28 19:14:08 +00:00
|
|
|
sockname_len = sizeof(struct sockaddr_in);
|
|
|
|
}
|
2014-11-28 14:49:32 +00:00
|
|
|
#else /* ENABLE_IPV6 */
|
2011-09-28 19:14:08 +00:00
|
|
|
memset(&sockname, 0, sizeof(struct sockaddr_in));
|
|
|
|
sockname.sin_family = AF_INET;
|
|
|
|
sockname.sin_port = htons(SSDP_PORT);
|
2014-11-28 14:50:06 +00:00
|
|
|
#ifdef SSDP_LISTEN_ON_SPECIFIC_ADDR
|
2014-11-06 10:15:43 +00:00
|
|
|
if(n_listen_addr == 1)
|
|
|
|
{
|
|
|
|
sockname.sin_addr.s_addr = GetIfAddrIPv4(listen_addr[0]);
|
|
|
|
if(sockname.sin_addr.s_addr == INADDR_NONE)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "no IPv4 address for interface %s",
|
|
|
|
listen_addr[0]);
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2014-11-28 14:50:06 +00:00
|
|
|
#endif /* SSDP_LISTEN_ON_SPECIFIC_ADDR */
|
2015-07-21 15:40:51 +00:00
|
|
|
sockname.sin_addr.s_addr = htonl(INADDR_ANY);
|
2011-09-28 19:14:08 +00:00
|
|
|
sockname_len = sizeof(struct sockaddr_in);
|
2014-11-28 14:49:32 +00:00
|
|
|
#endif /* ENABLE_IPV6 */
|
2011-09-28 19:14:08 +00:00
|
|
|
|
2015-08-06 14:07:07 +00:00
|
|
|
if(setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&loopchar, sizeof(loopchar)) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, "setsockopt(IP_MULTICAST_LOOP): %m");
|
|
|
|
}
|
|
|
|
|
2015-07-21 15:40:51 +00:00
|
|
|
if(setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, "setsockopt(IP_MULTICAST_TTL): %m");
|
|
|
|
}
|
|
|
|
|
2011-10-07 09:30:59 +00:00
|
|
|
if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, "setsockopt(SO_REUSEADDR): %m");
|
|
|
|
}
|
|
|
|
|
2011-09-28 19:14:08 +00:00
|
|
|
if(bind(s, (struct sockaddr *)&sockname, sockname_len) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "bind(udp%s): %m", ipv6 ? "6" : "");
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-28 16:38:54 +00:00
|
|
|
for(lan_addr = lan_addrs.lh_first; lan_addr != NULL; lan_addr = lan_addr->list.le_next)
|
2011-09-28 19:14:08 +00:00
|
|
|
{
|
2014-11-28 16:38:54 +00:00
|
|
|
if(AddDropMulticastMembership(s, lan_addr, ipv6, 0) < 0)
|
2011-09-28 19:14:08 +00:00
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, "Failed to add IPv%d multicast membership for interface %s.",
|
|
|
|
ipv6 ? 6 : 4,
|
2014-11-28 16:38:54 +00:00
|
|
|
lan_addr->ifname);
|
2011-09-28 19:14:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|