]> git.sesse.net Git - vlc/blob - src/stream_output/sap.c
quartztext: fixed [56310d5c1b8ecc0869046f33cadae28b00d2d9ca]
[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),
107                                VLC_OBJECT_GENERIC, "sap sender");
108     if (p_sap == NULL)
109         return NULL;
110
111     vlc_object_attach( p_sap, p_announce );
112     vlc_mutex_init (&p_sap->lock);
113     p_sap->first = NULL;
114     return p_sap;
115 }
116
117 void SAP_Destroy (sap_handler_t *p_sap)
118 {
119     assert (p_sap->first == NULL);
120     vlc_mutex_destroy (&p_sap->lock);
121     vlc_object_release (p_sap);
122 }
123
124 static sap_address_t *AddressCreate (vlc_object_t *obj, const char *group)
125 {
126     int fd = net_ConnectUDP (obj, group, IPPORT_SAP, 255);
127     if (fd == -1)
128         return NULL;
129
130     sap_address_t *addr = malloc (sizeof (*addr));
131     if (addr == NULL)
132     {
133         net_Close (fd);
134         return NULL;
135     }
136
137     strlcpy (addr->group, group, sizeof (addr->group));
138     addr->fd = fd;
139     addr->origlen = sizeof (addr->orig);
140     getsockname (fd, (struct sockaddr *)&addr->orig, &addr->origlen);
141
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;
146     addr->first = NULL;
147
148     if (vlc_clone (&addr->thread, RunThread, addr, VLC_THREAD_PRIORITY_LOW))
149     {
150         msg_Err (obj, "unable to spawn SAP announce thread");
151         net_Close (fd);
152         free (addr);
153         return NULL;
154     }
155     return addr;
156 }
157
158 static void AddressDestroy (sap_address_t *addr)
159 {
160     assert (addr->first == NULL);
161
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);
167     free (addr);
168 }
169
170 /**
171  * main SAP handler thread
172  * \param p_this the SAP Handler object
173  * \return nothing
174  */
175 static void *RunThread (void *self)
176 {
177     sap_address_t *addr = self;
178
179     vlc_mutex_lock (&addr->lock);
180     mutex_cleanup_push (&addr->lock);
181
182     for (;;)
183     {
184         sap_session_t *p_session;
185         mtime_t deadline;
186
187         while (addr->first == NULL)
188             vlc_cond_wait (&addr->wait, &addr->lock);
189
190         assert (addr->session_count > 0);
191
192         deadline = mdate ();
193         for (p_session = addr->first; p_session; p_session = p_session->next)
194         {
195             send (addr->fd, p_session->data, p_session->length, 0);
196             deadline += addr->interval * CLOCK_FREQ / addr->session_count;
197
198             if (vlc_cond_timedwait (&addr->wait, &addr->lock, deadline) == 0)
199                 break; /* list may have changed! */
200         }
201     }
202
203     vlc_cleanup_pop ();
204     assert (0);
205 }
206
207 /**
208  * Add a SAP announce
209  */
210 int SAP_Add (sap_handler_t *p_sap, session_descriptor_t *p_session)
211 {
212     int i;
213     char psz_addr[NI_MAXNUMERICHOST];
214     sap_session_t *p_sap_session;
215     mtime_t i_hash;
216     union
217     {
218         struct sockaddr     a;
219         struct sockaddr_in  in;
220         struct sockaddr_in6 in6;
221     } addr;
222     socklen_t addrlen;
223
224     addrlen = p_session->addrlen;
225     if ((addrlen == 0) || (addrlen > sizeof (addr)))
226     {
227         msg_Err( p_sap, "No/invalid address specified for SAP announce" );
228         return VLC_EGENERIC;
229     }
230
231     /* Determine SAP multicast address automatically */
232     memcpy (&addr, &p_session->addr, addrlen);
233
234     switch (addr.a.sa_family)
235     {
236 #if defined (HAVE_INET_PTON) || defined (WIN32)
237         case AF_INET6:
238         {
239             /* See RFC3513 for list of valid IPv6 scopes */
240             struct in6_addr *a6 = &addr.in6.sin6_addr;
241
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 ) )
245                 /* force flags to zero, preserve scope */
246                 a6->s6_addr[1] &= 0xf;
247             else
248                 /* Unicast IPv6 - assume global scope */
249                 memcpy( a6->s6_addr, "\xff\x0e", 2 );
250             break;
251         }
252 #endif
253
254         case AF_INET:
255         {
256             /* See RFC2365 for IPv4 scopes */
257             uint32_t ipv4 = addr.in.sin_addr.s_addr;
258
259             /* 224.0.0.0/24 => 224.0.0.255 */
260             if ((ipv4 & htonl (0xffffff00)) == htonl (0xe0000000))
261                 ipv4 =  htonl (0xe00000ff);
262             else
263             /* 239.255.0.0/16 => 239.255.255.255 */
264             if ((ipv4 & htonl (0xffff0000)) == htonl (0xefff0000))
265                 ipv4 =  htonl (0xefffffff);
266             else
267             /* 239.192.0.0/14 => 239.195.255.255 */
268             if ((ipv4 & htonl (0xfffc0000)) == htonl (0xefc00000))
269                 ipv4 =  htonl (0xefc3ffff);
270             else
271             if ((ipv4 & htonl (0xff000000)) == htonl (0xef000000))
272                 ipv4 = 0;
273             else
274             /* other addresses => 224.2.127.254 */
275                 ipv4 = htonl (0xe0027ffe);
276
277             if( ipv4 == 0 )
278             {
279                 msg_Err( p_sap, "Out-of-scope multicast address "
280                          "not supported by SAP" );
281                 return VLC_EGENERIC;
282             }
283
284             addr.in.sin_addr.s_addr = ipv4;
285             break;
286         }
287
288         default:
289             msg_Err( p_sap, "Address family %d not supported by SAP",
290                      addr.a.sa_family );
291             return VLC_EGENERIC;
292     }
293
294     i = vlc_getnameinfo( &addr.a, addrlen,
295                          psz_addr, sizeof( psz_addr ), NULL, NI_NUMERICHOST );
296
297     if( i )
298     {
299         msg_Err( p_sap, "%s", gai_strerror( i ) );
300         return VLC_EGENERIC;
301     }
302
303     /* Find/create SAP address thread */
304     msg_Dbg( p_sap, "using SAP address: %s", psz_addr);
305
306     vlc_mutex_lock (&p_sap->lock);
307     sap_address_t *sap_addr;
308     for (sap_addr = p_sap->first; sap_addr; sap_addr = sap_addr->next)
309         if (!strcmp (psz_addr, sap_addr->group))
310             break;
311
312     if (sap_addr == NULL)
313     {
314         sap_addr = AddressCreate (VLC_OBJECT(p_sap), psz_addr);
315         if (sap_addr == NULL)
316         {
317             vlc_mutex_unlock (&p_sap->lock);
318             return VLC_EGENERIC;
319         }
320         sap_addr->next = p_sap->first;
321         p_sap->first = sap_addr;
322     }
323     /* Switch locks.
324      * NEVER take the global SAP lock when holding a SAP thread lock! */
325     vlc_mutex_lock (&sap_addr->lock);
326     vlc_mutex_unlock (&p_sap->lock);
327
328     memcpy (&p_session->orig, &sap_addr->orig, sap_addr->origlen);
329     p_session->origlen = sap_addr->origlen;
330
331     size_t headsize = 20, length;
332     switch (p_session->orig.ss_family)
333     {
334 #ifdef AF_INET6
335         case AF_INET6:
336             headsize += 16;
337             break;
338 #endif
339         case AF_INET:
340             headsize += 4;
341             break;
342         default:
343             assert (0);
344     }
345
346     /* XXX: Check for dupes */
347     length = headsize + strlen (p_session->psz_sdp);
348     p_sap_session = malloc (sizeof (*p_sap_session) + length + 1);
349     if (p_sap_session == NULL)
350     {
351         vlc_mutex_unlock (&sap_addr->lock);
352         return VLC_EGENERIC; /* NOTE: we should destroy the thread if left unused */
353     }
354     p_sap_session->next = sap_addr->first;
355     sap_addr->first = p_sap_session;
356     p_sap_session->p_sd = p_session;
357     p_sap_session->length = length;
358
359     /* Build the SAP Headers */
360     uint8_t *psz_head = p_sap_session->data;
361
362     /* SAPv1, not encrypted, not compressed */
363     psz_head[0] = 0x20;
364     psz_head[1] = 0x00; /* No authentication length */
365
366     i_hash = mdate();
367     psz_head[2] = i_hash >> 8; /* Msg id hash */
368     psz_head[3] = i_hash;      /* Msg id hash 2 */
369
370     headsize = 4;
371     switch (p_session->orig.ss_family)
372     {
373 #ifdef AF_INET6
374         case AF_INET6:
375         {
376             struct in6_addr *a6 =
377                 &((struct sockaddr_in6 *)&p_session->orig)->sin6_addr;
378             memcpy (psz_head + headsize, a6, 16);
379             psz_head[0] |= 0x10; /* IPv6 flag */
380             headsize += 16;
381             break;
382         }
383 #endif
384         case AF_INET:
385         {
386             uint32_t ipv4 =
387                 (((struct sockaddr_in *)&p_session->orig)->sin_addr.s_addr);
388             memcpy (psz_head + headsize, &ipv4, 4);
389             headsize += 4;
390             break;
391         }
392
393     }
394
395     memcpy (psz_head + headsize, "application/sdp", 16);
396     headsize += 16;
397
398     /* Build the final message */
399     strcpy( (char *)psz_head + headsize, p_session->psz_sdp);
400
401     sap_addr->session_count++;
402     vlc_cond_signal (&sap_addr->wait);
403     vlc_mutex_unlock (&sap_addr->lock);
404     return VLC_SUCCESS;
405 }
406
407 /**
408  * Remove a SAP Announce
409  */
410 void SAP_Del (sap_handler_t *p_sap, const session_descriptor_t *p_session)
411 {
412     vlc_mutex_lock (&p_sap->lock);
413
414     /* TODO: give a handle back in SAP_Add, and use that... */
415     sap_address_t *addr, **paddr;
416     sap_session_t *session, **psession;
417
418     paddr = &p_sap->first;
419     for (addr = p_sap->first; addr; addr = addr->next)
420     {
421         psession = &addr->first;
422         vlc_mutex_lock (&addr->lock);
423         for (session = addr->first; session; session = session->next)
424         {
425             if (session->p_sd == p_session)
426                 goto found;
427             psession = &session->next;
428         }
429         vlc_mutex_unlock (&addr->lock);
430         paddr = &addr->next;
431     }
432     assert (0);
433
434 found:
435     *psession = session->next;
436
437     if (addr->first == NULL)
438         /* Last session for this address -> unlink the address */
439         *paddr = addr->next;
440     vlc_mutex_unlock (&p_sap->lock);
441
442     if (addr->first == NULL)
443     {
444         /* Last session for this address -> unlink the address */
445         vlc_mutex_unlock (&addr->lock);
446         AddressDestroy (addr);
447     }
448     else
449     {
450         addr->session_count--;
451         vlc_cond_signal (&addr->wait);
452         vlc_mutex_unlock (&addr->lock);
453     }
454
455     free (session);
456 }