]> git.sesse.net Git - vlc/blob - src/stream_output/announce.c
* configure.ac : we need to define HAVE_SLP_H
[vlc] / src / stream_output / 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 <errno.h>                                                 /* ENOMEM */
28 #include <stdlib.h>                                                /* free() */
29 #include <stdio.h>                                              /* sprintf() */
30 #include <string.h>                                            /* strerror() */
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  *  Split : split a string into two parts: the one which is before the delim
66  *               and the one which is after.
67  *               NULL is returned if delim is not found
68  ****************************************************************************/
69
70 static char * split( char *psz_in, char *psz_out1, char *psz_out2, char delim)
71 {
72     unsigned int i_count = 0; /* pos in input string */
73     unsigned int i_pos1  = 0; /* pos in out2 string */
74     unsigned int i_pos2  = 0;
75     char *psz_cur; /* store the pos of the first delim found */
76
77     /* Skip spaces at the beginning */
78     while( psz_in[i_count] == ' ' )
79     {
80         i_count++;
81     }
82
83     if( psz_in[i_count] == '\0' )
84     {
85         return NULL;
86     }
87
88     /* Look for delim */
89     while( psz_in[i_count] && psz_in[i_count] != delim )
90     {
91         psz_out1[i_pos1] = psz_in[i_count];
92         i_count++;
93         i_pos1++;
94     }
95     /* Mark the end of out1 */
96     psz_out1[i_pos1] = '\0';
97
98     if( psz_in[i_count] == '\0' )
99     {
100         return NULL;
101     }
102
103     /* store pos of the first delim */
104     psz_cur = psz_in + i_count;
105
106     /* skip all delim and all spaces */
107     while( psz_in[i_count] == ' ' || psz_in[i_count] == delim )
108     {
109         i_count++;
110     }
111
112     if( psz_in[i_count] == '\0' )
113     {
114         return psz_cur;
115     }
116
117     /* Store the second string */
118     while( psz_in[i_count] )
119     {
120         psz_out2[i_pos2] = psz_in[i_count];
121         i_pos2++;
122         i_count++;
123     }
124     psz_out2[i_pos2] = '\0';
125
126     return psz_cur;
127 }
128
129 /*****************************************************************************
130  * sout_SAPNew: Creates a SAP Session
131  *****************************************************************************/
132 sap_session_t * sout_SAPNew ( sout_instance_t *p_sout, char * psz_url_arg,
133                               char * psz_name_arg, int ip_version,
134                               char * psz_v6_scope )
135 {
136     sap_session_t       *p_sap; /* The SAP structure */
137     module_t            *p_network; /* Network module */
138     network_socket_t    socket_desc; /* Socket descriptor */
139     char                *sap_ipv6_addr = NULL; /* IPv6 built address */
140     char                *psz_eol; /* Used to parse IPv6 URIs */
141     int                 i_port; /* Port in numerical format */
142
143     /* Allocate the SAP structure */
144     p_sap = (sap_session_t *) malloc( sizeof ( sap_session_t ) ) ;
145     if ( !p_sap )
146     {
147         msg_Err( p_sout, "out of memory" );
148         return NULL;
149     }
150
151     /* Fill the information in the structure */
152     if( strstr( psz_url_arg, "[" ) )
153     {
154         /* We have an IPv6 address. Do not use ':' as the port separator */
155         psz_eol = strchr( psz_url_arg, ']' );
156         if( !psz_eol )
157         {
158             msg_Warn( p_sout, "no matching ], unable to parse URI");
159             return NULL;
160         }
161
162         if( !psz_eol++ )
163         {
164             sprintf( p_sap->psz_url, "%s", psz_url_arg );
165             sprintf( p_sap->psz_port, "%s", DEFAULT_PORT );
166         }
167         else
168         {
169             *psz_eol = '\0';
170             sprintf( p_sap->psz_url, "%s", psz_url_arg );
171             psz_eol++;
172             if( psz_eol )
173             {
174                 sprintf( p_sap->psz_port, "%s", psz_eol );
175             }
176         }
177     }
178     else
179     {
180         split( psz_url_arg, p_sap->psz_url, p_sap->psz_port, ':' );
181     }
182
183     /* Check if we have a port */
184     if( !strlen( p_sap->psz_port ) )
185     {
186         sprintf( p_sap->psz_port, "%s", DEFAULT_PORT );
187     }
188
189     /* Make sure our port is valid and atoi it */
190     i_port = atoi( p_sap->psz_port );
191
192     if( !i_port )
193     {
194         sprintf( p_sap->psz_port, "%s", DEFAULT_PORT );
195     }
196     else
197     {
198         sprintf( p_sap->psz_port, "%i", i_port );
199     }
200
201     /* The name that we send */
202     sprintf( p_sap->psz_name, "%s", psz_name_arg );
203
204     p_sap->i_ip_version = ip_version;
205
206     /* Only "6" triggers IPv6. IPv4 is default */
207     if( ip_version != 6 )
208     {
209         msg_Dbg( p_sout, "creating IPv4 SAP socket" );
210
211         /* Fill the socket descriptor */
212         socket_desc.i_type            = NETWORK_UDP;
213         socket_desc.psz_bind_addr     = "";
214         socket_desc.i_bind_port       = 0;
215         socket_desc.psz_server_addr   = SAP_IPV4_ADDR;
216         socket_desc.i_server_port     = SAP_PORT;
217         socket_desc.i_handle          = 0;
218         socket_desc.i_ttl             = 0;
219
220         /* Call the network module */
221         p_sout->p_private = (void*) &socket_desc;
222         if( !( p_network = module_Need( p_sout, "network", "ipv4" ) ) )
223         {
224              msg_Warn( p_sout, "failed to open a connection (udp)" );
225              return NULL;
226         }
227         module_Unneed( p_sout, p_network );
228
229         p_sap->i_socket = socket_desc.i_handle;
230         if( p_sap->i_socket < 0 )
231         {
232             msg_Warn( p_sout, "unable to initialize SAP" );
233             return NULL;
234         }
235     }
236     else
237     {
238         msg_Dbg( p_sout, "creating IPv6 SAP socket with scope %s",
239                          psz_v6_scope );
240
241         /* Initialize and build the IPv6 address to broadcast to */
242         sap_ipv6_addr = (char *) malloc( 28 * sizeof(char) );
243         if ( !sap_ipv6_addr )
244         {
245             msg_Err( p_sout, "out of memory" );
246             return NULL;
247         }
248         sprintf( sap_ipv6_addr, "%s%c%s",
249                  SAP_IPV6_ADDR_1, psz_v6_scope[0], SAP_IPV6_ADDR_2 );
250
251         /* Fill the socket descriptor */
252         socket_desc.i_type          = NETWORK_UDP;
253         socket_desc.psz_bind_addr   = "";
254         socket_desc.i_bind_port     = 0;
255         socket_desc.psz_server_addr = sap_ipv6_addr;
256         socket_desc.i_server_port   = SAP_PORT;
257         socket_desc.i_handle        = 0;
258         socket_desc.i_ttl           = 0;
259
260         /* Call the network module */
261         p_sout->p_private = (void *) &socket_desc;
262         if( !( p_network = module_Need( p_sout, "network", "ipv6" ) ) )
263         {
264             msg_Warn( p_sout, "failed to open a connection (udp)" );
265             return NULL;
266         }
267         module_Unneed( p_sout, p_network );
268
269         p_sap->i_socket = socket_desc.i_handle;
270         if( p_sap->i_socket <= 0 )
271         {
272             msg_Warn( p_sout, "unable to initialize SAP" );
273             return NULL;
274         }
275
276         /* Free what we allocated */
277         if( sap_ipv6_addr )
278         {
279             free( sap_ipv6_addr );
280         }
281     }
282
283     msg_Dbg( p_sout, "SAP initialization complete" );
284
285     return p_sap;
286 }
287
288 /*****************************************************************************
289  * sout_SAPDelete: Deletes a SAP Session
290  *****************************************************************************/
291 void sout_SAPDelete( sout_instance_t *p_sout, sap_session_t * p_sap )
292 {
293     int i_ret;
294
295 #if defined( UNDER_CE )
296     i_ret = CloseHandle( (HANDLE)p_sap->i_socket );
297 #elif defined( WIN32 )
298     i_ret = closesocket( p_sap->i_socket );
299 #else
300     i_ret = close( p_sap->i_socket );
301 #endif
302
303     if( i_ret )
304     {
305         msg_Err( p_sout, "unable to close SAP socket" );
306     }
307
308     free( p_sap );
309 }
310
311 /*****************************************************************************
312  * sout_SAPSend: Sends a SAP packet
313  *****************************************************************************/
314 void sout_SAPSend( sout_instance_t *p_sout, sap_session_t * p_sap )
315 {
316     char psz_msg[1000];                     /* SDP content */
317     char *psz_head;                         /* SAP header */
318     char *psz_send;                         /* What we send */
319     char *psz_type = "application/sdp";
320     int i_header_size;                      /* SAP header size */
321     int i_msg_size;                         /* SDP content size */
322     int i_size;                             /* Total size */
323     int i_ret = 0;
324
325     /* We send a packet every 24 calls to the function */
326     if( p_sap->i_calls++ < 24 )
327     {
328         return;
329     }
330
331     i_header_size = 8 + strlen( psz_type ) + 1;
332     psz_head = (char *) malloc( i_header_size * sizeof( char ) );
333
334     if( ! psz_head )
335     {
336         msg_Err( p_sout, "out of memory" );
337         return;
338     }
339
340     /* Create the SAP headers */
341     psz_head[0] = 0x20; /* Means IPv4, not encrypted, not compressed */
342     psz_head[1] = 0x00; /* No authentification */
343     psz_head[2] = 0x42; /* Version */
344     psz_head[3] = 0x12; /* Version */
345
346     psz_head[4] = 0x01; /* Source IP  FIXME: we should get the real address */
347     psz_head[5] = 0x02; /* idem */
348     psz_head[6] = 0x03; /* idem */
349     psz_head[7] = 0x04; /* idem */
350
351     strncpy( psz_head + 8, psz_type, 15 );
352     psz_head[ i_header_size-1 ] = '\0';
353
354     /* Create the SDP content */
355     /* Do not add spaces at beginning of the lines ! */
356     sprintf( psz_msg, "v=0\n"
357                       "o=VideoLAN 3247692199 3247895918 IN IP4 VideoLAN\n"
358                       "s=%s\n"
359                       "u=VideoLAN\n"
360                       "t=0 0\n"
361                       "m=audio %s udp 14\n"
362                       "c=IN IP4 %s/15\n"
363                       "a=type:test\n",
364              p_sap->psz_name, p_sap->psz_port, p_sap->psz_url );
365
366     i_msg_size = strlen( psz_msg );
367     i_size = i_msg_size + i_header_size;
368
369     /* Create the message */
370     psz_send = (char *) malloc( i_size*sizeof(char) );
371     if( !psz_send )
372     {
373         msg_Err( p_sout, "out of memory" );
374         return;
375     }
376
377     memcpy( psz_send, psz_head, i_header_size );
378     memcpy( psz_send + i_header_size, psz_msg, i_msg_size );
379
380     if( i_size < 1024 ) /* We mustn't send packets larger than 1024B */
381     {
382         i_ret = send( p_sap->i_socket, psz_send, i_size, 0 );
383     }
384
385     if( i_ret <= 0 )
386     {
387         msg_Warn( p_sout, "SAP send failed on socket %i (%s)",
388                           p_sap->i_socket, strerror(errno) );
389     }
390
391     p_sap->i_calls = 0;
392
393     /* Free what we allocated */
394     free( psz_send );
395     free( psz_head );
396 }
397
398
399    
400 /*****************************************************************************
401  * sout_SLPBuildName: Builds a service name according to SLP standard
402  *****************************************************************************/
403 char * sout_SLPBuildName(char *psz_url,char *psz_name)
404 {
405     char *psz_service;
406     unsigned int i_size;
407
408     /* name to build is: service:$(name).videolan://$(url) */
409
410     i_size =  8 + strlen(psz_name) + 12 + strlen(psz_url) + 1;
411
412     psz_service=(char *)malloc(i_size * sizeof(char));
413
414     snprintf( psz_service , i_size, 
415               "service:%s.videolan://%s",
416               psz_name,psz_url);
417     psz_service[i_size]='\0'; /* Just to make sure */    
418
419     return psz_service;
420     
421 }
422
423 /*****************************************************************************
424  * sout_SLPReport: Reporting function. Unused at the moment but needed
425  *****************************************************************************/
426 #ifdef HAVE_SLP_H
427 void sout_SLPReport(SLPHandle slp_handle,SLPError slp_error,void* cookie)
428 {
429 }  
430 #endif
431
432 /*****************************************************************************
433  * sout_SLPReg: Registers the program with SLP
434  *****************************************************************************/
435 int sout_SLPReg( sout_instance_t *p_sout, char * psz_url,
436                                char * psz_name)
437 {
438 #ifdef HAVE_SLP_H
439     SLPHandle   slp_handle; 
440     SLPError    slp_res;
441     char *psz_service= sout_SLPBuildName(psz_url,psz_name);
442         
443     if( SLPOpen( NULL, SLP_FALSE, &slp_handle ) != SLP_OK)
444     {
445         msg_Warn(p_sout,"Unable to initialize SLP");
446         return -1;
447     } 
448
449     msg_Info(p_sout , "Registering %s (name: %s) in SLP",
450                       psz_service , psz_name);
451     
452     slp_res = SLPReg ( slp_handle,
453             psz_service,
454             SLP_LIFETIME_MAXIMUM,
455             NULL,
456             psz_name,
457             SLP_TRUE,
458             sout_SLPReport,
459             NULL );
460     
461     if( slp_res != SLP_OK )
462     {
463         msg_Warn(p_sout,"Error while registering service: %i", slp_res );
464         return -1;
465     }
466
467     return 0;
468
469 #else /* This function should never be called if this is false */
470     return -1 
471 #endif
472 }
473
474
475 /*****************************************************************************
476  * sout_SLDePReg: Unregisters the program from SLP
477  *****************************************************************************/
478 int sout_SLPDereg( sout_instance_t *p_sout, char * psz_url,
479                                char * psz_name)
480 {
481 #ifdef HAVE_SLP_H
482
483     SLPHandle   slp_handle; 
484     SLPError    slp_res;
485     char *psz_service= sout_SLPBuildName(psz_url,psz_name);
486         
487     if( SLPOpen( NULL, SLP_FALSE, &slp_handle ) != SLP_OK)
488     {
489         msg_Warn(p_sout,"Unable to initialize SLP");
490         return -1;
491     } 
492
493     msg_Info(p_sout , "Unregistering %s from SLP",
494                       psz_service);
495     
496     slp_res = SLPDereg ( slp_handle,
497             psz_service,
498             sout_SLPReport,
499             NULL );
500     
501     if( slp_res != SLP_OK )
502     {
503         msg_Warn(p_sout,"Error while registering service: %i", slp_res );
504         return -1;
505     }
506
507     return 0;
508
509 #else /* This function should never be called if this is false */
510     return -1 
511 #endif
512 }