]> git.sesse.net Git - vlc/blob - src/stream_output/announce.c
* modules/control/joystick.c
[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 #include "announce.h"
50 #include "network.h"
51
52 #define SAP_IPV4_ADDR "224.2.127.254" /* Standard port and address for SAP */
53 #define SAP_PORT 9875
54
55 #define SAP_IPV6_ADDR_1 "FF0"
56 #define SAP_IPV6_ADDR_2 "::2:7FFE"
57
58 #define DEFAULT_PORT "1234"
59
60 /****************************************************************************
61  *  Split : split a string into two parts: the one which is before the delim
62  *               and the one which is after.
63  *               NULL is returned if delim is not found
64  ****************************************************************************/
65
66 static char * split( char *psz_in, char *psz_out1, char *psz_out2, char delim)
67 {
68     unsigned int i_count = 0; /* pos in input string */
69     unsigned int i_pos1  = 0; /* pos in out2 string */
70     unsigned int i_pos2  = 0;
71     char *psz_cur; /* store the pos of the first delim found */
72
73     /* Skip spaces at the beginning */
74     while( psz_in[i_count] == ' ' )
75     {
76         i_count++;
77     }
78
79     if( psz_in[i_count] == '\0' )
80     {
81         return NULL;
82     }
83
84     /* Look for delim */
85     while( psz_in[i_count] && psz_in[i_count] != delim )
86     {
87         psz_out1[i_pos1] = psz_in[i_count];
88         i_count++;
89         i_pos1++;
90     }
91     /* Mark the end of out1 */
92     psz_out1[i_pos1] = '\0';
93
94     if( psz_in[i_count] == '\0' )
95     {
96         return NULL;
97     }
98
99     /* store pos of the first delim */
100     psz_cur = psz_in + i_count;
101
102     /* skip all delim and all spaces */
103     while( psz_in[i_count] == ' ' || psz_in[i_count] == delim )
104     {
105         i_count++;
106     }
107
108     if( psz_in[i_count] == '\0' )
109     {
110         return psz_cur;
111     }
112
113     /* Store the second string */
114     while( psz_in[i_count] )
115     {
116         psz_out2[i_pos2] = psz_in[i_count];
117         i_pos2++;
118         i_count++;
119     }
120     psz_out2[i_pos2] = '\0';
121
122     return psz_cur;
123 }
124
125 /*****************************************************************************
126  * sout_SAPNew: Creates a SAP Session
127  *****************************************************************************/
128 sap_session_t * sout_SAPNew ( sout_instance_t *p_sout, char * psz_url_arg,
129                               char * psz_name_arg, int ip_version,
130                               char * psz_v6_scope )
131 {
132     sap_session_t       *p_sap; /* The SAP structure */
133     module_t            *p_network; /* Network module */
134     network_socket_t    socket_desc; /* Socket descriptor */
135     char                *sap_ipv6_addr = NULL; /* IPv6 built address */
136     char                *psz_eol; /* Used to parse IPv6 URIs */
137     int                 i_port; /* Port in numerical format */
138
139     /* Allocate the SAP structure */
140     p_sap = (sap_session_t *) malloc( sizeof ( sap_session_t ) ) ;
141     if ( !p_sap )
142     {
143         msg_Err( p_sout, "out of memory" );
144         return NULL;
145     }
146
147     /* Fill the information in the structure */
148     if( strstr( psz_url_arg, "[" ) )
149     {
150         /* We have an IPv6 address. Do not use ':' as the port separator */
151         psz_eol = strchr( psz_url_arg, ']' );
152         if( !psz_eol )
153         {
154             msg_Warn( p_sout, "no matching ], unable to parse URI");
155             return NULL;
156         }
157
158         if( !psz_eol++ )
159         {
160             sprintf( p_sap->psz_url, "%s", psz_url_arg );
161             sprintf( p_sap->psz_port, "%s", DEFAULT_PORT );
162         }
163         else
164         {
165             *psz_eol = '\0';
166             sprintf( p_sap->psz_url, "%s", psz_url_arg );
167             psz_eol++;
168             if( psz_eol )
169             {
170                 sprintf( p_sap->psz_port, "%s", psz_eol );
171             }
172         }
173     }
174     else
175     {
176         split( psz_url_arg, p_sap->psz_url, p_sap->psz_port, ':' );
177     }
178
179     /* Check if we have a port */
180     if( !strlen( p_sap->psz_port ) )
181     {
182         sprintf( p_sap->psz_port, "%s", DEFAULT_PORT );
183     }
184
185     /* Make sure our port is valid and atoi it */
186     i_port = atoi( p_sap->psz_port );
187
188     if( !i_port )
189     {
190         sprintf( p_sap->psz_port, "%s", DEFAULT_PORT );
191     }
192     else
193     {
194         sprintf( p_sap->psz_port, "%i", i_port );
195     }
196
197     /* The name that we send */
198     sprintf( p_sap->psz_name, "%s", psz_name_arg );
199
200     p_sap->i_ip_version = ip_version;
201
202     /* Only "6" triggers IPv6. IPv4 is default */
203     if( ip_version != 6 )
204     {
205         msg_Dbg( p_sout, "creating IPv4 SAP socket" );
206
207         /* Fill the socket descriptor */
208         socket_desc.i_type            = NETWORK_UDP;
209         socket_desc.psz_bind_addr     = "";
210         socket_desc.i_bind_port       = 0;
211         socket_desc.psz_server_addr   = SAP_IPV4_ADDR;
212         socket_desc.i_server_port     = SAP_PORT;
213         socket_desc.i_handle          = 0;
214
215         /* Call the network module */
216         p_sout->p_private = (void*) &socket_desc;
217         if( !( p_network = module_Need( p_sout, "network", "ipv4" ) ) )
218         {
219              msg_Warn( p_sout, "failed to open a connection (udp)" );
220              return NULL;
221         }
222         module_Unneed( p_sout, p_network );
223
224         p_sap->i_socket = socket_desc.i_handle;
225         if( p_sap->i_socket < 0 )
226         {
227             msg_Warn( p_sout, "unable to initialize SAP" );
228             return NULL;
229         }
230     }
231     else
232     {
233         msg_Dbg( p_sout, "creating IPv6 SAP socket with scope %s",
234                          psz_v6_scope );
235
236         /* Initialize and build the IPv6 address to broadcast to */
237         sap_ipv6_addr = (char *) malloc( 28 * sizeof(char) );
238         if ( !sap_ipv6_addr )
239         {
240             msg_Err( p_sout, "out of memory" );
241             return NULL;
242         }
243         sprintf( sap_ipv6_addr, "%s%c%s",
244                  SAP_IPV6_ADDR_1, psz_v6_scope[0], SAP_IPV6_ADDR_2 );
245
246         /* Fill the socket descriptor */
247         socket_desc.i_type          = NETWORK_UDP;
248         socket_desc.psz_bind_addr   = "";
249         socket_desc.i_bind_port     = 0;
250         socket_desc.psz_server_addr = sap_ipv6_addr;
251         socket_desc.i_server_port   = SAP_PORT;
252         socket_desc.i_handle        = 0;
253
254         /* Call the network module */
255         p_sout->p_private = (void *) &socket_desc;
256         if( !( p_network = module_Need( p_sout, "network", "ipv6" ) ) )
257         {
258             msg_Warn( p_sout, "failed to open a connection (udp)" );
259             return NULL;
260         }
261         module_Unneed( p_sout, p_network );
262
263         p_sap->i_socket = socket_desc.i_handle;
264         if( p_sap->i_socket <= 0 )
265         {
266             msg_Warn( p_sout, "unable to initialize SAP" );
267             return NULL;
268         }
269
270         /* Free what we allocated */
271         if( sap_ipv6_addr )
272         {
273             free( sap_ipv6_addr );
274         }
275     }
276
277     msg_Dbg( p_sout, "SAP initialization complete" );
278
279     return p_sap;
280 }
281
282 /*****************************************************************************
283  * sout_SAPDelete: Deletes a SAP Session
284  *****************************************************************************/
285 void sout_SAPDelete( sout_instance_t *p_sout, sap_session_t * p_sap )
286 {
287     int i_ret;
288
289 #if defined( UNDER_CE )
290     i_ret = CloseHandle( (HANDLE)p_sap->i_socket );
291 #elif defined( WIN32 )
292     i_ret = closesocket( p_sap->i_socket );
293 #else
294     i_ret = close( p_sap->i_socket );
295 #endif
296
297     if( i_ret )
298     {
299         msg_Err( p_sout, "unable to close SAP socket" );
300     }
301
302     free( p_sap );
303 }
304
305 /*****************************************************************************
306  * sout_SAPSend: Sends a SAP packet
307  *****************************************************************************/
308 void sout_SAPSend( sout_instance_t *p_sout, sap_session_t * p_sap )
309 {
310     char psz_msg[1000];                     /* SDP content */
311     char *psz_head;                         /* SAP header */
312     char *psz_send;                         /* What we send */
313     char *psz_type = "application/sdp";
314     int i_header_size;                      /* SAP header size */
315     int i_msg_size;                         /* SDP content size */
316     int i_size;                             /* Total size */
317     int i_ret = 0;
318
319     /* We send a packet every 24 calls to the function */
320     if( p_sap->i_calls++ < 24 )
321     {
322         return;
323     }
324
325     i_header_size = 8 + strlen( psz_type ) + 1;
326     psz_head = (char *) malloc( i_header_size * sizeof( char ) );
327
328     if( ! psz_head )
329     {
330         msg_Err( p_sout, "out of memory" );
331         return;
332     }
333
334     /* Create the SAP headers */
335     psz_head[0] = 0x20; /* Means IPv4, not encrypted, not compressed */
336     psz_head[1] = 0x00; /* No authentification */
337     psz_head[2] = 0x42; /* Version */
338     psz_head[3] = 0x12; /* Version */
339
340     psz_head[4] = 0x01; /* Source IP  FIXME: we should get the real address */
341     psz_head[5] = 0x02; /* idem */
342     psz_head[6] = 0x03; /* idem */
343     psz_head[7] = 0x04; /* idem */
344
345     strncpy( psz_head + 8, psz_type, 15 );
346     psz_head[ i_header_size-1 ] = '\0';
347
348     /* Create the SDP content */
349     /* Do not add spaces at beginning of the lines ! */
350     sprintf( psz_msg, "v=0\n"
351                       "o=VideoLAN 3247692199 3247895918 IN IP4 VideoLAN\n"
352                       "s=%s\n"
353                       "u=VideoLAN\n"
354                       "t=0 0\n"
355                       "m=audio %s udp 14\n"
356                       "c=IN IP4 %s/15\n"
357                       "a=type:test\n",
358              p_sap->psz_name, p_sap->psz_port, p_sap->psz_url );
359
360     i_msg_size = strlen( psz_msg );
361     i_size = i_msg_size + i_header_size;
362
363     /* Create the message */
364     psz_send = (char *) malloc( i_size*sizeof(char) );
365     if( !psz_send )
366     {
367         msg_Err( p_sout, "out of memory" );
368         return;
369     }
370
371     memcpy( psz_send, psz_head, i_header_size );
372     memcpy( psz_send + i_header_size, psz_msg, i_msg_size );
373
374     if( i_size < 1024 ) /* We mustn't send packets larger than 1024B */
375     {
376         i_ret = send( p_sap->i_socket, psz_send, i_size, 0 );
377     }
378
379     if( i_ret <= 0 )
380     {
381         msg_Warn( p_sout, "SAP send failed on socket %i (%s)",
382                           p_sap->i_socket, strerror(errno) );
383     }
384
385     p_sap->i_calls = 0;
386
387     /* Free what we allocated */
388     free( psz_send );
389     free( psz_head );
390 }
391