Changeset 0878dc57d541b93055dd3c1e14a26b16acf563bc
- Timestamp:
- 17/07/03 16:28:13
(5 years ago)
- Author:
- Sam Hocevar <sam@videolan.org>
- git-committer:
- Sam Hocevar <sam@videolan.org> 1058452093 +0000
- git-parent:
[aa935693f83221d868cd51522152ef3845222fd4]
- git-author:
- Sam Hocevar <sam@videolan.org> 1058452093 +0000
- Message:
- src/stream_output/announce.c:
+ Fixed Win32 port.
+ Speed optimizations in split().
+ More coding style fixes.
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| raa93569 |
r0878dc5 |
|
| 29 | 29 | #include <string.h> /* strerror() */ |
|---|
| 30 | 30 | |
|---|
| | 31 | #include <vlc/vlc.h> |
|---|
| | 32 | #include <vlc/sout.h> |
|---|
| | 33 | |
|---|
| 31 | 34 | #ifdef HAVE_UNISTD_H |
|---|
| 32 | 35 | # include <unistd.h> |
|---|
| 33 | 36 | #endif |
|---|
| 34 | 37 | |
|---|
| 35 | | #include <vlc/vlc.h> |
|---|
| 36 | | |
|---|
| 37 | | #include <vlc/sout.h> |
|---|
| | 38 | #ifdef WIN32 |
|---|
| | 39 | # include <winsock2.h> |
|---|
| | 40 | # include <ws2tcpip.h> |
|---|
| | 41 | # ifndef IN_MULTICAST |
|---|
| | 42 | # define IN_MULTICAST(a) IN_CLASSD(a) |
|---|
| | 43 | # endif |
|---|
| | 44 | #else |
|---|
| | 45 | # include <sys/socket.h> |
|---|
| | 46 | #endif |
|---|
| | 47 | |
|---|
| 38 | 48 | #undef DEBUG_BUFFER |
|---|
| 39 | 49 | |
|---|
| 40 | | #include <announce.h> |
|---|
| 41 | | #include <network.h> |
|---|
| | 50 | #include "announce.h" |
|---|
| | 51 | #include "network.h" |
|---|
| 42 | 52 | |
|---|
| 43 | 53 | #define SAP_IPV4_ADDR "224.2.127.254" /* Standard port and address for SAP */ |
|---|
| … | … | |
| 57 | 67 | static char * split( char *psz_in, char *psz_out1, char *psz_out2, char delim) |
|---|
| 58 | 68 | { |
|---|
| 59 | | unsigned int i_count = 0; /*pos in input string*/ |
|---|
| 60 | | unsigned int i_pos1 = 0; /*pos in out2 string */ |
|---|
| | 69 | unsigned int i_count = 0; /* pos in input string */ |
|---|
| | 70 | unsigned int i_pos1 = 0; /* pos in out2 string */ |
|---|
| 61 | 71 | 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)) |
|---|
| | 72 | char *psz_cur; /* store the pos of the first delim found */ |
|---|
| | 73 | |
|---|
| | 74 | /* Skip spaces at the beginning */ |
|---|
| | 75 | while( psz_in[i_count] == ' ' ) |
|---|
| 66 | 76 | { |
|---|
| 67 | 77 | i_count++; |
|---|
| 68 | 78 | } |
|---|
| 69 | | if(i_count == strlen(psz_in)) |
|---|
| | 79 | |
|---|
| | 80 | if( psz_in[i_count] == '\0' ) |
|---|
| | 81 | { |
|---|
| 70 | 82 | return NULL; |
|---|
| 71 | | |
|---|
| 72 | | /*Look for delim*/ |
|---|
| 73 | | while(psz_in[i_count] != delim && i_count < strlen(psz_in)) |
|---|
| | 83 | } |
|---|
| | 84 | |
|---|
| | 85 | /* Look for delim */ |
|---|
| | 86 | while( psz_in[i_count] && psz_in[i_count] != delim ) |
|---|
| 74 | 87 | { |
|---|
| 75 | 88 | psz_out1[i_pos1] = psz_in[i_count]; |
|---|
| … | … | |
| 78 | 91 | } |
|---|
| 79 | 92 | /* Mark the end of out1 */ |
|---|
| 80 | | psz_out1[i_pos1] = 0; |
|---|
| 81 | | |
|---|
| 82 | | if(i_count == strlen(psz_in)) |
|---|
| | 93 | psz_out1[i_pos1] = '\0'; |
|---|
| | 94 | |
|---|
| | 95 | if( psz_in[i_count] == '\0' ) |
|---|
| | 96 | { |
|---|
| 83 | 97 | 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)) |
|---|
| | 98 | } |
|---|
| | 99 | |
|---|
| | 100 | /* store pos of the first delim */ |
|---|
| | 101 | psz_cur = psz_in + i_count; |
|---|
| | 102 | |
|---|
| | 103 | /* skip all delim and all spaces */ |
|---|
| | 104 | while( psz_in[i_count] == ' ' || psz_in[i_count] == delim ) |
|---|
| 94 | 105 | { |
|---|
| 95 | 106 | i_count++; |
|---|
| 96 | 107 | } |
|---|
| 97 | 108 | |
|---|
| 98 | | if(i_count == strlen(psz_in)) |
|---|
| | 109 | if( psz_in[i_count] == '\0' ) |
|---|
| | 110 | { |
|---|
| 99 | 111 | return psz_cur; |
|---|
| 100 | | |
|---|
| 101 | | /*Store the second string*/ |
|---|
| 102 | | while(i_count < strlen(psz_in)) |
|---|
| | 112 | } |
|---|
| | 113 | |
|---|
| | 114 | /* Store the second string */ |
|---|
| | 115 | while( psz_in[i_count] ) |
|---|
| 103 | 116 | { |
|---|
| 104 | 117 | psz_out2[i_pos2] = psz_in[i_count]; |
|---|
| … | … | |
| 106 | 119 | i_count++; |
|---|
| 107 | 120 | } |
|---|
| 108 | | psz_out2[i_pos2] = 0; |
|---|
| | 121 | psz_out2[i_pos2] = '\0'; |
|---|
| 109 | 122 | |
|---|
| 110 | 123 | return psz_cur; |
|---|
| … | … | |
| 114 | 127 | * sout_SAPNew: Creates a SAP Session |
|---|
| 115 | 128 | *****************************************************************************/ |
|---|
| 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 ) |
|---|
| | 129 | sap_session_t * sout_SAPNew ( sout_instance_t *p_sout, char * psz_url_arg, |
|---|
| | 130 | char * psz_name_arg, int ip_version, |
|---|
| | 131 | char * psz_v6_scope ) |
|---|
| 120 | 132 | { |
|---|
| 121 | | sap_session_t *p_new; /* The SAP structure */ |
|---|
| | 133 | sap_session_t *p_sap; /* The SAP structure */ |
|---|
| 122 | 134 | module_t *p_network; /* Network module */ |
|---|
| 123 | 135 | network_socket_t socket_desc; /* Socket descriptor */ |
|---|
| 124 | | char psz_network[6]; /* IPv4 or IPv6 */ |
|---|
| 125 | | char *sap_ipv6_addr=NULL; /* IPv6 built address */ |
|---|
| | 136 | char *sap_ipv6_addr = NULL; /* IPv6 built address */ |
|---|
| 126 | 137 | char *psz_eol; /* Used to parse IPv6 URIs */ |
|---|
| 127 | 138 | int i_port; /* Port in numerical format */ |
|---|
| 128 | 139 | |
|---|
| 129 | 140 | /* Allocate the SAP structure */ |
|---|
| 130 | | p_new = (sap_session_t *) malloc( sizeof ( sap_session_t ) ) ; |
|---|
| 131 | | if ( !p_new ) |
|---|
| | 141 | p_sap = (sap_session_t *) malloc( sizeof ( sap_session_t ) ) ; |
|---|
| | 142 | if ( !p_sap ) |
|---|
| 132 | 143 | { |
|---|
| 133 | 144 | msg_Err( p_sout, "out of memory" ); |
|---|
| … | … | |
| 137 | 148 | /* Fill the information in the structure */ |
|---|
| 138 | 149 | if( strstr( psz_url_arg, "[" ) ) |
|---|
| 139 | | { /* We have an IPv6 address. Do not use ':' as the port separator */ |
|---|
| | 150 | { |
|---|
| | 151 | /* We have an IPv6 address. Do not use ':' as the port separator */ |
|---|
| 140 | 152 | psz_eol = strchr( psz_url_arg, ']' ); |
|---|
| 141 | | if( !psz_eol ) /* No matching ] ! Aborting */ |
|---|
| | 153 | if( !psz_eol ) |
|---|
| 142 | 154 | { |
|---|
| 143 | 155 | msg_Warn( p_sout, "no matching ], unable to parse URI"); |
|---|
| 144 | 156 | return NULL; |
|---|
| 145 | 157 | } |
|---|
| | 158 | |
|---|
| 146 | 159 | if( !psz_eol++ ) |
|---|
| 147 | 160 | { |
|---|
| 148 | | sprintf( p_new->psz_url, "%s", psz_url_arg ); |
|---|
| 149 | | sprintf( p_new->psz_port, "%s", DEFAULT_PORT ); |
|---|
| | 161 | sprintf( p_sap->psz_url, "%s", psz_url_arg ); |
|---|
| | 162 | sprintf( p_sap->psz_port, "%s", DEFAULT_PORT ); |
|---|
| 150 | 163 | } |
|---|
| 151 | 164 | else |
|---|
| 152 | 165 | { |
|---|
| 153 | 166 | *psz_eol = '\0'; |
|---|
| 154 | | sprintf( p_new->psz_url, "%s", psz_url_arg ); |
|---|
| | 167 | sprintf( p_sap->psz_url, "%s", psz_url_arg ); |
|---|
| 155 | 168 | psz_eol++; |
|---|
| 156 | 169 | if( psz_eol ) |
|---|
| 157 | 170 | { |
|---|
| 158 | | sprintf( p_new->psz_port, "%s", psz_eol ); |
|---|
| | 171 | sprintf( p_sap->psz_port, "%s", psz_eol ); |
|---|
| 159 | 172 | } |
|---|
| 160 | 173 | } |
|---|
| … | … | |
| 162 | 175 | else |
|---|
| 163 | 176 | { |
|---|
| 164 | | split( psz_url_arg, p_new->psz_url, p_new->psz_port, ':' ); |
|---|
| | 177 | split( psz_url_arg, p_sap->psz_url, p_sap->psz_port, ':' ); |
|---|
| 165 | 178 | } |
|---|
| 166 | 179 | |
|---|
| 167 | 180 | /* 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 ); |
|---|
| | 181 | if( !strlen( p_sap->psz_port ) ) |
|---|
| | 182 | { |
|---|
| | 183 | sprintf( p_sap->psz_port, "%s", DEFAULT_PORT ); |
|---|
| | 184 | } |
|---|
| | 185 | |
|---|
| | 186 | /* Make sure our port is valid and atoi it */ |
|---|
| | 187 | i_port = atoi( p_sap->psz_port ); |
|---|
| 175 | 188 | |
|---|
| 176 | 189 | if( !i_port ) |
|---|
| 177 | 190 | { |
|---|
| 178 | | sprintf( p_new->psz_port, "%s", DEFAULT_PORT ); |
|---|
| | 191 | sprintf( p_sap->psz_port, "%s", DEFAULT_PORT ); |
|---|
| 179 | 192 | } |
|---|
| 180 | 193 | else |
|---|
| 181 | 194 | { |
|---|
| 182 | | sprintf( p_new->psz_port, "%i", i_port ); |
|---|
| | 195 | sprintf( p_sap->psz_port, "%i", i_port ); |
|---|
| 183 | 196 | } |
|---|
| 184 | 197 | |
|---|
| 185 | 198 | /* The name that we send */ |
|---|
| 186 | | sprintf( p_new->psz_name, "%s", psz_name_arg ); |
|---|
| 187 | | |
|---|
| 188 | | p_new->i_ip_version = ip_version; |
|---|
| | 199 | sprintf( p_sap->psz_name, "%s", psz_name_arg ); |
|---|
| | 200 | |
|---|
| | 201 | p_sap->i_ip_version = ip_version; |
|---|
| 189 | 202 | |
|---|
| 190 | 203 | /* Only "6" triggers IPv6. IPv4 is default */ |
|---|
| … | … | |
| 202 | 215 | |
|---|
| 203 | 216 | /* Call the network module */ |
|---|
| 204 | | sprintf ( psz_network, "ipv4" ); |
|---|
| 205 | 217 | p_sout->p_private = (void*) &socket_desc; |
|---|
| 206 | | if( !( p_network = module_Need( p_sout, "network", psz_network ) ) ) |
|---|
| | 218 | if( !( p_network = module_Need( p_sout, "network", "ipv4" ) ) ) |
|---|
| 207 | 219 | { |
|---|
| 208 | 220 | msg_Warn( p_sout, "failed to open a connection (udp)" ); |
|---|
| … | … | |
| 211 | 223 | module_Unneed( p_sout, p_network ); |
|---|
| 212 | 224 | |
|---|
| 213 | | p_new->i_socket = socket_desc.i_handle; |
|---|
| 214 | | if( p_new->i_socket < 0 ) |
|---|
| | 225 | p_sap->i_socket = socket_desc.i_handle; |
|---|
| | 226 | if( p_sap->i_socket < 0 ) |
|---|
| 215 | 227 | { |
|---|
| 216 | 228 | msg_Warn( p_sout, "unable to initialize SAP" ); |
|---|
| … | … | |
| 241 | 253 | socket_desc.i_handle = 0; |
|---|
| 242 | 254 | |
|---|
| 243 | | sprintf ( psz_network, "ipv6" ); |
|---|
| 244 | | |
|---|
| 245 | 255 | /* Call the network module */ |
|---|
| 246 | 256 | p_sout->p_private = (void *) &socket_desc; |
|---|
| 247 | | if( !( p_network = module_Need( p_sout, "network", psz_network ) ) ) |
|---|
| | 257 | if( !( p_network = module_Need( p_sout, "network", "ipv6" ) ) ) |
|---|
| 248 | 258 | { |
|---|
| 249 | 259 | msg_Warn( p_sout, "failed to open a connection (udp)" ); |
|---|
| … | … | |
| 252 | 262 | module_Unneed( p_sout, p_network ); |
|---|
| 253 | 263 | |
|---|
| 254 | | p_new->i_socket = socket_desc.i_handle; |
|---|
| 255 | | if( p_new->i_socket <= 0 ) |
|---|
| | 264 | p_sap->i_socket = socket_desc.i_handle; |
|---|
| | 265 | if( p_sap->i_socket <= 0 ) |
|---|
| 256 | 266 | { |
|---|
| 257 | 267 | msg_Warn( p_sout, "unable to initialize SAP" ); |
|---|
| … | … | |
| 268 | 278 | msg_Dbg( p_sout, "SAP initialization complete" ); |
|---|
| 269 | 279 | |
|---|
| 270 | | return(p_new); |
|---|
| | 280 | return p_sap; |
|---|
| 271 | 281 | } |
|---|
| 272 | 282 | |
|---|
| … | … | |
| 274 | 284 | * sout_SAPDelete: Deletes a SAP Session |
|---|
| 275 | 285 | *****************************************************************************/ |
|---|
| 276 | | void sout_SAPDelete( sout_instance_t *p_sout, sap_session_t * p_this ) |
|---|
| | 286 | void sout_SAPDelete( sout_instance_t *p_sout, sap_session_t * p_sap ) |
|---|
| 277 | 287 | { |
|---|
| 278 | | if( close( p_this->i_socket ) ) |
|---|
| | 288 | int i_ret; |
|---|
| | 289 | |
|---|
| | 290 | #if defined( UNDER_CE ) |
|---|
| | 291 | i_ret = CloseHandle( (HANDLE)p_sap->i_handle ); |
|---|
| | 292 | #elif defined( WIN32 ) |
|---|
| | 293 | i_ret = closesocket( p_sap->i_handle ); |
|---|
| | 294 | #else |
|---|
| | 295 | i_ret = close( p_sap->i_handle ); |
|---|
| | 296 | #endif |
|---|
| | 297 | |
|---|
| | 298 | if( i_ret ) |
|---|
| 279 | 299 | { |
|---|
| 280 | 300 | msg_Err( p_sout, "unable to close SAP socket" ); |
|---|
| 281 | 301 | } |
|---|
| 282 | 302 | |
|---|
| 283 | | free( p_this ); |
|---|
| | 303 | free( p_sap ); |
|---|
| 284 | 304 | } |
|---|
| 285 | 305 | |
|---|
| … | … | |
| 287 | 307 | * sout_SAPSend: Sends a SAP packet |
|---|
| 288 | 308 | *****************************************************************************/ |
|---|
| 289 | | void sout_SAPSend( sout_instance_t *p_sout, sap_session_t * p_this) |
|---|
| | 309 | void sout_SAPSend( sout_instance_t *p_sout, sap_session_t * p_sap ) |
|---|
| 290 | 310 | { |
|---|
| 291 | 311 | char psz_msg[1000]; /* SDP content */ |
|---|
| … | … | |
| 299 | 319 | |
|---|
| 300 | 320 | /* We send a packet every 24 calls to the function */ |
|---|
| 301 | | if( p_this->i_calls++ < 24 ) |
|---|
| | 321 | if( p_sap->i_calls++ < 24 ) |
|---|
| 302 | 322 | { |
|---|
| 303 | 323 | return; |
|---|
| … | … | |
| 337 | 357 | "c=IN IP4 %s/15\n" |
|---|
| 338 | 358 | "a=type:test\n", |
|---|
| 339 | | p_this->psz_name, p_this->psz_port, p_this->psz_url ); |
|---|
| | 359 | p_sap->psz_name, p_sap->psz_port, p_sap->psz_url ); |
|---|
| 340 | 360 | |
|---|
| 341 | 361 | i_msg_size = strlen( psz_msg ); |
|---|
| … | … | |
| 355 | 375 | if( i_size < 1024 ) /* We mustn't send packets larger than 1024B */ |
|---|
| 356 | 376 | { |
|---|
| 357 | | if( p_this->i_ip_version == 6 ) |
|---|
| 358 | | { |
|---|
| 359 | | i_ret = send( p_this->i_socket, psz_send, i_size, 0 ); |
|---|
| | 377 | if( p_sap->i_ip_version == 6 ) |
|---|
| | 378 | { |
|---|
| | 379 | i_ret = send( p_sap->i_socket, psz_send, i_size, 0 ); |
|---|
| 360 | 380 | } |
|---|
| 361 | 381 | else |
|---|
| 362 | 382 | { |
|---|
| 363 | | i_ret = send( p_this->i_socket, psz_send, i_size, 0 ); |
|---|
| | 383 | i_ret = send( p_sap->i_socket, psz_send, i_size, 0 ); |
|---|
| 364 | 384 | } |
|---|
| 365 | 385 | } |
|---|
| … | … | |
| 368 | 388 | { |
|---|
| 369 | 389 | msg_Warn( p_sout, "SAP send failed on socket %i (%s)", |
|---|
| 370 | | p_this->i_socket, strerror(errno) ); |
|---|
| 371 | | } |
|---|
| 372 | | |
|---|
| 373 | | p_this->i_calls = 0; |
|---|
| | 390 | p_sap->i_socket, strerror(errno) ); |
|---|
| | 391 | } |
|---|
| | 392 | |
|---|
| | 393 | p_sap->i_calls = 0; |
|---|
| 374 | 394 | |
|---|
| 375 | 395 | /* Free what we allocated */ |
|---|