1 /*****************************************************************************
2 * sap.c : SAP announce handler
3 *****************************************************************************
4 * Copyright (C) 2002-2008 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
29 #include <vlc_common.h>
31 #include <stdlib.h> /* free() */
32 #include <stdio.h> /* sprintf() */
37 #include <vlc_network.h>
39 #include "stream_output.h"
42 /* SAP is always on that port */
43 #define IPPORT_SAP 9875
45 struct session_descriptor_t
47 struct sockaddr_storage orig;
49 struct sockaddr_storage addr;
56 /* A SAP session descriptor, enqueued in the SAP handler queue */
57 typedef struct sap_session_t
59 struct sap_session_t *next;
60 const session_descriptor_t *p_sd;
65 /* A SAP announce address. For each of these, we run the
66 * control flow algorithm */
67 typedef struct sap_address_t
69 struct sap_address_t *next;
75 char group[NI_MAXNUMERICHOST];
76 struct sockaddr_storage orig;
81 unsigned session_count;
85 /* The SAP handler, running in a separate thread */
94 #define SAP_MAX_BUFFER 65534
95 #define MIN_INTERVAL 2
96 #define MAX_INTERVAL 300
98 static void *RunThread (void *);
101 * Create the SAP handler
103 * \param p_announce a VLC object
104 * \return the newly created SAP handler or NULL on error
106 static sap_handler_t *SAP_Create (vlc_object_t *p_announce)
108 sap_handler_t *p_sap;
110 p_sap = vlc_custom_create (p_announce, sizeof (*p_sap), "sap sender");
114 vlc_mutex_init (&p_sap->lock);
119 static void SAP_Destroy (sap_handler_t *p_sap)
121 assert (p_sap->first == NULL);
122 vlc_mutex_destroy (&p_sap->lock);
123 vlc_object_release (p_sap);
126 static sap_address_t *AddressCreate (vlc_object_t *obj, const char *group)
128 int fd = net_ConnectUDP (obj, group, IPPORT_SAP, 255);
132 sap_address_t *addr = malloc (sizeof (*addr));
139 strlcpy (addr->group, group, sizeof (addr->group));
141 addr->origlen = sizeof (addr->orig);
142 getsockname (fd, (struct sockaddr *)&addr->orig, &addr->origlen);
144 addr->interval = var_CreateGetInteger (obj, "sap-interval");
145 vlc_mutex_init (&addr->lock);
146 vlc_cond_init (&addr->wait);
147 addr->session_count = 0;
150 if (vlc_clone (&addr->thread, RunThread, addr, VLC_THREAD_PRIORITY_LOW))
152 msg_Err (obj, "unable to spawn SAP announce thread");
160 static void AddressDestroy (sap_address_t *addr)
162 assert (addr->first == NULL);
164 vlc_cancel (addr->thread);
165 vlc_join (addr->thread, NULL);
166 vlc_cond_destroy (&addr->wait);
167 vlc_mutex_destroy (&addr->lock);
168 net_Close (addr->fd);
173 * main SAP handler thread
174 * \param p_this the SAP Handler object
178 static void *RunThread (void *self)
180 sap_address_t *addr = self;
182 vlc_mutex_lock (&addr->lock);
183 mutex_cleanup_push (&addr->lock);
187 sap_session_t *p_session;
190 while (addr->first == NULL)
191 vlc_cond_wait (&addr->wait, &addr->lock);
193 assert (addr->session_count > 0);
196 for (p_session = addr->first; p_session; p_session = p_session->next)
198 send (addr->fd, p_session->data, p_session->length, 0);
199 deadline += addr->interval * CLOCK_FREQ / addr->session_count;
201 if (vlc_cond_timedwait (&addr->wait, &addr->lock, deadline) == 0)
202 break; /* list may have changed! */
213 static int SAP_Add (sap_handler_t *p_sap, session_descriptor_t *p_session)
216 char psz_addr[NI_MAXNUMERICHOST];
217 sap_session_t *p_sap_session;
222 struct sockaddr_in in;
223 struct sockaddr_in6 in6;
227 addrlen = p_session->addrlen;
228 if ((addrlen == 0) || (addrlen > sizeof (addr)))
230 msg_Err( p_sap, "No/invalid address specified for SAP announce" );
234 /* Determine SAP multicast address automatically */
235 memcpy (&addr, &p_session->addr, addrlen);
237 switch (addr.a.sa_family)
239 #if defined (HAVE_INET_PTON) || defined (_WIN32)
242 /* See RFC3513 for list of valid IPv6 scopes */
243 struct in6_addr *a6 = &addr.in6.sin6_addr;
245 memcpy( a6->s6_addr + 2, "\x00\x00\x00\x00\x00\x00"
246 "\x00\x00\x00\x00\x00\x02\x7f\xfe", 14 );
247 if( IN6_IS_ADDR_MULTICAST( a6 ) )
248 /* force flags to zero, preserve scope */
249 a6->s6_addr[1] &= 0xf;
251 /* Unicast IPv6 - assume global scope */
252 memcpy( a6->s6_addr, "\xff\x0e", 2 );
259 /* See RFC2365 for IPv4 scopes */
260 uint32_t ipv4 = addr.in.sin_addr.s_addr;
262 /* 224.0.0.0/24 => 224.0.0.255 */
263 if ((ipv4 & htonl (0xffffff00)) == htonl (0xe0000000))
264 ipv4 = htonl (0xe00000ff);
266 /* 239.255.0.0/16 => 239.255.255.255 */
267 if ((ipv4 & htonl (0xffff0000)) == htonl (0xefff0000))
268 ipv4 = htonl (0xefffffff);
270 /* 239.192.0.0/14 => 239.195.255.255 */
271 if ((ipv4 & htonl (0xfffc0000)) == htonl (0xefc00000))
272 ipv4 = htonl (0xefc3ffff);
274 if ((ipv4 & htonl (0xff000000)) == htonl (0xef000000))
277 /* other addresses => 224.2.127.254 */
278 ipv4 = htonl (0xe0027ffe);
282 msg_Err( p_sap, "Out-of-scope multicast address "
283 "not supported by SAP" );
287 addr.in.sin_addr.s_addr = ipv4;
292 msg_Err( p_sap, "Address family %d not supported by SAP",
297 i = vlc_getnameinfo( &addr.a, addrlen,
298 psz_addr, sizeof( psz_addr ), NULL, NI_NUMERICHOST );
302 msg_Err( p_sap, "%s", gai_strerror( i ) );
306 /* Find/create SAP address thread */
307 msg_Dbg( p_sap, "using SAP address: %s", psz_addr);
309 vlc_mutex_lock (&p_sap->lock);
310 sap_address_t *sap_addr;
311 for (sap_addr = p_sap->first; sap_addr; sap_addr = sap_addr->next)
312 if (!strcmp (psz_addr, sap_addr->group))
315 if (sap_addr == NULL)
317 sap_addr = AddressCreate (VLC_OBJECT(p_sap), psz_addr);
318 if (sap_addr == NULL)
320 vlc_mutex_unlock (&p_sap->lock);
323 sap_addr->next = p_sap->first;
324 p_sap->first = sap_addr;
327 * NEVER take the global SAP lock when holding a SAP thread lock! */
328 vlc_mutex_lock (&sap_addr->lock);
329 vlc_mutex_unlock (&p_sap->lock);
331 memcpy (&p_session->orig, &sap_addr->orig, sap_addr->origlen);
332 p_session->origlen = sap_addr->origlen;
334 size_t headsize = 20, length;
335 switch (p_session->orig.ss_family)
349 /* XXX: Check for dupes */
350 length = headsize + strlen (p_session->psz_sdp);
351 p_sap_session = malloc (sizeof (*p_sap_session) + length + 1);
352 if (p_sap_session == NULL)
354 vlc_mutex_unlock (&sap_addr->lock);
355 return VLC_EGENERIC; /* NOTE: we should destroy the thread if left unused */
357 p_sap_session->next = sap_addr->first;
358 sap_addr->first = p_sap_session;
359 p_sap_session->p_sd = p_session;
360 p_sap_session->length = length;
362 /* Build the SAP Headers */
363 uint8_t *psz_head = p_sap_session->data;
365 /* SAPv1, not encrypted, not compressed */
367 psz_head[1] = 0x00; /* No authentication length */
370 psz_head[2] = i_hash >> 8; /* Msg id hash */
371 psz_head[3] = i_hash; /* Msg id hash 2 */
374 switch (p_session->orig.ss_family)
379 struct in6_addr *a6 =
380 &((struct sockaddr_in6 *)&p_session->orig)->sin6_addr;
381 memcpy (psz_head + headsize, a6, 16);
382 psz_head[0] |= 0x10; /* IPv6 flag */
390 (((struct sockaddr_in *)&p_session->orig)->sin_addr.s_addr);
391 memcpy (psz_head + headsize, &ipv4, 4);
398 memcpy (psz_head + headsize, "application/sdp", 16);
401 /* Build the final message */
402 strcpy( (char *)psz_head + headsize, p_session->psz_sdp);
404 sap_addr->session_count++;
405 vlc_cond_signal (&sap_addr->wait);
406 vlc_mutex_unlock (&sap_addr->lock);
411 * Remove a SAP Announce
413 static void SAP_Del (sap_handler_t *p_sap,
414 const session_descriptor_t *p_session)
416 vlc_mutex_lock (&p_sap->lock);
418 /* TODO: give a handle back in SAP_Add, and use that... */
419 sap_address_t *addr, **paddr;
420 sap_session_t *session, **psession;
422 paddr = &p_sap->first;
423 for (addr = p_sap->first; addr; addr = addr->next)
425 psession = &addr->first;
426 vlc_mutex_lock (&addr->lock);
427 for (session = addr->first; session; session = session->next)
429 if (session->p_sd == p_session)
431 psession = &session->next;
433 vlc_mutex_unlock (&addr->lock);
439 *psession = session->next;
441 if (addr->first == NULL)
442 /* Last session for this address -> unlink the address */
444 vlc_mutex_unlock (&p_sap->lock);
446 if (addr->first == NULL)
448 /* Last session for this address -> unlink the address */
449 vlc_mutex_unlock (&addr->lock);
450 AddressDestroy (addr);
454 addr->session_count--;
455 vlc_cond_signal (&addr->wait);
456 vlc_mutex_unlock (&addr->lock);
462 /****************************************************************************
463 * Sout-side functions
464 ****************************************************************************/
466 static void sap_destroy (vlc_object_t *p_this)
468 libvlc_priv (p_this->p_libvlc)->p_sap = NULL;
471 #undef sout_AnnounceRegisterSDP
473 static vlc_mutex_t sap_mutex = VLC_STATIC_MUTEX;
476 * Registers a new session with the announce handler, using a pregenerated SDP
478 * \param obj a VLC object
479 * \param psz_sdp the SDP to register
480 * \param psz_dst session address (needed for SAP address auto detection)
481 * \return the new session descriptor structure
483 session_descriptor_t *
484 sout_AnnounceRegisterSDP( vlc_object_t *obj, const char *psz_sdp,
485 const char *psz_dst )
487 session_descriptor_t *p_session = calloc( 1, sizeof (*p_session) );
491 p_session->psz_sdp = strdup( psz_sdp );
493 /* GRUIK. We should not convert back-and-forth from string to numbers */
494 struct addrinfo *res;
495 if (vlc_getaddrinfo (psz_dst, 0, NULL, &res) == 0)
497 if (res->ai_addrlen <= sizeof (p_session->addr))
498 memcpy (&p_session->addr, res->ai_addr,
499 p_session->addrlen = res->ai_addrlen);
503 vlc_mutex_lock (&sap_mutex);
504 sap_handler_t *p_sap = libvlc_priv (obj->p_libvlc)->p_sap;
507 p_sap = SAP_Create (VLC_OBJECT (obj->p_libvlc));
508 libvlc_priv (obj->p_libvlc)->p_sap = p_sap;
509 vlc_object_set_destructor ((vlc_object_t *)p_sap, sap_destroy);
512 vlc_object_hold ((vlc_object_t *)p_sap);
513 vlc_mutex_unlock (&sap_mutex);
518 msg_Dbg (obj, "adding SAP session");
519 if (SAP_Add (p_sap, p_session))
521 vlc_mutex_lock (&sap_mutex);
522 vlc_object_release ((vlc_object_t *)p_sap);
523 vlc_mutex_unlock (&sap_mutex);
530 free (p_session->psz_sdp);
535 #undef sout_AnnounceUnRegister
537 * Unregisters an existing session
539 * \param obj a VLC object
540 * \param p_session the session descriptor
541 * \return VLC_SUCCESS or an error
543 int sout_AnnounceUnRegister( vlc_object_t *obj,
544 session_descriptor_t *p_session )
546 sap_handler_t *p_sap = libvlc_priv (obj->p_libvlc)->p_sap;
548 msg_Dbg (obj, "removing SAP session");
549 SAP_Del (p_sap, p_session);
551 vlc_mutex_lock (&sap_mutex);
552 vlc_object_release ((vlc_object_t *)p_sap);
553 vlc_mutex_unlock (&sap_mutex);
555 free (p_session->psz_sdp);