2014-02-06 09:55:28 +00:00
|
|
|
/* $Id: minissdp.c,v 1.57 2014/02/06 09:52:03 nanard Exp $ */
|
2011-09-28 19:13:20 +00:00
|
|
|
/* MiniUPnP project
|
|
|
|
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
|
2014-02-06 09:55:28 +00:00
|
|
|
* (c) 2006-2014 Thomas Bernard
|
2011-09-28 19:13:20 +00:00
|
|
|
* This software is subject to the conditions detailed
|
|
|
|
* in the LICENCE file provided within the distribution */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2012-09-28 09:13:48 +00:00
|
|
|
#include <stdlib.h>
|
2011-09-28 19:13:20 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2012-02-02 17:45:43 +00:00
|
|
|
#include <errno.h>
|
2011-09-28 19:13:20 +00:00
|
|
|
#include <syslog.h>
|
2012-06-23 23:41:47 +00:00
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "upnpdescstrings.h"
|
|
|
|
#include "miniupnpdpath.h"
|
|
|
|
#include "upnphttp.h"
|
|
|
|
#include "upnpglobalvars.h"
|
|
|
|
#include "minissdp.h"
|
|
|
|
#include "upnputils.h"
|
2012-06-23 23:41:47 +00:00
|
|
|
#include "getroute.h"
|
2011-09-28 19:13:20 +00:00
|
|
|
#include "codelength.h"
|
|
|
|
|
|
|
|
/* SSDP ip/port */
|
|
|
|
#define SSDP_PORT (1900)
|
|
|
|
#define SSDP_MCAST_ADDR ("239.255.255.250")
|
2012-04-06 17:30:19 +00:00
|
|
|
#define LL_SSDP_MCAST_ADDR "FF02::C"
|
|
|
|
#define SL_SSDP_MCAST_ADDR "FF05::C"
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
/* AddMulticastMembership()
|
|
|
|
* param s socket
|
|
|
|
* param ifaddr ip v4 address
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
AddMulticastMembership(int s, in_addr_t ifaddr)
|
|
|
|
{
|
|
|
|
struct ip_mreq imr; /* Ip multicast membership */
|
|
|
|
|
|
|
|
/* setting up imr structure */
|
|
|
|
imr.imr_multiaddr.s_addr = inet_addr(SSDP_MCAST_ADDR);
|
|
|
|
/*imr.imr_interface.s_addr = htonl(INADDR_ANY);*/
|
|
|
|
imr.imr_interface.s_addr = ifaddr; /*inet_addr(ifaddr);*/
|
2012-03-01 01:44:38 +00:00
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)&imr, sizeof(struct ip_mreq)) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "setsockopt(udp, IP_ADD_MEMBERSHIP): %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* AddMulticastMembershipIPv6()
|
|
|
|
* param s socket (IPv6)
|
|
|
|
* To be improved to target specific network interfaces */
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
static int
|
|
|
|
AddMulticastMembershipIPv6(int s)
|
|
|
|
{
|
|
|
|
struct ipv6_mreq mr;
|
|
|
|
/*unsigned int ifindex;*/
|
|
|
|
|
|
|
|
memset(&mr, 0, sizeof(mr));
|
|
|
|
inet_pton(AF_INET6, LL_SSDP_MCAST_ADDR, &mr.ipv6mr_multiaddr);
|
|
|
|
/*mr.ipv6mr_interface = ifindex;*/
|
|
|
|
mr.ipv6mr_interface = 0; /* 0 : all interfaces */
|
|
|
|
#ifndef IPV6_ADD_MEMBERSHIP
|
|
|
|
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
|
|
|
|
#endif
|
|
|
|
if(setsockopt(s, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mr, sizeof(struct ipv6_mreq)) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "setsockopt(udp, IPV6_ADD_MEMBERSHIP): %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
inet_pton(AF_INET6, SL_SSDP_MCAST_ADDR, &mr.ipv6mr_multiaddr);
|
|
|
|
if(setsockopt(s, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mr, sizeof(struct ipv6_mreq)) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "setsockopt(udp, IPV6_ADD_MEMBERSHIP): %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-01 01:44:38 +00:00
|
|
|
/* Open and configure the socket listening for
|
2011-09-28 19:13:20 +00:00
|
|
|
* SSDP udp packets sent on 239.255.255.250 port 1900
|
|
|
|
* SSDP v6 udp packets sent on FF02::C, or FF05::C, port 1900 */
|
|
|
|
int
|
|
|
|
OpenAndConfSSDPReceiveSocket(int ipv6)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
struct sockaddr_storage sockname;
|
|
|
|
socklen_t sockname_len;
|
|
|
|
struct lan_addr_s * lan_addr;
|
|
|
|
int j = 1;
|
|
|
|
|
|
|
|
if( (s = socket(ipv6 ? PF_INET6 : PF_INET, SOCK_DGRAM, 0)) < 0)
|
|
|
|
{
|
2012-02-09 20:33:24 +00:00
|
|
|
syslog(LOG_ERR, "%s: socket(udp): %m",
|
|
|
|
"OpenAndConfSSDPReceiveSocket");
|
2011-09-28 19:13:20 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&sockname, 0, sizeof(struct sockaddr_storage));
|
|
|
|
if(ipv6) {
|
|
|
|
struct sockaddr_in6 * saddr = (struct sockaddr_in6 *)&sockname;
|
|
|
|
saddr->sin6_family = AF_INET6;
|
|
|
|
saddr->sin6_port = htons(SSDP_PORT);
|
|
|
|
saddr->sin6_addr = in6addr_any;
|
|
|
|
sockname_len = sizeof(struct sockaddr_in6);
|
|
|
|
} else {
|
|
|
|
struct sockaddr_in * saddr = (struct sockaddr_in *)&sockname;
|
|
|
|
saddr->sin_family = AF_INET;
|
|
|
|
saddr->sin_port = htons(SSDP_PORT);
|
|
|
|
/* NOTE : it seems it doesnt work when binding on the specific address */
|
|
|
|
/*saddr->sin_addr.s_addr = inet_addr(UPNP_MCAST_ADDR);*/
|
|
|
|
saddr->sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
/*saddr->sin_addr.s_addr = inet_addr(ifaddr);*/
|
|
|
|
sockname_len = sizeof(struct sockaddr_in);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &j, sizeof(j)) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, "setsockopt(udp, SO_REUSEADDR): %m");
|
|
|
|
}
|
|
|
|
|
2012-02-09 20:33:24 +00:00
|
|
|
if(!set_non_blocking(s))
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, "%s: set_non_blocking(): %m",
|
|
|
|
"OpenAndConfSSDPReceiveSocket");
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
if(bind(s, (struct sockaddr *)&sockname, sockname_len) < 0)
|
|
|
|
{
|
2012-02-09 20:33:24 +00:00
|
|
|
syslog(LOG_ERR, "%s: bind(udp%s): %m",
|
|
|
|
"OpenAndConfSSDPReceiveSocket", ipv6 ? "6" : "");
|
2011-09-28 19:13:20 +00:00
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
if(ipv6)
|
|
|
|
{
|
2014-02-03 09:42:38 +00:00
|
|
|
if(AddMulticastMembershipIPv6(s) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING,
|
|
|
|
"Failed to add IPv6 multicast membership");
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
for(lan_addr = lan_addrs.lh_first; lan_addr != NULL; lan_addr = lan_addr->list.le_next)
|
|
|
|
{
|
|
|
|
if(AddMulticastMembership(s, lan_addr->addr.s_addr) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING,
|
2012-03-01 01:44:38 +00:00
|
|
|
"Failed to add multicast membership for interface %s",
|
2012-05-24 16:52:27 +00:00
|
|
|
lan_addr->str ? lan_addr->str : "NULL");
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open the UDP socket used to send SSDP notifications to
|
|
|
|
* the multicast group reserved for them */
|
|
|
|
static int
|
|
|
|
OpenAndConfSSDPNotifySocket(in_addr_t addr)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
unsigned char loopchar = 0;
|
|
|
|
int bcast = 1;
|
2012-04-12 21:37:59 +00:00
|
|
|
unsigned char ttl = 2; /* UDA v1.1 says :
|
|
|
|
The TTL for the IP packet SHOULD default to 2 and
|
|
|
|
SHOULD be configurable. */
|
|
|
|
/* TODO: Make TTL be configurable */
|
2011-09-28 19:13:20 +00:00
|
|
|
struct in_addr mc_if;
|
|
|
|
struct sockaddr_in sockname;
|
2012-03-01 01:44:38 +00:00
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
if( (s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "socket(udp_notify): %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mc_if.s_addr = addr; /*inet_addr(addr);*/
|
|
|
|
|
|
|
|
if(setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&loopchar, sizeof(loopchar)) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "setsockopt(udp_notify, IP_MULTICAST_LOOP): %m");
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, (char *)&mc_if, sizeof(mc_if)) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "setsockopt(udp_notify, IP_MULTICAST_IF): %m");
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-03-01 01:44:38 +00:00
|
|
|
|
2012-04-12 21:37:59 +00:00
|
|
|
if(setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, "setsockopt(udp_notify, IP_MULTICAST_TTL,): %m");
|
|
|
|
}
|
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
if(setsockopt(s, SOL_SOCKET, SO_BROADCAST, &bcast, sizeof(bcast)) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "setsockopt(udp_notify, SO_BROADCAST): %m");
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&sockname, 0, sizeof(struct sockaddr_in));
|
|
|
|
sockname.sin_family = AF_INET;
|
|
|
|
sockname.sin_addr.s_addr = addr; /*inet_addr(addr);*/
|
|
|
|
|
|
|
|
if (bind(s, (struct sockaddr *)&sockname, sizeof(struct sockaddr_in)) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "bind(udp_notify): %m");
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2012-04-06 17:30:19 +00:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
/* open the UDP socket used to send SSDP notifications to
|
|
|
|
* the multicast group reserved for them. IPv6 */
|
|
|
|
static int
|
|
|
|
OpenAndConfSSDPNotifySocketIPv6(unsigned int if_index)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
unsigned int loop = 0;
|
|
|
|
|
|
|
|
s = socket(PF_INET6, SOCK_DGRAM, 0);
|
|
|
|
if(s < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "socket(udp_notify IPv6): %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, &if_index, sizeof(if_index)) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "setsockopt(udp_notify IPv6, IPV6_MULTICAST_IF, %u): %m", if_index);
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &loop, sizeof(loop)) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "setsockopt(udp_notify, IPV6_MULTICAST_LOOP): %m");
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
int
|
|
|
|
OpenAndConfSSDPNotifySockets(int * sockets)
|
|
|
|
/*OpenAndConfSSDPNotifySockets(int * sockets,
|
|
|
|
struct lan_addr_s * lan_addr, int n_lan_addr)*/
|
|
|
|
{
|
2012-04-06 17:30:19 +00:00
|
|
|
int i;
|
2011-09-28 19:13:20 +00:00
|
|
|
struct lan_addr_s * lan_addr;
|
|
|
|
|
2012-04-06 17:30:19 +00:00
|
|
|
for(i=0, lan_addr = lan_addrs.lh_first;
|
|
|
|
lan_addr != NULL;
|
|
|
|
lan_addr = lan_addr->list.le_next)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
sockets[i] = OpenAndConfSSDPNotifySocket(lan_addr->addr.s_addr);
|
|
|
|
if(sockets[i] < 0)
|
2012-04-06 17:30:19 +00:00
|
|
|
goto error;
|
|
|
|
i++;
|
|
|
|
#ifdef ENABLE_IPV6
|
2014-02-06 09:55:28 +00:00
|
|
|
if(ipv6_enabled)
|
|
|
|
{
|
|
|
|
sockets[i] = OpenAndConfSSDPNotifySocketIPv6(lan_addr->index);
|
|
|
|
if(sockets[i] < 0)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sockets[i] = -1;
|
|
|
|
}
|
2012-04-06 17:30:19 +00:00
|
|
|
i++;
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2012-04-06 17:30:19 +00:00
|
|
|
error:
|
|
|
|
while(--i >= 0)
|
|
|
|
{
|
|
|
|
close(sockets[i]);
|
|
|
|
sockets[i] = -1;
|
|
|
|
}
|
|
|
|
return -1;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* response from a LiveBox (Wanadoo)
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
CACHE-CONTROL: max-age=1800
|
|
|
|
DATE: Thu, 01 Jan 1970 04:03:23 GMT
|
|
|
|
EXT:
|
|
|
|
LOCATION: http://192.168.0.1:49152/gatedesc.xml
|
|
|
|
SERVER: Linux/2.4.17, UPnP/1.0, Intel SDK for UPnP devices /1.2
|
|
|
|
ST: upnp:rootdevice
|
|
|
|
USN: uuid:75802409-bccb-40e7-8e6c-fa095ecce13e::upnp:rootdevice
|
|
|
|
|
|
|
|
* response from a Linksys 802.11b :
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Cache-Control:max-age=120
|
|
|
|
Location:http://192.168.5.1:5678/rootDesc.xml
|
|
|
|
Server:NT/5.0 UPnP/1.0
|
|
|
|
ST:upnp:rootdevice
|
|
|
|
USN:uuid:upnp-InternetGatewayDevice-1_0-0090a2777777::upnp:rootdevice
|
|
|
|
EXT:
|
|
|
|
*/
|
|
|
|
|
2013-06-11 18:03:15 +00:00
|
|
|
/* Responds to a SSDP "M-SEARCH"
|
|
|
|
* s : socket to use
|
|
|
|
* addr : peer
|
|
|
|
* st, st_len : ST: header
|
|
|
|
* suffix : suffix for USN: header
|
|
|
|
* host, port : our HTTP host, port
|
|
|
|
*/
|
2011-09-28 19:13:20 +00:00
|
|
|
static void
|
2013-06-11 18:03:15 +00:00
|
|
|
SendSSDPResponse(int s, const struct sockaddr * addr,
|
|
|
|
const char * st, int st_len, const char * suffix,
|
2013-06-13 14:03:36 +00:00
|
|
|
const char * host, unsigned short port, const char * uuidvalue)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
int l, n;
|
|
|
|
char buf[512];
|
2012-02-09 20:33:24 +00:00
|
|
|
char addr_str[64];
|
2011-09-28 19:13:20 +00:00
|
|
|
socklen_t addrlen;
|
2013-02-07 09:59:31 +00:00
|
|
|
int st_is_uuid;
|
2013-02-07 09:56:38 +00:00
|
|
|
#ifdef ENABLE_HTTP_DATE
|
|
|
|
char http_date[64];
|
|
|
|
time_t t;
|
|
|
|
struct tm tm;
|
|
|
|
|
|
|
|
time(&t);
|
|
|
|
gmtime_r(&t, &tm);
|
|
|
|
strftime(http_date, sizeof(http_date),
|
|
|
|
"%a, %d %b %Y %H:%M:%S GMT", &tm);
|
|
|
|
#endif
|
|
|
|
|
2013-02-07 09:59:31 +00:00
|
|
|
st_is_uuid = (st_len == (int)strlen(uuidvalue)) &&
|
|
|
|
(memcmp(uuidvalue, st, st_len) == 0);
|
2012-03-01 01:44:38 +00:00
|
|
|
/*
|
2011-09-28 19:13:20 +00:00
|
|
|
* follow guideline from document "UPnP Device Architecture 1.0"
|
|
|
|
* uppercase is recommended.
|
|
|
|
* DATE: is recommended
|
|
|
|
* SERVER: OS/ver UPnP/1.0 miniupnpd/1.0
|
2012-03-01 01:44:38 +00:00
|
|
|
* - check what to put in the 'Cache-Control' header
|
2011-09-28 19:13:20 +00:00
|
|
|
*
|
|
|
|
* have a look at the document "UPnP Device Architecture v1.1 */
|
|
|
|
l = snprintf(buf, sizeof(buf), "HTTP/1.1 200 OK\r\n"
|
|
|
|
"CACHE-CONTROL: max-age=120\r\n"
|
2013-02-07 09:56:38 +00:00
|
|
|
#ifdef ENABLE_HTTP_DATE
|
|
|
|
"DATE: %s\r\n"
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
"ST: %.*s%s\r\n"
|
2013-02-07 09:59:31 +00:00
|
|
|
"USN: %s%s%.*s%s\r\n"
|
2011-09-28 19:13:20 +00:00
|
|
|
"EXT:\r\n"
|
|
|
|
"SERVER: " MINIUPNPD_SERVER_STRING "\r\n"
|
|
|
|
"LOCATION: http://%s:%u" ROOTDESC_PATH "\r\n"
|
2012-04-06 17:30:19 +00:00
|
|
|
"OPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n" /* UDA v1.1 */
|
2011-09-28 19:13:20 +00:00
|
|
|
"01-NLS: %u\r\n" /* same as BOOTID. UDA v1.1 */
|
|
|
|
"BOOTID.UPNP.ORG: %u\r\n" /* UDA v1.1 */
|
|
|
|
"CONFIGID.UPNP.ORG: %u\r\n" /* UDA v1.1 */
|
|
|
|
"\r\n",
|
2013-02-07 09:56:38 +00:00
|
|
|
#ifdef ENABLE_HTTP_DATE
|
|
|
|
http_date,
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
st_len, st, suffix,
|
2013-02-07 09:59:31 +00:00
|
|
|
uuidvalue, st_is_uuid ? "" : "::",
|
|
|
|
st_is_uuid ? 0 : st_len, st, suffix,
|
2011-09-28 19:13:20 +00:00
|
|
|
host, (unsigned int)port,
|
|
|
|
upnp_bootid, upnp_bootid, upnp_configid);
|
2013-04-26 15:18:28 +00:00
|
|
|
if(l<0)
|
|
|
|
{
|
2013-06-11 18:03:15 +00:00
|
|
|
syslog(LOG_ERR, "%s: snprintf failed %m",
|
|
|
|
"SendSSDPResponse()");
|
2013-04-26 15:18:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if((unsigned)l>=sizeof(buf))
|
|
|
|
{
|
2013-06-11 18:03:15 +00:00
|
|
|
syslog(LOG_WARNING, "%s: truncated output",
|
|
|
|
"SendSSDPResponse()");
|
2013-04-26 15:18:28 +00:00
|
|
|
l = sizeof(buf) - 1;
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
addrlen = (addr->sa_family == AF_INET6)
|
|
|
|
? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
|
|
|
|
n = sendto(s, buf, l, 0,
|
|
|
|
addr, addrlen);
|
2012-02-09 20:33:24 +00:00
|
|
|
sockaddr_to_string(addr, addr_str, sizeof(addr_str));
|
|
|
|
syslog(LOG_INFO, "SSDP Announce %d bytes to %s ST: %.*s",n,
|
|
|
|
addr_str,
|
2011-09-28 19:13:20 +00:00
|
|
|
l, buf);
|
|
|
|
if(n < 0)
|
|
|
|
{
|
2012-02-09 20:33:24 +00:00
|
|
|
/* XXX handle EINTR, EAGAIN, EWOULDBLOCK */
|
2011-09-28 19:13:20 +00:00
|
|
|
syslog(LOG_ERR, "sendto(udp): %m");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-28 09:13:48 +00:00
|
|
|
#ifndef IGD_V2
|
|
|
|
#define IGD_VER 1
|
|
|
|
#define WANIPC_VER 1
|
|
|
|
#else
|
|
|
|
#define IGD_VER 2
|
|
|
|
#define WANIPC_VER 2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
const char * s;
|
|
|
|
const int version;
|
2013-06-13 14:03:36 +00:00
|
|
|
const char * uuid;
|
2012-09-28 09:13:48 +00:00
|
|
|
} const known_service_types[] =
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2013-06-13 14:03:36 +00:00
|
|
|
{"upnp:rootdevice", 0, uuidvalue_igd},
|
|
|
|
{"urn:schemas-upnp-org:device:InternetGatewayDevice:", IGD_VER, uuidvalue_igd},
|
|
|
|
{"urn:schemas-upnp-org:device:WANConnectionDevice:", 1, uuidvalue_wcd},
|
|
|
|
{"urn:schemas-upnp-org:device:WANDevice:", 1, uuidvalue_wan},
|
|
|
|
{"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:", 1, uuidvalue_wan},
|
|
|
|
{"urn:schemas-upnp-org:service:WANIPConnection:", WANIPC_VER, uuidvalue_wcd},
|
2013-06-05 09:19:23 +00:00
|
|
|
#ifndef UPNP_STRICT
|
|
|
|
/* We use WAN IP Connection, not PPP connection,
|
|
|
|
* but buggy control points may try to use WanPPPConnection
|
|
|
|
* anyway */
|
2013-06-13 14:03:36 +00:00
|
|
|
{"urn:schemas-upnp-org:service:WANPPPConnection:", 1, uuidvalue_wcd},
|
2013-06-05 09:19:23 +00:00
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
#ifdef ENABLE_L3F_SERVICE
|
2013-06-13 14:03:36 +00:00
|
|
|
{"urn:schemas-upnp-org:service:Layer3Forwarding:", 1, uuidvalue_igd},
|
2011-09-28 19:13:20 +00:00
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_6FC_SERVICE
|
2013-06-13 14:03:36 +00:00
|
|
|
{"url:schemas-upnp-org:service:WANIPv6FirewallControl:", 1, uuidvalue_wcd},
|
2011-09-28 19:13:20 +00:00
|
|
|
#endif
|
2013-06-13 14:03:36 +00:00
|
|
|
{0, 0, 0}
|
2011-09-28 19:13:20 +00:00
|
|
|
};
|
|
|
|
|
2013-02-07 12:27:09 +00:00
|
|
|
static void
|
|
|
|
SendSSDPNotify(int s, const struct sockaddr * dest,
|
|
|
|
const char * host, unsigned short port,
|
|
|
|
const char * nt, const char * suffix,
|
|
|
|
const char * usn1, const char * usn2, const char * usn3,
|
|
|
|
unsigned int lifetime, int ipv6)
|
|
|
|
{
|
|
|
|
char bufr[512];
|
|
|
|
int n, l;
|
|
|
|
|
|
|
|
l = snprintf(bufr, sizeof(bufr),
|
|
|
|
"NOTIFY * HTTP/1.1\r\n"
|
|
|
|
"HOST: %s:%d\r\n"
|
|
|
|
"CACHE-CONTROL: max-age=%u\r\n"
|
|
|
|
"LOCATION: http://%s:%d" ROOTDESC_PATH"\r\n"
|
|
|
|
"SERVER: " MINIUPNPD_SERVER_STRING "\r\n"
|
|
|
|
"NT: %s%s\r\n"
|
|
|
|
"USN: %s%s%s%s\r\n"
|
|
|
|
"NTS: ssdp:alive\r\n"
|
|
|
|
"OPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n" /* UDA v1.1 */
|
|
|
|
"01-NLS: %u\r\n" /* same as BOOTID field. UDA v1.1 */
|
|
|
|
"BOOTID.UPNP.ORG: %u\r\n" /* UDA v1.1 */
|
|
|
|
"CONFIGID.UPNP.ORG: %u\r\n" /* UDA v1.1 */
|
|
|
|
"\r\n",
|
|
|
|
ipv6 ? "[" LL_SSDP_MCAST_ADDR "]" : SSDP_MCAST_ADDR,
|
|
|
|
SSDP_PORT,
|
|
|
|
lifetime,
|
|
|
|
host, port,
|
|
|
|
nt, suffix, /* NT: */
|
|
|
|
usn1, usn2, usn3, suffix, /* USN: */
|
|
|
|
upnp_bootid, upnp_bootid, upnp_configid );
|
|
|
|
if(l<0)
|
|
|
|
{
|
2013-04-26 15:18:28 +00:00
|
|
|
syslog(LOG_ERR, "SendSSDPNotify() snprintf error");
|
2013-02-07 12:27:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if((unsigned int)l >= sizeof(bufr))
|
|
|
|
{
|
2013-04-26 15:18:28 +00:00
|
|
|
syslog(LOG_WARNING, "SendSSDPNotify(): truncated output");
|
|
|
|
l = sizeof(bufr) - 1;
|
2013-02-07 12:27:09 +00:00
|
|
|
}
|
|
|
|
n = sendto(s, bufr, l, 0, dest,
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)
|
|
|
|
#else
|
|
|
|
sizeof(struct sockaddr_in)
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
if(n < 0)
|
|
|
|
{
|
|
|
|
/* XXX handle EINTR, EAGAIN, EWOULDBLOCK */
|
|
|
|
syslog(LOG_ERR, "sendto(udp_notify=%d, %s): %m", s,
|
|
|
|
host ? host : "NULL");
|
|
|
|
}
|
|
|
|
else if(n != l)
|
|
|
|
{
|
|
|
|
syslog(LOG_NOTICE, "sendto() sent %d out of %d bytes", n, l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
static void
|
|
|
|
SendSSDPNotifies(int s, const char * host, unsigned short port,
|
2012-04-06 17:30:19 +00:00
|
|
|
unsigned int lifetime, int ipv6)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-04-06 17:30:19 +00:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct sockaddr_storage sockname;
|
|
|
|
#else
|
2011-09-28 19:13:20 +00:00
|
|
|
struct sockaddr_in sockname;
|
2012-04-06 17:30:19 +00:00
|
|
|
#endif
|
2013-02-07 12:27:09 +00:00
|
|
|
int i=0;
|
2012-10-04 22:21:00 +00:00
|
|
|
char ver_str[4];
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2012-04-06 17:30:19 +00:00
|
|
|
memset(&sockname, 0, sizeof(sockname));
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
if(ipv6)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 * p = (struct sockaddr_in6 *)&sockname;
|
|
|
|
p->sin6_family = AF_INET6;
|
|
|
|
p->sin6_port = htons(SSDP_PORT);
|
|
|
|
inet_pton(AF_INET6, LL_SSDP_MCAST_ADDR, &(p->sin6_addr));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2012-05-27 23:18:35 +00:00
|
|
|
{
|
2012-04-06 17:30:19 +00:00
|
|
|
struct sockaddr_in *p = (struct sockaddr_in *)&sockname;
|
|
|
|
p->sin_family = AF_INET;
|
|
|
|
p->sin_port = htons(SSDP_PORT);
|
|
|
|
p->sin_addr.s_addr = inet_addr(SSDP_MCAST_ADDR);
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2012-09-28 09:13:48 +00:00
|
|
|
while(known_service_types[i].s)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-10-04 22:21:00 +00:00
|
|
|
if(i==0)
|
|
|
|
ver_str[0] = '\0';
|
|
|
|
else
|
|
|
|
snprintf(ver_str, sizeof(ver_str), "%d", known_service_types[i].version);
|
2013-02-07 12:27:09 +00:00
|
|
|
SendSSDPNotify(s, (struct sockaddr *)&sockname, host, port,
|
|
|
|
known_service_types[i].s, ver_str, /* NT: */
|
2013-06-13 14:03:36 +00:00
|
|
|
known_service_types[i].uuid, "::",
|
|
|
|
known_service_types[i].s, /* ver_str, USN: */
|
2013-02-07 12:27:09 +00:00
|
|
|
lifetime, ipv6);
|
2013-06-14 17:10:59 +00:00
|
|
|
if(0==memcmp(known_service_types[i].s,
|
|
|
|
"urn:schemas-upnp-org:device", sizeof("urn:schemas-upnp-org:device")-1))
|
|
|
|
{
|
2013-02-07 12:27:09 +00:00
|
|
|
SendSSDPNotify(s, (struct sockaddr *)&sockname, host, port,
|
2013-06-14 17:10:59 +00:00
|
|
|
known_service_types[i].uuid, "", /* NT: */
|
|
|
|
known_service_types[i].uuid, "", "", /* ver_str, USN: */
|
2013-02-07 12:27:09 +00:00
|
|
|
lifetime, ipv6);
|
2013-06-14 17:10:59 +00:00
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SendSSDPNotifies2(int * sockets,
|
|
|
|
unsigned short port,
|
|
|
|
unsigned int lifetime)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct lan_addr_s * lan_addr;
|
2012-04-06 17:30:19 +00:00
|
|
|
for(i=0, lan_addr = lan_addrs.lh_first;
|
|
|
|
lan_addr != NULL;
|
|
|
|
lan_addr = lan_addr->list.le_next)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-04-06 17:30:19 +00:00
|
|
|
SendSSDPNotifies(sockets[i], lan_addr->str, port,
|
|
|
|
lifetime, 0);
|
|
|
|
i++;
|
|
|
|
#ifdef ENABLE_IPV6
|
2014-02-06 09:55:28 +00:00
|
|
|
if(sockets[i] >= 0)
|
|
|
|
{
|
|
|
|
SendSSDPNotifies(sockets[i], ipv6_addr_for_http_with_brackets, port,
|
|
|
|
lifetime, 1);
|
|
|
|
}
|
2012-04-06 17:30:19 +00:00
|
|
|
i++;
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ProcessSSDPRequest()
|
|
|
|
* process SSDP M-SEARCH requests and responds to them */
|
|
|
|
void
|
|
|
|
ProcessSSDPRequest(int s, unsigned short port)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
char bufr[1500];
|
|
|
|
socklen_t len_r;
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct sockaddr_storage sendername;
|
|
|
|
len_r = sizeof(struct sockaddr_storage);
|
|
|
|
#else
|
|
|
|
struct sockaddr_in sendername;
|
|
|
|
len_r = sizeof(struct sockaddr_in);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
n = recvfrom(s, bufr, sizeof(bufr), 0,
|
|
|
|
(struct sockaddr *)&sendername, &len_r);
|
|
|
|
if(n < 0)
|
|
|
|
{
|
2012-02-02 17:45:43 +00:00
|
|
|
/* EAGAIN, EWOULDBLOCK, EINTR : silently ignore (try again next time)
|
|
|
|
* other errors : log to LOG_ERR */
|
|
|
|
if(errno != EAGAIN &&
|
|
|
|
errno != EWOULDBLOCK &&
|
|
|
|
errno != EINTR)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "recvfrom(udp): %m");
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ProcessSSDPData(s, bufr, n, (struct sockaddr *)&sendername, port);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ProcessSSDPData(int s, const char *bufr, int n,
|
|
|
|
const struct sockaddr * sender, unsigned short port) {
|
|
|
|
int i, l;
|
|
|
|
struct lan_addr_s * lan_addr = NULL;
|
|
|
|
const char * st = NULL;
|
|
|
|
int st_len = 0;
|
2012-09-28 09:13:48 +00:00
|
|
|
int st_ver = 0;
|
2011-09-28 19:13:20 +00:00
|
|
|
char sender_str[64];
|
2012-10-04 22:21:00 +00:00
|
|
|
char ver_str[4];
|
2011-09-28 19:13:20 +00:00
|
|
|
const char * announced_host = NULL;
|
2012-06-23 23:41:47 +00:00
|
|
|
#ifdef UPNP_STRICT
|
2013-02-07 10:00:15 +00:00
|
|
|
#ifdef ENABLE_IPV6
|
2012-06-23 23:41:47 +00:00
|
|
|
char announced_host_buf[64];
|
2012-09-28 09:04:04 +00:00
|
|
|
#endif
|
2013-02-07 10:00:15 +00:00
|
|
|
int mx_value = -1;
|
2012-06-23 23:41:47 +00:00
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
/* get the string representation of the sender address */
|
|
|
|
sockaddr_to_string(sender, sender_str, sizeof(sender_str));
|
2013-02-06 14:20:04 +00:00
|
|
|
lan_addr = get_lan_for_peer(sender);
|
|
|
|
if(lan_addr == NULL)
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, "SSDP packet sender %s not from a LAN, ignoring",
|
|
|
|
sender_str);
|
|
|
|
return;
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
if(memcmp(bufr, "NOTIFY", 6) == 0)
|
|
|
|
{
|
|
|
|
/* ignore NOTIFY packets. We could log the sender and device type */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(memcmp(bufr, "M-SEARCH", 8) == 0)
|
|
|
|
{
|
|
|
|
i = 0;
|
|
|
|
while(i < n)
|
|
|
|
{
|
|
|
|
while((i < n - 1) && (bufr[i] != '\r' || bufr[i+1] != '\n'))
|
|
|
|
i++;
|
|
|
|
i += 2;
|
|
|
|
if((i < n - 3) && (strncasecmp(bufr+i, "st:", 3) == 0))
|
|
|
|
{
|
|
|
|
st = bufr+i+3;
|
|
|
|
st_len = 0;
|
|
|
|
while((*st == ' ' || *st == '\t') && (st < bufr + n))
|
|
|
|
st++;
|
|
|
|
while(st[st_len]!='\r' && st[st_len]!='\n'
|
|
|
|
&& (st + st_len < bufr + n))
|
|
|
|
st_len++;
|
2012-09-28 09:13:48 +00:00
|
|
|
l = st_len;
|
|
|
|
while(l > 0 && st[l-1] != ':')
|
|
|
|
l--;
|
|
|
|
st_ver = atoi(st+l);
|
|
|
|
syslog(LOG_DEBUG, "ST: %.*s (ver=%d)", st_len, st, st_ver);
|
2011-09-28 19:13:20 +00:00
|
|
|
/*j = 0;*/
|
|
|
|
/*while(bufr[i+j]!='\r') j++;*/
|
|
|
|
/*syslog(LOG_INFO, "%.*s", j, bufr+i);*/
|
|
|
|
}
|
2013-02-07 10:00:15 +00:00
|
|
|
#ifdef UPNP_STRICT
|
|
|
|
else if((i < n - 3) && (strncasecmp(bufr+i, "mx:", 3) == 0))
|
|
|
|
{
|
|
|
|
const char * mx;
|
|
|
|
int mx_len;
|
|
|
|
mx = bufr+i+3;
|
|
|
|
mx_len = 0;
|
|
|
|
while((*mx == ' ' || *mx == '\t') && (mx < bufr + n))
|
|
|
|
mx++;
|
|
|
|
while(mx[mx_len]!='\r' && mx[mx_len]!='\n'
|
|
|
|
&& (mx + mx_len < bufr + n))
|
|
|
|
mx_len++;
|
|
|
|
mx_value = atoi(mx);
|
|
|
|
syslog(LOG_DEBUG, "MX: %.*s (value=%d)", mx_len, mx, mx_value);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#ifdef UPNP_STRICT
|
|
|
|
if(mx_value < 0) {
|
|
|
|
syslog(LOG_INFO, "ignoring SSDP packet missing MX: header");
|
|
|
|
return;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2013-02-07 10:00:15 +00:00
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
/*syslog(LOG_INFO, "SSDP M-SEARCH packet received from %s",
|
|
|
|
sender_str );*/
|
|
|
|
if(st && (st_len > 0))
|
|
|
|
{
|
|
|
|
/* TODO : doesnt answer at once but wait for a random time */
|
|
|
|
syslog(LOG_INFO, "SSDP M-SEARCH from %s ST: %.*s",
|
|
|
|
sender_str, st_len, st);
|
|
|
|
/* find in which sub network the client is */
|
|
|
|
if(sender->sa_family == AF_INET)
|
|
|
|
{
|
|
|
|
if (lan_addr == NULL)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "Can't find in which sub network the client is");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
announced_host = lan_addr->str;
|
|
|
|
}
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* IPv6 address with brackets */
|
2012-06-23 23:41:47 +00:00
|
|
|
#ifdef UPNP_STRICT
|
2013-02-06 14:06:59 +00:00
|
|
|
int index;
|
2012-06-23 23:41:47 +00:00
|
|
|
struct in6_addr addr6;
|
|
|
|
size_t addr6_len = sizeof(addr6);
|
|
|
|
/* retrieve the IPv6 address which
|
|
|
|
* will be used locally to reach sender */
|
|
|
|
memset(&addr6, 0, sizeof(addr6));
|
2013-02-06 14:06:59 +00:00
|
|
|
if(get_src_for_route_to (sender, &addr6, &addr6_len, &index) < 0) {
|
2012-06-23 23:41:47 +00:00
|
|
|
syslog(LOG_WARNING, "get_src_for_route_to() failed, using %s", ipv6_addr_for_http_with_brackets);
|
|
|
|
announced_host = ipv6_addr_for_http_with_brackets;
|
|
|
|
} else {
|
|
|
|
if(inet_ntop(AF_INET6, &addr6,
|
|
|
|
announced_host_buf+1,
|
|
|
|
sizeof(announced_host_buf) - 2)) {
|
|
|
|
announced_host_buf[0] = '[';
|
|
|
|
i = strlen(announced_host_buf);
|
2012-06-28 19:00:49 +00:00
|
|
|
if(i < (int)sizeof(announced_host_buf) - 1) {
|
2012-06-23 23:41:47 +00:00
|
|
|
announced_host_buf[i] = ']';
|
|
|
|
announced_host_buf[i+1] = '\0';
|
|
|
|
} else {
|
|
|
|
syslog(LOG_NOTICE, "cannot suffix %s with ']'",
|
|
|
|
announced_host_buf);
|
|
|
|
}
|
|
|
|
announced_host = announced_host_buf;
|
|
|
|
} else {
|
|
|
|
syslog(LOG_NOTICE, "inet_ntop() failed %m");
|
|
|
|
announced_host = ipv6_addr_for_http_with_brackets;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2011-09-28 19:13:20 +00:00
|
|
|
announced_host = ipv6_addr_for_http_with_brackets;
|
2012-06-23 23:41:47 +00:00
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* Responds to request with a device as ST header */
|
2012-09-28 09:13:48 +00:00
|
|
|
for(i = 0; known_service_types[i].s; i++)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-09-28 09:13:48 +00:00
|
|
|
l = (int)strlen(known_service_types[i].s);
|
|
|
|
if(l<=st_len && (0 == memcmp(st, known_service_types[i].s, l))
|
|
|
|
#ifdef UPNP_STRICT
|
|
|
|
&& (st_ver <= known_service_types[i].version)
|
|
|
|
/* only answer for service version lower or equal of supported one */
|
|
|
|
#endif
|
|
|
|
)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
syslog(LOG_INFO, "Single search found");
|
2013-06-11 18:03:15 +00:00
|
|
|
SendSSDPResponse(s, sender,
|
|
|
|
st, st_len, "",
|
2013-06-13 14:03:36 +00:00
|
|
|
announced_host, port,
|
|
|
|
known_service_types[i].uuid);
|
2011-09-28 19:13:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Responds to request with ST: ssdp:all */
|
|
|
|
/* strlen("ssdp:all") == 8 */
|
|
|
|
if(st_len==8 && (0 == memcmp(st, "ssdp:all", 8)))
|
|
|
|
{
|
|
|
|
syslog(LOG_INFO, "ssdp:all found");
|
2012-09-28 09:13:48 +00:00
|
|
|
for(i=0; known_service_types[i].s; i++)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-10-04 22:21:00 +00:00
|
|
|
if(i==0)
|
|
|
|
ver_str[0] = '\0';
|
|
|
|
else
|
|
|
|
snprintf(ver_str, sizeof(ver_str), "%d", known_service_types[i].version);
|
2012-09-28 09:13:48 +00:00
|
|
|
l = (int)strlen(known_service_types[i].s);
|
2013-06-11 18:03:15 +00:00
|
|
|
SendSSDPResponse(s, sender,
|
|
|
|
known_service_types[i].s, l, ver_str,
|
2013-06-13 14:03:36 +00:00
|
|
|
announced_host, port,
|
|
|
|
known_service_types[i].uuid);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2012-09-28 09:13:48 +00:00
|
|
|
/* also answer for uuid */
|
2013-06-13 14:03:36 +00:00
|
|
|
SendSSDPResponse(s, sender, uuidvalue_igd, strlen(uuidvalue_igd), "",
|
|
|
|
announced_host, port, uuidvalue_igd);
|
|
|
|
SendSSDPResponse(s, sender, uuidvalue_wan, strlen(uuidvalue_wan), "",
|
|
|
|
announced_host, port, uuidvalue_wan);
|
|
|
|
SendSSDPResponse(s, sender, uuidvalue_wcd, strlen(uuidvalue_wcd), "",
|
|
|
|
announced_host, port, uuidvalue_wcd);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
/* responds to request by UUID value */
|
2013-06-13 14:03:36 +00:00
|
|
|
l = (int)strlen(uuidvalue_igd);
|
|
|
|
if(l==st_len)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2013-06-13 14:03:36 +00:00
|
|
|
if(0 == memcmp(st, uuidvalue_igd, l))
|
|
|
|
{
|
|
|
|
syslog(LOG_INFO, "ssdp:uuid (IGD) found");
|
|
|
|
SendSSDPResponse(s, sender, st, st_len, "",
|
|
|
|
announced_host, port, uuidvalue_igd);
|
|
|
|
}
|
|
|
|
else if(0 == memcmp(st, uuidvalue_wan, l))
|
|
|
|
{
|
|
|
|
syslog(LOG_INFO, "ssdp:uuid (WAN) found");
|
|
|
|
SendSSDPResponse(s, sender, st, st_len, "",
|
|
|
|
announced_host, port, uuidvalue_wan);
|
|
|
|
}
|
|
|
|
else if(0 == memcmp(st, uuidvalue_wcd, l))
|
|
|
|
{
|
|
|
|
syslog(LOG_INFO, "ssdp:uuid (WCD) found");
|
|
|
|
SendSSDPResponse(s, sender, st, st_len, "",
|
|
|
|
announced_host, port, uuidvalue_wcd);
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
syslog(LOG_INFO, "Invalid SSDP M-SEARCH from %s", sender_str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
syslog(LOG_NOTICE, "Unknown udp packet received from %s", sender_str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-07 12:27:09 +00:00
|
|
|
static int
|
|
|
|
SendSSDPbyebye(int s, const struct sockaddr * dest,
|
|
|
|
const char * nt, const char * suffix,
|
|
|
|
const char * usn1, const char * usn2, const char * usn3,
|
|
|
|
int ipv6)
|
|
|
|
{
|
|
|
|
int n, l;
|
|
|
|
char bufr[512];
|
|
|
|
|
|
|
|
l = snprintf(bufr, sizeof(bufr),
|
|
|
|
"NOTIFY * HTTP/1.1\r\n"
|
|
|
|
"HOST: %s:%d\r\n"
|
|
|
|
"NT: %s%s\r\n"
|
|
|
|
"USN: %s%s%s%s\r\n"
|
|
|
|
"NTS: ssdp:byebye\r\n"
|
|
|
|
"OPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n" /* UDA v1.1 */
|
|
|
|
"01-NLS: %u\r\n" /* same as BOOTID field. UDA v1.1 */
|
|
|
|
"BOOTID.UPNP.ORG: %u\r\n" /* UDA v1.1 */
|
|
|
|
"CONFIGID.UPNP.ORG: %u\r\n" /* UDA v1.1 */
|
|
|
|
"\r\n",
|
|
|
|
ipv6 ? "[" LL_SSDP_MCAST_ADDR "]" : SSDP_MCAST_ADDR,
|
|
|
|
SSDP_PORT,
|
|
|
|
nt, suffix, /* NT: */
|
|
|
|
usn1, usn2, usn3, suffix, /* USN: */
|
|
|
|
upnp_bootid, upnp_bootid, upnp_configid);
|
|
|
|
if(l<0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "SendSSDPbyebye() snprintf error");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if((unsigned int)l >= sizeof(bufr))
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, "SendSSDPbyebye(): truncated output");
|
2013-04-26 15:18:28 +00:00
|
|
|
l = sizeof(bufr) - 1;
|
2013-02-07 12:27:09 +00:00
|
|
|
}
|
|
|
|
n = sendto(s, bufr, l, 0, dest,
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)
|
|
|
|
#else
|
|
|
|
sizeof(struct sockaddr_in)
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
if(n < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "sendto(udp_shutdown=%d): %m", s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if(n != l)
|
|
|
|
{
|
|
|
|
syslog(LOG_NOTICE, "sendto() sent %d out of %d bytes", n, l);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-01 01:44:38 +00:00
|
|
|
/* This will broadcast ssdp:byebye notifications to inform
|
2011-09-28 19:13:20 +00:00
|
|
|
* the network that UPnP is going down. */
|
|
|
|
int
|
|
|
|
SendSSDPGoodbye(int * sockets, int n_sockets)
|
|
|
|
{
|
|
|
|
struct sockaddr_in sockname;
|
2012-04-06 17:30:19 +00:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct sockaddr_in6 sockname6;
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
int i, j;
|
2012-10-04 22:21:00 +00:00
|
|
|
char ver_str[4];
|
2012-02-09 20:33:24 +00:00
|
|
|
int ret = 0;
|
2012-04-06 17:30:19 +00:00
|
|
|
int ipv6 = 0;
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
memset(&sockname, 0, sizeof(struct sockaddr_in));
|
|
|
|
sockname.sin_family = AF_INET;
|
|
|
|
sockname.sin_port = htons(SSDP_PORT);
|
|
|
|
sockname.sin_addr.s_addr = inet_addr(SSDP_MCAST_ADDR);
|
2012-04-06 17:30:19 +00:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
memset(&sockname6, 0, sizeof(struct sockaddr_in6));
|
|
|
|
sockname6.sin6_family = AF_INET6;
|
|
|
|
sockname6.sin6_port = htons(SSDP_PORT);
|
|
|
|
inet_pton(AF_INET6, LL_SSDP_MCAST_ADDR, &(sockname6.sin6_addr));
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
for(j=0; j<n_sockets; j++)
|
|
|
|
{
|
2012-04-06 17:30:19 +00:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
ipv6 = j & 1;
|
|
|
|
#endif
|
2012-09-28 09:13:48 +00:00
|
|
|
for(i=0; known_service_types[i].s; i++)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-10-04 22:21:00 +00:00
|
|
|
if(i==0)
|
|
|
|
ver_str[0] = '\0';
|
|
|
|
else
|
|
|
|
snprintf(ver_str, sizeof(ver_str), "%d", known_service_types[i].version);
|
2013-02-07 12:27:09 +00:00
|
|
|
ret += SendSSDPbyebye(sockets[j],
|
2012-04-06 17:53:26 +00:00
|
|
|
#ifdef ENABLE_IPV6
|
2013-02-07 12:27:09 +00:00
|
|
|
ipv6 ? (struct sockaddr *)&sockname6 : (struct sockaddr *)&sockname,
|
2012-04-06 17:53:26 +00:00
|
|
|
#else
|
2013-02-07 12:27:09 +00:00
|
|
|
(struct sockaddr *)&sockname,
|
2012-04-06 17:53:26 +00:00
|
|
|
#endif
|
2013-02-07 12:27:09 +00:00
|
|
|
known_service_types[i].s, ver_str, /* NT: */
|
2013-06-13 14:03:36 +00:00
|
|
|
known_service_types[i].uuid, "::",
|
|
|
|
known_service_types[i].s, /* ver_str, USN: */
|
2013-02-07 12:27:09 +00:00
|
|
|
ipv6);
|
2013-06-15 12:51:28 +00:00
|
|
|
if(0==memcmp(known_service_types[i].s,
|
|
|
|
"urn:schemas-upnp-org:device", sizeof("urn:schemas-upnp-org:device")-1))
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2013-02-07 12:27:09 +00:00
|
|
|
ret += SendSSDPbyebye(sockets[j],
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
ipv6 ? (struct sockaddr *)&sockname6 : (struct sockaddr *)&sockname,
|
|
|
|
#else
|
|
|
|
(struct sockaddr *)&sockname,
|
|
|
|
#endif
|
2013-06-15 12:51:28 +00:00
|
|
|
known_service_types[i].uuid, "", /* NT: */
|
|
|
|
known_service_types[i].uuid, "", "", /* ver_str, USN: */
|
2013-02-07 12:27:09 +00:00
|
|
|
ipv6);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-09 20:33:24 +00:00
|
|
|
return ret;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* SubmitServicesToMiniSSDPD() :
|
|
|
|
* register services offered by MiniUPnPd to a running instance of
|
|
|
|
* MiniSSDPd */
|
|
|
|
int
|
|
|
|
SubmitServicesToMiniSSDPD(const char * host, unsigned short port) {
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
int s;
|
|
|
|
unsigned char buffer[2048];
|
|
|
|
char strbuf[256];
|
|
|
|
unsigned char * p;
|
2012-08-21 17:33:26 +00:00
|
|
|
int i, l, n;
|
2012-10-04 22:21:00 +00:00
|
|
|
char ver_str[4];
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
s = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if(s < 0) {
|
|
|
|
syslog(LOG_ERR, "socket(unix): %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
strncpy(addr.sun_path, minissdpdsocketpath, sizeof(addr.sun_path));
|
|
|
|
if(connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
|
|
|
|
syslog(LOG_ERR, "connect(\"%s\"): %m", minissdpdsocketpath);
|
2012-08-21 17:33:26 +00:00
|
|
|
close(s);
|
2011-09-28 19:13:20 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-09-28 09:13:48 +00:00
|
|
|
for(i = 0; known_service_types[i].s; i++) {
|
2012-08-21 17:33:26 +00:00
|
|
|
buffer[0] = 4; /* request type 4 : submit service */
|
|
|
|
/* 4 strings following : ST (service type), USN, Server, Location */
|
2011-09-28 19:13:20 +00:00
|
|
|
p = buffer + 1;
|
2012-09-28 09:13:48 +00:00
|
|
|
l = (int)strlen(known_service_types[i].s);
|
2011-09-28 19:13:20 +00:00
|
|
|
if(i > 0)
|
|
|
|
l++;
|
|
|
|
CODELENGTH(l, p);
|
2012-09-28 09:13:48 +00:00
|
|
|
memcpy(p, known_service_types[i].s, l);
|
2011-09-28 19:13:20 +00:00
|
|
|
if(i > 0)
|
|
|
|
p[l-1] = '1';
|
|
|
|
p += l;
|
2012-10-04 22:21:00 +00:00
|
|
|
if(i==0)
|
|
|
|
ver_str[0] = '\0';
|
|
|
|
else
|
|
|
|
snprintf(ver_str, sizeof(ver_str), "%d", known_service_types[i].version);
|
2012-03-01 01:44:38 +00:00
|
|
|
l = snprintf(strbuf, sizeof(strbuf), "%s::%s%s",
|
2013-06-13 14:03:36 +00:00
|
|
|
known_service_types[i].uuid, known_service_types[i].s, ver_str);
|
2013-04-26 15:18:28 +00:00
|
|
|
if(l<0) {
|
|
|
|
syslog(LOG_WARNING, "SubmitServicesToMiniSSDPD: snprintf %m");
|
|
|
|
continue;
|
|
|
|
} else if((unsigned)l>=sizeof(strbuf)) {
|
|
|
|
l = sizeof(strbuf) - 1;
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
CODELENGTH(l, p);
|
|
|
|
memcpy(p, strbuf, l);
|
|
|
|
p += l;
|
|
|
|
l = (int)strlen(MINIUPNPD_SERVER_STRING);
|
|
|
|
CODELENGTH(l, p);
|
|
|
|
memcpy(p, MINIUPNPD_SERVER_STRING, l);
|
|
|
|
p += l;
|
|
|
|
l = snprintf(strbuf, sizeof(strbuf), "http://%s:%u" ROOTDESC_PATH,
|
|
|
|
host, (unsigned int)port);
|
2013-04-26 15:18:28 +00:00
|
|
|
if(l<0) {
|
|
|
|
syslog(LOG_WARNING, "SubmitServicesToMiniSSDPD: snprintf %m");
|
|
|
|
continue;
|
|
|
|
} else if((unsigned)l>=sizeof(strbuf)) {
|
|
|
|
l = sizeof(strbuf) - 1;
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
CODELENGTH(l, p);
|
|
|
|
memcpy(p, strbuf, l);
|
|
|
|
p += l;
|
2012-08-21 17:33:26 +00:00
|
|
|
/* now write the encoded data */
|
|
|
|
n = p - buffer; /* bytes to send */
|
|
|
|
p = buffer; /* start */
|
|
|
|
while(n > 0) {
|
|
|
|
l = write(s, p, n);
|
|
|
|
if (l < 0) {
|
|
|
|
syslog(LOG_ERR, "write(): %m");
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
} else if (l == 0) {
|
|
|
|
syslog(LOG_ERR, "write() returned 0");
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
p += l;
|
|
|
|
n -= l;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|