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