1 /*****************************************************************************
2 * announce.c : Session announcement
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
6 * Authors: Clément Stenac <zorglub@via.ecp.fr>
7 * Damien Lucas <nitrox@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* free() */
29 #include <errno.h> /* ENOMEM */
30 #include <stdio.h> /* sprintf() */
40 # include <winsock2.h>
41 # include <ws2tcpip.h>
43 # define IN_MULTICAST(a) IN_CLASSD(a)
46 # include <sys/socket.h>
56 #define SAP_IPV4_ADDR "224.2.127.254" /* Standard port and address for SAP */
59 #define SAP_IPV6_ADDR_1 "FF0"
60 #define SAP_IPV6_ADDR_2 "::2:7FFE"
62 #define DEFAULT_PORT 1234
65 /*****************************************************************************
66 * SDPGenerateUDP: create a SDP file
67 *****************************************************************************/
68 char * SDPGenerateUDP(char * psz_name_arg,char * psz_url_arg)
70 /* Create the SDP content */
71 /* Do not add spaces at beginning of the lines ! */
74 vlc_url_t * p_url; /*where parsed url will be stored*/
77 /*Allocate the URL structure*/
78 p_url = (vlc_url_t *) malloc( sizeof( vlc_url_t) );
85 vlc_UrlParse( p_url, psz_url_arg , 0);
92 if(p_url->i_port == 0)
94 p_url->i_port = DEFAULT_PORT;
97 psz_sdp = malloc( sizeof("v=0\n"
98 "o=VideoLAN 3247692199 3247895918 IN IP4 VideoLAN\n"
105 + strlen(psz_name_arg)
106 + 20 /*lengh of a 64 bits int"*/
107 + strlen(p_url->psz_host)+1);
115 sprintf( psz_sdp,"v=0\n"
116 "o=VideoLAN 3247692199 3247895918 IN IP4 VideoLAN\n"
120 "m=video %i udp 33\n"
123 psz_name_arg, p_url->i_port, p_url->psz_host );
125 vlc_UrlClean( p_url );
138 /*****************************************************************************
139 * sout_SAPNew: Creates a SAP Session
140 *****************************************************************************/
141 sap_session_t2 * sout_SAPNew ( sout_instance_t *p_sout,
144 char * psz_v6_scope )
146 sap_session_t2 *p_sap; /* The SAP structure */
147 char *sap_ipv6_addr = NULL; /* IPv6 built address */
150 var_Create( p_sout, "ipv6", VLC_VAR_BOOL );
151 var_Create( p_sout, "ipv4", VLC_VAR_BOOL );
153 /* Allocate the SAP structure */
154 p_sap = (sap_session_t2 *) malloc( sizeof ( sap_session_t2 ) ) ;
157 msg_Err( p_sout, "out of memory" );
165 p_sap->i_ip_version = ip_version;
167 p_sap->psz_sdp = psz_sdp_arg;
169 if( ip_version != 6 )
171 val.b_bool = VLC_FALSE;
172 var_Set( p_sout, "ipv6", val);
173 val.b_bool = VLC_TRUE;
174 var_Set( p_sout, "ipv4", val);
175 p_sap->i_socket = net_OpenUDP(p_sout, "", 0, SAP_IPV4_ADDR, SAP_PORT );
179 val.b_bool = VLC_TRUE;
180 var_Set( p_sout, "ipv6", val);
181 val.b_bool = VLC_FALSE;
182 var_Set( p_sout, "ipv4", val);
184 sap_ipv6_addr = (char *) malloc( 28 * sizeof(char) );
185 if ( !sap_ipv6_addr )
187 msg_Err( p_sout, "out of memory" );
190 sprintf( sap_ipv6_addr, "%s%c%s",
191 SAP_IPV6_ADDR_1, psz_v6_scope[0], SAP_IPV6_ADDR_2 );
193 p_sap->i_socket = net_OpenUDP(p_sout, "", 0, sap_ipv6_addr, SAP_PORT );
197 free( sap_ipv6_addr );
201 if((int)p_sap->i_socket <= 0)
203 msg_Warn(p_sout, "invalid SAP socket");
207 /* Free what we allocated */
209 msg_Dbg( p_sout, "SAP initialization complete" );
214 /*****************************************************************************
215 * sout_SAPDelete: Deletes a SAP Session
216 *****************************************************************************/
217 void sout_SAPDelete( sout_instance_t *p_sout, sap_session_t2 * p_sap )
221 #if defined( UNDER_CE )
222 i_ret = CloseHandle( (HANDLE)p_sap->i_socket );
223 #elif defined( WIN32 )
224 i_ret = closesocket( p_sap->i_socket );
226 i_ret = close( p_sap->i_socket );
231 msg_Err( p_sout, "unable to close SAP socket" );
236 free(p_sap->psz_sdp);
237 p_sap->psz_sdp = NULL;
243 /*****************************************************************************
244 * sout_SAPSend: Sends a SAP packet
245 *****************************************************************************/
246 void sout_SAPSend( sout_instance_t *p_sout, sap_session_t2 * p_sap )
248 char *psz_msg; /* SDP content */
249 char *psz_head; /* SAP header */
250 char *psz_send; /* What we send */
251 char *psz_type = "application/sdp";
252 int i_header_size; /* SAP header size */
253 int i_msg_size; /* SDP content size */
254 int i_size; /* Total size */
257 /* We send a packet every 24 calls to the function */
258 if( p_sap->i_calls++ < 24 )
263 i_header_size = 8 + strlen( psz_type ) + 1;
264 psz_head = (char *) malloc( i_header_size * sizeof( char ) );
268 msg_Err( p_sout, "out of memory" );
272 /* Create the SAP headers */
273 psz_head[0] = 0x20; /* Means IPv4, not encrypted, not compressed */
274 psz_head[1] = 0x00; /* No authentification */
275 psz_head[2] = 0x42; /* Version */
276 psz_head[3] = 0x12; /* Version */
278 psz_head[4] = 0x01; /* Source IP FIXME: we should get the real address */
279 psz_head[5] = 0x02; /* idem */
280 psz_head[6] = 0x03; /* idem */
281 psz_head[7] = 0x04; /* idem */
283 strncpy( psz_head + 8, psz_type, 15 );
284 psz_head[ i_header_size-1 ] = '\0';
286 psz_msg = p_sap->psz_sdp;
290 msg_Err( p_sout, "no sdp packet" );
294 i_msg_size = strlen( psz_msg );
295 i_size = i_msg_size + i_header_size;
297 /* Create the message */
298 psz_send = (char *) malloc( i_size*sizeof(char) );
301 msg_Err( p_sout, "out of memory" );
305 memcpy( psz_send, psz_head, i_header_size );
306 memcpy( psz_send + i_header_size, psz_msg, i_msg_size );
308 if( i_size < 1024 ) /* We mustn't send packets larger than 1024B */
310 i_ret = net_Write(p_sout, p_sap->i_socket, psz_send, i_size );
315 msg_Warn( p_sout, "SAP send failed on socket %i (%s)",
316 p_sap->i_socket, strerror(errno) );
321 /* Free what we allocated */
327 /*****************************************************************************
328 * sout_SLPBuildName: Builds a service name according to SLP standard
329 *****************************************************************************/
330 static char * sout_SLPBuildName(char *psz_url,char *psz_name)
335 /* name to build is: service:vlc.services.videolan://$(url) */
337 i_size = 8 + 12 + 12 + 5 + strlen(psz_url) + 1;
339 psz_service=(char *)malloc(i_size * sizeof(char));
341 snprintf( psz_service , i_size,
342 "service:vlc.services.videolan://udp:@%s",
346 psz_service[i_size]='\0'; /* Just to make sure */
352 /*****************************************************************************
353 * sout_SLPReport: Reporting function. Unused at the moment but needed
354 *****************************************************************************/
355 static void sout_SLPReport(SLPHandle slp_handle,SLPError slp_error,void* cookie)
360 /*****************************************************************************
361 * sout_SLPReg: Registers the program with SLP
362 *****************************************************************************/
363 int sout_SLPReg( sout_instance_t *p_sout, char * psz_url,
367 SLPHandle slp_handle;
369 char *psz_service= sout_SLPBuildName(psz_url,psz_name);
371 if( SLPOpen( NULL, SLP_FALSE, &slp_handle ) != SLP_OK)
373 msg_Warn(p_sout,"Unable to initialize SLP");
377 msg_Info(p_sout , "Registering %s (name: %s) in SLP",
378 psz_service , psz_name);
380 slp_res = SLPReg ( slp_handle,
382 SLP_LIFETIME_MAXIMUM,
389 if( slp_res != SLP_OK )
391 msg_Warn(p_sout,"Error while registering service: %i", slp_res );
397 #else /* This function should never be called if this is false */
403 /*****************************************************************************
404 * sout_SLDePReg: Unregisters the program from SLP
405 *****************************************************************************/
406 int sout_SLPDereg( sout_instance_t *p_sout, char * psz_url,
411 SLPHandle slp_handle;
413 char *psz_service= sout_SLPBuildName(psz_url,psz_name);
415 if( SLPOpen( NULL, SLP_FALSE, &slp_handle ) != SLP_OK)
417 msg_Warn(p_sout,"Unable to initialize SLP");
421 msg_Info(p_sout , "Unregistering %s from SLP",
424 slp_res = SLPDereg ( slp_handle,
429 if( slp_res != SLP_OK )
431 msg_Warn(p_sout,"Error while registering service: %i", slp_res );
437 #else /* This function should never be called if this is false */