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() */
39 /* SAP is always on that port */
42 #define DEFAULT_PORT "1234"
46 /* SAP Specific structures */
49 #define SAP_IDLE ((mtime_t)(0.100*CLOCK_FREQ))
50 #define SAP_MAX_BUFFER 65534
51 #define MIN_INTERVAL 2
52 #define MAX_INTERVAL 300
54 /* A SAP announce address. For each of these, we run the
55 * control flow algorithm */
59 char psz_machine[NI_MAXNUMERICHOST];
61 int i_rfd; /* Read socket */
62 int i_wfd; /* Write socket */
64 /* Used for flow control */
73 /*****************************************************************************
75 *****************************************************************************/
76 static void RunThread( vlc_object_t *p_this);
77 static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address );
78 static char *SDPGenerate( sap_handler_t *p_sap,
79 const session_descriptor_t *p_session,
80 const sap_address_t *p_addr );
82 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
83 sap_session_t *p_session );
86 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
87 session_descriptor_t *p_session );
89 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
90 session_descriptor_t *p_session );
92 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
96 * Create the SAP handler
98 * \param p_announce the parent announce_handler
99 * \return the newly created SAP handler or NULL on error
101 sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce )
103 sap_handler_t *p_sap;
105 p_sap = vlc_object_create( p_announce, sizeof( sap_handler_t ) );
109 msg_Err( p_announce, "out of memory" );
113 vlc_mutex_init( p_sap, &p_sap->object_lock );
115 p_sap->pf_add = announce_SAPAnnounceAdd;
116 p_sap->pf_del = announce_SAPAnnounceDel;
118 p_sap->i_sessions = 0;
119 p_sap->i_addresses = 0;
120 p_sap->i_current_session = 0;
122 p_sap->b_control = config_GetInt( p_sap, "sap-flow-control");
124 if( vlc_thread_create( p_sap, "sap handler", RunThread,
125 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
127 msg_Dbg( p_announce, "Unable to spawn SAP handler thread");
131 msg_Dbg( p_announce, "thread created, %i sessions", p_sap->i_sessions);
136 * Destroy the SAP handler
137 * \param p_this the SAP Handler to destroy
140 void announce_SAPHandlerDestroy( sap_handler_t *p_sap )
144 vlc_mutex_destroy( &p_sap->object_lock );
146 /* Free the remaining sessions */
147 for( i = 0 ; i< p_sap->i_sessions ; i++)
149 sap_session_t *p_session = p_sap->pp_sessions[i];
150 FREE( p_session->psz_sdp );
151 FREE( p_session->psz_data );
152 REMOVE_ELEM( p_sap->pp_sessions, p_sap->i_sessions , i );
156 /* Free the remaining addresses */
157 for( i = 0 ; i< p_sap->i_addresses ; i++)
159 sap_address_t *p_address = p_sap->pp_addresses[i];
160 FREE( p_address->psz_address );
161 if( p_address->i_rfd > -1 )
163 net_Close( p_address->i_rfd );
165 if( p_address->i_wfd > -1 && p_sap->b_control )
167 net_Close( p_address->i_wfd );
169 REMOVE_ELEM( p_sap->pp_addresses, p_sap->i_addresses, i );
173 /* Free the structure */
174 vlc_object_destroy( p_sap );
178 * main SAP handler thread
179 * \param p_this the SAP Handler object
182 static void RunThread( vlc_object_t *p_this)
184 sap_handler_t *p_sap = (sap_handler_t*)p_this;
185 sap_session_t *p_session;
187 while( !p_sap->b_die )
191 /* If needed, get the rate info */
192 if( p_sap->b_control == VLC_TRUE )
194 for( i = 0 ; i< p_sap->i_addresses ; i++)
196 if( p_sap->pp_addresses[i]->b_enabled == VLC_TRUE )
198 CalculateRate( p_sap, p_sap->pp_addresses[i] );
203 /* Find the session to announce */
204 vlc_mutex_lock( &p_sap->object_lock );
205 if( p_sap->i_sessions > p_sap->i_current_session + 1)
207 p_sap->i_current_session++;
209 else if( p_sap->i_sessions > 0)
211 p_sap->i_current_session = 0;
215 vlc_mutex_unlock( &p_sap->object_lock );
219 p_session = p_sap->pp_sessions[p_sap->i_current_session];
220 vlc_mutex_unlock( &p_sap->object_lock );
222 /* And announce it */
223 if( p_session->p_address->b_enabled == VLC_TRUE &&
224 p_session->p_address->b_ready == VLC_TRUE )
226 announce_SendSAPAnnounce( p_sap, p_session );
233 /* Add a SAP announce */
234 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
235 session_descriptor_t *p_session )
237 int i_header_size, i;
238 char *psz_head, psz_addr[NI_MAXNUMERICHOST];
239 vlc_bool_t b_ipv6 = VLC_FALSE;
240 sap_session_t *p_sap_session;
242 struct addrinfo hints, *res;
243 struct sockaddr_storage addr;
245 vlc_mutex_lock( &p_sap->object_lock );
247 if( p_session->psz_uri == NULL )
249 vlc_mutex_unlock( &p_sap->object_lock );
250 msg_Err( p_sap, "*FIXME* Unexpected NULL URI for SAP announce" );
251 msg_Err( p_sap, "This should not happen. VLC needs fixing." );
255 /* Determine SAP multicast address automatically */
256 memset( &hints, 0, sizeof( hints ) );
257 hints.ai_socktype = SOCK_DGRAM;
258 hints.ai_flags = AI_NUMERICHOST;
260 i = vlc_getaddrinfo( (vlc_object_t *)p_sap, p_session->psz_uri, 0,
264 vlc_mutex_unlock( &p_sap->object_lock );
265 msg_Err( p_sap, "Invalid URI for SAP announce: %s: %s",
266 p_session->psz_uri, vlc_gai_strerror( i ) );
270 if( (unsigned)res->ai_addrlen > sizeof( addr ) )
272 vlc_mutex_unlock( &p_sap->object_lock );
273 vlc_freeaddrinfo( res );
274 msg_Err( p_sap, "Unsupported address family of size %d > %u",
275 res->ai_addrlen, (unsigned) sizeof( addr ) );
279 memcpy( &addr, res->ai_addr, res->ai_addrlen );
281 switch( addr.ss_family )
283 #if defined (HAVE_INET_PTON) || defined (WIN32)
286 /* See RFC3513 for list of valid IPv6 scopes */
287 struct in6_addr *a6 = &((struct sockaddr_in6 *)&addr)->sin6_addr;
289 memcpy( a6->s6_addr + 2, "\x00\x00\x00\x00\x00\x00"
290 "\x00\x00\x00\x00\x00\x02\x7f\xfe", 14 );
291 if( IN6_IS_ADDR_MULTICAST( a6 ) )
292 /* force flags to zero, preserve scope */
293 a6->s6_addr[1] &= 0xf;
295 /* Unicast IPv6 - assume global scope */
296 memcpy( a6->s6_addr, "\xff\x0e", 2 );
305 /* See RFC2365 for IPv4 scopes */
308 ipv4 = ntohl( ((struct sockaddr_in *)&addr)->sin_addr.s_addr );
309 /* 224.0.0.0/24 => 224.0.0.255 */
310 if ((ipv4 & 0xffffff00) == 0xe0000000)
313 /* 239.255.0.0/16 => 239.255.255.255 */
314 if ((ipv4 & 0xffff0000) == 0xefff0000)
317 /* 239.192.0.0/14 => 239.195.255.255 */
318 if ((ipv4 & 0xfffc0000) == 0xefc00000)
321 /* other addresses => 224.2.127.254 */
324 ((struct sockaddr_in *)&addr)->sin_addr.s_addr = htonl( ipv4 );
329 vlc_mutex_unlock( &p_sap->object_lock );
330 vlc_freeaddrinfo( res );
331 msg_Err( p_sap, "Address family %d not supported by SAP",
336 i = vlc_getnameinfo( (struct sockaddr *)&addr, res->ai_addrlen,
337 psz_addr, sizeof( psz_addr ), NULL, NI_NUMERICHOST );
338 vlc_freeaddrinfo( res );
342 vlc_mutex_unlock( &p_sap->object_lock );
343 msg_Err( p_sap, "%s", vlc_gai_strerror( i ) );
347 msg_Dbg( p_sap, "using SAP address: %s", psz_addr);
349 /* XXX: Check for dupes */
350 p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t));
351 p_sap_session->p_address = NULL;
353 /* Add the address to the buffer */
354 for( i = 0; i < p_sap->i_addresses; i++)
356 if( !strcmp( psz_addr, p_sap->pp_addresses[i]->psz_address ) )
358 p_sap_session->p_address = p_sap->pp_addresses[i];
363 if( p_sap_session->p_address == NULL )
365 sap_address_t *p_address = (sap_address_t *)
366 malloc( sizeof(sap_address_t) );
369 msg_Err( p_sap, "out of memory" );
372 p_address->psz_address = strdup( psz_addr );
373 p_address->i_port = 9875;
374 p_address->i_wfd = net_OpenUDP( p_sap, "", 0, psz_addr,
376 if( p_address->i_wfd != -1 )
380 net_StopRecv( p_address->i_wfd );
381 net_GetSockAddress( p_address->i_wfd, p_address->psz_machine,
384 /* removes scope if present */
385 ptr = strchr( p_address->psz_machine, '%' );
390 if( p_sap->b_control == VLC_TRUE )
392 p_address->i_rfd = net_OpenUDP( p_sap, psz_addr,
393 p_address->i_port, "", 0 );
394 if( p_address->i_rfd != -1 )
395 net_StopSend( p_address->i_rfd );
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;
425 /* Build the SAP Headers */
426 i_header_size = ( b_ipv6 ? 16 : 4 ) + 20;
427 psz_head = (char *) malloc( i_header_size * sizeof( char ) );
428 if( psz_head == NULL )
430 msg_Err( p_sap, "out of memory" );
434 /* SAPv1, not encrypted, not compressed */
435 psz_head[0] = b_ipv6 ? 0x30 : 0x20;
436 psz_head[1] = 0x00; /* No authentification length */
439 psz_head[2] = (i_hash & 0xFF00) >> 8; /* Msg id hash */
440 psz_head[3] = (i_hash & 0xFF); /* Msg id hash 2 */
442 #if defined (HAVE_INET_PTON) || defined (WIN32)
445 inet_pton( AF_INET6, /* can't fail */
446 p_sap_session->p_address->psz_machine,
452 inet_pton( AF_INET, /* can't fail */
453 p_sap_session->p_address->psz_machine,
458 memcpy( psz_head + (b_ipv6 ? 20 : 8), "application/sdp", 15 );
460 /* If needed, build the SDP */
461 if( p_session->psz_sdp == NULL )
463 p_session->psz_sdp = SDPGenerate( p_sap, p_session,
464 p_sap_session->p_address );
465 if( p_session->psz_sdp == NULL )
467 vlc_mutex_unlock( &p_sap->object_lock );
472 p_sap_session->psz_sdp = strdup( p_session->psz_sdp );
473 p_sap_session->i_last = 0;
475 psz_head[ i_header_size-1 ] = '\0';
476 p_sap_session->i_length = i_header_size + strlen( p_sap_session->psz_sdp);
478 p_sap_session->psz_data = (uint8_t *)malloc( sizeof(char)*
479 p_sap_session->i_length );
481 /* Build the final message */
482 memcpy( p_sap_session->psz_data, psz_head, i_header_size );
483 memcpy( p_sap_session->psz_data+i_header_size, p_sap_session->psz_sdp,
484 strlen( p_sap_session->psz_sdp) );
488 /* Enqueue the announce */
489 INSERT_ELEM( p_sap->pp_sessions,
493 msg_Dbg( p_sap,"Addresses: %i Sessions: %i",
494 p_sap->i_addresses,p_sap->i_sessions);
496 /* Remember the SAP session for later deletion */
497 p_session->p_sap = p_sap_session;
499 vlc_mutex_unlock( &p_sap->object_lock );
504 /* Remove a SAP Announce */
505 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
506 session_descriptor_t *p_session )
509 vlc_mutex_lock( &p_sap->object_lock );
511 msg_Dbg( p_sap,"removing SAP announce %p",p_session->p_sap);
513 /* Dequeue the announce */
514 for( i = 0; i< p_sap->i_sessions; i++)
516 if( p_session->p_sap == p_sap->pp_sessions[i] )
518 REMOVE_ELEM( p_sap->pp_sessions,
522 FREE( p_session->p_sap->psz_sdp );
523 FREE( p_session->p_sap->psz_data );
524 free( p_session->p_sap );
529 /* XXX: Dequeue the address too if it is not used anymore
530 * TODO: - address refcount
531 - send a SAP deletion packet */
533 msg_Dbg( p_sap,"%i announcements remaining", p_sap->i_sessions );
535 vlc_mutex_unlock( &p_sap->object_lock );
540 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
541 sap_session_t *p_session )
545 /* This announce has never been sent yet */
546 if( p_session->i_last == 0 )
548 p_session->i_next = mdate()+ p_session->p_address->i_interval*1000000;
549 p_session->i_last = 1;
553 if( p_session->i_next < mdate() )
556 msg_Dbg( p_sap, "Sending announce");
558 i_ret = net_Write( p_sap, p_session->p_address->i_wfd, NULL,
560 p_session->i_length );
561 if( i_ret != p_session->i_length )
563 msg_Warn( p_sap, "SAP send failed on address %s (%i %i)",
564 p_session->p_address->psz_address,
565 i_ret, p_session->i_length );
567 p_session->i_last = p_session->i_next;
568 p_session->i_next = p_session->i_last
569 + p_session->p_address->i_interval*1000000;
578 static char *SDPGenerate( sap_handler_t *p_sap,
579 const session_descriptor_t *p_session,
580 const sap_address_t *p_addr )
582 int64_t i_sdp_id = mdate();
583 int i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff);
584 char *psz_group, *psz_name, psz_uribuf[NI_MAXNUMERICHOST], *psz_uri,
588 psz_group = p_session->psz_group;
589 psz_name = p_session->psz_name;
591 /* FIXME: really check that psz_uri is a real IP address
592 * FIXME: make a common function to obtain a canonical IP address */
593 ipv = ( strchr( p_session->psz_uri, ':' ) != NULL) ? '6' : '4';
594 if( *p_session->psz_uri == '[' )
598 strncpy( psz_uribuf, p_session->psz_uri + 1, sizeof( psz_uribuf ) );
599 psz_uribuf[sizeof( psz_uribuf ) - 1] = '\0';
600 ptr = strchr( psz_uribuf, '%' );
603 ptr = strchr( psz_uribuf, ']' );
606 psz_uri = psz_uribuf;
609 psz_uri = p_session->psz_uri;
611 /* see the lists in modules/stream_out/rtp.c for compliance stuff */
612 if( asprintf( &psz_sdp,
614 "o=- "I64Fd" %d IN IP%c %s\r\n"
617 "c=IN IP%c %s/%d\r\n"
618 "m=video %d %s %d\r\n"
619 "a=tool:"PACKAGE_STRING"\r\n"
620 "a=type:broadcast\r\n"
622 i_sdp_id, i_sdp_version,
623 ipv, p_addr->psz_machine,
624 psz_name, ipv, psz_uri, p_session->i_ttl,
626 p_session->b_rtp ? "RTP/AVP" : "udp",
627 p_session->i_payload,
628 psz_group ? "a=x-plgroup:" : "",
629 psz_group ? psz_group : "", psz_group ? "\r\n" : "" ) == -1 )
632 msg_Dbg( p_sap, "Generated SDP (%i bytes):\n%s", strlen(psz_sdp),
637 static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address )
640 uint8_t buffer[SAP_MAX_BUFFER];
645 if( p_address->t1 == 0 )
647 p_address->t1 = mdate();
652 /* Might be too slow if we have huge data */
653 i_read = net_ReadNonBlock( p_sap, p_address->i_rfd, NULL, buffer,
656 } while( i_read > 0 && i_tot < SAP_MAX_BUFFER );
660 /* We calculate the rate every 5 seconds */
661 if( i_temp - p_address->t1 < 5000000 )
663 p_address->i_buff += i_tot;
668 i_rate = (int)(8*1000000*((mtime_t)p_address->i_buff + (mtime_t)i_tot ) /
669 (i_temp - p_address->t1 ));
671 p_address->i_limit = 10000;
673 p_address->i_interval = ((1000*i_rate / p_address->i_limit) *
674 (MAX_INTERVAL - MIN_INTERVAL))/1000 + MIN_INTERVAL;
676 if( p_address->i_interval > MAX_INTERVAL || p_address->i_interval < 0 )
678 p_address->i_interval = MAX_INTERVAL;
681 msg_Dbg( p_sap,"%s:%i : Rate=%i, Interval = %i s",
682 p_address->psz_address,p_address->i_port,
684 p_address->i_interval );
687 p_address->b_ready = VLC_TRUE;
689 p_address->t1 = i_temp;
690 p_address->i_buff = 0;