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