Changeset ff475a9143389b8026408362b900881668db4ea4
- Timestamp:
- 05/01/04 16:07:16
(5 years ago)
- Author:
- Laurent Aimar <fenrir@videolan.org>
- git-committer:
- Laurent Aimar <fenrir@videolan.org> 1073315236 +0000
- git-parent:
[fbbfa7bff5904e41cba1f2e64c0527f593f3b823]
- git-author:
- Laurent Aimar <fenrir@videolan.org> 1073315236 +0000
- Message:
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r3d6ee48 |
rff475a9 |
|
| 3 | 3 | ***************************************************************************** |
|---|
| 4 | 4 | * Copyright (C) 2003 VideoLAN |
|---|
| 5 | | * $Id: tcp.c,v 1.2 2003/12/04 16:49:43 sam Exp $ |
|---|
| | 5 | * $Id: tcp.c,v 1.3 2004/01/05 15:07:16 fenrir Exp $ |
|---|
| 6 | 6 | * |
|---|
| 7 | 7 | * Authors: Laurent Aimar <fenrir@via.ecp.fr> |
|---|
| … | … | |
| 30 | 30 | #include <vlc/input.h> |
|---|
| 31 | 31 | |
|---|
| 32 | | #include <sys/stat.h> |
|---|
| 33 | | #include <errno.h> |
|---|
| 34 | | #include <fcntl.h> |
|---|
| 35 | | |
|---|
| 36 | | #ifdef HAVE_SYS_TIME_H |
|---|
| 37 | | # include <sys/time.h> |
|---|
| 38 | | #endif |
|---|
| 39 | | |
|---|
| 40 | | #ifdef HAVE_UNISTD_H |
|---|
| 41 | | # include <unistd.h> |
|---|
| 42 | | #endif |
|---|
| 43 | | |
|---|
| 44 | | #ifdef WIN32 |
|---|
| 45 | | # include <winsock2.h> |
|---|
| 46 | | # include <ws2tcpip.h> |
|---|
| 47 | | # ifndef IN_MULTICAST |
|---|
| 48 | | # define IN_MULTICAST(a) IN_CLASSD(a) |
|---|
| 49 | | # endif |
|---|
| 50 | | #else |
|---|
| 51 | | # include <sys/socket.h> |
|---|
| 52 | | #endif |
|---|
| 53 | | |
|---|
| 54 | 32 | #include "network.h" |
|---|
| 55 | 33 | |
|---|
| … | … | |
| 71 | 49 | set_capability( "access", 0 ); |
|---|
| 72 | 50 | add_shortcut( "tcp" ); |
|---|
| 73 | | add_shortcut( "tcp4" ); |
|---|
| 74 | | add_shortcut( "tcp6" ); |
|---|
| 75 | 51 | set_callbacks( Open, Close ); |
|---|
| 76 | 52 | vlc_module_end(); |
|---|
| … | … | |
| 95 | 71 | access_sys_t *p_sys; |
|---|
| 96 | 72 | |
|---|
| 97 | | char *psz_network; |
|---|
| 98 | 73 | char *psz_dup = strdup(p_input->psz_name); |
|---|
| 99 | 74 | char *psz_parser = psz_dup; |
|---|
| 100 | 75 | |
|---|
| 101 | | network_socket_t sock; |
|---|
| 102 | | module_t *p_network; |
|---|
| 103 | | |
|---|
| 104 | | vlc_value_t val; |
|---|
| 105 | | |
|---|
| 106 | | /* Select ip version */ |
|---|
| 107 | | psz_network = ""; |
|---|
| 108 | | if( config_GetInt( p_input, "ipv4" ) ) |
|---|
| 109 | | { |
|---|
| 110 | | psz_network = "ipv4"; |
|---|
| 111 | | } |
|---|
| 112 | | if( config_GetInt( p_input, "ipv6" ) ) |
|---|
| 113 | | { |
|---|
| 114 | | psz_network = "ipv6"; |
|---|
| 115 | | } |
|---|
| 116 | | if( *p_input->psz_access ) |
|---|
| 117 | | { |
|---|
| 118 | | /* Find out which shortcut was used */ |
|---|
| 119 | | if( !strncmp( p_input->psz_access, "tcp6", 5 ) ) |
|---|
| 120 | | { |
|---|
| 121 | | psz_network = "ipv6"; |
|---|
| 122 | | } |
|---|
| 123 | | else if( !strncmp( p_input->psz_access, "tcp4", 5 ) ) |
|---|
| 124 | | { |
|---|
| 125 | | psz_network = "ipv4"; |
|---|
| 126 | | } |
|---|
| 127 | | } |
|---|
| | 76 | vlc_value_t val; |
|---|
| 128 | 77 | |
|---|
| 129 | 78 | /* Parse server:port */ |
|---|
| … | … | |
| 140 | 89 | psz_parser++; |
|---|
| 141 | 90 | } |
|---|
| 142 | | |
|---|
| 143 | 91 | if( *psz_parser != ':' || psz_parser == psz_dup ) |
|---|
| 144 | 92 | { |
|---|
| … | … | |
| 147 | 95 | return VLC_EGENERIC; |
|---|
| 148 | 96 | } |
|---|
| 149 | | |
|---|
| 150 | 97 | *psz_parser++ = '\0'; |
|---|
| 151 | 98 | |
|---|
| 152 | | /* Prepare the network_socket_t structure */ |
|---|
| 153 | | sock.i_type = NETWORK_TCP; |
|---|
| 154 | | sock.psz_bind_addr = ""; |
|---|
| 155 | | sock.i_bind_port = 0; |
|---|
| 156 | | sock.psz_server_addr = psz_dup; |
|---|
| 157 | | sock.i_server_port = atoi( psz_parser ); |
|---|
| 158 | | sock.i_ttl = 0; |
|---|
| 159 | | |
|---|
| 160 | | if( sock.i_server_port <= 0 ) |
|---|
| | 99 | if( atoi( psz_parser ) <= 0 ) |
|---|
| 161 | 100 | { |
|---|
| 162 | | msg_Err( p_input, "invalid port number (%d)", sock.i_server_port ); |
|---|
| | 101 | msg_Err( p_input, "invalid port number (%d)", atoi( psz_parser ) ); |
|---|
| 163 | 102 | free( psz_dup ); |
|---|
| 164 | 103 | return VLC_EGENERIC; |
|---|
| 165 | 104 | } |
|---|
| 166 | 105 | |
|---|
| 167 | | /* connecting */ |
|---|
| 168 | | msg_Dbg( p_input, "opening server=%s:%d", |
|---|
| 169 | | sock.psz_server_addr, sock.i_server_port ); |
|---|
| 170 | | p_input->p_private = (void*)&sock; |
|---|
| 171 | | p_network = module_Need( p_input, "network", psz_network ); |
|---|
| | 106 | /* Connect */ |
|---|
| | 107 | p_input->p_access_data = p_sys = malloc( sizeof( access_sys_t ) ); |
|---|
| | 108 | p_sys->fd = net_OpenTCP( p_input, psz_dup, atoi( psz_parser ) ); |
|---|
| 172 | 109 | free( psz_dup ); |
|---|
| 173 | | if( p_network == NULL ) |
|---|
| | 110 | |
|---|
| | 111 | if( p_sys->fd < 0 ) |
|---|
| 174 | 112 | { |
|---|
| | 113 | free( p_sys ); |
|---|
| 175 | 114 | return VLC_EGENERIC; |
|---|
| 176 | 115 | } |
|---|
| 177 | | module_Unneed( p_input, p_network ); |
|---|
| 178 | | |
|---|
| 179 | | p_input->p_access_data = p_sys = malloc( sizeof( access_sys_t ) ); |
|---|
| 180 | | p_sys->fd = sock.i_handle; |
|---|
| 181 | 116 | |
|---|
| 182 | 117 | p_input->pf_read = Read; |
|---|
| … | … | |
| 211 | 146 | msg_Info( p_input, "closing TCP target `%s'", p_input->psz_source ); |
|---|
| 212 | 147 | |
|---|
| 213 | | #ifdef UNDER_CE |
|---|
| 214 | | CloseHandle( (HANDLE)p_sys->fd ); |
|---|
| 215 | | #elif defined( WIN32 ) |
|---|
| 216 | | closesocket( p_sys->fd ); |
|---|
| 217 | | #else |
|---|
| 218 | | close( p_sys->fd ); |
|---|
| 219 | | #endif |
|---|
| 220 | | |
|---|
| | 148 | net_Close( p_sys->fd ); |
|---|
| 221 | 149 | free( p_sys ); |
|---|
| 222 | 150 | } |
|---|
| … | … | |
| 227 | 155 | static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len ) |
|---|
| 228 | 156 | { |
|---|
| 229 | | #ifdef UNDER_CE |
|---|
| 230 | | return -1; |
|---|
| 231 | | #else |
|---|
| 232 | 157 | access_sys_t *p_sys = p_input->p_access_data; |
|---|
| 233 | | struct timeval timeout; |
|---|
| 234 | | fd_set fds; |
|---|
| 235 | | ssize_t i_recv; |
|---|
| 236 | | int i_ret; |
|---|
| 237 | 158 | |
|---|
| 238 | | do |
|---|
| 239 | | { |
|---|
| 240 | | if( p_input->b_die || p_input->b_error ) |
|---|
| 241 | | { |
|---|
| 242 | | return 0; |
|---|
| 243 | | } |
|---|
| 244 | | |
|---|
| 245 | | /* Initialize file descriptor set */ |
|---|
| 246 | | FD_ZERO( &fds ); |
|---|
| 247 | | FD_SET( p_sys->fd, &fds ); |
|---|
| 248 | | |
|---|
| 249 | | /* We'll wait 0.5 second if nothing happens */ |
|---|
| 250 | | timeout.tv_sec = 0; |
|---|
| 251 | | timeout.tv_usec = 500000; |
|---|
| 252 | | } while( ( i_ret = select( p_sys->fd + 1, &fds, NULL, NULL, &timeout )) == 0 || |
|---|
| 253 | | ( i_ret < 0 && errno == EINTR ) ); |
|---|
| 254 | | |
|---|
| 255 | | if( i_ret < 0 ) |
|---|
| 256 | | { |
|---|
| 257 | | msg_Err( p_input, "network select error (%s)", strerror(errno) ); |
|---|
| 258 | | return -1; |
|---|
| 259 | | } |
|---|
| 260 | | |
|---|
| 261 | | if( ( i_recv = recv( p_sys->fd, p_buffer, i_len, 0 ) ) < 0 ) |
|---|
| 262 | | { |
|---|
| 263 | | msg_Err( p_input, "recv failed (%s)", strerror(errno) ); |
|---|
| 264 | | } |
|---|
| 265 | | return i_recv; |
|---|
| 266 | | #endif |
|---|
| | 159 | return net_Read( p_input, p_sys->fd, p_buffer, i_len, VLC_FALSE ); |
|---|
| 267 | 160 | } |
|---|
| 268 | 161 | |
|---|