1 /*****************************************************************************
2 * sap.c : SAP announce handler
3 *****************************************************************************
4 * Copyright (C) 2002-2005 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* free() */
29 #include <stdio.h> /* sprintf() */
30 #include <string.h> /* strerror() */
31 #include <ctype.h> /* tolower(), isxdigit() */
37 #if defined( WIN32 ) || defined( UNDER_CE )
38 # if defined(UNDER_CE) && defined(sockaddr_storage)
39 # undef sockaddr_storage
41 # include <winsock2.h>
42 # include <ws2tcpip.h>
45 # include <arpa/inet.h>
49 /* SAP is always on that port */
52 #define DEFAULT_PORT "1234"
56 /* SAP Specific structures */
59 #define SAP_IDLE ((mtime_t)(0.100*CLOCK_FREQ))
60 #define SAP_MAX_BUFFER 65534
61 #define MIN_INTERVAL 2
62 #define MAX_INTERVAL 300
64 /* A SAP announce address. For each of these, we run the
65 * control flow algorithm */
69 char psz_machine[NI_MAXNUMERICHOST];
71 int i_rfd; /* Read socket */
72 int i_wfd; /* Write socket */
74 /* Used for flow control */
83 /*****************************************************************************
85 *****************************************************************************/
86 static void RunThread( vlc_object_t *p_this);
87 static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address );
88 static char *SDPGenerate( sap_handler_t *p_sap,
89 const session_descriptor_t *p_session,
90 const sap_address_t *p_addr );
92 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
93 sap_session_t *p_session );
96 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
97 session_descriptor_t *p_session );
99 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
100 session_descriptor_t *p_session );
102 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
106 * Create the SAP handler
108 * \param p_announce the parent announce_handler
109 * \return the newly created SAP handler or NULL on error
111 sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce )
113 sap_handler_t *p_sap;
115 p_sap = vlc_object_create( p_announce, sizeof( sap_handler_t ) );
119 msg_Err( p_announce, "out of memory" );
123 vlc_mutex_init( p_sap, &p_sap->object_lock );
125 p_sap->pf_add = announce_SAPAnnounceAdd;
126 p_sap->pf_del = announce_SAPAnnounceDel;
128 p_sap->i_sessions = 0;
129 p_sap->i_addresses = 0;
130 p_sap->i_current_session = 0;
132 p_sap->b_control = config_GetInt( p_sap, "sap-flow-control");
134 if( vlc_thread_create( p_sap, "sap handler", RunThread,
135 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
137 msg_Dbg( p_announce, "Unable to spawn SAP handler thread");
141 msg_Dbg( p_announce, "thread created, %i sessions", p_sap->i_sessions);
146 * Destroy the SAP handler
147 * \param p_this the SAP Handler to destroy
150 void announce_SAPHandlerDestroy( sap_handler_t *p_sap )
154 vlc_mutex_destroy( &p_sap->object_lock );
156 /* Free the remaining sessions */
157 for( i = 0 ; i< p_sap->i_sessions ; i++)
159 sap_session_t *p_session = p_sap->pp_sessions[i];
160 FREE( p_session->psz_sdp );
161 FREE( p_session->psz_data );
162 REMOVE_ELEM( p_sap->pp_sessions, p_sap->i_sessions , i );
166 /* Free the remaining addresses */
167 for( i = 0 ; i< p_sap->i_addresses ; i++)
169 sap_address_t *p_address = p_sap->pp_addresses[i];
170 FREE( p_address->psz_address );
171 if( p_address->i_rfd > -1 )
173 net_Close( p_address->i_rfd );
175 if( p_address->i_wfd > -1 && p_sap->b_control )
177 net_Close( p_address->i_wfd );
179 REMOVE_ELEM( p_sap->pp_addresses, p_sap->i_addresses, i );
183 /* Free the structure */
184 vlc_object_destroy( p_sap );
188 * main SAP handler thread
189 * \param p_this the SAP Handler object
192 static void RunThread( vlc_object_t *p_this)
194 sap_handler_t *p_sap = (sap_handler_t*)p_this;
195 sap_session_t *p_session;
197 while( !p_sap->b_die )
201 /* If needed, get the rate info */
202 if( p_sap->b_control == VLC_TRUE )
204 for( i = 0 ; i< p_sap->i_addresses ; i++)
206 if( p_sap->pp_addresses[i]->b_enabled == VLC_TRUE )
208 CalculateRate( p_sap, p_sap->pp_addresses[i] );
213 /* Find the session to announce */
214 vlc_mutex_lock( &p_sap->object_lock );
215 if( p_sap->i_sessions > p_sap->i_current_session + 1)
217 p_sap->i_current_session++;
219 else if( p_sap->i_sessions > 0)
221 p_sap->i_current_session = 0;
225 vlc_mutex_unlock( &p_sap->object_lock );
229 p_session = p_sap->pp_sessions[p_sap->i_current_session];
230 vlc_mutex_unlock( &p_sap->object_lock );
232 /* And announce it */
233 if( p_session->p_address->b_enabled == VLC_TRUE &&
234 p_session->p_address->b_ready == VLC_TRUE )
236 announce_SendSAPAnnounce( p_sap, p_session );
243 /* Add a SAP announce */
244 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
245 session_descriptor_t *p_session )
247 int i_header_size, i;
248 char *psz_head, psz_addr[NI_MAXNUMERICHOST];
249 vlc_bool_t b_ipv6 = VLC_FALSE;
250 sap_session_t *p_sap_session;
252 struct addrinfo hints, *res;
253 struct sockaddr_storage addr;
255 vlc_mutex_lock( &p_sap->object_lock );
257 if( p_session->psz_uri == NULL )
259 vlc_mutex_unlock( &p_sap->object_lock );
260 msg_Err( p_sap, "*FIXME* Unexpected NULL URI for SAP announce" );
261 msg_Err( p_sap, "This should not happen. VLC needs fixing." );
265 /* Determine SAP multicast address automatically */
266 memset( &hints, 0, sizeof( hints ) );
267 hints.ai_socktype = SOCK_DGRAM;
268 hints.ai_flags = AI_NUMERICHOST;
270 i = vlc_getaddrinfo( (vlc_object_t *)p_sap, p_session->psz_uri, 0,
274 vlc_mutex_unlock( &p_sap->object_lock );
275 msg_Err( p_sap, "Invalid URI for SAP announce: %s: %s",
276 p_session->psz_uri, vlc_gai_strerror( i ) );
280 if( (unsigned)res->ai_addrlen > sizeof( addr ) )
282 vlc_mutex_unlock( &p_sap->object_lock );
283 vlc_freeaddrinfo( res );
284 msg_Err( p_sap, "Unsupported address family of size %d > %u",
285 res->ai_addrlen, sizeof( addr ) );
289 memcpy( &addr, res->ai_addr, res->ai_addrlen );
291 switch( addr.ss_family )
293 #if defined (HAVE_INET_PTON) || defined (WIN32)
296 /* See RFC3513 for list of valid IPv6 scopes */
297 struct in6_addr *a6 = &((struct sockaddr_in6 *)&addr)->sin6_addr;
299 memcpy( a6->s6_addr + 2, "\x00\x00\x00\x00\x00\x00"
300 "\x00\x00\x00\x00\x00\x02\x7f\xfe", 14 );
301 if( IN6_IS_ADDR_MULTICAST( a6 ) )
302 /* force flags to zero, preserve scope */
303 a6->s6_addr[1] &= 0xf;
305 /* Unicast IPv6 - assume global scope */
306 memcpy( a6->s6_addr, "\xff\x0e", 2 );
315 /* See RFC2365 for IPv4 scopes */
318 ipv4 = ntohl( ((struct sockaddr_in *)&addr)->sin_addr.s_addr );
319 /* 224.0.0.0/24 => 224.0.0.255 */
320 if ((ipv4 & 0xffffff00) == 0xe0000000)
323 /* 239.255.0.0/16 => 239.255.255.255 */
324 if ((ipv4 & 0xffff0000) == 0xefff0000)
327 /* 239.192.0.0/14 => 239.195.255.255 */
328 if ((ipv4 & 0xfffc0000) == 0xefc00000)
331 /* other addresses => 224.2.127.254 */
337 vlc_mutex_unlock( &p_sap->object_lock );
338 vlc_freeaddrinfo( res );
339 msg_Err( p_sap, "Address family %d not supported by SAP",
344 i = vlc_getnameinfo( (struct sockaddr *)&addr, res->ai_addrlen,
345 psz_addr, sizeof( psz_addr ), NULL, NI_NUMERICHOST );
346 vlc_freeaddrinfo( res );
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_address = NULL;
361 /* Add the address to the buffer */
362 for( i = 0; i < p_sap->i_addresses; i++)
364 if( !strcmp( psz_addr, p_sap->pp_addresses[i]->psz_address ) )
366 p_sap_session->p_address = p_sap->pp_addresses[i];
371 if( p_sap_session->p_address == NULL )
373 sap_address_t *p_address = (sap_address_t *)
374 malloc( sizeof(sap_address_t) );
377 msg_Err( p_sap, "out of memory" );
380 p_address->psz_address = strdup( psz_addr );
381 p_address->i_port = 9875;
382 p_address->i_wfd = net_OpenUDP( p_sap, "", 0, psz_addr,
384 if( p_address->i_wfd != -1 )
388 net_StopRecv( p_address->i_wfd );
389 net_GetSockAddress( p_address->i_wfd, p_address->psz_machine,
392 /* removes scope if present */
393 ptr = strchr( p_address->psz_machine, '%' );
398 if( p_sap->b_control == VLC_TRUE )
400 p_address->i_rfd = net_OpenUDP( p_sap, psz_addr,
401 p_address->i_port, "", 0 );
402 if( p_address->i_rfd != -1 )
403 net_StopSend( p_address->i_rfd );
404 p_address->i_buff = 0;
405 p_address->b_enabled = VLC_TRUE;
406 p_address->b_ready = VLC_FALSE;
407 p_address->i_limit = 10000; /* 10000 bps */
412 p_address->b_enabled = VLC_TRUE;
413 p_address->b_ready = VLC_TRUE;
414 p_address->i_interval = config_GetInt( p_sap,"sap-interval");
415 p_address->i_rfd = -1;
418 if( p_address->i_wfd == -1 || (p_address->i_rfd == -1
419 && p_sap->b_control ) )
421 msg_Warn( p_sap, "disabling address" );
422 p_address->b_enabled = VLC_FALSE;
425 INSERT_ELEM( p_sap->pp_addresses,
429 p_sap_session->p_address = p_address;
433 /* Build the SAP Headers */
434 i_header_size = ( b_ipv6 ? 16 : 4 ) + 20;
435 psz_head = (char *) malloc( i_header_size * sizeof( char ) );
436 if( psz_head == NULL )
438 msg_Err( p_sap, "out of memory" );
442 /* SAPv1, not encrypted, not compressed */
443 psz_head[0] = b_ipv6 ? 0x30 : 0x20;
444 psz_head[1] = 0x00; /* No authentification length */
447 psz_head[2] = (i_hash & 0xFF00) >> 8; /* Msg id hash */
448 psz_head[3] = (i_hash & 0xFF); /* Msg id hash 2 */
450 inet_pton( b_ipv6 ? AF_INET6 : AF_INET, /* can't fail */
451 p_sap_session->p_address->psz_machine, psz_head + 4 );
453 memcpy( psz_head + (b_ipv6 ? 20 : 8), "application/sdp", 15 );
455 /* If needed, build the SDP */
456 if( p_session->psz_sdp == NULL )
458 p_session->psz_sdp = SDPGenerate( p_sap, p_session,
459 p_sap_session->p_address );
460 if( p_session->psz_sdp == NULL )
462 vlc_mutex_unlock( &p_sap->object_lock );
467 p_sap_session->psz_sdp = strdup( p_session->psz_sdp );
468 p_sap_session->i_last = 0;
470 psz_head[ i_header_size-1 ] = '\0';
471 p_sap_session->i_length = i_header_size + strlen( p_sap_session->psz_sdp);
473 p_sap_session->psz_data = (uint8_t *)malloc( sizeof(char)*
474 p_sap_session->i_length );
476 /* Build the final message */
477 memcpy( p_sap_session->psz_data, psz_head, i_header_size );
478 memcpy( p_sap_session->psz_data+i_header_size, p_sap_session->psz_sdp,
479 strlen( p_sap_session->psz_sdp) );
483 /* Enqueue the announce */
484 INSERT_ELEM( p_sap->pp_sessions,
488 msg_Dbg( p_sap,"Addresses: %i Sessions: %i",
489 p_sap->i_addresses,p_sap->i_sessions);
491 /* Remember the SAP session for later deletion */
492 p_session->p_sap = p_sap_session;
494 vlc_mutex_unlock( &p_sap->object_lock );
499 /* Remove a SAP Announce */
500 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
501 session_descriptor_t *p_session )
504 vlc_mutex_lock( &p_sap->object_lock );
506 msg_Dbg( p_sap,"removing SAP announce %p",p_session->p_sap);
508 /* Dequeue the announce */
509 for( i = 0; i< p_sap->i_sessions; i++)
511 if( p_session->p_sap == p_sap->pp_sessions[i] )
513 REMOVE_ELEM( p_sap->pp_sessions,
517 FREE( p_session->p_sap->psz_sdp );
518 FREE( p_session->p_sap->psz_data );
519 free( p_session->p_sap );
524 /* XXX: Dequeue the address too if it is not used anymore
525 * TODO: - address refcount
526 - send a SAP deletion packet */
528 msg_Dbg( p_sap,"%i announces remaining", p_sap->i_sessions );
530 vlc_mutex_unlock( &p_sap->object_lock );
535 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
536 sap_session_t *p_session )
540 /* This announce has never been sent yet */
541 if( p_session->i_last == 0 )
543 p_session->i_next = mdate()+ p_session->p_address->i_interval*1000000;
544 p_session->i_last = 1;
548 if( p_session->i_next < mdate() )
551 msg_Dbg( p_sap, "Sending announce");
553 i_ret = net_Write( p_sap, p_session->p_address->i_wfd, NULL,
555 p_session->i_length );
556 if( i_ret != (int)p_session->i_length )
558 msg_Warn( p_sap, "SAP send failed on address %s (%i %i)",
559 p_session->p_address->psz_address,
560 i_ret, p_session->i_length );
562 p_session->i_last = p_session->i_next;
563 p_session->i_next = p_session->i_last
564 + p_session->p_address->i_interval*1000000;
573 static char *SDPGenerate( sap_handler_t *p_sap,
574 const session_descriptor_t *p_session,
575 const sap_address_t *p_addr )
577 int64_t i_sdp_id = mdate();
578 int i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff);
579 char *psz_group, *psz_name, psz_uribuf[NI_MAXNUMERICHOST], *psz_uri,
583 psz_group = p_session->psz_group;
584 psz_name = p_session->psz_name;
586 /* FIXME: really check that psz_uri is a real IP address
587 * FIXME: make a common function to obtain a canonical IP address */
588 ipv = ( strchr( p_session->psz_uri, ':' ) != NULL) ? '6' : '4';
589 if( *p_session->psz_uri == '[' )
593 strncpy( psz_uribuf, p_session->psz_uri + 1, sizeof( psz_uribuf ) );
594 psz_uribuf[sizeof( psz_uribuf ) - 1] = '\0';
595 ptr = strchr( psz_uribuf, '%' );
598 ptr = strchr( psz_uribuf, ']' );
601 psz_uri = psz_uribuf;
604 psz_uri = p_session->psz_uri;
606 /* see the lists in modules/stream_out/rtp.c for compliance stuff */
607 if( asprintf( &psz_sdp,
609 "o=- "I64Fd" %d IN IP%c %s\r\n"
612 "c=IN IP%c %s/%d\r\n"
613 "m=video %d udp %d\r\n"
614 "a=tool:"PACKAGE_STRING"\r\n"
615 "a=type:broadcast\r\n"
617 i_sdp_id, i_sdp_version,
618 ipv, p_addr->psz_machine,
620 psz_uri, p_session->i_ttl,
621 p_session->i_port, p_session->i_payload,
622 psz_group ? "a=x-plgroup:" : "",
623 psz_group ?: "", psz_group ? "\r\n" : "" ) == -1 )
626 msg_Dbg( p_sap, "Generated SDP (%i bytes):\n%s", strlen(psz_sdp),
631 static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address )
634 uint8_t buffer[SAP_MAX_BUFFER];
639 if( p_address->t1 == 0 )
641 p_address->t1 = mdate();
646 /* Might be too slow if we have huge data */
647 i_read = net_ReadNonBlock( p_sap, p_address->i_rfd, NULL, buffer,
650 } while( i_read > 0 && i_tot < SAP_MAX_BUFFER );
654 /* We calculate the rate every 5 seconds */
655 if( i_temp - p_address->t1 < 5000000 )
657 p_address->i_buff += i_tot;
662 i_rate = (int)(8*1000000*((mtime_t)p_address->i_buff + (mtime_t)i_tot ) /
663 (i_temp - p_address->t1 ));
665 p_address->i_limit = 10000;
667 p_address->i_interval = ((1000*i_rate / p_address->i_limit) *
668 (MAX_INTERVAL - MIN_INTERVAL))/1000 + MIN_INTERVAL;
670 if( p_address->i_interval > MAX_INTERVAL || p_address->i_interval < 0 )
672 p_address->i_interval = MAX_INTERVAL;
675 msg_Dbg( p_sap,"%s:%i : Rate=%i, Interval = %i s",
676 p_address->psz_address,p_address->i_port,
678 p_address->i_interval );
681 p_address->b_ready = VLC_TRUE;
683 p_address->t1 = i_temp;
684 p_address->i_buff = 0;