]> git.sesse.net Git - vlc/blob - src/stream_output/sap.c
181a423aa5bdf94e1cdde0df34922a184216a8fd
[vlc] / src / stream_output / sap.c
1 /*****************************************************************************
2  * sap.c : SAP announce handler
3  *****************************************************************************
4  * Copyright (C) 2002-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Rémi Denis-Courmont <rem # videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34
35 #include <stdlib.h>                                                /* free() */
36 #include <stdio.h>                                              /* sprintf() */
37 #include <string.h>
38 #include <assert.h>
39
40 #include <vlc_sout.h>
41 #include <vlc_network.h>
42
43 #include "stream_output.h"
44 #include "libvlc.h"
45
46 /* SAP is always on that port */
47 #define IPPORT_SAP 9875
48
49 /* A SAP session descriptor, enqueued in the SAP handler queue */
50 typedef struct sap_session_t
51 {
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;
57
58 /* A SAP announce address. For each of these, we run the
59  * control flow algorithm */
60 typedef struct sap_address_t
61 {
62     struct sap_address_t   *next;
63
64     vlc_thread_t            thread;
65     vlc_mutex_t             lock;
66     vlc_cond_t              wait;
67
68     char                    group[NI_MAXNUMERICHOST];
69     struct sockaddr_storage orig;
70     socklen_t               origlen;
71     int                     fd;
72     unsigned                interval;
73
74     unsigned                session_count;
75     sap_session_t          *first;
76 } sap_address_t;
77
78 /* The SAP handler, running in a separate thread */
79 struct sap_handler_t
80 {
81     VLC_COMMON_MEMBERS
82
83     vlc_mutex_t    lock;
84     sap_address_t *first;
85 };
86
87 #define SAP_MAX_BUFFER 65534
88 #define MIN_INTERVAL 2
89 #define MAX_INTERVAL 300
90
91 /*****************************************************************************
92  * Local prototypes
93  *****************************************************************************/
94 static void *RunThread (void *);
95
96 /**
97  * Create the SAP handler
98  *
99  * \param p_announce a VLC object
100  * \return the newly created SAP handler or NULL on error
101  */
102 sap_handler_t *SAP_Create (vlc_object_t *p_announce)
103 {
104     sap_handler_t *p_sap;
105
106     p_sap = vlc_custom_create (p_announce, sizeof (*p_sap), "sap sender");
107     if (p_sap == NULL)
108         return NULL;
109
110     vlc_mutex_init (&p_sap->lock);
111     p_sap->first = NULL;
112     return p_sap;
113 }
114
115 void SAP_Destroy (sap_handler_t *p_sap)
116 {
117     assert (p_sap->first == NULL);
118     vlc_mutex_destroy (&p_sap->lock);
119     vlc_object_release (p_sap);
120 }
121
122 static sap_address_t *AddressCreate (vlc_object_t *obj, const char *group)
123 {
124     int fd = net_ConnectUDP (obj, group, IPPORT_SAP, 255);
125     if (fd == -1)
126         return NULL;
127
128     sap_address_t *addr = malloc (sizeof (*addr));
129     if (addr == NULL)
130     {
131         net_Close (fd);
132         return NULL;
133     }
134
135     strlcpy (addr->group, group, sizeof (addr->group));
136     addr->fd = fd;
137     addr->origlen = sizeof (addr->orig);
138     getsockname (fd, (struct sockaddr *)&addr->orig, &addr->origlen);
139
140     addr->interval = var_CreateGetInteger (obj, "sap-interval");
141     vlc_mutex_init (&addr->lock);
142     vlc_cond_init (&addr->wait);
143     addr->session_count = 0;
144     addr->first = NULL;
145
146     if (vlc_clone (&addr->thread, RunThread, addr, VLC_THREAD_PRIORITY_LOW))
147     {
148         msg_Err (obj, "unable to spawn SAP announce thread");
149         net_Close (fd);
150         free (addr);
151         return NULL;
152     }
153     return addr;
154 }
155
156 static void AddressDestroy (sap_address_t *addr)
157 {
158     assert (addr->first == NULL);
159
160     vlc_cancel (addr->thread);
161     vlc_join (addr->thread, NULL);
162     vlc_cond_destroy (&addr->wait);
163     vlc_mutex_destroy (&addr->lock);
164     net_Close (addr->fd);
165     free (addr);
166 }
167
168 /**
169  * main SAP handler thread
170  * \param p_this the SAP Handler object
171  * \return nothing
172  */
173 VLC_NORETURN
174 static void *RunThread (void *self)
175 {
176     sap_address_t *addr = self;
177
178     vlc_mutex_lock (&addr->lock);
179     mutex_cleanup_push (&addr->lock);
180
181     for (;;)
182     {
183         sap_session_t *p_session;
184         mtime_t deadline;
185
186         while (addr->first == NULL)
187             vlc_cond_wait (&addr->wait, &addr->lock);
188
189         assert (addr->session_count > 0);
190
191         deadline = mdate ();
192         for (p_session = addr->first; p_session; p_session = p_session->next)
193         {
194             send (addr->fd, p_session->data, p_session->length, 0);
195             deadline += addr->interval * CLOCK_FREQ / addr->session_count;
196
197             if (vlc_cond_timedwait (&addr->wait, &addr->lock, deadline) == 0)
198                 break; /* list may have changed! */
199         }
200     }
201
202     vlc_cleanup_pop ();
203     assert (0);
204 }
205
206 /**
207  * Add a SAP announce
208  */
209 int SAP_Add (sap_handler_t *p_sap, session_descriptor_t *p_session)
210 {
211     int i;
212     char psz_addr[NI_MAXNUMERICHOST];
213     sap_session_t *p_sap_session;
214     mtime_t i_hash;
215     union
216     {
217         struct sockaddr     a;
218         struct sockaddr_in  in;
219         struct sockaddr_in6 in6;
220     } addr;
221     socklen_t addrlen;
222
223     addrlen = p_session->addrlen;
224     if ((addrlen == 0) || (addrlen > sizeof (addr)))
225     {
226         msg_Err( p_sap, "No/invalid address specified for SAP announce" );
227         return VLC_EGENERIC;
228     }
229
230     /* Determine SAP multicast address automatically */
231     memcpy (&addr, &p_session->addr, addrlen);
232
233     switch (addr.a.sa_family)
234     {
235 #if defined (HAVE_INET_PTON) || defined (WIN32)
236         case AF_INET6:
237         {
238             /* See RFC3513 for list of valid IPv6 scopes */
239             struct in6_addr *a6 = &addr.in6.sin6_addr;
240
241             memcpy( a6->s6_addr + 2, "\x00\x00\x00\x00\x00\x00"
242                    "\x00\x00\x00\x00\x00\x02\x7f\xfe", 14 );
243             if( IN6_IS_ADDR_MULTICAST( a6 ) )
244                 /* force flags to zero, preserve scope */
245                 a6->s6_addr[1] &= 0xf;
246             else
247                 /* Unicast IPv6 - assume global scope */
248                 memcpy( a6->s6_addr, "\xff\x0e", 2 );
249             break;
250         }
251 #endif
252
253         case AF_INET:
254         {
255             /* See RFC2365 for IPv4 scopes */
256             uint32_t ipv4 = addr.in.sin_addr.s_addr;
257
258             /* 224.0.0.0/24 => 224.0.0.255 */
259             if ((ipv4 & htonl (0xffffff00)) == htonl (0xe0000000))
260                 ipv4 =  htonl (0xe00000ff);
261             else
262             /* 239.255.0.0/16 => 239.255.255.255 */
263             if ((ipv4 & htonl (0xffff0000)) == htonl (0xefff0000))
264                 ipv4 =  htonl (0xefffffff);
265             else
266             /* 239.192.0.0/14 => 239.195.255.255 */
267             if ((ipv4 & htonl (0xfffc0000)) == htonl (0xefc00000))
268                 ipv4 =  htonl (0xefc3ffff);
269             else
270             if ((ipv4 & htonl (0xff000000)) == htonl (0xef000000))
271                 ipv4 = 0;
272             else
273             /* other addresses => 224.2.127.254 */
274                 ipv4 = htonl (0xe0027ffe);
275
276             if( ipv4 == 0 )
277             {
278                 msg_Err( p_sap, "Out-of-scope multicast address "
279                          "not supported by SAP" );
280                 return VLC_EGENERIC;
281             }
282
283             addr.in.sin_addr.s_addr = ipv4;
284             break;
285         }
286
287         default:
288             msg_Err( p_sap, "Address family %d not supported by SAP",
289                      addr.a.sa_family );
290             return VLC_EGENERIC;
291     }
292
293     i = vlc_getnameinfo( &addr.a, addrlen,
294                          psz_addr, sizeof( psz_addr ), NULL, NI_NUMERICHOST );
295
296     if( i )
297     {
298         msg_Err( p_sap, "%s", gai_strerror( i ) );
299         return VLC_EGENERIC;
300     }
301
302     /* Find/create SAP address thread */
303     msg_Dbg( p_sap, "using SAP address: %s", psz_addr);
304
305     vlc_mutex_lock (&p_sap->lock);
306     sap_address_t *sap_addr;
307     for (sap_addr = p_sap->first; sap_addr; sap_addr = sap_addr->next)
308         if (!strcmp (psz_addr, sap_addr->group))
309             break;
310
311     if (sap_addr == NULL)
312     {
313         sap_addr = AddressCreate (VLC_OBJECT(p_sap), psz_addr);
314         if (sap_addr == NULL)
315         {
316             vlc_mutex_unlock (&p_sap->lock);
317             return VLC_EGENERIC;
318         }
319         sap_addr->next = p_sap->first;
320         p_sap->first = sap_addr;
321     }
322     /* Switch locks.
323      * NEVER take the global SAP lock when holding a SAP thread lock! */
324     vlc_mutex_lock (&sap_addr->lock);
325     vlc_mutex_unlock (&p_sap->lock);
326
327     memcpy (&p_session->orig, &sap_addr->orig, sap_addr->origlen);
328     p_session->origlen = sap_addr->origlen;
329
330     size_t headsize = 20, length;
331     switch (p_session->orig.ss_family)
332     {
333 #ifdef AF_INET6
334         case AF_INET6:
335             headsize += 16;
336             break;
337 #endif
338         case AF_INET:
339             headsize += 4;
340             break;
341         default:
342             assert (0);
343     }
344
345     /* XXX: Check for dupes */
346     length = headsize + strlen (p_session->psz_sdp);
347     p_sap_session = malloc (sizeof (*p_sap_session) + length + 1);
348     if (p_sap_session == NULL)
349     {
350         vlc_mutex_unlock (&sap_addr->lock);
351         return VLC_EGENERIC; /* NOTE: we should destroy the thread if left unused */
352     }
353     p_sap_session->next = sap_addr->first;
354     sap_addr->first = p_sap_session;
355     p_sap_session->p_sd = p_session;
356     p_sap_session->length = length;
357
358     /* Build the SAP Headers */
359     uint8_t *psz_head = p_sap_session->data;
360
361     /* SAPv1, not encrypted, not compressed */
362     psz_head[0] = 0x20;
363     psz_head[1] = 0x00; /* No authentication length */
364
365     i_hash = mdate();
366     psz_head[2] = i_hash >> 8; /* Msg id hash */
367     psz_head[3] = i_hash;      /* Msg id hash 2 */
368
369     headsize = 4;
370     switch (p_session->orig.ss_family)
371     {
372 #ifdef AF_INET6
373         case AF_INET6:
374         {
375             struct in6_addr *a6 =
376                 &((struct sockaddr_in6 *)&p_session->orig)->sin6_addr;
377             memcpy (psz_head + headsize, a6, 16);
378             psz_head[0] |= 0x10; /* IPv6 flag */
379             headsize += 16;
380             break;
381         }
382 #endif
383         case AF_INET:
384         {
385             uint32_t ipv4 =
386                 (((struct sockaddr_in *)&p_session->orig)->sin_addr.s_addr);
387             memcpy (psz_head + headsize, &ipv4, 4);
388             headsize += 4;
389             break;
390         }
391
392     }
393
394     memcpy (psz_head + headsize, "application/sdp", 16);
395     headsize += 16;
396
397     /* Build the final message */
398     strcpy( (char *)psz_head + headsize, p_session->psz_sdp);
399
400     sap_addr->session_count++;
401     vlc_cond_signal (&sap_addr->wait);
402     vlc_mutex_unlock (&sap_addr->lock);
403     return VLC_SUCCESS;
404 }
405
406 /**
407  * Remove a SAP Announce
408  */
409 void SAP_Del (sap_handler_t *p_sap, const session_descriptor_t *p_session)
410 {
411     vlc_mutex_lock (&p_sap->lock);
412
413     /* TODO: give a handle back in SAP_Add, and use that... */
414     sap_address_t *addr, **paddr;
415     sap_session_t *session, **psession;
416
417     paddr = &p_sap->first;
418     for (addr = p_sap->first; addr; addr = addr->next)
419     {
420         psession = &addr->first;
421         vlc_mutex_lock (&addr->lock);
422         for (session = addr->first; session; session = session->next)
423         {
424             if (session->p_sd == p_session)
425                 goto found;
426             psession = &session->next;
427         }
428         vlc_mutex_unlock (&addr->lock);
429         paddr = &addr->next;
430     }
431     assert (0);
432
433 found:
434     *psession = session->next;
435
436     if (addr->first == NULL)
437         /* Last session for this address -> unlink the address */
438         *paddr = addr->next;
439     vlc_mutex_unlock (&p_sap->lock);
440
441     if (addr->first == NULL)
442     {
443         /* Last session for this address -> unlink the address */
444         vlc_mutex_unlock (&addr->lock);
445         AddressDestroy (addr);
446     }
447     else
448     {
449         addr->session_count--;
450         vlc_cond_signal (&addr->wait);
451         vlc_mutex_unlock (&addr->lock);
452     }
453
454     free (session);
455 }