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