File: openssdpsocket.c

package info (click to toggle)
minissdpd 1.2.20130907-3+deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 304 kB
  • ctags: 198
  • sloc: ansic: 3,891; makefile: 185; sh: 90
file content (235 lines) | stat: -rw-r--r-- 6,142 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* $Id: openssdpsocket.c,v 1.12 2012/05/21 17:13:11 nanard Exp $ */
/* MiniUPnP project
 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
 * (c) 2006-2012 Thomas Bernard
 * 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"
#include "upnputils.h"

/* 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")

/**
 * Get the IPv4 address from a string
 * representing the address or the interface name
 */
static in_addr_t
GetIfAddrIPv4(const char * ifaddr)
{
	in_addr_t addr;
	int s;
	struct ifreq ifr;
	int ifrlen;

	/* let's suppose ifaddr is a IPv4 address
	 * such as 192.168.1.1 */
	addr = inet_addr(ifaddr);
	if(addr != INADDR_NONE)
	{
		return addr;
	}
	/* let's suppose the ifaddr was in fact an interface name
	 * such as eth0 */
	s = socket(PF_INET, SOCK_DGRAM, 0);
	if(s < 0)
	{
		syslog(LOG_ERR, "socket(PF_INET, SOCK_DGRAM): %m");
		return INADDR_NONE;
	}
	memset(&ifr, 0, sizeof(struct ifreq));
	strncpy(ifr.ifr_name, ifaddr, IFNAMSIZ);
	if(ioctl(s, SIOCGIFADDR, &ifr, &ifrlen) < 0)
	{
		syslog(LOG_ERR, "ioctl(s, SIOCGIFADDR, ...): %m");
		close(s);
		return INADDR_NONE;
	}
	syslog(LOG_DEBUG, "GetIfAddrIPv4(%s) = %s", ifaddr,
	       inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
	addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
	close(s);
	return addr;
}

/**
 * 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
AddDropMulticastMembership(int s, const char * ifaddr, int ipv6, int drop)
{
	struct ip_mreq imr;	/* Ip multicast membership */
#ifdef ENABLE_IPV6
	struct ipv6_mreq mr;
	unsigned int ifindex;
#endif

	if(s <= 0)
		return -1;	/* nothing to do */
#ifdef ENABLE_IPV6
	if(ipv6)
	{
		ifindex = if_nametoindex(ifaddr);
		memset(&mr, 0, sizeof(mr));
		inet_pton(AF_INET6, LL_SSDP_MCAST_ADDR, &mr.ipv6mr_multiaddr);
		mr.ipv6mr_interface = ifindex;
		if(setsockopt(s, IPPROTO_IPV6, drop ? IPV6_LEAVE_GROUP : IPV6_JOIN_GROUP,
		   &mr, sizeof(struct ipv6_mreq)) < 0)
		{
			syslog(LOG_ERR, "setsockopt(udp, %s)(%s, %s): %m",
			       drop ? "IPV6_LEAVE_GROUP" : "IPV6_JOIN_GROUP",
			       LL_SSDP_MCAST_ADDR,
			       ifaddr);
			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)
		{
			syslog(LOG_ERR, "setsockopt(udp, %s)(%s, %s): %m",
			       drop ? "IPV6_LEAVE_GROUP" : "IPV6_JOIN_GROUP",
			       SL_SSDP_MCAST_ADDR,
			       ifaddr);
			return -1;
		}
	}
	else
	{
#endif
	    /* 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 = inet_addr(ifaddr);*/
		imr.imr_interface.s_addr = GetIfAddrIPv4(ifaddr);
		if(imr.imr_interface.s_addr == INADDR_NONE)
		{
			syslog(LOG_ERR, "no IPv4 address for interface %s",
			       ifaddr);
			return -1;
		}

		if (setsockopt(s, IPPROTO_IP, drop ? IP_DROP_MEMBERSHIP : IP_ADD_MEMBERSHIP,
		    (void *)&imr, sizeof(struct ip_mreq)) < 0)
		{
	        syslog(LOG_ERR, "setsockopt(udp, %s)(%s): %m",
			       drop ? "IP_DROP_MEMBERSHIP" : "IP_ADD_MEMBERSHIP",
			       ifaddr);
			return -1;
	    }
#ifdef ENABLE_IPV6
	}
#endif

	return 0;
}

int
OpenAndConfSSDPReceiveSocket(int n_listen_addr,
							 const char * * listen_addr,
                             int ipv6)
{
	int s;
	int opt = 1;
#ifdef ENABLE_IPV6
	struct sockaddr_storage sockname;
#else
	struct sockaddr_in sockname;
#endif
	socklen_t sockname_len;

#ifdef ENABLE_IPV6
	if( (s = socket(ipv6 ? PF_INET6 : PF_INET, SOCK_DGRAM, 0)) < 0)
#else
	if( (s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
#endif
	{
		syslog(LOG_ERR, "socket(udp): %m");
		return -1;
	}

	if(!set_non_blocking(s)) {
		syslog(LOG_WARNING, "Failed to set SSDP socket non blocking : %m");
	}

#ifdef ENABLE_IPV6
	memset(&sockname, 0, sizeof(struct sockaddr_storage));
	if(ipv6)
	{
#ifdef IPV6_V6ONLY
		if(setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
		              (char *)&opt, sizeof(opt)) < 0)
		{
			syslog(LOG_WARNING, "setsockopt(IPV6_V6ONLY): %m");
		}
#endif
		struct sockaddr_in6 * sa = (struct sockaddr_in6 *)&sockname;
    	sa->sin6_family = AF_INET6;
    	sa->sin6_port = htons(SSDP_PORT);
		sa->sin6_addr = in6addr_any;
		sockname_len = sizeof(struct sockaddr_in6);
	}
	else
	{
		struct sockaddr_in * sa = (struct sockaddr_in *)&sockname;
    	sa->sin_family = AF_INET;
    	sa->sin_port = htons(SSDP_PORT);
    	sa->sin_addr.s_addr = htonl(INADDR_ANY);
		sockname_len = sizeof(struct sockaddr_in);
	}
#else
	memset(&sockname, 0, sizeof(struct sockaddr_in));
    sockname.sin_family = AF_INET;
    sockname.sin_port = htons(SSDP_PORT);
	/* NOTE : it seems it doesnt work when binding on the specific address */
    /*sockname.sin_addr.s_addr = inet_addr(UPNP_MCAST_ADDR);*/
    sockname.sin_addr.s_addr = htonl(INADDR_ANY);
    /*sockname.sin_addr.s_addr = inet_addr(ifaddr);*/
	sockname_len = sizeof(struct sockaddr_in);
#endif

	if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
	{
		syslog(LOG_WARNING, "setsockopt(SO_REUSEADDR): %m");
	}

    if(bind(s, (struct sockaddr *)&sockname, sockname_len) < 0)
	{
		syslog(LOG_ERR, "bind(udp%s): %m", ipv6 ? "6" : "");
		close(s);
		return -1;
    }

	while(n_listen_addr>0)
	{
		n_listen_addr--;
		if(AddDropMulticastMembership(s, listen_addr[n_listen_addr], ipv6, 0) < 0)
		{
			syslog(LOG_WARNING, "Failed to add IPv%d multicast membership for interface %s.",
			       ipv6 ? 6 : 4,
			       listen_addr[n_listen_addr] );
		}
	}

	return s;
}