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 *****************************************************************************/
35 #include <stdlib.h> /* free() */
36 #include <stdio.h> /* sprintf() */
38 #include <ctype.h> /* tolower(), isxdigit() */
42 #include <vlc_network.h>
43 #include <vlc_charset.h>
45 #include "stream_output.h"
48 /* SAP is always on that port */
51 #define DEFAULT_PORT "1234"
55 /* SAP Specific structures */
58 #define SAP_IDLE ((mtime_t)(0.100*CLOCK_FREQ))
59 #define SAP_MAX_BUFFER 65534
60 #define MIN_INTERVAL 2
61 #define MAX_INTERVAL 300
63 /* A SAP announce address. For each of these, we run the
64 * control flow algorithm */
68 struct sockaddr_storage orig;
70 int i_rfd; /* Read socket */
71 int i_wfd; /* Write socket */
73 /* Used for flow control */
82 /* A SAP session descriptor, enqueued in the SAP handler queue */
83 struct sap_session_t {
86 sap_address_t *p_address;
87 session_descriptor_t *p_sd;
89 /* Last and next send */
94 /*****************************************************************************
96 *****************************************************************************/
97 static void RunThread( vlc_object_t *p_this);
98 static int ComputeRate( sap_address_t *p_address );
100 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
101 sap_session_t *p_session );
104 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
105 session_descriptor_t *p_session );
107 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
108 session_descriptor_t *p_session );
110 static void announce_SAPHandlerDestructor( vlc_object_t *p_this );
114 * Create the SAP handler
116 * \param p_announce the parent announce_handler
117 * \return the newly created SAP handler or NULL on error
119 sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce )
121 sap_handler_t *p_sap;
123 p_sap = vlc_custom_create( VLC_OBJECT(p_announce), sizeof( sap_handler_t ),
124 VLC_OBJECT_ANNOUNCE, "announce" );
127 msg_Err( p_announce, "out of memory" );
131 p_sap->psz_object_name = "sap announcer";
133 vlc_mutex_init( p_sap, &p_sap->object_lock );
135 p_sap->pf_add = announce_SAPAnnounceAdd;
136 p_sap->pf_del = announce_SAPAnnounceDel;
138 p_sap->i_sessions = 0;
139 p_sap->i_addresses = 0;
140 p_sap->i_current_session = 0;
142 p_sap->b_control = config_GetInt( p_sap, "sap-flow-control");
144 if( vlc_thread_create( p_sap, "sap handler", RunThread,
145 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
147 msg_Dbg( p_announce, "unable to spawn SAP handler thread");
148 vlc_object_release( p_sap );
152 vlc_object_set_destructor( p_sap, announce_SAPHandlerDestructor );
154 msg_Dbg( p_announce, "thread created, %i sessions", p_sap->i_sessions);
159 static void announce_SAPHandlerDestructor( vlc_object_t * p_this )
161 sap_handler_t *p_sap = (sap_handler_t *)p_this;
164 /* Free the remaining sessions */
165 for( i = 0 ; i< p_sap->i_sessions ; i++)
167 sap_session_t *p_session = p_sap->pp_sessions[i];
168 FREENULL( p_session->psz_data );
169 REMOVE_ELEM( p_sap->pp_sessions, p_sap->i_sessions , i );
170 FREENULL( p_session );
173 /* Free the remaining addresses */
174 for( i = 0 ; i< p_sap->i_addresses ; i++)
176 sap_address_t *p_address = p_sap->pp_addresses[i];
177 FREENULL( p_address->psz_address );
178 if( p_address->i_rfd > -1 )
180 net_Close( p_address->i_rfd );
182 if( p_address->i_wfd > -1 && p_sap->b_control )
184 net_Close( p_address->i_wfd );
186 REMOVE_ELEM( p_sap->pp_addresses, p_sap->i_addresses, i );
187 FREENULL( p_address );
192 * main SAP handler thread
193 * \param p_this the SAP Handler object
196 static void RunThread( vlc_object_t *p_this)
198 sap_handler_t *p_sap = (sap_handler_t*)p_this;
199 sap_session_t *p_session;
201 while( !p_sap->b_die )
205 /* If needed, get the rate info */
206 if( p_sap->b_control == VLC_TRUE )
208 for( i = 0 ; i< p_sap->i_addresses ; i++)
210 if( p_sap->pp_addresses[i]->b_enabled == VLC_TRUE )
212 ComputeRate( p_sap->pp_addresses[i] );
217 /* Find the session to announce */
218 vlc_mutex_lock( &p_sap->object_lock );
219 if( p_sap->i_sessions > p_sap->i_current_session + 1)
221 p_sap->i_current_session++;
223 else if( p_sap->i_sessions > 0)
225 p_sap->i_current_session = 0;
229 vlc_mutex_unlock( &p_sap->object_lock );
233 p_session = p_sap->pp_sessions[p_sap->i_current_session];
234 vlc_mutex_unlock( &p_sap->object_lock );
236 /* And announce it */
237 if( p_session->p_address->b_enabled == VLC_TRUE &&
238 p_session->p_address->b_ready == VLC_TRUE )
240 announce_SendSAPAnnounce( p_sap, p_session );
247 /* Add a SAP announce */
248 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
249 session_descriptor_t *p_session )
252 char psz_addr[NI_MAXNUMERICHOST];
253 vlc_bool_t b_ipv6 = VLC_FALSE, b_ssm = VLC_FALSE;
254 sap_session_t *p_sap_session;
256 struct sockaddr_storage addr;
259 vlc_mutex_lock( &p_sap->object_lock );
260 addrlen = p_session->addrlen;
261 if ((addrlen == 0) || (addrlen > sizeof (addr)))
263 vlc_mutex_unlock( &p_sap->object_lock );
264 msg_Err( p_sap, "No/invalid address specified for SAP announce" );
268 /* Determine SAP multicast address automatically */
269 memcpy (&addr, &p_session->addr, addrlen);
271 switch( p_session->addr.ss_family )
273 #if defined (HAVE_INET_PTON) || defined (WIN32)
276 /* See RFC3513 for list of valid IPv6 scopes */
277 struct in6_addr *a6 = &((struct sockaddr_in6 *)&addr)->sin6_addr;
279 memcpy( a6->s6_addr + 2, "\x00\x00\x00\x00\x00\x00"
280 "\x00\x00\x00\x00\x00\x02\x7f\xfe", 14 );
281 if( IN6_IS_ADDR_MULTICAST( a6 ) )
283 /* SSM <=> ff3x::/32 */
284 b_ssm = (U32_AT (a6->s6_addr) & 0xfff0ffff) == 0xff300000;
286 /* force flags to zero, preserve scope */
287 a6->s6_addr[1] &= 0xf;
290 /* Unicast IPv6 - assume global scope */
291 memcpy( a6->s6_addr, "\xff\x0e", 2 );
300 /* See RFC2365 for IPv4 scopes */
303 ipv4 = ntohl( ((struct sockaddr_in *)&addr)->sin_addr.s_addr );
304 /* 224.0.0.0/24 => 224.0.0.255 */
305 if ((ipv4 & 0xffffff00) == 0xe0000000)
308 /* 239.255.0.0/16 => 239.255.255.255 */
309 if ((ipv4 & 0xffff0000) == 0xefff0000)
312 /* 239.192.0.0/14 => 239.195.255.255 */
313 if ((ipv4 & 0xfffc0000) == 0xefc00000)
316 if ((ipv4 & 0xff000000) == 0xef000000)
319 /* other addresses => 224.2.127.254 */
321 /* SSM: 232.0.0.0/8 */
322 b_ssm = (ipv4 >> 24) == 232;
328 msg_Err( p_sap, "Out-of-scope multicast address "
329 "not supported by SAP" );
330 vlc_mutex_unlock( &p_sap->object_lock );
334 ((struct sockaddr_in *)&addr)->sin_addr.s_addr = htonl( ipv4 );
339 vlc_mutex_unlock( &p_sap->object_lock );
340 msg_Err( p_sap, "Address family %d not supported by SAP",
345 i = vlc_getnameinfo( (struct sockaddr *)&addr, addrlen,
346 psz_addr, sizeof( psz_addr ), NULL, NI_NUMERICHOST );
350 vlc_mutex_unlock( &p_sap->object_lock );
351 msg_Err( p_sap, "%s", vlc_gai_strerror( i ) );
355 msg_Dbg( p_sap, "using SAP address: %s", psz_addr);
357 /* XXX: Check for dupes */
358 p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t));
359 p_sap_session->p_sd = p_session;
360 p_sap_session->p_address = NULL;
362 /* Add the address to the buffer */
363 for( i = 0; i < p_sap->i_addresses; i++)
365 if( !strcmp( psz_addr, p_sap->pp_addresses[i]->psz_address ) )
367 p_sap_session->p_address = p_sap->pp_addresses[i];
372 if( p_sap_session->p_address == NULL )
374 sap_address_t *p_address = (sap_address_t *)
375 malloc( sizeof(sap_address_t) );
378 vlc_mutex_unlock( &p_sap->object_lock );
381 p_address->psz_address = strdup( psz_addr );
382 p_address->i_wfd = net_ConnectUDP( VLC_OBJECT(p_sap), psz_addr, SAP_PORT, 255 );
383 if( p_address->i_wfd != -1 )
385 shutdown( p_address->i_wfd, SHUT_RD );
386 p_address->origlen = sizeof (p_address->orig);
387 getsockname (p_address->i_wfd, (struct sockaddr *)&p_address->orig,
388 &p_address->origlen);
391 if( p_sap->b_control == VLC_TRUE )
393 p_address->i_rfd = net_ListenUDP1( (vlc_object_t*)p_sap, psz_addr, SAP_PORT );
394 if( p_address->i_rfd != -1 )
395 shutdown( p_address->i_rfd, SHUT_WR );
396 p_address->i_buff = 0;
397 p_address->b_enabled = VLC_TRUE;
398 p_address->b_ready = VLC_FALSE;
399 p_address->i_limit = 10000; /* 10000 bps */
404 p_address->b_enabled = VLC_TRUE;
405 p_address->b_ready = VLC_TRUE;
406 p_address->i_interval = config_GetInt( p_sap,"sap-interval");
407 p_address->i_rfd = -1;
410 if( p_address->i_wfd == -1 || (p_address->i_rfd == -1
411 && p_sap->b_control ) )
413 msg_Warn( p_sap, "disabling address" );
414 p_address->b_enabled = VLC_FALSE;
417 INSERT_ELEM( p_sap->pp_addresses,
421 p_sap_session->p_address = p_address;
424 memcpy (&p_session->orig, &p_sap_session->p_address->orig,
425 p_session->origlen = p_sap_session->p_address->origlen);
427 size_t headsize = 20;
428 switch (p_session->orig.ss_family)
439 msg_Err( p_sap, "Address family %d not supported by SAP",
441 vlc_mutex_unlock( &p_sap->object_lock );
445 /* If needed, build the SDP */
446 assert( p_session->psz_sdp != NULL );
448 p_sap_session->i_last = 0;
449 p_sap_session->i_length = headsize + strlen (p_session->psz_sdp);
450 p_sap_session->psz_data = malloc (p_sap_session->i_length + 1);
451 if (p_sap_session->psz_data == NULL)
453 free (p_session->psz_sdp);
454 vlc_mutex_unlock( &p_sap->object_lock );
458 /* Build the SAP Headers */
459 uint8_t *psz_head = p_sap_session->psz_data;
461 /* SAPv1, not encrypted, not compressed */
463 psz_head[1] = 0x00; /* No authentification length */
466 psz_head[2] = i_hash >> 8; /* Msg id hash */
467 psz_head[3] = i_hash; /* Msg id hash 2 */
470 switch (p_session->orig.ss_family)
475 struct in6_addr *a6 =
476 &((struct sockaddr_in6 *)&p_session->orig)->sin6_addr;
477 memcpy (psz_head + headsize, a6, 16);
478 psz_head[0] |= 0x10; /* IPv6 flag */
486 (((struct sockaddr_in *)&p_session->orig)->sin_addr.s_addr);
487 memcpy (psz_head + headsize, &ipv4, 4);
494 memcpy (psz_head + headsize, "application/sdp", 16);
497 /* Build the final message */
498 strcpy( (char *)psz_head + headsize, p_session->psz_sdp);
500 /* Enqueue the announce */
501 INSERT_ELEM( p_sap->pp_sessions,
505 msg_Dbg( p_sap,"%i addresses, %i sessions",
506 p_sap->i_addresses,p_sap->i_sessions);
508 vlc_mutex_unlock( &p_sap->object_lock );
513 /* Remove a SAP Announce */
514 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
515 session_descriptor_t *p_session )
518 vlc_mutex_lock( &p_sap->object_lock );
520 msg_Dbg( p_sap, "removing session %p from SAP", p_session);
522 /* Dequeue the announce */
523 for( i = 0; i< p_sap->i_sessions; i++)
525 if( p_session == p_sap->pp_sessions[i]->p_sd )
527 sap_session_t *p_mysession = p_sap->pp_sessions[i];
528 REMOVE_ELEM( p_sap->pp_sessions,
532 free( p_mysession->psz_data );
538 /* XXX: Dequeue the address too if it is not used anymore
539 * TODO: - address refcount
540 - send a SAP deletion packet */
542 msg_Dbg( p_sap,"%i announcements remaining", p_sap->i_sessions );
544 vlc_mutex_unlock( &p_sap->object_lock );
549 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
550 sap_session_t *p_session )
554 /* This announce has never been sent yet */
555 if( p_session->i_last == 0 )
557 p_session->i_next = mdate()+ p_session->p_address->i_interval*1000000;
558 p_session->i_last = 1;
562 if( p_session->i_next < mdate() )
565 msg_Dbg( p_sap, "sending announce");
567 i_ret = net_Write( p_sap, p_session->p_address->i_wfd, NULL,
569 p_session->i_length );
570 if( i_ret != (int)p_session->i_length )
572 msg_Warn( p_sap, "SAP send failed on address %s (%i %i)",
573 p_session->p_address->psz_address,
574 i_ret, p_session->i_length );
576 p_session->i_last = p_session->i_next;
577 p_session->i_next = p_session->i_last
578 + p_session->p_address->i_interval*1000000;
583 static int ComputeRate( sap_address_t *p_address )
585 uint8_t buffer[SAP_MAX_BUFFER];
590 if( p_address->t1 == 0 )
592 p_address->t1 = mdate();
597 /* Might be too slow if we have huge data */
598 ssize_t i_read = recv( p_address->i_rfd, buffer, SAP_MAX_BUFFER, 0 );
606 /* We calculate the rate every 5 seconds */
607 if( i_temp - p_address->t1 < 5000000 )
609 p_address->i_buff += i_tot;
614 i_rate = (int)(8*1000000*((mtime_t)p_address->i_buff + (mtime_t)i_tot ) /
615 (i_temp - p_address->t1 ));
617 p_address->i_limit = 10000;
619 p_address->i_interval = ((1000*i_rate / p_address->i_limit) *
620 (MAX_INTERVAL - MIN_INTERVAL))/1000 + MIN_INTERVAL;
622 if( p_address->i_interval > MAX_INTERVAL || p_address->i_interval < 0 )
624 p_address->i_interval = MAX_INTERVAL;
627 msg_Dbg( p_sap,"%s:%i: rate=%i, interval = %i s",
628 p_address->psz_address,SAP_PORT, i_rate, p_address->i_interval );
631 p_address->b_ready = VLC_TRUE;
633 p_address->t1 = i_temp;
634 p_address->i_buff = 0;