]> git.sesse.net Git - vlc/blob - modules/stream_out/announce.c
* Little cleanup of sap announces :
[vlc] / modules / stream_out / announce.c
1 /*****************************************************************************
2  * announce.c : Session announcement
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  *
6  * Authors: ClĂ©ment Stenac <zorglub@via.ecp.fr>
7  *          Damien Lucas <nitrox@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                                /* free() */
28
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <stdio.h>                                              /* sprintf() */
31
32 #include <vlc/vlc.h>
33 #include <vlc/sout.h>
34
35 #ifdef HAVE_UNISTD_H
36 #   include <unistd.h>
37 #endif
38
39 #ifdef WIN32
40 #   include <winsock2.h>
41 #   include <ws2tcpip.h>
42 #   ifndef IN_MULTICAST
43 #       define IN_MULTICAST(a) IN_CLASSD(a)
44 #   endif
45 #else
46 #   include <sys/socket.h>
47 #endif
48
49 #ifdef HAVE_SLP_H
50 # include <slp.h>
51 #endif
52
53 #include "announce.h"
54 #include "network.h"
55
56 #define SAP_IPV4_ADDR "224.2.127.254" /* Standard port and address for SAP */
57 #define SAP_PORT 9875
58
59 #define SAP_IPV6_ADDR_1 "FF0"
60 #define SAP_IPV6_ADDR_2 "::2:7FFE"
61
62 #define DEFAULT_PORT 1234
63
64
65 /*****************************************************************************
66  * SDPGenerateUDP: create a SDP file
67  *****************************************************************************/
68 char * SDPGenerateUDP(char * psz_name_arg,char * psz_url_arg)
69 {
70     /* Create the SDP content */
71     /* Do not add spaces at beginning of the lines ! */
72
73     char                * psz_sdp;
74     vlc_url_t           * p_url; /*where parsed url will be stored*/
75
76
77     /*Allocate the URL structure*/
78     p_url = (vlc_url_t *) malloc( sizeof( vlc_url_t) );
79     if ( ! p_url )
80     {
81         return NULL;
82     }
83
84
85     vlc_UrlParse( p_url, psz_url_arg , 0);
86
87     if (!p_url->psz_host)
88     {
89         return NULL;
90     }
91
92     if(p_url->i_port == 0)
93     {
94         p_url->i_port = DEFAULT_PORT;
95     }
96
97     psz_sdp = malloc( sizeof("v=0\n"
98                    "o=VideoLAN 3247692199 3247895918 IN IP4 VideoLAN\n"
99                    "s=\n"
100                    "u=VideoLAN\n"
101                    "t=0 0\n"
102                    "m=video  udp 33\n"
103                    "c=IN IP4 /15\n"
104                    "a=type:test\n")
105            + strlen(psz_name_arg)
106            + 20                             /*lengh of a 64 bits int"*/
107            + strlen(p_url->psz_host)+1);
108
109
110     if ( !psz_sdp )
111     {
112         return NULL;
113     }
114
115     sprintf( psz_sdp,"v=0\n"
116                       "o=VideoLAN 3247692199 3247895918 IN IP4 VideoLAN\n"
117                       "s=%s\n"
118                       "u=VideoLAN\n"
119                       "t=0 0\n"
120                       "m=video %i udp 33\n"
121                       "c=IN IP4 %s/15\n"
122                       "a=type:test\n",
123              psz_name_arg, p_url->i_port, p_url->psz_host );
124
125     vlc_UrlClean( p_url );
126
127     if (p_url)
128     {
129         free(p_url);
130     }
131
132     p_url = NULL;
133
134     return psz_sdp;
135 }
136
137
138 /*****************************************************************************
139  * sout_SAPNew: Creates a SAP Session
140  *****************************************************************************/
141 sap_session_t * sout_SAPNew ( sout_instance_t *p_sout,
142                                      char * psz_sdp_arg,
143                                      int ip_version,
144                                      char * psz_v6_scope )
145 {
146     sap_session_t       *p_sap; /* The SAP structure */
147     char                *sap_ipv6_addr = NULL; /* IPv6 built address */
148     vlc_value_t         val;
149
150     var_Create( p_sout, "ipv6", VLC_VAR_BOOL );
151     var_Create( p_sout, "ipv4", VLC_VAR_BOOL );
152
153     /* Allocate the SAP structure */
154     p_sap = (sap_session_t *) malloc( sizeof ( sap_session_t ) ) ;
155     if ( !p_sap )
156     {
157         msg_Err( p_sout, "out of memory" );
158         return NULL;
159     }
160
161     p_sap->i_socket = 0;
162
163     p_sap->psz_sdp=NULL;
164
165     p_sap->i_ip_version = ip_version;
166
167     p_sap->psz_sdp = psz_sdp_arg;
168
169     if( ip_version != 6 )
170     {
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 );
176     }
177     else
178     {
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);
183
184         sap_ipv6_addr = (char *) malloc( 28 * sizeof(char) );
185         if ( !sap_ipv6_addr )
186         {
187             msg_Err( p_sout, "out of memory" );
188             return NULL;
189         }
190         sprintf( sap_ipv6_addr, "%s%c%s",
191                  SAP_IPV6_ADDR_1, psz_v6_scope[0], SAP_IPV6_ADDR_2 );
192
193         p_sap->i_socket = net_OpenUDP(p_sout, "", 0, sap_ipv6_addr, SAP_PORT );
194
195         if( sap_ipv6_addr )
196         {
197             free( sap_ipv6_addr );
198         }
199     }
200
201     if((int)p_sap->i_socket <= 0)
202     {
203         msg_Warn(p_sout, "invalid SAP socket");
204         return NULL;
205     }
206
207         /* Free what we allocated */
208
209     msg_Dbg( p_sout, "SAP initialization complete" );
210
211     return p_sap;
212 }
213
214 /*****************************************************************************
215  * sout_SAPDelete: Deletes a SAP Session
216  *****************************************************************************/
217 void sout_SAPDelete( sout_instance_t *p_sout, sap_session_t * p_sap )
218 {
219     int i_ret;
220
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 );
225 #else
226     i_ret = close( p_sap->i_socket );
227 #endif
228
229     if( i_ret )
230     {
231         msg_Err( p_sout, "unable to close SAP socket" );
232     }
233
234     if (p_sap->psz_sdp)
235     {
236         free(p_sap->psz_sdp);
237         p_sap->psz_sdp = NULL;
238     }
239
240     free( p_sap );
241 }
242
243 /*****************************************************************************
244  * sout_SAPSend: Sends a SAP packet
245  *****************************************************************************/
246 void sout_SAPSend( sout_instance_t *p_sout, sap_session_t * p_sap )
247 {
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 */
255     int i_ret = 0;
256
257     /* We send a packet every 24 calls to the function */
258     if( p_sap->i_calls++ < 24 )
259     {
260         return;
261     }
262
263     i_header_size = 8 + strlen( psz_type ) + 1;
264     psz_head = (char *) malloc( i_header_size * sizeof( char ) );
265
266     if( ! psz_head )
267     {
268         msg_Err( p_sout, "out of memory" );
269         return;
270     }
271
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 */
277
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 */
282
283     strncpy( psz_head + 8, psz_type, 15 );
284     psz_head[ i_header_size-1 ] = '\0';
285
286     psz_msg = p_sap->psz_sdp;
287
288     if(!psz_msg)
289     {
290         msg_Err( p_sout, "no sdp packet" );
291         return;
292     }
293
294     i_msg_size = strlen( psz_msg );
295     i_size = i_msg_size + i_header_size;
296
297     /* Create the message */
298     psz_send = (char *) malloc( i_size*sizeof(char) );
299     if( !psz_send )
300     {
301         msg_Err( p_sout, "out of memory" );
302         return;
303     }
304
305     memcpy( psz_send, psz_head, i_header_size );
306     memcpy( psz_send + i_header_size, psz_msg, i_msg_size );
307
308     if( i_size < 1024 ) /* We mustn't send packets larger than 1024B */
309     {
310         i_ret = net_Write(p_sout, p_sap->i_socket, psz_send, i_size );
311     }
312
313     if( i_ret <= 0 )
314     {
315         msg_Warn( p_sout, "SAP send failed on socket %i (%s)",
316                           p_sap->i_socket, strerror(errno) );
317     }
318
319     p_sap->i_calls = 0;
320
321     /* Free what we allocated */
322     free( psz_send );
323     free( psz_head );
324 }
325
326 #ifdef HAVE_SLP_H
327 /*****************************************************************************
328  * sout_SLPBuildName: Builds a service name according to SLP standard
329  *****************************************************************************/
330 static char * sout_SLPBuildName(char *psz_url,char *psz_name)
331 {
332     char *psz_service;
333     unsigned int i_size;
334
335     /* name to build is: service:vlc.services.videolan://$(url) */
336
337     i_size =  8 + 12 + 12 + 5 + strlen(psz_url) + 1;
338
339     psz_service=(char *)malloc(i_size * sizeof(char));
340
341     snprintf( psz_service , i_size,
342               "service:vlc.services.videolan://udp:@%s",
343               psz_url);
344         /* How piggy  ! */
345
346     psz_service[i_size]='\0'; /* Just to make sure */
347
348     return psz_service;
349
350 }
351
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)
356 {
357 }
358 #endif
359
360 /*****************************************************************************
361  * sout_SLPReg: Registers the program with SLP
362  *****************************************************************************/
363 int sout_SLPReg( sout_instance_t *p_sout, char * psz_url,
364                                char * psz_name)
365 {
366 #ifdef HAVE_SLP_H
367     SLPHandle   slp_handle;
368     SLPError    slp_res;
369     char *psz_service= sout_SLPBuildName(psz_url,psz_name);
370
371     if( SLPOpen( NULL, SLP_FALSE, &slp_handle ) != SLP_OK)
372     {
373         msg_Warn(p_sout,"Unable to initialize SLP");
374         return -1;
375     }
376
377     msg_Info(p_sout , "Registering %s (name: %s) in SLP",
378                       psz_service , psz_name);
379
380     slp_res = SLPReg ( slp_handle,
381             psz_service,
382             SLP_LIFETIME_MAXIMUM,
383             NULL,
384             psz_name,
385             SLP_TRUE,
386             sout_SLPReport,
387             NULL );
388
389     if( slp_res != SLP_OK )
390     {
391         msg_Warn(p_sout,"Error while registering service: %i", slp_res );
392         return -1;
393     }
394
395     return 0;
396
397 #else /* This function should never be called if this is false */
398     return -1;
399 #endif
400 }
401
402
403 /*****************************************************************************
404  * sout_SLDePReg: Unregisters the program from SLP
405  *****************************************************************************/
406 int sout_SLPDereg( sout_instance_t *p_sout, char * psz_url,
407                                char * psz_name)
408 {
409 #ifdef HAVE_SLP_H
410
411     SLPHandle   slp_handle;
412     SLPError    slp_res;
413     char *psz_service= sout_SLPBuildName(psz_url,psz_name);
414
415     if( SLPOpen( NULL, SLP_FALSE, &slp_handle ) != SLP_OK)
416     {
417         msg_Warn(p_sout,"Unable to initialize SLP");
418         return -1;
419     }
420
421     msg_Info(p_sout , "Unregistering %s from SLP",
422                       psz_service);
423
424     slp_res = SLPDereg ( slp_handle,
425             psz_service,
426             sout_SLPReport,
427             NULL );
428
429     if( slp_res != SLP_OK )
430     {
431         msg_Warn(p_sout,"Error while registering service: %i", slp_res );
432         return -1;
433     }
434
435     return 0;
436
437 #else /* This function should never be called if this is false */
438     return -1;
439 #endif
440 }