]> git.sesse.net Git - vlc/blob - src/stream_output/announce.c
* src/stream_output/announce.c: inet_pton() isn't supported on win32 so disabled...
[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 <unistd.h>
32
33 #include <vlc/vlc.h>
34
35 #include <vlc/sout.h>
36 #undef DEBUG_BUFFER
37
38 #include <announce.h>
39 #include <network.h>
40
41 #define SAP_IPV4_ADDR "224.2.127.254" /* Standard port and address for SAP */
42 #define SAP_PORT 9875
43
44 #define SAP_IPV6_ADDR_1 "FF0"
45 #define SAP_IPV6_ADDR_2 "::2:7FFE"
46
47 /*****************************************************************************
48  * sout_SAPNew: Creates a SAP Session
49  *****************************************************************************/
50 sap_session_t * sout_SAPNew ( sout_instance_t *p_sout , 
51                 char * psz_url_arg , char *psz_port_arg , 
52                 char * psz_name_arg, int ip_version, 
53                 char * psz_v6_scope )
54 {
55       
56     sap_session_t       *p_new; /* The SAP structure */
57     module_t            *p_network; /* Network module */
58     network_socket_t    socket_desc; /* Socket descriptor */
59     char                psz_network[6]; /* IPv4 or IPv6 */
60     struct              sockaddr_in addr; /* IPv4 connection structure */
61     struct              sockaddr_in6 addr6;/* IPv6 connection structure */
62     char                *sap_ipv6_addr=NULL; /* IPv6 built address */     
63     void                *net_ipv6_addr=NULL; /* IPv6 address in net-format */
64     int                 i_status=0;  /* Problems on system calls*/
65     
66     /* Allocate the SAP structure */ 
67     p_new = (sap_session_t *)malloc( sizeof ( sap_session_t ) ) ;
68     if ( !p_new )
69     {
70         msg_Err( p_sout, "No memory left" );        
71         return NULL;
72     }
73                 
74     /* Fill the information in the structure */
75     sprintf ( p_new->psz_url , "%s" , psz_url_arg );
76     sprintf ( p_new->psz_name , "%s" , psz_name_arg );
77     /* Port is not implemented in sout */
78     sprintf ( p_new->psz_port, "%s" , psz_port_arg );
79
80     p_new->i_ip_version = ip_version;
81                         
82     /* Only "6" triggers IPv6. IPv4 is default */
83     if(ip_version != 6)
84     {
85         msg_Dbg( p_sout , "Creating IPv4 SAP socket" );
86         
87         /* Fill the socket descriptor */
88         socket_desc.i_type            = NETWORK_UDP;
89         socket_desc.psz_bind_addr     = "";
90         socket_desc.i_bind_port       = 0;
91         socket_desc.psz_server_addr   = SAP_IPV4_ADDR;
92         socket_desc.i_server_port     = SAP_PORT;
93         socket_desc.i_handle          = 0;
94        
95         /* Call the network module */ 
96         sprintf ( psz_network, "ipv4" ); 
97         p_sout->p_private=(void*) &socket_desc;
98         if( !( p_network = module_Need( p_sout, "network", psz_network ) ) )
99         {
100              msg_Warn( p_sout, "failed to open a connection (udp)" );
101              return NULL;
102         }
103         module_Unneed( p_sout, p_network );
104               
105         p_new->socket   =       socket_desc.i_handle;
106         if(p_new->socket <= 0 )
107         {
108             msg_Warn( p_sout, "Unable to initialize SAP" );
109             return NULL;
110         }
111         
112         /* Fill the sockaddr_in structure */ 
113         memset( &addr , 0 , sizeof(addr) );
114         addr.sin_family      = AF_INET;
115         addr.sin_addr.s_addr = inet_addr(SAP_IPV4_ADDR);
116         addr.sin_port        = htons( SAP_PORT );
117         p_new->addr = addr;
118     }
119     else
120     {
121         msg_Dbg(p_sout , "Creating IPv6 SAP socket with scope %s" 
122                         , psz_v6_scope );
123
124         /* Initialize and build the IPv6 address to broadcast to */
125         sap_ipv6_addr = (char *)malloc(28*sizeof(char));
126         if ( !sap_ipv6_addr )
127         {
128             msg_Err( p_sout, "No memory left" );        
129             return NULL;
130         }
131         sprintf(sap_ipv6_addr,"%s%c%s",
132                          SAP_IPV6_ADDR_1,
133                          psz_v6_scope[0],
134                          SAP_IPV6_ADDR_2); 
135
136         /* Convert it to network format */
137         net_ipv6_addr = (struct in6_addr *)malloc( sizeof(struct in6_addr) );
138         if ( !net_ipv6_addr )
139         {
140             msg_Err( p_sout, "No memory left" );        
141             return NULL;
142         }
143         
144 #ifndef WIN32
145         i_status         = inet_pton(AF_INET6,sap_ipv6_addr,net_ipv6_addr);
146 #endif
147         if(i_status < 0 )
148         {
149            msg_Warn(p_sout,"Unable to convert address to network format");
150            return NULL;
151         }
152         else if(i_status == 0)
153         {
154             msg_Warn(p_sout,"Adresse de diffusion SAP invalide"); 
155             return NULL;
156         }
157         
158         /* Fill the socket descriptor */
159         socket_desc.i_type        = NETWORK_UDP;
160         socket_desc.psz_bind_addr = ""; 
161         socket_desc.i_bind_port   = 0;
162         socket_desc.psz_server_addr = sap_ipv6_addr;
163         socket_desc.i_server_port     = SAP_PORT;
164         socket_desc.i_handle          = 0;
165             
166         sprintf ( psz_network, "ipv6" ); 
167         
168         /* Call the network module */
169         p_sout->p_private=(void*) &socket_desc;
170         if( !( p_network = module_Need( p_sout, "network", psz_network ) ) )
171         {
172             msg_Warn( p_sout, "failed to open a connection (udp)" );
173             return NULL;
174         }
175         module_Unneed( p_sout, p_network );
176                
177         p_new->socket   =       socket_desc.i_handle;
178
179         if(p_new->socket <= 0 )
180         {
181              msg_Warn( p_sout, "Unable to initialize SAP" );
182              return NULL;
183          }
184                 
185         /* Fill the sockaddr_in structure */
186         memset( &addr6 , 0 , sizeof(addr6) );
187         addr6.sin6_family      = AF_INET6;
188         memcpy( &addr6.sin6_addr.s6_addr , net_ipv6_addr ,
189                    sizeof(addr6.sin6_addr.s6_addr) );
190             
191         addr6.sin6_port        = htons( SAP_PORT );
192         p_new->addr6 = addr6;
193
194         /* Free what we allocated */
195         if( sap_ipv6_addr ) free(sap_ipv6_addr);
196         if( net_ipv6_addr ) free(net_ipv6_addr);
197     }
198             
199     msg_Dbg (p_sout,"SAP initialization complete");
200   
201     return(p_new);
202 }
203
204 /*****************************************************************************
205  * sout_SAPDelete: Deletes a SAP Session 
206  *****************************************************************************/
207 void sout_SAPDelete( sout_instance_t *p_sout , sap_session_t * p_this )
208 {
209         if( close(p_this->socket) )
210                 msg_Err ( p_sout, "Unable to close SAP socket");
211         
212         if( p_this ) free( p_this );        
213 }       
214
215 /*****************************************************************************
216  * sout_SAPSend: Sends a SAP packet 
217  *****************************************************************************/
218 void sout_SAPSend( sout_instance_t *p_sout, sap_session_t * p_this)
219 {
220         
221     char *sap_head;                         /* SAP header */
222     char sap_msg[1000];                     /* SDP content */
223     char *sap_send;                         /* What we send */
224     char *payload_type="application/sdp";   
225     int i_send_result=0;                    /* Result of send */
226     int i;
227     int i_header_size;                      /* SAP header size */
228     int i_msg_size;                         /* SDP content size */
229     int i_size;                             /* Total size */
230     
231     /* We send a packet every 24 calls to the function */
232     if( p_this->sendnow == 24 )
233     {
234         i_header_size = 9 + strlen( payload_type );
235         sap_head = ( char * )malloc( i_header_size * sizeof( char ) );
236         
237         if( ! sap_head )         
238         {
239             msg_Warn( p_sout , "No memory left");
240             return;
241         }       
242          
243         /* Create the SAP headers */
244         sap_head[0]=0x20; /* Means IPv4, not encrypted, not compressed */
245         sap_head[1]=0x00; /* No authentification */
246         sap_head[2]=0x42; /* Version */
247         sap_head[3]=0x12; /* Version */
248           
249         sap_head[4]=0x01; /* Source IP  FIXME: we should get the real address */
250         sap_head[5]=0x02; /* idem */
251         sap_head[6]=0x03; /* idem */
252         sap_head[7]=0x04; /* idem */
253           
254         strncpy( sap_head+8 , payload_type , 15 );
255         sap_head[ i_header_size-1 ] = '\0'; 
256        
257         /* Create the SDP content */
258         /* Do not add spaces at beginning of the lines ! */  
259           sprintf(sap_msg,"v=0\n\
260 o=VideoLAN 3247692199 3247895918 IN IP4 VideoLAN\n\
261 s=%s\n\
262 u=VideoLAN\n\
263 t=0 0\n\
264 m=audio %s udp 14\n\
265 c=IN IP4 %s/15\n\
266 a=type:test\n", p_this->psz_name , p_this->psz_port , p_this->psz_url );
267      
268        i_msg_size = strlen( sap_msg );     
269        i_size = i_msg_size + i_header_size;
270      
271        /* Create the message */
272        sap_send = ( char* )malloc( i_size*sizeof(char) ); 
273        if(! sap_send)
274        {
275            msg_Err( p_sout ,  "No memory left") ;
276           return;
277        } 
278       
279        for(i=0 ; i<i_header_size ; i++)
280        {
281            sap_send[i] = sap_head[i];
282        }
283        
284        for( ;  i<i_size; i++)
285        {
286            sap_send[i] = sap_msg[i-i_header_size];
287        }
288     
289       if(i_size<1024) /* We mustn't send packets larger than 1024B */     
290       {
291           if( p_this->i_ip_version == 6)
292           {
293                i_send_result =  sendto( p_this->socket , sap_send ,
294                            i_size , 0 , 
295                            (struct sockaddr *)&p_this->addr6, 
296                            sizeof(p_this->addr6) );
297           } 
298           else
299           {
300                 i_send_result =  sendto( p_this->socket , sap_send ,
301                              i_size , 0 , 
302                              (struct sockaddr *)&p_this->addr , 
303                              sizeof(p_this->addr) );
304            }
305       }
306      
307       if(i_send_result == -1)
308       {
309           msg_Warn(p_sout , "SAP Send failed on socket %i. " ,
310                           p_this->socket );
311           perror("sendto"); 
312        }
313       
314        p_this->sendnow = 0;
315        
316        /* Free what we allocated */
317        if(sap_send) free(sap_send);
318        if(sap_head) free(sap_head);
319    }
320     
321    p_this->sendnow++;  
322 }