Mercurial > mplayer.hg
annotate libmpdemux/network.c @ 16892:3470c810527b
libvo input cleanup: remove the dependency on libinput,
remove most of the crappy mappings (like O->o or ESC->q).
author | albeu |
---|---|
date | Wed, 02 Nov 2005 21:50:55 +0000 |
parents | 0a31740dd5e6 |
children | 6ff3379a0862 |
rev | line source |
---|---|
903 | 1 /* |
2 * Network layer for MPlayer | |
3 * by Bertrand BAUDET <bertrand_baudet@yahoo.com> | |
4 * (C) 2001, MPlayer team. | |
5 */ | |
6 | |
1028 | 7 //#define DUMP2FILE |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
8 |
1430 | 9 #include <stdio.h> |
10 #include <stdlib.h> | |
11 #include <string.h> | |
833 | 12 #include <unistd.h> |
1430 | 13 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
14 #include <errno.h> |
903 | 15 #include <ctype.h> |
833 | 16 |
2555
66837325b929
config.h cleanup, few things added to steram/demuxer headers
arpi
parents:
2489
diff
changeset
|
17 #include "config.h" |
66837325b929
config.h cleanup, few things added to steram/demuxer headers
arpi
parents:
2489
diff
changeset
|
18 |
10281 | 19 #ifndef HAVE_WINSOCK2 |
20 #define closesocket close | |
21 #else | |
22 #include <winsock2.h> | |
23 #include <ws2tcpip.h> | |
24 #endif | |
25 | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
26 #include "stream.h" |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
27 #include "demuxer.h" |
9749 | 28 #include "../m_config.h" |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
29 |
841 | 30 #include "network.h" |
903 | 31 #include "http.h" |
11583
2af52902e7dc
Send HTTP Cookies (reading from mozilla/netscape files) support by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>. Disabled by default.
alex
parents:
11505
diff
changeset
|
32 #include "cookies.h" |
903 | 33 #include "url.h" |
841 | 34 |
6466 | 35 #include "../version.h" |
36 | |
4121 | 37 extern int verbose; |
7867 | 38 extern int stream_cache_size; |
4121 | 39 |
7862
013c255225d8
mpdemux.c|h moved to libinput, mpdemux_check_interrupt() -> mp_input_check_interrupt()
arpi
parents:
7513
diff
changeset
|
40 extern int mp_input_check_interrupt(int time); |
013c255225d8
mpdemux.c|h moved to libinput, mpdemux_check_interrupt() -> mp_input_check_interrupt()
arpi
parents:
7513
diff
changeset
|
41 |
11583
2af52902e7dc
Send HTTP Cookies (reading from mozilla/netscape files) support by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>. Disabled by default.
alex
parents:
11505
diff
changeset
|
42 /* Variables for the command line option -user, -passwd, -bandwidth, |
2af52902e7dc
Send HTTP Cookies (reading from mozilla/netscape files) support by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>. Disabled by default.
alex
parents:
11505
diff
changeset
|
43 -user-agent and -nocookies */ |
2af52902e7dc
Send HTTP Cookies (reading from mozilla/netscape files) support by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>. Disabled by default.
alex
parents:
11505
diff
changeset
|
44 |
7867 | 45 char *network_username=NULL; |
46 char *network_password=NULL; | |
47 int network_bandwidth=0; | |
11583
2af52902e7dc
Send HTTP Cookies (reading from mozilla/netscape files) support by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>. Disabled by default.
alex
parents:
11505
diff
changeset
|
48 int network_cookies_enabled = 0; |
11228 | 49 char *network_useragent=NULL; |
6558
b379b061ce50
Read username/password from the -user -pass command line options.
bertrand
parents:
6555
diff
changeset
|
50 |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
51 /* IPv6 options */ |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
52 int network_prefer_ipv4 = 0; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
53 int network_ipv4_only_proxy = 0; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
54 |
6558
b379b061ce50
Read username/password from the -user -pass command line options.
bertrand
parents:
6555
diff
changeset
|
55 |
15585 | 56 mime_struct_t mime_type_table[] = { |
3042 | 57 // MP3 streaming, some MP3 streaming server answer with audio/mpeg |
4729
f51bd827ed1c
fixed MP3 ICY detection, return detected file format for open_stream
alex
parents:
4652
diff
changeset
|
58 { "audio/mpeg", DEMUXER_TYPE_AUDIO }, |
3042 | 59 // MPEG streaming |
14002
009337ffcfc4
forced autodetection of file format when content-type is video-x-mpeg to give mpeg-ts a chance
nicodvb
parents:
12894
diff
changeset
|
60 { "video/mpeg", DEMUXER_TYPE_UNKNOWN }, |
009337ffcfc4
forced autodetection of file format when content-type is video-x-mpeg to give mpeg-ts a chance
nicodvb
parents:
12894
diff
changeset
|
61 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN }, |
009337ffcfc4
forced autodetection of file format when content-type is video-x-mpeg to give mpeg-ts a chance
nicodvb
parents:
12894
diff
changeset
|
62 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN }, |
3042 | 63 // AVI ??? => video/x-msvideo |
64 { "video/x-msvideo", DEMUXER_TYPE_AVI }, | |
65 // MOV => video/quicktime | |
66 { "video/quicktime", DEMUXER_TYPE_MOV }, | |
67 // ASF | |
68 { "audio/x-ms-wax", DEMUXER_TYPE_ASF }, | |
69 { "audio/x-ms-wma", DEMUXER_TYPE_ASF }, | |
70 { "video/x-ms-asf", DEMUXER_TYPE_ASF }, | |
71 { "video/x-ms-afs", DEMUXER_TYPE_ASF }, | |
72 { "video/x-ms-wvx", DEMUXER_TYPE_ASF }, | |
73 { "video/x-ms-wmv", DEMUXER_TYPE_ASF }, | |
74 { "video/x-ms-wma", DEMUXER_TYPE_ASF }, | |
4783 | 75 // Playlists |
7393 | 76 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST }, |
4783 | 77 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST }, |
78 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST }, | |
6032 | 79 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST }, |
80 // Real Media | |
15209
b9c199dd123f
Do not force real demuxer on x-pn-realaudio mimetype
rtognimp
parents:
14720
diff
changeset
|
81 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL }, |
8114
ba66375adb6f
Added OGG streaming support, patch from Balatoni Denes <pnis@coder.hu>
bertrand
parents:
8027
diff
changeset
|
82 // OGG Streaming |
12175 | 83 { "application/x-ogg", DEMUXER_TYPE_OGG }, |
84 // NullSoft Streaming Video | |
12477
1550ef7117c4
Add detection of nsa streamed by aol ultravox server
rtognimp
parents:
12390
diff
changeset
|
85 { "video/nsv", DEMUXER_TYPE_NSV}, |
15585 | 86 { "misc/ultravox", DEMUXER_TYPE_NSV}, |
87 { NULL, DEMUXER_TYPE_UNKNOWN}, | |
3042 | 88 }; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
89 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
90 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
91 streaming_ctrl_t * |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
92 streaming_ctrl_new( ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
93 streaming_ctrl_t *streaming_ctrl; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
94 streaming_ctrl = (streaming_ctrl_t*)malloc(sizeof(streaming_ctrl_t)); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
95 if( streaming_ctrl==NULL ) { |
5915 | 96 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Failed to allocate memory\n"); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
97 return NULL; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
98 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
99 memset( streaming_ctrl, 0, sizeof(streaming_ctrl_t) ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
100 return streaming_ctrl; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
101 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
102 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
103 void |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
104 streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
105 if( streaming_ctrl==NULL ) return; |
4251
05affdf4bdcd
Moved network related code from open.c to network.c
bertrand
parents:
4236
diff
changeset
|
106 if( streaming_ctrl->url ) url_free( streaming_ctrl->url ); |
05affdf4bdcd
Moved network related code from open.c to network.c
bertrand
parents:
4236
diff
changeset
|
107 if( streaming_ctrl->buffer ) free( streaming_ctrl->buffer ); |
05affdf4bdcd
Moved network related code from open.c to network.c
bertrand
parents:
4236
diff
changeset
|
108 if( streaming_ctrl->data ) free( streaming_ctrl->data ); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
109 free( streaming_ctrl ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
110 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
111 |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
112 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
113 // Converts an address family constant to a string |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
114 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
115 char *af2String(int af) { |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
116 switch (af) { |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
117 case AF_INET: return "AF_INET"; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
118 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
119 #ifdef HAVE_AF_INET6 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
120 case AF_INET6: return "AF_INET6"; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
121 #endif |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
122 default: return "Unknown address family!"; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
123 } |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
124 } |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
125 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
126 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
127 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
128 // Connect to a server using a TCP connection, with specified address family |
7251
cc83ad6af513
Return -2 for fatal error while connecting to a server.
bertrand
parents:
7007
diff
changeset
|
129 // return -2 for fatal error, like unable to resolve name, connection timeout... |
cc83ad6af513
Return -2 for fatal error while connecting to a server.
bertrand
parents:
7007
diff
changeset
|
130 // return -1 is unable to connect to a particular port |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
131 |
833 | 132 int |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
10317
diff
changeset
|
133 connect2Server_with_af(char *host, int port, int af,int verb) { |
833 | 134 int socket_server_fd; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
135 int err, err_len; |
4825
41d2da3bd082
Make blocking call in libmpdemux interuptable (only with new input,
albeu
parents:
4817
diff
changeset
|
136 int ret,count = 0; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
137 fd_set set; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
138 struct timeval tv; |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
139 union { |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
140 struct sockaddr_in four; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
141 #ifdef HAVE_AF_INET6 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
142 struct sockaddr_in6 six; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
143 #endif |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
144 } server_address; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
145 size_t server_address_size; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
146 void *our_s_addr; // Pointer to sin_addr or sin6_addr |
7953 | 147 struct hostent *hp=NULL; |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
148 char buf[255]; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
149 |
10281 | 150 #ifdef HAVE_WINSOCK2 |
151 u_long val; | |
152 #endif | |
153 | |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
154 socket_server_fd = socket(af, SOCK_STREAM, 0); |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
155 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
156 |
833 | 157 if( socket_server_fd==-1 ) { |
11227 | 158 // mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create %s socket:\n", af2String(af)); |
7251
cc83ad6af513
Return -2 for fatal error while connecting to a server.
bertrand
parents:
7007
diff
changeset
|
159 return -2; |
833 | 160 } |
161 | |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
162 switch (af) { |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
163 case AF_INET: our_s_addr = (void *) &server_address.four.sin_addr; break; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
164 #ifdef HAVE_AF_INET6 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
165 case AF_INET6: our_s_addr = (void *) &server_address.six.sin6_addr; break; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
166 #endif |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
167 default: |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
168 mp_msg(MSGT_NETWORK,MSGL_ERR, "Unknown address family %d:\n", af); |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
169 return -2; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
170 } |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
171 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
172 |
14642
38572280e8e7
bzero is deprecated patch by Gianluigi Tiesi <mplayer at netfarm.it>
faust3
parents:
14265
diff
changeset
|
173 memset(&server_address, 0, sizeof(server_address)); |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
174 |
10281 | 175 #ifndef HAVE_WINSOCK2 |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
176 #ifdef USE_ATON |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
177 if (inet_aton(host, our_s_addr)!=1) |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
178 #else |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
179 if (inet_pton(af, host, our_s_addr)!=1) |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
180 #endif |
10281 | 181 #else |
182 if ( inet_addr(host)==INADDR_NONE ) | |
183 #endif | |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
184 { |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
10317
diff
changeset
|
185 if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,"Resolving %s for %s...\n", host, af2String(af)); |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
186 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
187 #ifdef HAVE_GETHOSTBYNAME2 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
188 hp=(struct hostent*)gethostbyname2( host, af ); |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
189 #else |
3042 | 190 hp=(struct hostent*)gethostbyname( host ); |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
191 #endif |
841 | 192 if( hp==NULL ) { |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
10317
diff
changeset
|
193 if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,"Couldn't resolve name for %s: %s\n", af2String(af), host); |
7251
cc83ad6af513
Return -2 for fatal error while connecting to a server.
bertrand
parents:
7007
diff
changeset
|
194 return -2; |
833 | 195 } |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
196 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
197 memcpy( our_s_addr, (void*)hp->h_addr, hp->h_length ); |
833 | 198 } |
10281 | 199 #ifdef HAVE_WINSOCK2 |
200 else { | |
201 unsigned long addr = inet_addr(host); | |
202 memcpy( our_s_addr, (void*)&addr, sizeof(addr) ); | |
203 } | |
204 #endif | |
3042 | 205 |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
206 switch (af) { |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
207 case AF_INET: |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
208 server_address.four.sin_family=af; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
209 server_address.four.sin_port=htons(port); |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
210 server_address_size = sizeof(server_address.four); |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
211 break; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
212 #ifdef HAVE_AF_INET6 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
213 case AF_INET6: |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
214 server_address.six.sin6_family=af; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
215 server_address.six.sin6_port=htons(port); |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
216 server_address_size = sizeof(server_address.six); |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
217 break; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
218 #endif |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
219 default: |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
220 mp_msg(MSGT_NETWORK,MSGL_ERR, "Unknown address family %d:\n", af); |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
221 return -2; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
222 } |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
223 |
10281 | 224 #if defined(USE_ATON) || defined(HAVE_WINSOCK2) |
9900 | 225 strncpy( buf, inet_ntoa( *((struct in_addr*)our_s_addr) ), 255); |
226 #else | |
227 inet_ntop(af, our_s_addr, buf, 255); | |
228 #endif | |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
10317
diff
changeset
|
229 if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,"Connecting to server %s[%s]:%d ...\n", host, buf , port ); |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
230 |
3042 | 231 // Turn the socket as non blocking so we can timeout on the connection |
10281 | 232 #ifndef HAVE_WINSOCK2 |
3042 | 233 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK ); |
10281 | 234 #else |
235 val = 1; | |
236 ioctlsocket( socket_server_fd, FIONBIO, &val ); | |
237 #endif | |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
238 if( connect( socket_server_fd, (struct sockaddr*)&server_address, server_address_size )==-1 ) { |
10281 | 239 #ifndef HAVE_WINSOCK2 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
240 if( errno!=EINPROGRESS ) { |
10281 | 241 #else |
242 if( (WSAGetLastError() != WSAEINPROGRESS) && (WSAGetLastError() != WSAEWOULDBLOCK) ) { | |
243 #endif | |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
10317
diff
changeset
|
244 if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to connect to server with %s\n", af2String(af)); |
10281 | 245 closesocket(socket_server_fd); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
246 return -1; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
247 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
248 } |
6477 | 249 tv.tv_sec = 0; |
250 tv.tv_usec = 500000; | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
251 FD_ZERO( &set ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
252 FD_SET( socket_server_fd, &set ); |
3042 | 253 // When the connection will be made, we will have a writable fd |
4825
41d2da3bd082
Make blocking call in libmpdemux interuptable (only with new input,
albeu
parents:
4817
diff
changeset
|
254 while((ret = select(socket_server_fd+1, NULL, &set, NULL, &tv)) == 0) { |
6454 | 255 if( ret<0 ) mp_msg(MSGT_NETWORK,MSGL_ERR,"select failed\n"); |
4825
41d2da3bd082
Make blocking call in libmpdemux interuptable (only with new input,
albeu
parents:
4817
diff
changeset
|
256 else if(ret > 0) break; |
7862
013c255225d8
mpdemux.c|h moved to libinput, mpdemux_check_interrupt() -> mp_input_check_interrupt()
arpi
parents:
7513
diff
changeset
|
257 else if(count > 30 || mp_input_check_interrupt(500)) { |
6477 | 258 if(count > 30) |
5915 | 259 mp_msg(MSGT_NETWORK,MSGL_ERR,"Connection timeout\n"); |
4825
41d2da3bd082
Make blocking call in libmpdemux interuptable (only with new input,
albeu
parents:
4817
diff
changeset
|
260 else |
5915 | 261 mp_msg(MSGT_NETWORK,MSGL_V,"Connection interuppted by user\n"); |
8824 | 262 return -3; |
4825
41d2da3bd082
Make blocking call in libmpdemux interuptable (only with new input,
albeu
parents:
4817
diff
changeset
|
263 } |
41d2da3bd082
Make blocking call in libmpdemux interuptable (only with new input,
albeu
parents:
4817
diff
changeset
|
264 count++; |
41d2da3bd082
Make blocking call in libmpdemux interuptable (only with new input,
albeu
parents:
4817
diff
changeset
|
265 FD_ZERO( &set ); |
41d2da3bd082
Make blocking call in libmpdemux interuptable (only with new input,
albeu
parents:
4817
diff
changeset
|
266 FD_SET( socket_server_fd, &set ); |
6477 | 267 tv.tv_sec = 0; |
268 tv.tv_usec = 500000; | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
269 } |
3042 | 270 |
271 // Turn back the socket as blocking | |
10281 | 272 #ifndef HAVE_WINSOCK2 |
3042 | 273 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) & ~O_NONBLOCK ); |
10281 | 274 #else |
275 val = 0; | |
276 ioctlsocket( socket_server_fd, FIONBIO, &val ); | |
277 #endif | |
3494
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
278 // Check if there were any error |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
279 err_len = sizeof(int); |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
280 ret = getsockopt(socket_server_fd,SOL_SOCKET,SO_ERROR,&err,&err_len); |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
281 if(ret < 0) { |
5915 | 282 mp_msg(MSGT_NETWORK,MSGL_ERR,"getsockopt failed : %s\n",strerror(errno)); |
7251
cc83ad6af513
Return -2 for fatal error while connecting to a server.
bertrand
parents:
7007
diff
changeset
|
283 return -2; |
3494
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
284 } |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
285 if(err > 0) { |
5915 | 286 mp_msg(MSGT_NETWORK,MSGL_ERR,"Connect error : %s\n",strerror(err)); |
3494
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
287 return -1; |
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
288 } |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
289 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
290 return socket_server_fd; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
291 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
292 |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
293 // Connect to a server using a TCP connection |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
294 // return -2 for fatal error, like unable to resolve name, connection timeout... |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
295 // return -1 is unable to connect to a particular port |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
296 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
297 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
298 int |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
10317
diff
changeset
|
299 connect2Server(char *host, int port, int verb) { |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
300 #ifdef HAVE_AF_INET6 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
301 int r; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
302 int s = -2; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
303 |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
10317
diff
changeset
|
304 r = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET:AF_INET6,verb); |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
305 if (r > -1) return r; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
306 |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
10317
diff
changeset
|
307 s = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET6:AF_INET,verb); |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
308 if (s == -2) return r; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
309 return s; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
310 #else |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
10317
diff
changeset
|
311 return connect2Server_with_af(host, port, AF_INET,verb); |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
312 #endif |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
313 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
314 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
315 } |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
316 |
4146
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
317 URL_t* |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
318 check4proxies( URL_t *url ) { |
4652 | 319 URL_t *url_out = NULL; |
4251
05affdf4bdcd
Moved network related code from open.c to network.c
bertrand
parents:
4236
diff
changeset
|
320 if( url==NULL ) return NULL; |
4652 | 321 url_out = url_new( url->url ); |
4146
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
322 if( !strcasecmp(url->protocol, "http_proxy") ) { |
5915 | 323 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: http://%s:%d\n", url->hostname, url->port ); |
4652 | 324 return url_out; |
4146
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
325 } |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
326 // Check if the http_proxy environment variable is set. |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
327 if( !strcasecmp(url->protocol, "http") ) { |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
328 char *proxy; |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
329 proxy = getenv("http_proxy"); |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
330 if( proxy!=NULL ) { |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
331 // We got a proxy, build the URL to use it |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
332 int len; |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
333 char *new_url; |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
334 URL_t *tmp_url; |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
335 URL_t *proxy_url = url_new( proxy ); |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
336 |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
337 if( proxy_url==NULL ) { |
5915 | 338 mp_msg(MSGT_NETWORK,MSGL_WARN,"Invalid proxy setting...Trying without proxy.\n"); |
4652 | 339 return url_out; |
4146
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
340 } |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
341 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
342 #ifdef HAVE_AF_INET6 |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
343 if (network_ipv4_only_proxy && (gethostbyname(url->hostname)==NULL)) { |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
344 mp_msg(MSGT_NETWORK,MSGL_WARN, |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
345 "Could not find resolve remote hostname for AF_INET. Trying without proxy.\n"); |
16419 | 346 url_free(proxy_url); |
9691
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
347 return url_out; |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
348 } |
ed72c158215d
Added IPv6 support, patch by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>
bertrand
parents:
8939
diff
changeset
|
349 #endif |
4146
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
350 |
5915 | 351 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url ); |
4146
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
352 len = strlen( proxy_url->hostname ) + strlen( url->url ) + 20; // 20 = http_proxy:// + port |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
353 new_url = malloc( len+1 ); |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
354 if( new_url==NULL ) { |
5915 | 355 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
16419 | 356 url_free(proxy_url); |
4652 | 357 return url_out; |
4146
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
358 } |
5915 | 359 sprintf(new_url, "http_proxy://%s:%d/%s", proxy_url->hostname, proxy_url->port, url->url ); |
4146
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
360 tmp_url = url_new( new_url ); |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
361 if( tmp_url==NULL ) { |
16419 | 362 free( new_url ); |
363 url_free( proxy_url ); | |
4652 | 364 return url_out; |
4146
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
365 } |
4652 | 366 url_free( url_out ); |
367 url_out = tmp_url; | |
4146
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
368 free( new_url ); |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
369 url_free( proxy_url ); |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
370 } |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
371 } |
4652 | 372 return url_out; |
4146
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
373 } |
925046ea34ec
Added support for the environment variable http_proxy.
bertrand
parents:
4145
diff
changeset
|
374 |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
375 int |
11965 | 376 http_send_request( URL_t *url, off_t pos ) { |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
377 HTTP_header_t *http_hdr; |
4121 | 378 URL_t *server_url; |
11228 | 379 char str[256]; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
380 int fd; |
4121 | 381 int ret; |
382 int proxy = 0; // Boolean | |
383 | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
384 http_hdr = http_new_header(); |
4121 | 385 |
4145 | 386 if( !strcasecmp(url->protocol, "http_proxy") ) { |
4121 | 387 proxy = 1; |
388 server_url = url_new( (url->file)+1 ); | |
389 http_set_uri( http_hdr, server_url->url ); | |
390 } else { | |
391 server_url = url; | |
392 http_set_uri( http_hdr, server_url->file ); | |
393 } | |
12638
2ebd553876c8
RFC compliance patch by Eric Lammerts <eric@lammerts.org>
alex
parents:
12610
diff
changeset
|
394 if (server_url->port && server_url->port != 80) |
2ebd553876c8
RFC compliance patch by Eric Lammerts <eric@lammerts.org>
alex
parents:
12610
diff
changeset
|
395 snprintf(str, 256, "Host: %s:%d", server_url->hostname, server_url->port ); |
2ebd553876c8
RFC compliance patch by Eric Lammerts <eric@lammerts.org>
alex
parents:
12610
diff
changeset
|
396 else |
2ebd553876c8
RFC compliance patch by Eric Lammerts <eric@lammerts.org>
alex
parents:
12610
diff
changeset
|
397 snprintf(str, 256, "Host: %s", server_url->hostname ); |
3585 | 398 http_set_field( http_hdr, str); |
11228 | 399 if (network_useragent) |
400 { | |
401 snprintf(str, 256, "User-Agent: %s", network_useragent); | |
402 http_set_field(http_hdr, str); | |
403 } | |
404 else | |
405 http_set_field( http_hdr, "User-Agent: MPlayer/"VERSION); | |
11965 | 406 |
16013 | 407 http_set_field(http_hdr, "Icy-MetaData: 1"); |
408 | |
11965 | 409 if(pos>0) { |
410 // Extend http_send_request with possibility to do partial content retrieval | |
16750
0a31740dd5e6
Use PRI?64 defines as format strings for 64 bit variables.
reimar
parents:
16419
diff
changeset
|
411 snprintf(str, 256, "Range: bytes=%"PRId64"-", (int64_t)pos); |
11965 | 412 http_set_field(http_hdr, str); |
413 } | |
11583
2af52902e7dc
Send HTTP Cookies (reading from mozilla/netscape files) support by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>. Disabled by default.
alex
parents:
11505
diff
changeset
|
414 |
2af52902e7dc
Send HTTP Cookies (reading from mozilla/netscape files) support by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>. Disabled by default.
alex
parents:
11505
diff
changeset
|
415 if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url ); |
2af52902e7dc
Send HTTP Cookies (reading from mozilla/netscape files) support by Dave Lambley <mplayer-dev-eng@dlambley.freeserve.co.uk>. Disabled by default.
alex
parents:
11505
diff
changeset
|
416 |
12352 | 417 http_set_field( http_hdr, "Connection: close"); |
6515 | 418 http_add_basic_authentication( http_hdr, url->username, url->password ); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
419 if( http_build_request( http_hdr )==NULL ) { |
16417 | 420 goto err_out; |
833 | 421 } |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
422 |
4121 | 423 if( proxy ) { |
424 if( url->port==0 ) url->port = 8080; // Default port for the proxy server | |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
10317
diff
changeset
|
425 fd = connect2Server( url->hostname, url->port,1 ); |
4121 | 426 url_free( server_url ); |
427 } else { | |
428 if( server_url->port==0 ) server_url->port = 80; // Default port for the web server | |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
10317
diff
changeset
|
429 fd = connect2Server( server_url->hostname, server_url->port,1 ); |
4121 | 430 } |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
431 if( fd<0 ) { |
16417 | 432 goto err_out; |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
433 } |
5915 | 434 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer ); |
4121 | 435 |
10206
35e306346e59
Using recv/send instead read/write for proper MinGW support (it's a 4.2BSD standard). Patch by FloDt <flodt8@yahoo.de>
alex
parents:
10199
diff
changeset
|
436 ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, 0 ); |
7953 | 437 if( ret!=(int)http_hdr->buffer_size ) { |
5915 | 438 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error while sending HTTP request: didn't sent all the request\n"); |
16417 | 439 goto err_out; |
4121 | 440 } |
441 | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
442 http_free( http_hdr ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
443 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
444 return fd; |
16417 | 445 err_out: |
446 http_free(http_hdr); | |
447 return -1; | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
448 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
449 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
450 HTTP_header_t * |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
451 http_read_response( int fd ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
452 HTTP_header_t *http_hdr; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
453 char response[BUFFER_SIZE]; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
454 int i; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
455 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
456 http_hdr = http_new_header(); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
457 if( http_hdr==NULL ) { |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
458 return NULL; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
459 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
460 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
461 do { |
10206
35e306346e59
Using recv/send instead read/write for proper MinGW support (it's a 4.2BSD standard). Patch by FloDt <flodt8@yahoo.de>
alex
parents:
10199
diff
changeset
|
462 i = recv( fd, response, BUFFER_SIZE, 0 ); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
463 if( i<0 ) { |
5915 | 464 mp_msg(MSGT_NETWORK,MSGL_ERR,"Read failed\n"); |
3365 | 465 http_free( http_hdr ); |
466 return NULL; | |
467 } | |
468 if( i==0 ) { | |
5915 | 469 mp_msg(MSGT_NETWORK,MSGL_ERR,"http_read_response read 0 -ie- EOF\n"); |
3365 | 470 http_free( http_hdr ); |
471 return NULL; | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
472 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
473 http_response_append( http_hdr, response, i ); |
2489
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
474 } while( !http_is_header_entire( http_hdr ) ); |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
475 http_response_parse( http_hdr ); |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
476 return http_hdr; |
833 | 477 } |
478 | |
6555 | 479 int |
480 http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) { | |
481 char *aut; | |
7953 | 482 |
6558
b379b061ce50
Read username/password from the -user -pass command line options.
bertrand
parents:
6555
diff
changeset
|
483 if( *auth_retry==1 ) { |
6555 | 484 mp_msg(MSGT_NETWORK,MSGL_ERR,"Authentication failed\n"); |
6570
52ecfeddf864
Changed the -pass option to -passwd to avoid clash with mencoder option.
bertrand
parents:
6558
diff
changeset
|
485 mp_msg(MSGT_NETWORK,MSGL_ERR,"Please use the option -user and -passwd to provide your username/password for a list of URLs,\n"); |
6558
b379b061ce50
Read username/password from the -user -pass command line options.
bertrand
parents:
6555
diff
changeset
|
486 mp_msg(MSGT_NETWORK,MSGL_ERR,"or form an URL like: http://username:password@hostname/file\n"); |
6555 | 487 return -1; |
488 } | |
489 if( *auth_retry>0 ) { | |
490 if( url->username ) { | |
491 free( url->username ); | |
492 url->username = NULL; | |
493 } | |
494 if( url->password ) { | |
495 free( url->password ); | |
496 url->password = NULL; | |
497 } | |
498 } | |
6558
b379b061ce50
Read username/password from the -user -pass command line options.
bertrand
parents:
6555
diff
changeset
|
499 |
6555 | 500 aut = http_get_field(http_hdr, "WWW-Authenticate"); |
501 if( aut!=NULL ) { | |
502 char *aut_space; | |
503 aut_space = strstr(aut, "realm="); | |
504 if( aut_space!=NULL ) aut_space += 6; | |
6558
b379b061ce50
Read username/password from the -user -pass command line options.
bertrand
parents:
6555
diff
changeset
|
505 mp_msg(MSGT_NETWORK,MSGL_INFO,"Authentication required for %s\n", aut_space); |
6555 | 506 } else { |
6558
b379b061ce50
Read username/password from the -user -pass command line options.
bertrand
parents:
6555
diff
changeset
|
507 mp_msg(MSGT_NETWORK,MSGL_INFO,"Authentication required\n"); |
6555 | 508 } |
7867 | 509 if( network_username ) { |
510 url->username = strdup(network_username); | |
6555 | 511 if( url->username==NULL ) { |
512 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
513 return -1; | |
514 } | |
515 } else { | |
6558
b379b061ce50
Read username/password from the -user -pass command line options.
bertrand
parents:
6555
diff
changeset
|
516 mp_msg(MSGT_NETWORK,MSGL_ERR,"Unable to read the username\n"); |
6570
52ecfeddf864
Changed the -pass option to -passwd to avoid clash with mencoder option.
bertrand
parents:
6558
diff
changeset
|
517 mp_msg(MSGT_NETWORK,MSGL_ERR,"Please use the option -user and -passwd to provide your username/password for a list of URLs,\n"); |
6558
b379b061ce50
Read username/password from the -user -pass command line options.
bertrand
parents:
6555
diff
changeset
|
518 mp_msg(MSGT_NETWORK,MSGL_ERR,"or form an URL like: http://username:password@hostname/file\n"); |
6555 | 519 return -1; |
520 } | |
7867 | 521 if( network_password ) { |
522 url->password = strdup(network_password); | |
6558
b379b061ce50
Read username/password from the -user -pass command line options.
bertrand
parents:
6555
diff
changeset
|
523 if( url->password==NULL ) { |
b379b061ce50
Read username/password from the -user -pass command line options.
bertrand
parents:
6555
diff
changeset
|
524 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
b379b061ce50
Read username/password from the -user -pass command line options.
bertrand
parents:
6555
diff
changeset
|
525 return -1; |
b379b061ce50
Read username/password from the -user -pass command line options.
bertrand
parents:
6555
diff
changeset
|
526 } |
b379b061ce50
Read username/password from the -user -pass command line options.
bertrand
parents:
6555
diff
changeset
|
527 } else { |
b379b061ce50
Read username/password from the -user -pass command line options.
bertrand
parents:
6555
diff
changeset
|
528 mp_msg(MSGT_NETWORK,MSGL_INFO,"No password provided, trying blank password\n"); |
b379b061ce50
Read username/password from the -user -pass command line options.
bertrand
parents:
6555
diff
changeset
|
529 } |
6555 | 530 (*auth_retry)++; |
531 return 0; | |
532 } | |
533 | |
11965 | 534 int |
535 http_seek( stream_t *stream, off_t pos ) { | |
536 HTTP_header_t *http_hdr = NULL; | |
537 int fd; | |
538 if( stream==NULL ) return 0; | |
539 | |
540 if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream | |
541 fd = http_send_request( stream->streaming_ctrl->url, pos ); | |
542 if( fd<0 ) return 0; | |
543 | |
544 http_hdr = http_read_response( fd ); | |
545 | |
546 if( http_hdr==NULL ) return 0; | |
547 | |
548 switch( http_hdr->status_code ) { | |
549 case 200: | |
550 case 206: // OK | |
551 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") ); | |
552 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") ); | |
553 if( http_hdr->body_size>0 ) { | |
554 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { | |
555 http_free( http_hdr ); | |
556 return -1; | |
557 } | |
558 } | |
559 break; | |
560 default: | |
561 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server return %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); | |
562 close( fd ); | |
563 fd = -1; | |
564 } | |
565 stream->fd = fd; | |
566 | |
567 if( http_hdr ) { | |
568 http_free( http_hdr ); | |
569 stream->streaming_ctrl->data = NULL; | |
570 } | |
571 | |
572 stream->pos=pos; | |
573 | |
574 return 1; | |
575 } | |
576 | |
577 | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
578 int |
3042 | 579 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) { |
4251
05affdf4bdcd
Moved network related code from open.c to network.c
bertrand
parents:
4236
diff
changeset
|
580 //printf("streaming_bufferize\n"); |
3042 | 581 streaming_ctrl->buffer = (char*)malloc(size); |
582 if( streaming_ctrl->buffer==NULL ) { | |
5915 | 583 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
3042 | 584 return -1; |
585 } | |
586 memcpy( streaming_ctrl->buffer, buffer, size ); | |
587 streaming_ctrl->buffer_size = size; | |
3599 | 588 return size; |
3042 | 589 } |
590 | |
591 int | |
592 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) { | |
593 int len=0; | |
594 //printf("nop_streaming_read\n"); | |
595 if( stream_ctrl->buffer_size!=0 ) { | |
596 int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos; | |
4251
05affdf4bdcd
Moved network related code from open.c to network.c
bertrand
parents:
4236
diff
changeset
|
597 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size); |
3042 | 598 len = (size<buffer_len)?size:buffer_len; |
599 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len ); | |
600 stream_ctrl->buffer_pos += len; | |
4251
05affdf4bdcd
Moved network related code from open.c to network.c
bertrand
parents:
4236
diff
changeset
|
601 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos ); |
3042 | 602 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) { |
603 free( stream_ctrl->buffer ); | |
604 stream_ctrl->buffer = NULL; | |
605 stream_ctrl->buffer_size = 0; | |
606 stream_ctrl->buffer_pos = 0; | |
4251
05affdf4bdcd
Moved network related code from open.c to network.c
bertrand
parents:
4236
diff
changeset
|
607 //printf("buffer cleaned\n"); |
3042 | 608 } |
4251
05affdf4bdcd
Moved network related code from open.c to network.c
bertrand
parents:
4236
diff
changeset
|
609 //printf("read %d bytes from buffer\n", len ); |
3042 | 610 } |
611 | |
612 if( len<size ) { | |
3365 | 613 int ret; |
10206
35e306346e59
Using recv/send instead read/write for proper MinGW support (it's a 4.2BSD standard). Patch by FloDt <flodt8@yahoo.de>
alex
parents:
10199
diff
changeset
|
614 ret = recv( fd, buffer+len, size-len, 0 ); |
3494
fb9de639ed30
Applied the patch from Alban Bedel <albeu@free.fr>.
bertrand
parents:
3453
diff
changeset
|
615 if( ret<0 ) { |
5915 | 616 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno)); |
3365 | 617 } |
618 len += ret; | |
3042 | 619 //printf("read %d bytes from network\n", len ); |
620 } | |
621 | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
622 return len; |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
623 } |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
624 |
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
625 int |
3042 | 626 nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) { |
627 return -1; | |
7953 | 628 // To shut up gcc warning |
629 fd++; | |
630 pos++; | |
631 stream_ctrl=NULL; | |
3042 | 632 } |
633 | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
634 |
15954 | 635 void fixup_network_stream_cache(stream_t *stream) { |
15585 | 636 if(stream->streaming_ctrl->buffering) { |
637 if(stream_cache_size<0) { | |
638 // cache option not set, will use our computed value. | |
639 // buffer in KBytes, *5 because the prefill is 20% of the buffer. | |
640 stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5; | |
641 if( stream_cache_size<64 ) stream_cache_size = 64; // 16KBytes min buffer | |
642 } | |
643 mp_msg(MSGT_NETWORK,MSGL_INFO,"Cache size set to %d KBytes\n", stream_cache_size); | |
644 } | |
9922 | 645 } |
646 | |
647 | |
903 | 648 int |
3042 | 649 streaming_stop( stream_t *stream ) { |
650 stream->streaming_ctrl->status = streaming_stopped_e; | |
999
92833c9472e8
Continue implementation of the network streaming part.
bertrand
parents:
903
diff
changeset
|
651 return 0; |
903 | 652 } |