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