1 /*****************************************************************************
2 * announce.c : Session announcement
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
6 * Authors: Clément Stenac <zorglub@via.ecp.fr>
7 * Damien Lucas <nitrox@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* free() */
28 #include <stdio.h> /* sprintf() */
29 #include <string.h> /* strerror() */
41 #define SAP_IPV4_ADDR "224.2.127.254" /* Standard port and address for SAP */
44 #define SAP_IPV6_ADDR_1 "FF0"
45 #define SAP_IPV6_ADDR_2 "::2:7FFE"
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,
55 sap_session_t *p_new; /* The SAP structure */
56 module_t *p_network; /* Network module */
57 network_socket_t socket_desc; /* Socket descriptor */
58 char psz_network[6]; /* IPv4 or IPv6 */
59 char *sap_ipv6_addr=NULL; /* IPv6 built address */
61 /* Allocate the SAP structure */
62 p_new = (sap_session_t *)malloc( sizeof ( sap_session_t ) ) ;
65 msg_Err( p_sout, "No memory left" );
69 /* Fill the information in the structure */
70 sprintf ( p_new->psz_url , "%s" , psz_url_arg );
71 sprintf ( p_new->psz_name , "%s" , psz_name_arg );
72 /* Port is not implemented in sout */
73 sprintf ( p_new->psz_port, "%s" , psz_port_arg );
75 p_new->i_ip_version = ip_version;
77 /* Only "6" triggers IPv6. IPv4 is default */
80 msg_Dbg( p_sout , "Creating IPv4 SAP socket" );
82 /* Fill the socket descriptor */
83 socket_desc.i_type = NETWORK_UDP;
84 socket_desc.psz_bind_addr = "";
85 socket_desc.i_bind_port = 0;
86 socket_desc.psz_server_addr = SAP_IPV4_ADDR;
87 socket_desc.i_server_port = SAP_PORT;
88 socket_desc.i_handle = 0;
90 /* Call the network module */
91 sprintf ( psz_network, "ipv4" );
92 p_sout->p_private=(void*) &socket_desc;
93 if( !( p_network = module_Need( p_sout, "network", psz_network ) ) )
95 msg_Warn( p_sout, "failed to open a connection (udp)" );
98 module_Unneed( p_sout, p_network );
100 p_new->socket = socket_desc.i_handle;
101 if(p_new->socket <= 0 )
103 msg_Warn( p_sout, "Unable to initialize SAP" );
109 msg_Dbg(p_sout , "Creating IPv6 SAP socket with scope %s"
112 /* Initialize and build the IPv6 address to broadcast to */
113 sap_ipv6_addr = (char *)malloc(28*sizeof(char));
114 if ( !sap_ipv6_addr )
116 msg_Err( p_sout, "No memory left" );
119 sprintf(sap_ipv6_addr,"%s%c%s",
124 /* Fill the socket descriptor */
125 socket_desc.i_type = NETWORK_UDP;
126 socket_desc.psz_bind_addr = "";
127 socket_desc.i_bind_port = 0;
128 socket_desc.psz_server_addr = sap_ipv6_addr;
129 socket_desc.i_server_port = SAP_PORT;
130 socket_desc.i_handle = 0;
132 sprintf ( psz_network, "ipv6" );
134 /* Call the network module */
135 p_sout->p_private=(void*) &socket_desc;
136 if( !( p_network = module_Need( p_sout, "network", psz_network ) ) )
138 msg_Warn( p_sout, "failed to open a connection (udp)" );
141 module_Unneed( p_sout, p_network );
143 p_new->socket = socket_desc.i_handle;
145 if(p_new->socket <= 0 )
147 msg_Warn( p_sout, "Unable to initialize SAP" );
151 /* Free what we allocated */
152 if( sap_ipv6_addr ) free(sap_ipv6_addr);
155 msg_Dbg (p_sout,"SAP initialization complete");
160 /*****************************************************************************
161 * sout_SAPDelete: Deletes a SAP Session
162 *****************************************************************************/
163 void sout_SAPDelete( sout_instance_t *p_sout , sap_session_t * p_this )
165 if( close(p_this->socket) )
167 msg_Err ( p_sout, "Unable to close SAP socket");
170 if( p_this ) free( p_this );
173 /*****************************************************************************
174 * sout_SAPSend: Sends a SAP packet
175 *****************************************************************************/
176 void sout_SAPSend( sout_instance_t *p_sout, sap_session_t * p_this)
178 char *sap_head; /* SAP header */
179 char sap_msg[1000]; /* SDP content */
180 char *sap_send; /* What we send */
181 char *payload_type="application/sdp";
182 int i_send_result=0; /* Result of send */
184 int i_header_size; /* SAP header size */
185 int i_msg_size; /* SDP content size */
186 int i_size; /* Total size */
188 /* We send a packet every 24 calls to the function */
189 if( p_this->sendnow == 24 )
191 i_header_size = 9 + strlen( payload_type );
192 sap_head = ( char * )malloc( i_header_size * sizeof( char ) );
196 msg_Warn( p_sout , "No memory left");
200 /* Create the SAP headers */
201 sap_head[0]=0x20; /* Means IPv4, not encrypted, not compressed */
202 sap_head[1]=0x00; /* No authentification */
203 sap_head[2]=0x42; /* Version */
204 sap_head[3]=0x12; /* Version */
206 sap_head[4]=0x01; /* Source IP FIXME: we should get the real address */
207 sap_head[5]=0x02; /* idem */
208 sap_head[6]=0x03; /* idem */
209 sap_head[7]=0x04; /* idem */
211 strncpy( sap_head+8 , payload_type , 15 );
212 sap_head[ i_header_size-1 ] = '\0';
214 /* Create the SDP content */
215 /* Do not add spaces at beginning of the lines ! */
216 sprintf( sap_msg, "v=0\n"
217 "o=VideoLAN 3247692199 3247895918 IN IP4 VideoLAN\n"
221 "m=audio %s udp 14\n"
224 p_this->psz_name , p_this->psz_port , p_this->psz_url );
226 i_msg_size = strlen( sap_msg );
227 i_size = i_msg_size + i_header_size;
229 /* Create the message */
230 sap_send = ( char* )malloc( i_size*sizeof(char) );
233 msg_Err( p_sout , "No memory left") ;
237 for( i = 0 ; i < i_header_size ; i++ )
239 sap_send[i] = sap_head[i];
242 for( ; i < i_size ; i++ )
244 sap_send[i] = sap_msg[i-i_header_size];
247 if( i_size < 1024 ) /* We mustn't send packets larger than 1024B */
249 if( p_this->i_ip_version == 6)
251 i_send_result = send( p_this->socket, sap_send, i_size, 0 );
255 i_send_result = send( p_this->socket, sap_send, i_size, 0 );
259 if( i_send_result == -1 )
261 msg_Warn(p_sout, "SAP send failed on socket %i", p_this->socket );
267 /* Free what we allocated */
268 if(sap_send) free(sap_send);
269 if(sap_head) free(sap_head);