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"
47 /* SAP is always on that port */
50 #define DEFAULT_PORT "1234"
54 /* SAP Specific structures */
57 #define SAP_IDLE ((mtime_t)(0.100*CLOCK_FREQ))
58 #define SAP_MAX_BUFFER 65534
59 #define MIN_INTERVAL 2
60 #define MAX_INTERVAL 300
62 /* A SAP announce address. For each of these, we run the
63 * control flow algorithm */
67 struct sockaddr_storage orig;
69 int i_rfd; /* Read socket */
70 int i_wfd; /* Write socket */
72 /* Used for flow control */
81 /* A SAP session descriptor, enqueued in the SAP handler queue */
82 struct sap_session_t {
85 sap_address_t *p_address;
86 session_descriptor_t *p_sd;
88 /* Last and next send */
93 /*****************************************************************************
95 *****************************************************************************/
96 static void RunThread( vlc_object_t *p_this);
97 static int ComputeRate( sap_address_t *p_address );
99 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
100 sap_session_t *p_session );
103 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
104 session_descriptor_t *p_session );
106 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
107 session_descriptor_t *p_session );
109 static void announce_SAPHandlerDestructor( vlc_object_t *p_this );
113 * Create the SAP handler
115 * \param p_announce the parent announce_handler
116 * \return the newly created SAP handler or NULL on error
118 sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce )
120 sap_handler_t *p_sap;
122 p_sap = vlc_object_create( p_announce, sizeof( sap_handler_t ) );
126 msg_Err( p_announce, "out of memory" );
130 vlc_mutex_init( p_sap, &p_sap->object_lock );
132 p_sap->pf_add = announce_SAPAnnounceAdd;
133 p_sap->pf_del = announce_SAPAnnounceDel;
135 p_sap->i_sessions = 0;
136 p_sap->i_addresses = 0;
137 p_sap->i_current_session = 0;
139 p_sap->b_control = config_GetInt( p_sap, "sap-flow-control");
141 if( vlc_thread_create( p_sap, "sap handler", RunThread,
142 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
144 msg_Dbg( p_announce, "unable to spawn SAP handler thread");
145 vlc_object_release( p_sap );
149 vlc_object_set_destructor( p_sap, announce_SAPHandlerDestructor );
151 msg_Dbg( p_announce, "thread created, %i sessions", p_sap->i_sessions);
156 static void announce_SAPHandlerDestructor( vlc_object_t * p_this )
158 sap_handler_t *p_sap = (sap_handler_t *)p_this;
161 /* Free the remaining sessions */
162 for( i = 0 ; i< p_sap->i_sessions ; i++)
164 sap_session_t *p_session = p_sap->pp_sessions[i];
165 FREENULL( p_session->psz_data );
166 REMOVE_ELEM( p_sap->pp_sessions, p_sap->i_sessions , i );
167 FREENULL( p_session );
170 /* Free the remaining addresses */
171 for( i = 0 ; i< p_sap->i_addresses ; i++)
173 sap_address_t *p_address = p_sap->pp_addresses[i];
174 FREENULL( p_address->psz_address );
175 if( p_address->i_rfd > -1 )
177 net_Close( p_address->i_rfd );
179 if( p_address->i_wfd > -1 && p_sap->b_control )
181 net_Close( p_address->i_wfd );
183 REMOVE_ELEM( p_sap->pp_addresses, p_sap->i_addresses, i );
184 FREENULL( p_address );
189 * main SAP handler thread
190 * \param p_this the SAP Handler object
193 static void RunThread( vlc_object_t *p_this)
195 sap_handler_t *p_sap = (sap_handler_t*)p_this;
196 sap_session_t *p_session;
198 while( !p_sap->b_die )
202 /* If needed, get the rate info */
203 if( p_sap->b_control == VLC_TRUE )
205 for( i = 0 ; i< p_sap->i_addresses ; i++)
207 if( p_sap->pp_addresses[i]->b_enabled == VLC_TRUE )
209 ComputeRate( p_sap->pp_addresses[i] );
214 /* Find the session to announce */
215 vlc_mutex_lock( &p_sap->object_lock );
216 if( p_sap->i_sessions > p_sap->i_current_session + 1)
218 p_sap->i_current_session++;
220 else if( p_sap->i_sessions > 0)
222 p_sap->i_current_session = 0;
226 vlc_mutex_unlock( &p_sap->object_lock );
230 p_session = p_sap->pp_sessions[p_sap->i_current_session];
231 vlc_mutex_unlock( &p_sap->object_lock );
233 /* And announce it */
234 if( p_session->p_address->b_enabled == VLC_TRUE &&
235 p_session->p_address->b_ready == VLC_TRUE )
237 announce_SendSAPAnnounce( p_sap, p_session );
244 /* Add a SAP announce */
245 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
246 session_descriptor_t *p_session )
249 char psz_addr[NI_MAXNUMERICHOST];
250 vlc_bool_t b_ipv6 = VLC_FALSE, b_ssm = VLC_FALSE;
251 sap_session_t *p_sap_session;
253 struct sockaddr_storage addr;
256 vlc_mutex_lock( &p_sap->object_lock );
257 addrlen = p_session->addrlen;
258 if ((addrlen == 0) || (addrlen > sizeof (addr)))
260 vlc_mutex_unlock( &p_sap->object_lock );
261 msg_Err( p_sap, "No/invalid address specified for SAP announce" );
265 /* Determine SAP multicast address automatically */
266 memcpy (&addr, &p_session->addr, addrlen);
268 switch( p_session->addr.ss_family )
270 #if defined (HAVE_INET_PTON) || defined (WIN32)
273 /* See RFC3513 for list of valid IPv6 scopes */
274 struct in6_addr *a6 = &((struct sockaddr_in6 *)&addr)->sin6_addr;
276 memcpy( a6->s6_addr + 2, "\x00\x00\x00\x00\x00\x00"
277 "\x00\x00\x00\x00\x00\x02\x7f\xfe", 14 );
278 if( IN6_IS_ADDR_MULTICAST( a6 ) )
280 /* SSM <=> ff3x::/32 */
281 b_ssm = (U32_AT (a6->s6_addr) & 0xfff0ffff) == 0xff300000;
283 /* force flags to zero, preserve scope */
284 a6->s6_addr[1] &= 0xf;
287 /* Unicast IPv6 - assume global scope */
288 memcpy( a6->s6_addr, "\xff\x0e", 2 );
297 /* See RFC2365 for IPv4 scopes */
300 ipv4 = ntohl( ((struct sockaddr_in *)&addr)->sin_addr.s_addr );
301 /* 224.0.0.0/24 => 224.0.0.255 */
302 if ((ipv4 & 0xffffff00) == 0xe0000000)
305 /* 239.255.0.0/16 => 239.255.255.255 */
306 if ((ipv4 & 0xffff0000) == 0xefff0000)
309 /* 239.192.0.0/14 => 239.195.255.255 */
310 if ((ipv4 & 0xfffc0000) == 0xefc00000)
313 if ((ipv4 & 0xff000000) == 0xef000000)
316 /* other addresses => 224.2.127.254 */
318 /* SSM: 232.0.0.0/8 */
319 b_ssm = (ipv4 >> 24) == 232;
325 msg_Err( p_sap, "Out-of-scope multicast address "
326 "not supported by SAP" );
327 vlc_mutex_unlock( &p_sap->object_lock );
331 ((struct sockaddr_in *)&addr)->sin_addr.s_addr = htonl( ipv4 );
336 vlc_mutex_unlock( &p_sap->object_lock );
337 msg_Err( p_sap, "Address family %d not supported by SAP",
342 i = vlc_getnameinfo( (struct sockaddr *)&addr, addrlen,
343 psz_addr, sizeof( psz_addr ), NULL, NI_NUMERICHOST );
347 vlc_mutex_unlock( &p_sap->object_lock );
348 msg_Err( p_sap, "%s", vlc_gai_strerror( i ) );
352 msg_Dbg( p_sap, "using SAP address: %s", psz_addr);
354 /* XXX: Check for dupes */
355 p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t));
356 p_sap_session->p_sd = p_session;
357 p_sap_session->p_address = NULL;
359 /* Add the address to the buffer */
360 for( i = 0; i < p_sap->i_addresses; i++)
362 if( !strcmp( psz_addr, p_sap->pp_addresses[i]->psz_address ) )
364 p_sap_session->p_address = p_sap->pp_addresses[i];
369 if( p_sap_session->p_address == NULL )
371 sap_address_t *p_address = (sap_address_t *)
372 malloc( sizeof(sap_address_t) );
375 vlc_mutex_unlock( &p_sap->object_lock );
378 p_address->psz_address = strdup( psz_addr );
379 p_address->i_wfd = net_ConnectUDP( VLC_OBJECT(p_sap), psz_addr, SAP_PORT, 255 );
380 if( p_address->i_wfd != -1 )
382 shutdown( p_address->i_wfd, SHUT_RD );
383 p_address->origlen = sizeof (p_address->orig);
384 getsockname (p_address->i_wfd, (struct sockaddr *)&p_address->orig,
385 &p_address->origlen);
388 if( p_sap->b_control == VLC_TRUE )
390 p_address->i_rfd = net_ListenUDP1( (vlc_object_t*)p_sap, psz_addr, SAP_PORT );
391 if( p_address->i_rfd != -1 )
392 shutdown( p_address->i_rfd, SHUT_WR );
393 p_address->i_buff = 0;
394 p_address->b_enabled = VLC_TRUE;
395 p_address->b_ready = VLC_FALSE;
396 p_address->i_limit = 10000; /* 10000 bps */
401 p_address->b_enabled = VLC_TRUE;
402 p_address->b_ready = VLC_TRUE;
403 p_address->i_interval = config_GetInt( p_sap,"sap-interval");
404 p_address->i_rfd = -1;
407 if( p_address->i_wfd == -1 || (p_address->i_rfd == -1
408 && p_sap->b_control ) )
410 msg_Warn( p_sap, "disabling address" );
411 p_address->b_enabled = VLC_FALSE;
414 INSERT_ELEM( p_sap->pp_addresses,
418 p_sap_session->p_address = p_address;
421 memcpy (&p_session->orig, &p_sap_session->p_address->orig,
422 p_session->origlen = p_sap_session->p_address->origlen);
424 size_t headsize = 20;
425 switch (p_session->orig.ss_family)
436 msg_Err( p_sap, "Address family %d not supported by SAP",
438 vlc_mutex_unlock( &p_sap->object_lock );
442 /* If needed, build the SDP */
443 assert( p_session->psz_sdp != NULL );
445 p_sap_session->i_last = 0;
446 p_sap_session->i_length = headsize + strlen (p_session->psz_sdp);
447 p_sap_session->psz_data = malloc (p_sap_session->i_length + 1);
448 if (p_sap_session->psz_data == NULL)
450 free (p_session->psz_sdp);
451 vlc_mutex_unlock( &p_sap->object_lock );
455 /* Build the SAP Headers */
456 uint8_t *psz_head = p_sap_session->psz_data;
458 /* SAPv1, not encrypted, not compressed */
460 psz_head[1] = 0x00; /* No authentification length */
463 psz_head[2] = i_hash >> 8; /* Msg id hash */
464 psz_head[3] = i_hash; /* Msg id hash 2 */
467 switch (p_session->orig.ss_family)
472 struct in6_addr *a6 =
473 &((struct sockaddr_in6 *)&p_session->orig)->sin6_addr;
474 memcpy (psz_head + headsize, a6, 16);
475 psz_head[0] |= 0x10; /* IPv6 flag */
483 (((struct sockaddr_in *)&p_session->orig)->sin_addr.s_addr);
484 memcpy (psz_head + headsize, &ipv4, 4);
491 memcpy (psz_head + headsize, "application/sdp", 16);
494 /* Build the final message */
495 strcpy( (char *)psz_head + headsize, p_session->psz_sdp);
497 /* Enqueue the announce */
498 INSERT_ELEM( p_sap->pp_sessions,
502 msg_Dbg( p_sap,"%i addresses, %i sessions",
503 p_sap->i_addresses,p_sap->i_sessions);
505 vlc_mutex_unlock( &p_sap->object_lock );
510 /* Remove a SAP Announce */
511 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
512 session_descriptor_t *p_session )
515 vlc_mutex_lock( &p_sap->object_lock );
517 msg_Dbg( p_sap, "removing session %p from SAP", p_session);
519 /* Dequeue the announce */
520 for( i = 0; i< p_sap->i_sessions; i++)
522 if( p_session == p_sap->pp_sessions[i]->p_sd )
524 sap_session_t *p_mysession = p_sap->pp_sessions[i];
525 REMOVE_ELEM( p_sap->pp_sessions,
529 free( p_mysession->psz_data );
535 /* XXX: Dequeue the address too if it is not used anymore
536 * TODO: - address refcount
537 - send a SAP deletion packet */
539 msg_Dbg( p_sap,"%i announcements remaining", p_sap->i_sessions );
541 vlc_mutex_unlock( &p_sap->object_lock );
546 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
547 sap_session_t *p_session )
551 /* This announce has never been sent yet */
552 if( p_session->i_last == 0 )
554 p_session->i_next = mdate()+ p_session->p_address->i_interval*1000000;
555 p_session->i_last = 1;
559 if( p_session->i_next < mdate() )
562 msg_Dbg( p_sap, "sending announce");
564 i_ret = net_Write( p_sap, p_session->p_address->i_wfd, NULL,
566 p_session->i_length );
567 if( i_ret != (int)p_session->i_length )
569 msg_Warn( p_sap, "SAP send failed on address %s (%i %i)",
570 p_session->p_address->psz_address,
571 i_ret, p_session->i_length );
573 p_session->i_last = p_session->i_next;
574 p_session->i_next = p_session->i_last
575 + p_session->p_address->i_interval*1000000;
580 static int ComputeRate( sap_address_t *p_address )
582 uint8_t buffer[SAP_MAX_BUFFER];
587 if( p_address->t1 == 0 )
589 p_address->t1 = mdate();
594 /* Might be too slow if we have huge data */
595 ssize_t i_read = recv( p_address->i_rfd, buffer, SAP_MAX_BUFFER, 0 );
603 /* We calculate the rate every 5 seconds */
604 if( i_temp - p_address->t1 < 5000000 )
606 p_address->i_buff += i_tot;
611 i_rate = (int)(8*1000000*((mtime_t)p_address->i_buff + (mtime_t)i_tot ) /
612 (i_temp - p_address->t1 ));
614 p_address->i_limit = 10000;
616 p_address->i_interval = ((1000*i_rate / p_address->i_limit) *
617 (MAX_INTERVAL - MIN_INTERVAL))/1000 + MIN_INTERVAL;
619 if( p_address->i_interval > MAX_INTERVAL || p_address->i_interval < 0 )
621 p_address->i_interval = MAX_INTERVAL;
624 msg_Dbg( p_sap,"%s:%i: rate=%i, interval = %i s",
625 p_address->psz_address,SAP_PORT, i_rate, p_address->i_interval );
628 p_address->b_ready = VLC_TRUE;
630 p_address->t1 = i_temp;
631 p_address->i_buff = 0;