libvlc_version.h: build-time version macros
[vlc/asuraparaju-public.git] / src / stream_output / sap.c
blob40e03e8bd603dac78c6bfc140c4d4c3a6f6f303d
1 /*****************************************************************************
2 * sap.c : SAP announce handler
3 *****************************************************************************
4 * Copyright (C) 2002-2008 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
35 #include <stdlib.h> /* free() */
36 #include <stdio.h> /* sprintf() */
37 #include <string.h>
38 #include <assert.h>
40 #include <vlc_sout.h>
41 #include <vlc_network.h>
43 #include "stream_output.h"
44 #include "libvlc.h"
46 /* SAP is always on that port */
47 #define IPPORT_SAP 9875
49 /* A SAP session descriptor, enqueued in the SAP handler queue */
50 typedef struct sap_session_t
52 struct sap_session_t *next;
53 const session_descriptor_t *p_sd;
54 size_t length;
55 uint8_t data[];
56 } sap_session_t;
58 /* A SAP announce address. For each of these, we run the
59 * control flow algorithm */
60 typedef struct sap_address_t
62 struct sap_address_t *next;
64 vlc_thread_t thread;
65 vlc_mutex_t lock;
66 vlc_cond_t wait;
68 char group[NI_MAXNUMERICHOST];
69 struct sockaddr_storage orig;
70 socklen_t origlen;
71 int fd;
72 unsigned interval;
74 unsigned session_count;
75 sap_session_t *first;
76 } sap_address_t;
78 /* The SAP handler, running in a separate thread */
79 struct sap_handler_t
81 VLC_COMMON_MEMBERS
83 vlc_mutex_t lock;
84 sap_address_t *first;
87 #define SAP_MAX_BUFFER 65534
88 #define MIN_INTERVAL 2
89 #define MAX_INTERVAL 300
91 /*****************************************************************************
92 * Local prototypes
93 *****************************************************************************/
94 static void *RunThread (void *);
96 /**
97 * Create the SAP handler
99 * \param p_announce a VLC object
100 * \return the newly created SAP handler or NULL on error
102 sap_handler_t *SAP_Create (vlc_object_t *p_announce)
104 sap_handler_t *p_sap;
106 p_sap = vlc_custom_create (p_announce, sizeof (*p_sap),
107 VLC_OBJECT_GENERIC, "sap sender");
108 if (p_sap == NULL)
109 return NULL;
111 vlc_mutex_init (&p_sap->lock);
112 p_sap->first = NULL;
113 return p_sap;
116 void SAP_Destroy (sap_handler_t *p_sap)
118 assert (p_sap->first == NULL);
119 vlc_mutex_destroy (&p_sap->lock);
120 vlc_object_release (p_sap);
123 static sap_address_t *AddressCreate (vlc_object_t *obj, const char *group)
125 int fd = net_ConnectUDP (obj, group, IPPORT_SAP, 255);
126 if (fd == -1)
127 return NULL;
129 sap_address_t *addr = malloc (sizeof (*addr));
130 if (addr == NULL)
132 net_Close (fd);
133 return NULL;
136 strlcpy (addr->group, group, sizeof (addr->group));
137 addr->fd = fd;
138 addr->origlen = sizeof (addr->orig);
139 getsockname (fd, (struct sockaddr *)&addr->orig, &addr->origlen);
141 addr->interval = var_CreateGetInteger (obj, "sap-interval");
142 vlc_mutex_init (&addr->lock);
143 vlc_cond_init (&addr->wait);
144 addr->session_count = 0;
145 addr->first = NULL;
147 if (vlc_clone (&addr->thread, RunThread, addr, VLC_THREAD_PRIORITY_LOW))
149 msg_Err (obj, "unable to spawn SAP announce thread");
150 net_Close (fd);
151 free (addr);
152 return NULL;
154 return addr;
157 static void AddressDestroy (sap_address_t *addr)
159 assert (addr->first == NULL);
161 vlc_cancel (addr->thread);
162 vlc_join (addr->thread, NULL);
163 vlc_cond_destroy (&addr->wait);
164 vlc_mutex_destroy (&addr->lock);
165 net_Close (addr->fd);
166 free (addr);
170 * main SAP handler thread
171 * \param p_this the SAP Handler object
172 * \return nothing
174 static void *RunThread (void *self)
176 sap_address_t *addr = self;
178 vlc_mutex_lock (&addr->lock);
179 mutex_cleanup_push (&addr->lock);
181 for (;;)
183 sap_session_t *p_session;
184 mtime_t deadline;
186 while (addr->first == NULL)
187 vlc_cond_wait (&addr->wait, &addr->lock);
189 assert (addr->session_count > 0);
191 deadline = mdate ();
192 for (p_session = addr->first; p_session; p_session = p_session->next)
194 send (addr->fd, p_session->data, p_session->length, 0);
195 deadline += addr->interval * CLOCK_FREQ / addr->session_count;
197 if (vlc_cond_timedwait (&addr->wait, &addr->lock, deadline) == 0)
198 break; /* list may have changed! */
202 vlc_cleanup_pop ();
203 assert (0);
207 * Add a SAP announce
209 int SAP_Add (sap_handler_t *p_sap, session_descriptor_t *p_session)
211 int i;
212 char psz_addr[NI_MAXNUMERICHOST];
213 bool b_ipv6 = false, b_ssm = false;
214 sap_session_t *p_sap_session;
215 mtime_t i_hash;
216 union
218 struct sockaddr a;
219 struct sockaddr_in in;
220 struct sockaddr_in6 in6;
221 } addr;
222 socklen_t addrlen;
224 addrlen = p_session->addrlen;
225 if ((addrlen == 0) || (addrlen > sizeof (addr)))
227 msg_Err( p_sap, "No/invalid address specified for SAP announce" );
228 return VLC_EGENERIC;
231 /* Determine SAP multicast address automatically */
232 memcpy (&addr, &p_session->addr, addrlen);
234 switch (addr.a.sa_family)
236 #if defined (HAVE_INET_PTON) || defined (WIN32)
237 case AF_INET6:
239 /* See RFC3513 for list of valid IPv6 scopes */
240 struct in6_addr *a6 = &addr.in6.sin6_addr;
242 memcpy( a6->s6_addr + 2, "\x00\x00\x00\x00\x00\x00"
243 "\x00\x00\x00\x00\x00\x02\x7f\xfe", 14 );
244 if( IN6_IS_ADDR_MULTICAST( a6 ) )
246 /* SSM <=> ff3x::/32 */
247 b_ssm = (U32_AT (a6->s6_addr) & 0xfff0ffff) == 0xff300000;
249 /* force flags to zero, preserve scope */
250 a6->s6_addr[1] &= 0xf;
252 else
253 /* Unicast IPv6 - assume global scope */
254 memcpy( a6->s6_addr, "\xff\x0e", 2 );
256 b_ipv6 = true;
257 break;
259 #endif
261 case AF_INET:
263 /* See RFC2365 for IPv4 scopes */
264 uint32_t ipv4 = addr.in.sin_addr.s_addr;
266 /* 224.0.0.0/24 => 224.0.0.255 */
267 if ((ipv4 & htonl (0xffffff00)) == htonl (0xe0000000))
268 ipv4 = htonl (0xe00000ff);
269 else
270 /* 239.255.0.0/16 => 239.255.255.255 */
271 if ((ipv4 & htonl (0xffff0000)) == htonl (0xefff0000))
272 ipv4 = htonl (0xefffffff);
273 else
274 /* 239.192.0.0/14 => 239.195.255.255 */
275 if ((ipv4 & htonl (0xfffc0000)) == htonl (0xefc00000))
276 ipv4 = htonl (0xefc3ffff);
277 else
278 if ((ipv4 & htonl (0xff000000)) == htonl (0xef000000))
279 ipv4 = 0;
280 else
281 /* other addresses => 224.2.127.254 */
283 /* SSM: 232.0.0.0/8 */
284 b_ssm = (ipv4 & htonl (255 << 24)) == htonl (232 << 24);
285 ipv4 = htonl (0xe0027ffe);
288 if( ipv4 == 0 )
290 msg_Err( p_sap, "Out-of-scope multicast address "
291 "not supported by SAP" );
292 return VLC_EGENERIC;
295 addr.in.sin_addr.s_addr = ipv4;
296 break;
299 default:
300 msg_Err( p_sap, "Address family %d not supported by SAP",
301 addr.a.sa_family );
302 return VLC_EGENERIC;
305 i = vlc_getnameinfo( &addr.a, addrlen,
306 psz_addr, sizeof( psz_addr ), NULL, NI_NUMERICHOST );
308 if( i )
310 msg_Err( p_sap, "%s", gai_strerror( i ) );
311 return VLC_EGENERIC;
314 /* Find/create SAP address thread */
315 msg_Dbg( p_sap, "using SAP address: %s", psz_addr);
317 vlc_mutex_lock (&p_sap->lock);
318 sap_address_t *sap_addr;
319 for (sap_addr = p_sap->first; sap_addr; sap_addr = sap_addr->next)
320 if (!strcmp (psz_addr, sap_addr->group))
321 break;
323 if (sap_addr == NULL)
325 sap_addr = AddressCreate (VLC_OBJECT(p_sap), psz_addr);
326 if (sap_addr == NULL)
328 vlc_mutex_unlock (&p_sap->lock);
329 return VLC_EGENERIC;
331 sap_addr->next = p_sap->first;
332 p_sap->first = sap_addr;
334 /* Switch locks.
335 * NEVER take the global SAP lock when holding a SAP thread lock! */
336 vlc_mutex_lock (&sap_addr->lock);
337 vlc_mutex_unlock (&p_sap->lock);
339 memcpy (&p_session->orig, &sap_addr->orig, sap_addr->origlen);
340 p_session->origlen = sap_addr->origlen;
342 size_t headsize = 20, length;
343 switch (p_session->orig.ss_family)
345 #ifdef AF_INET6
346 case AF_INET6:
347 headsize += 16;
348 break;
349 #endif
350 case AF_INET:
351 headsize += 4;
352 break;
353 default:
354 assert (0);
357 /* XXX: Check for dupes */
358 length = headsize + strlen (p_session->psz_sdp);
359 p_sap_session = malloc (sizeof (*p_sap_session) + length + 1);
360 if (p_sap_session == NULL)
362 vlc_mutex_unlock (&sap_addr->lock);
363 return VLC_EGENERIC; /* NOTE: we should destroy the thread if left unused */
365 p_sap_session->next = sap_addr->first;
366 sap_addr->first = p_sap_session;
367 p_sap_session->p_sd = p_session;
368 p_sap_session->length = length;
370 /* Build the SAP Headers */
371 uint8_t *psz_head = p_sap_session->data;
373 /* SAPv1, not encrypted, not compressed */
374 psz_head[0] = 0x20;
375 psz_head[1] = 0x00; /* No authentication length */
377 i_hash = mdate();
378 psz_head[2] = i_hash >> 8; /* Msg id hash */
379 psz_head[3] = i_hash; /* Msg id hash 2 */
381 headsize = 4;
382 switch (p_session->orig.ss_family)
384 #ifdef AF_INET6
385 case AF_INET6:
387 struct in6_addr *a6 =
388 &((struct sockaddr_in6 *)&p_session->orig)->sin6_addr;
389 memcpy (psz_head + headsize, a6, 16);
390 psz_head[0] |= 0x10; /* IPv6 flag */
391 headsize += 16;
392 break;
394 #endif
395 case AF_INET:
397 uint32_t ipv4 =
398 (((struct sockaddr_in *)&p_session->orig)->sin_addr.s_addr);
399 memcpy (psz_head + headsize, &ipv4, 4);
400 headsize += 4;
401 break;
406 memcpy (psz_head + headsize, "application/sdp", 16);
407 headsize += 16;
409 /* Build the final message */
410 strcpy( (char *)psz_head + headsize, p_session->psz_sdp);
412 sap_addr->session_count++;
413 vlc_cond_signal (&sap_addr->wait);
414 vlc_mutex_unlock (&sap_addr->lock);
415 return VLC_SUCCESS;
419 * Remove a SAP Announce
421 void SAP_Del (sap_handler_t *p_sap, const session_descriptor_t *p_session)
423 vlc_mutex_lock (&p_sap->lock);
425 /* TODO: give a handle back in SAP_Add, and use that... */
426 sap_address_t *addr, **paddr;
427 sap_session_t *session, **psession;
429 paddr = &p_sap->first;
430 for (addr = p_sap->first; addr; addr = addr->next)
432 psession = &addr->first;
433 vlc_mutex_lock (&addr->lock);
434 for (session = addr->first; session; session = session->next)
436 if (session->p_sd == p_session)
437 goto found;
438 psession = &session->next;
440 vlc_mutex_unlock (&addr->lock);
441 paddr = &addr->next;
443 assert (0);
445 found:
446 *psession = session->next;
448 if (addr->first == NULL)
449 /* Last session for this address -> unlink the address */
450 *paddr = addr->next;
451 vlc_mutex_unlock (&p_sap->lock);
453 if (addr->first == NULL)
455 /* Last session for this address -> unlink the address */
456 vlc_mutex_unlock (&addr->lock);
457 AddressDestroy (addr);
459 else
461 addr->session_count--;
462 vlc_cond_signal (&addr->wait);
463 vlc_mutex_unlock (&addr->lock);
466 free (session);