1 /*****************************************************************************
2 * sap.c : SAP announce handler
3 *****************************************************************************
4 * Copyright (C) 2002-2007 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 *****************************************************************************/
31 #include <stdlib.h> /* free() */
32 #include <stdio.h> /* sprintf() */
33 #include <string.h> /* strerror() */
34 #include <ctype.h> /* tolower(), isxdigit() */
37 #include <vlc_network.h>
38 #include <vlc_charset.h>
40 #include "stream_output.h"
42 /* SAP is always on that port */
45 #define DEFAULT_PORT "1234"
49 /* SAP Specific structures */
52 #define SAP_IDLE ((mtime_t)(0.100*CLOCK_FREQ))
53 #define SAP_MAX_BUFFER 65534
54 #define MIN_INTERVAL 2
55 #define MAX_INTERVAL 300
57 /* A SAP announce address. For each of these, we run the
58 * control flow algorithm */
62 struct sockaddr_storage orig;
64 int i_rfd; /* Read socket */
65 int i_wfd; /* Write socket */
67 /* Used for flow control */
76 /* A SAP session descriptor, enqueued in the SAP handler queue */
77 struct sap_session_t {
80 sap_address_t *p_address;
81 session_descriptor_t *p_sd;
83 /* Last and next send */
88 /*****************************************************************************
90 *****************************************************************************/
91 static void RunThread( vlc_object_t *p_this);
92 static int ComputeRate( sap_address_t *p_address );
93 static char *SDPGenerate( sap_handler_t *p_sap,
94 const session_descriptor_t *p_session,
97 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
98 sap_session_t *p_session );
101 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
102 session_descriptor_t *p_session );
104 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
105 session_descriptor_t *p_session );
109 * Create the SAP handler
111 * \param p_announce the parent announce_handler
112 * \return the newly created SAP handler or NULL on error
114 sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce )
116 sap_handler_t *p_sap;
118 p_sap = vlc_object_create( p_announce, sizeof( sap_handler_t ) );
122 msg_Err( p_announce, "out of memory" );
126 vlc_mutex_init( p_sap, &p_sap->object_lock );
128 p_sap->pf_add = announce_SAPAnnounceAdd;
129 p_sap->pf_del = announce_SAPAnnounceDel;
131 p_sap->i_sessions = 0;
132 p_sap->i_addresses = 0;
133 p_sap->i_current_session = 0;
135 p_sap->b_control = config_GetInt( p_sap, "sap-flow-control");
137 if( vlc_thread_create( p_sap, "sap handler", RunThread,
138 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
140 msg_Dbg( p_announce, "unable to spawn SAP handler thread");
144 msg_Dbg( p_announce, "thread created, %i sessions", p_sap->i_sessions);
149 * Destroy the SAP handler
150 * \param p_this the SAP Handler to destroy
153 void announce_SAPHandlerDestroy( sap_handler_t *p_sap )
157 vlc_mutex_destroy( &p_sap->object_lock );
159 /* Free the remaining sessions */
160 for( i = 0 ; i< p_sap->i_sessions ; i++)
162 sap_session_t *p_session = p_sap->pp_sessions[i];
163 FREENULL( p_session->psz_data );
164 REMOVE_ELEM( p_sap->pp_sessions, p_sap->i_sessions , i );
165 FREENULL( p_session );
168 /* Free the remaining addresses */
169 for( i = 0 ; i< p_sap->i_addresses ; i++)
171 sap_address_t *p_address = p_sap->pp_addresses[i];
172 FREENULL( p_address->psz_address );
173 if( p_address->i_rfd > -1 )
175 net_Close( p_address->i_rfd );
177 if( p_address->i_wfd > -1 && p_sap->b_control )
179 net_Close( p_address->i_wfd );
181 REMOVE_ELEM( p_sap->pp_addresses, p_sap->i_addresses, i );
182 FREENULL( p_address );
185 /* Free the structure */
186 vlc_object_destroy( p_sap );
190 * main SAP handler thread
191 * \param p_this the SAP Handler object
194 static void RunThread( vlc_object_t *p_this)
196 sap_handler_t *p_sap = (sap_handler_t*)p_this;
197 sap_session_t *p_session;
199 while( !p_sap->b_die )
203 /* If needed, get the rate info */
204 if( p_sap->b_control == VLC_TRUE )
206 for( i = 0 ; i< p_sap->i_addresses ; i++)
208 if( p_sap->pp_addresses[i]->b_enabled == VLC_TRUE )
210 ComputeRate( p_sap->pp_addresses[i] );
215 /* Find the session to announce */
216 vlc_mutex_lock( &p_sap->object_lock );
217 if( p_sap->i_sessions > p_sap->i_current_session + 1)
219 p_sap->i_current_session++;
221 else if( p_sap->i_sessions > 0)
223 p_sap->i_current_session = 0;
227 vlc_mutex_unlock( &p_sap->object_lock );
231 p_session = p_sap->pp_sessions[p_sap->i_current_session];
232 vlc_mutex_unlock( &p_sap->object_lock );
234 /* And announce it */
235 if( p_session->p_address->b_enabled == VLC_TRUE &&
236 p_session->p_address->b_ready == VLC_TRUE )
238 announce_SendSAPAnnounce( p_sap, p_session );
245 /* Add a SAP announce */
246 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
247 session_descriptor_t *p_session )
250 char psz_addr[NI_MAXNUMERICHOST];
251 vlc_bool_t b_ipv6 = VLC_FALSE, b_ssm = VLC_FALSE;
252 sap_session_t *p_sap_session;
254 struct sockaddr_storage addr;
257 vlc_mutex_lock( &p_sap->object_lock );
258 addrlen = p_session->addrlen;
259 if ((addrlen == 0) || (addrlen > sizeof (addr)))
261 vlc_mutex_unlock( &p_sap->object_lock );
262 msg_Err( p_sap, "No/invalid address specified for SAP announce" );
266 /* Determine SAP multicast address automatically */
267 memcpy (&addr, &p_session->addr, addrlen);
269 switch( p_session->addr.ss_family )
271 #if defined (HAVE_INET_PTON) || defined (WIN32)
274 /* See RFC3513 for list of valid IPv6 scopes */
275 struct in6_addr *a6 = &((struct sockaddr_in6 *)&addr)->sin6_addr;
277 memcpy( a6->s6_addr + 2, "\x00\x00\x00\x00\x00\x00"
278 "\x00\x00\x00\x00\x00\x02\x7f\xfe", 14 );
279 if( IN6_IS_ADDR_MULTICAST( a6 ) )
281 /* SSM <=> ff3x::/32 */
282 b_ssm = (U32_AT (a6->s6_addr) & 0xfff0ffff) == 0xff300000;
284 /* force flags to zero, preserve scope */
285 a6->s6_addr[1] &= 0xf;
288 /* Unicast IPv6 - assume global scope */
289 memcpy( a6->s6_addr, "\xff\x0e", 2 );
298 /* See RFC2365 for IPv4 scopes */
301 ipv4 = ntohl( ((struct sockaddr_in *)&addr)->sin_addr.s_addr );
302 /* 224.0.0.0/24 => 224.0.0.255 */
303 if ((ipv4 & 0xffffff00) == 0xe0000000)
306 /* 239.255.0.0/16 => 239.255.255.255 */
307 if ((ipv4 & 0xffff0000) == 0xefff0000)
310 /* 239.192.0.0/14 => 239.195.255.255 */
311 if ((ipv4 & 0xfffc0000) == 0xefc00000)
314 if ((ipv4 & 0xff000000) == 0xef000000)
317 /* other addresses => 224.2.127.254 */
319 /* SSM: 232.0.0.0/8 */
320 b_ssm = (ipv4 >> 24) == 232;
326 msg_Err( p_sap, "Out-of-scope multicast address "
327 "not supported by SAP" );
328 vlc_mutex_unlock( &p_sap->object_lock );
332 ((struct sockaddr_in *)&addr)->sin_addr.s_addr = htonl( ipv4 );
337 vlc_mutex_unlock( &p_sap->object_lock );
338 msg_Err( p_sap, "Address family %d not supported by SAP",
343 i = vlc_getnameinfo( (struct sockaddr *)&addr, addrlen,
344 psz_addr, sizeof( psz_addr ), NULL, NI_NUMERICHOST );
348 vlc_mutex_unlock( &p_sap->object_lock );
349 msg_Err( p_sap, "%s", vlc_gai_strerror( i ) );
353 msg_Dbg( p_sap, "using SAP address: %s", psz_addr);
355 /* XXX: Check for dupes */
356 p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t));
357 p_sap_session->p_sd = p_session;
358 p_sap_session->p_address = NULL;
360 /* Add the address to the buffer */
361 for( i = 0; i < p_sap->i_addresses; i++)
363 if( !strcmp( psz_addr, p_sap->pp_addresses[i]->psz_address ) )
365 p_sap_session->p_address = p_sap->pp_addresses[i];
370 if( p_sap_session->p_address == NULL )
372 sap_address_t *p_address = (sap_address_t *)
373 malloc( sizeof(sap_address_t) );
376 vlc_mutex_unlock( &p_sap->object_lock );
379 p_address->psz_address = strdup( psz_addr );
380 p_address->i_wfd = net_ConnectUDP( VLC_OBJECT(p_sap), psz_addr, SAP_PORT, 255 );
381 if( p_address->i_wfd != -1 )
383 shutdown( p_address->i_wfd, SHUT_RD );
384 p_address->origlen = sizeof (p_address->orig);
385 getsockname (p_address->i_wfd, (struct sockaddr *)&p_address->orig,
386 &p_address->origlen);
389 if( p_sap->b_control == VLC_TRUE )
391 p_address->i_rfd = net_ListenUDP1( (vlc_object_t*)p_sap, psz_addr, SAP_PORT );
392 if( p_address->i_rfd != -1 )
393 shutdown( p_address->i_rfd, SHUT_WR );
394 p_address->i_buff = 0;
395 p_address->b_enabled = VLC_TRUE;
396 p_address->b_ready = VLC_FALSE;
397 p_address->i_limit = 10000; /* 10000 bps */
402 p_address->b_enabled = VLC_TRUE;
403 p_address->b_ready = VLC_TRUE;
404 p_address->i_interval = config_GetInt( p_sap,"sap-interval");
405 p_address->i_rfd = -1;
408 if( p_address->i_wfd == -1 || (p_address->i_rfd == -1
409 && p_sap->b_control ) )
411 msg_Warn( p_sap, "disabling address" );
412 p_address->b_enabled = VLC_FALSE;
415 INSERT_ELEM( p_sap->pp_addresses,
419 p_sap_session->p_address = p_address;
422 if (p_session->origlen == 0)
423 memcpy (&p_session->orig, &p_sap_session->p_address->orig,
424 p_session->origlen = p_sap_session->p_address->origlen);
426 size_t headsize = 20;
427 switch (p_session->orig.ss_family)
438 msg_Err( p_sap, "Address family %d not supported by SAP",
440 vlc_mutex_unlock( &p_sap->object_lock );
444 /* If needed, build the SDP */
445 if( p_session->psz_sdp == NULL )
447 p_session->psz_sdp = SDPGenerate( p_sap, p_session, b_ssm );
448 if( p_session->psz_sdp == NULL )
450 vlc_mutex_unlock( &p_sap->object_lock );
455 p_sap_session->i_last = 0;
456 p_sap_session->i_length = headsize + strlen (p_session->psz_sdp);
457 p_sap_session->psz_data = malloc (p_sap_session->i_length + 1);
458 if (p_sap_session->psz_data == NULL)
460 free (p_session->psz_sdp);
461 vlc_mutex_unlock( &p_sap->object_lock );
465 /* Build the SAP Headers */
466 uint8_t *psz_head = p_sap_session->psz_data;
468 /* SAPv1, not encrypted, not compressed */
470 psz_head[1] = 0x00; /* No authentification length */
473 psz_head[2] = i_hash >> 8; /* Msg id hash */
474 psz_head[3] = i_hash; /* Msg id hash 2 */
477 switch (p_session->orig.ss_family)
482 struct in6_addr *a6 =
483 &((struct sockaddr_in6 *)&p_session->orig)->sin6_addr;
484 memcpy (psz_head + headsize, a6, 16);
485 psz_head[0] |= 0x10; /* IPv6 flag */
493 (((struct sockaddr_in *)&p_session->orig)->sin_addr.s_addr);
494 memcpy (psz_head + headsize, &ipv4, 4);
501 memcpy (psz_head + headsize, "application/sdp", 16);
504 /* Build the final message */
505 strcpy( (char *)psz_head + headsize, p_session->psz_sdp);
507 /* Enqueue the announce */
508 INSERT_ELEM( p_sap->pp_sessions,
512 msg_Dbg( p_sap,"%i addresses, %i sessions",
513 p_sap->i_addresses,p_sap->i_sessions);
515 vlc_mutex_unlock( &p_sap->object_lock );
520 /* Remove a SAP Announce */
521 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
522 session_descriptor_t *p_session )
525 vlc_mutex_lock( &p_sap->object_lock );
527 msg_Dbg( p_sap, "removing session %p from SAP", p_session);
529 /* Dequeue the announce */
530 for( i = 0; i< p_sap->i_sessions; i++)
532 if( p_session == p_sap->pp_sessions[i]->p_sd )
534 sap_session_t *p_mysession = p_sap->pp_sessions[i];
535 REMOVE_ELEM( p_sap->pp_sessions,
539 free( p_mysession->psz_data );
545 /* XXX: Dequeue the address too if it is not used anymore
546 * TODO: - address refcount
547 - send a SAP deletion packet */
549 msg_Dbg( p_sap,"%i announcements remaining", p_sap->i_sessions );
551 vlc_mutex_unlock( &p_sap->object_lock );
556 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
557 sap_session_t *p_session )
561 /* This announce has never been sent yet */
562 if( p_session->i_last == 0 )
564 p_session->i_next = mdate()+ p_session->p_address->i_interval*1000000;
565 p_session->i_last = 1;
569 if( p_session->i_next < mdate() )
572 msg_Dbg( p_sap, "sending announce");
574 i_ret = net_Write( p_sap, p_session->p_address->i_wfd, NULL,
576 p_session->i_length );
577 if( i_ret != (int)p_session->i_length )
579 msg_Warn( p_sap, "SAP send failed on address %s (%i %i)",
580 p_session->p_address->psz_address,
581 i_ret, p_session->i_length );
583 p_session->i_last = p_session->i_next;
584 p_session->i_next = p_session->i_last
585 + p_session->p_address->i_interval*1000000;
590 static char *SDPGenerate( sap_handler_t *p_sap,
591 const session_descriptor_t *p_session,
594 char *psz_group, *psz_name, *psz_sdp;
596 char *head = sdp_Start (p_session->psz_name, p_session->description,
597 p_session->url, p_session->email, p_session->phone,
598 (const struct sockaddr *)&p_session->orig, p_session->origlen,
599 (const struct sockaddr *)&p_session->addr, p_session->addrlen);
603 psz_group = p_session->psz_group;
604 psz_name = p_session->psz_name;
607 if ((psz_group == NULL)
608 || (asprintf (&plgroup, "a=x-plgroup:%s\r\n", psz_group) == -1))
611 const char *comedia = NULL;
612 if (!strncasecmp (p_session->sdpformat, "DCCP", 4)
613 || !strncasecmp (p_session->sdpformat, "TCP", 3))
614 comedia = "a=setup:passive\r\n"
615 "a=connection:new\r\n";
617 int res = asprintf (&psz_sdp, "%s" "%s" "%s"
622 ntohs (net_GetPort ((const struct sockaddr *)&p_session->addr)),
623 p_session->sdpformat);
629 msg_Dbg( p_sap, "Generated SDP (%u bytes):\n%s",
630 (unsigned)strlen(psz_sdp), psz_sdp );
634 static int ComputeRate( sap_address_t *p_address )
636 uint8_t buffer[SAP_MAX_BUFFER];
641 if( p_address->t1 == 0 )
643 p_address->t1 = mdate();
648 /* Might be too slow if we have huge data */
649 ssize_t i_read = recv( p_address->i_rfd, buffer, SAP_MAX_BUFFER, 0 );
657 /* We calculate the rate every 5 seconds */
658 if( i_temp - p_address->t1 < 5000000 )
660 p_address->i_buff += i_tot;
665 i_rate = (int)(8*1000000*((mtime_t)p_address->i_buff + (mtime_t)i_tot ) /
666 (i_temp - p_address->t1 ));
668 p_address->i_limit = 10000;
670 p_address->i_interval = ((1000*i_rate / p_address->i_limit) *
671 (MAX_INTERVAL - MIN_INTERVAL))/1000 + MIN_INTERVAL;
673 if( p_address->i_interval > MAX_INTERVAL || p_address->i_interval < 0 )
675 p_address->i_interval = MAX_INTERVAL;
678 msg_Dbg( p_sap,"%s:%i: rate=%i, interval = %i s",
679 p_address->psz_address,SAP_PORT, i_rate, p_address->i_interval );
682 p_address->b_ready = VLC_TRUE;
684 p_address->t1 = i_temp;
685 p_address->i_buff = 0;