1 /*****************************************************************************
2 * sap.c : SAP announce handler
3 *****************************************************************************
4 * Copyright (C) 2002-2008 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Rémi Denis-Courmont <rem # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
35 #include <stdlib.h> /* free() */
36 #include <stdio.h> /* sprintf() */
38 #include <ctype.h> /* tolower(), isxdigit() */
42 #include <vlc_network.h>
44 #include "stream_output.h"
47 /* SAP is always on that port */
48 #define IPPORT_SAP 9875
50 /* A SAP session descriptor, enqueued in the SAP handler queue */
51 typedef struct sap_session_t
53 struct sap_session_t *next;
54 const session_descriptor_t *p_sd;
59 /* A SAP announce address. For each of these, we run the
60 * control flow algorithm */
61 typedef struct sap_address_t
63 struct sap_address_t *next;
69 char group[NI_MAXNUMERICHOST];
70 struct sockaddr_storage orig;
75 unsigned session_count;
79 /* The SAP handler, running in a separate thread */
88 #define SAP_MAX_BUFFER 65534
89 #define MIN_INTERVAL 2
90 #define MAX_INTERVAL 300
92 /*****************************************************************************
94 *****************************************************************************/
95 static void *RunThread (void *);
98 * Create the SAP handler
100 * \param p_announce a VLC object
101 * \return the newly created SAP handler or NULL on error
103 sap_handler_t *SAP_Create (vlc_object_t *p_announce)
105 sap_handler_t *p_sap;
107 p_sap = vlc_custom_create (p_announce, sizeof (*p_sap),
108 VLC_OBJECT_GENERIC, "sap sender");
112 vlc_mutex_init (&p_sap->lock);
117 void SAP_Destroy (sap_handler_t *p_sap)
119 assert (p_sap->first == NULL);
120 vlc_mutex_destroy (&p_sap->lock);
121 vlc_object_release (p_sap);
124 static sap_address_t *AddressCreate (vlc_object_t *obj, const char *group)
126 int fd = net_ConnectUDP (obj, group, IPPORT_SAP, 255);
130 sap_address_t *addr = malloc (sizeof (*addr));
137 strlcpy (addr->group, group, sizeof (addr->group));
139 addr->origlen = sizeof (addr->orig);
140 getsockname (fd, (struct sockaddr *)&addr->orig, &addr->origlen);
142 addr->interval = var_CreateGetInteger (obj, "sap-interval");
143 vlc_mutex_init (&addr->lock);
144 vlc_cond_init (&addr->wait);
145 addr->session_count = 0;
148 if (vlc_clone (&addr->thread, RunThread, addr, VLC_THREAD_PRIORITY_LOW))
150 msg_Err (obj, "unable to spawn SAP announce thread");
158 static void AddressDestroy (sap_address_t *addr)
160 assert (addr->first == NULL);
162 vlc_cancel (addr->thread);
163 vlc_join (addr->thread, NULL);
164 vlc_cond_destroy (&addr->wait);
165 vlc_mutex_destroy (&addr->lock);
166 net_Close (addr->fd);
171 * main SAP handler thread
172 * \param p_this the SAP Handler object
175 static void *RunThread (void *self)
177 sap_address_t *addr = self;
179 vlc_mutex_lock (&addr->lock);
180 mutex_cleanup_push (&addr->lock);
184 sap_session_t *p_session;
187 while (addr->first == NULL)
188 vlc_cond_wait (&addr->wait, &addr->lock);
190 assert (addr->session_count > 0);
193 for (p_session = addr->first; p_session; p_session = p_session->next)
195 send (addr->fd, p_session->data, p_session->length, 0);
196 deadline += addr->interval * CLOCK_FREQ / addr->session_count;
198 if (vlc_cond_timedwait (&addr->wait, &addr->lock, deadline) == 0)
199 break; /* list may have changed! */
210 int SAP_Add (sap_handler_t *p_sap, session_descriptor_t *p_session)
213 char psz_addr[NI_MAXNUMERICHOST];
214 bool b_ipv6 = false, b_ssm = false;
215 sap_session_t *p_sap_session;
217 struct sockaddr_storage addr;
220 addrlen = p_session->addrlen;
221 if ((addrlen == 0) || (addrlen > sizeof (addr)))
223 msg_Err( p_sap, "No/invalid address specified for SAP announce" );
227 /* Determine SAP multicast address automatically */
228 memcpy (&addr, &p_session->addr, addrlen);
230 switch( p_session->addr.ss_family )
232 #if defined (HAVE_INET_PTON) || defined (WIN32)
235 /* See RFC3513 for list of valid IPv6 scopes */
236 struct in6_addr *a6 = &((struct sockaddr_in6 *)&addr)->sin6_addr;
238 memcpy( a6->s6_addr + 2, "\x00\x00\x00\x00\x00\x00"
239 "\x00\x00\x00\x00\x00\x02\x7f\xfe", 14 );
240 if( IN6_IS_ADDR_MULTICAST( a6 ) )
242 /* SSM <=> ff3x::/32 */
243 b_ssm = (U32_AT (a6->s6_addr) & 0xfff0ffff) == 0xff300000;
245 /* force flags to zero, preserve scope */
246 a6->s6_addr[1] &= 0xf;
249 /* Unicast IPv6 - assume global scope */
250 memcpy( a6->s6_addr, "\xff\x0e", 2 );
259 /* See RFC2365 for IPv4 scopes */
262 ipv4 = ntohl( ((struct sockaddr_in *)&addr)->sin_addr.s_addr );
263 /* 224.0.0.0/24 => 224.0.0.255 */
264 if ((ipv4 & 0xffffff00) == 0xe0000000)
267 /* 239.255.0.0/16 => 239.255.255.255 */
268 if ((ipv4 & 0xffff0000) == 0xefff0000)
271 /* 239.192.0.0/14 => 239.195.255.255 */
272 if ((ipv4 & 0xfffc0000) == 0xefc00000)
275 if ((ipv4 & 0xff000000) == 0xef000000)
278 /* other addresses => 224.2.127.254 */
280 /* SSM: 232.0.0.0/8 */
281 b_ssm = (ipv4 >> 24) == 232;
287 msg_Err( p_sap, "Out-of-scope multicast address "
288 "not supported by SAP" );
292 ((struct sockaddr_in *)&addr)->sin_addr.s_addr = htonl( ipv4 );
297 msg_Err( p_sap, "Address family %d not supported by SAP",
302 i = vlc_getnameinfo( (struct sockaddr *)&addr, addrlen,
303 psz_addr, sizeof( psz_addr ), NULL, NI_NUMERICHOST );
307 msg_Err( p_sap, "%s", vlc_gai_strerror( i ) );
311 /* Find/create SAP address thread */
312 msg_Dbg( p_sap, "using SAP address: %s", psz_addr);
314 vlc_mutex_lock (&p_sap->lock);
315 sap_address_t *sap_addr;
316 for (sap_addr = p_sap->first; sap_addr; sap_addr = sap_addr->next)
317 if (!strcmp (psz_addr, sap_addr->group))
320 if (sap_addr == NULL)
322 sap_addr = AddressCreate (VLC_OBJECT(p_sap), psz_addr);
323 if (sap_addr == NULL)
325 vlc_mutex_unlock (&p_sap->lock);
328 sap_addr->next = p_sap->first;
329 p_sap->first = sap_addr;
332 * NEVER take the global SAP lock when holding a SAP thread lock! */
333 vlc_mutex_lock (&sap_addr->lock);
334 vlc_mutex_unlock (&p_sap->lock);
336 memcpy (&p_session->orig, &sap_addr->orig, sap_addr->origlen);
337 p_session->origlen = sap_addr->origlen;
339 size_t headsize = 20, length;
340 switch (p_session->orig.ss_family)
354 /* XXX: Check for dupes */
355 length = headsize + strlen (p_session->psz_sdp);
356 p_sap_session = malloc (sizeof (*p_sap_session) + length + 1);
357 if (p_sap_session == NULL)
359 vlc_mutex_unlock (&sap_addr->lock);
360 return VLC_EGENERIC; /* NOTE: we should destroy the thread if left unused */
362 p_sap_session->next = sap_addr->first;
363 sap_addr->first = p_sap_session;
364 p_sap_session->p_sd = p_session;
365 p_sap_session->length = length;
367 /* Build the SAP Headers */
368 uint8_t *psz_head = p_sap_session->data;
370 /* SAPv1, not encrypted, not compressed */
372 psz_head[1] = 0x00; /* No authentification length */
375 psz_head[2] = i_hash >> 8; /* Msg id hash */
376 psz_head[3] = i_hash; /* Msg id hash 2 */
379 switch (p_session->orig.ss_family)
384 struct in6_addr *a6 =
385 &((struct sockaddr_in6 *)&p_session->orig)->sin6_addr;
386 memcpy (psz_head + headsize, a6, 16);
387 psz_head[0] |= 0x10; /* IPv6 flag */
395 (((struct sockaddr_in *)&p_session->orig)->sin_addr.s_addr);
396 memcpy (psz_head + headsize, &ipv4, 4);
403 memcpy (psz_head + headsize, "application/sdp", 16);
406 /* Build the final message */
407 strcpy( (char *)psz_head + headsize, p_session->psz_sdp);
409 sap_addr->session_count++;
410 vlc_cond_signal (&sap_addr->wait);
411 vlc_mutex_unlock (&sap_addr->lock);
416 * Remove a SAP Announce
418 void SAP_Del (sap_handler_t *p_sap, const session_descriptor_t *p_session)
420 vlc_mutex_lock (&p_sap->lock);
422 /* TODO: give a handle back in SAP_Add, and use that... */
423 sap_address_t *addr, **paddr;
424 sap_session_t *session, **psession;
426 paddr = &p_sap->first;
427 for (addr = p_sap->first; addr; addr = addr->next)
429 psession = &addr->first;
430 vlc_mutex_lock (&addr->lock);
431 for (session = addr->first; session; session = session->next)
433 if (session->p_sd == p_session)
435 psession = &session->next;
437 vlc_mutex_unlock (&addr->lock);
443 *psession = session->next;
445 if (addr->first == NULL)
446 /* Last session for this address -> unlink the address */
448 vlc_mutex_unlock (&p_sap->lock);
450 if (addr->first == NULL)
452 /* Last session for this address -> unlink the address */
453 vlc_mutex_unlock (&addr->lock);
454 AddressDestroy (addr);
458 addr->session_count--;
459 vlc_cond_signal (&addr->wait);
460 vlc_mutex_unlock (&addr->lock);