Mercurial > mplayer.hg
annotate libmpdemux/stream_ftp.c @ 18133:d4b79bf291c1
A new German translation of the XML documentation is in the works.
author | diego |
---|---|
date | Tue, 18 Apr 2006 08:03:20 +0000 |
parents | a63fa57ecbd7 |
children | d2d9d011203f |
rev | line source |
---|---|
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
1 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
2 #include "config.h" |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
3 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
4 #ifdef HAVE_FTP |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
5 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
6 #include <stdlib.h> |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
7 #include <stdio.h> |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
8 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
9 #include <sys/types.h> |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
10 #include <sys/stat.h> |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
11 #include <fcntl.h> |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
12 #include <unistd.h> |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
13 #include <errno.h> |
10627 | 14 #ifndef HAVE_WINSOCK2 |
10639 | 15 #include <sys/socket.h> |
10627 | 16 #define closesocket close |
17 #else | |
18 #include <winsock2.h> | |
19 #endif | |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
20 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
21 #include "mp_msg.h" |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
22 #include "stream.h" |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
23 #include "help_mp.h" |
17012 | 24 #include "m_option.h" |
25 #include "m_struct.h" | |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
26 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
27 static struct stream_priv_s { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
28 char* user; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
29 char* pass; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
30 char* host; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
31 int port; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
32 char* filename; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
33 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
34 char *cput,*cget; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
35 int handle; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
36 int cavail,cleft; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
37 char *buf; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
38 } stream_priv_dflts = { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
39 "anonymous","no@spam", |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
40 NULL, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
41 21, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
42 NULL, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
43 NULL, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
44 NULL, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
45 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
46 0, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
47 0,0, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
48 NULL |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
49 }; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
50 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
51 #define BUFSIZE 2048 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
52 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
53 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f) |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
54 /// URL definition |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
55 static m_option_t stream_opts_fields[] = { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
56 {"username", ST_OFF(user), CONF_TYPE_STRING, 0, 0 ,0, NULL}, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
57 {"password", ST_OFF(pass), CONF_TYPE_STRING, 0, 0 ,0, NULL}, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
58 {"hostname", ST_OFF(host), CONF_TYPE_STRING, 0, 0 ,0, NULL}, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
59 {"port", ST_OFF(port), CONF_TYPE_INT, 0, 0 ,65635, NULL}, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
60 {"filename", ST_OFF(filename), CONF_TYPE_STRING, 0, 0 ,0, NULL}, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
61 { NULL, NULL, 0, 0, 0, 0, NULL } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
62 }; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
63 static struct m_struct_st stream_opts = { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
64 "ftp", |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
65 sizeof(struct stream_priv_s), |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
66 &stream_priv_dflts, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
67 stream_opts_fields |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
68 }; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
69 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
70 #define TELNET_IAC 255 /* interpret as command: */ |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
71 #define TELNET_IP 244 /* interrupt process--permanently */ |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
72 #define TELNET_SYNCH 242 /* for telfunc calls */ |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
73 |
17854
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
74 // Check if there is something to read on a fd. This avoid hanging |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
75 // forever if the network stop responding. |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
76 static int fd_can_read(int fd,int timeout) { |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
77 fd_set fds; |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
78 struct timeval tv; |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
79 |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
80 FD_ZERO(&fds); |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
81 FD_SET(fd,&fds); |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
82 tv.tv_sec = timeout; |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
83 tv.tv_usec = 0; |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
84 |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
85 return (select(fd+1, &fds, NULL, NULL, &tv) > 0); |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
86 } |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
87 |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
88 /* |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
89 * read a line of text |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
90 * |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
91 * return -1 on error or bytecount |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
92 */ |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
93 static int readline(char *buf,int max,struct stream_priv_s *ctl) |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
94 { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
95 int x,retval = 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
96 char *end,*bp=buf; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
97 int eof = 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
98 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
99 do { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
100 if (ctl->cavail > 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
101 x = (max >= ctl->cavail) ? ctl->cavail : max-1; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
102 end = memccpy(bp,ctl->cget,'\n',x); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
103 if (end != NULL) |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
104 x = end - bp; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
105 retval += x; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
106 bp += x; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
107 *bp = '\0'; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
108 max -= x; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
109 ctl->cget += x; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
110 ctl->cavail -= x; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
111 if (end != NULL) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
112 bp -= 2; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
113 if (strcmp(bp,"\r\n") == 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
114 *bp++ = '\n'; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
115 *bp++ = '\0'; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
116 --retval; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
117 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
118 break; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
119 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
120 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
121 if (max == 1) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
122 *buf = '\0'; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
123 break; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
124 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
125 if (ctl->cput == ctl->cget) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
126 ctl->cput = ctl->cget = ctl->buf; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
127 ctl->cavail = 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
128 ctl->cleft = BUFSIZE; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
129 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
130 if(eof) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
131 if (retval == 0) |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
132 retval = -1; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
133 break; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
134 } |
17854
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
135 |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
136 if(!fd_can_read(ctl->handle, 15)) { |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
137 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] read timed out\n"); |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
138 retval = -1; |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
139 break; |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
140 } |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
141 |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
142 if ((x = recv(ctl->handle,ctl->cput,ctl->cleft,0)) == -1) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
143 mp_msg(MSGT_STREAM,MSGL_ERR, "[ftp] read error: %s\n",strerror(errno)); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
144 retval = -1; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
145 break; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
146 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
147 if (x == 0) |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
148 eof = 1; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
149 ctl->cleft -= x; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
150 ctl->cavail += x; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
151 ctl->cput += x; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
152 } while (1); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
153 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
154 return retval; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
155 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
156 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
157 /* |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
158 * read a response from the server |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
159 * |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
160 * return 0 if first char doesn't match |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
161 * return 1 if first char matches |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
162 */ |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
163 static int readresp(struct stream_priv_s* ctl,char* rsp) |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
164 { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
165 static char response[256]; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
166 char match[5]; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
167 int r; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
168 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
169 if (readline(response,256,ctl) == -1) |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
170 return 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
171 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
172 r = atoi(response)/100; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
173 if(rsp) strcpy(rsp,response); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
174 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
175 mp_msg(MSGT_STREAM,MSGL_V, "[ftp] < %s",response); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
176 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
177 if (response[3] == '-') { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
178 strncpy(match,response,3); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
179 match[3] = ' '; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
180 match[4] = '\0'; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
181 do { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
182 if (readline(response,256,ctl) == -1) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
183 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] Control socket read failed\n"); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
184 return 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
185 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
186 mp_msg(MSGT_OPEN,MSGL_V, "[ftp] < %s",response); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
187 } while (strncmp(response,match,4)); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
188 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
189 return r; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
190 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
191 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
192 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
193 static int FtpSendCmd(const char *cmd, struct stream_priv_s *nControl,char* rsp) |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
194 { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
195 int l = strlen(cmd); |
16761
1eeb10c3e534
Make FtpSendCmd() function more user friendly by making it append the necessary "\r\n" line break (instead of the caller)
gpoirier
parents:
16750
diff
changeset
|
196 int hascrlf = cmd[l - 2] == '\r' && cmd[l - 1] == '\n'; |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
197 |
17854
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
198 if(hascrlf && l == 2) mp_msg(MSGT_STREAM,MSGL_V, "\n"); |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
199 else mp_msg(MSGT_STREAM,MSGL_V, "[ftp] > %s",cmd); |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
200 while(l > 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
201 int s = send(nControl->handle,cmd,l,0); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
202 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
203 if(s <= 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
204 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] write error: %s\n",strerror(errno)); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
205 return 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
206 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
207 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
208 cmd += s; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
209 l -= s; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
210 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
211 |
16761
1eeb10c3e534
Make FtpSendCmd() function more user friendly by making it append the necessary "\r\n" line break (instead of the caller)
gpoirier
parents:
16750
diff
changeset
|
212 if (hascrlf) |
1eeb10c3e534
Make FtpSendCmd() function more user friendly by making it append the necessary "\r\n" line break (instead of the caller)
gpoirier
parents:
16750
diff
changeset
|
213 return readresp(nControl,rsp); |
1eeb10c3e534
Make FtpSendCmd() function more user friendly by making it append the necessary "\r\n" line break (instead of the caller)
gpoirier
parents:
16750
diff
changeset
|
214 else |
1eeb10c3e534
Make FtpSendCmd() function more user friendly by making it append the necessary "\r\n" line break (instead of the caller)
gpoirier
parents:
16750
diff
changeset
|
215 return FtpSendCmd("\r\n", nControl, rsp); |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
216 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
217 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
218 static int FtpOpenPort(struct stream_priv_s* p) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
219 int resp,fd; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
220 char rsp_txt[256]; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
221 char* par,str[128]; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
222 int num[6]; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
223 |
16761
1eeb10c3e534
Make FtpSendCmd() function more user friendly by making it append the necessary "\r\n" line break (instead of the caller)
gpoirier
parents:
16750
diff
changeset
|
224 resp = FtpSendCmd("PASV",p,rsp_txt); |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
225 if(resp != 2) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
226 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command 'PASV' failed: %s\n",rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
227 return 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
228 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
229 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
230 par = strchr(rsp_txt,'('); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
231 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
232 if(!par || !par[0] || !par[1]) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
233 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] invalid server response: %s ??\n",rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
234 return 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
235 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
236 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
237 sscanf(par+1,"%u,%u,%u,%u,%u,%u",&num[0],&num[1],&num[2], |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
238 &num[3],&num[4],&num[5]); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
239 snprintf(str,127,"%d.%d.%d.%d",num[0],num[1],num[2],num[3]); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
240 fd = connect2Server(str,(num[4]<<8)+num[5],0); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
241 |
17854
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
242 if(fd < 0) |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
243 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] failed to create data connection\n"); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
244 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
245 return fd; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
246 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
247 |
17854
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
248 static int FtpOpenData(stream_t* s,size_t newpos) { |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
249 struct stream_priv_s* p = s->priv; |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
250 int resp; |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
251 char str[256],rsp_txt[256]; |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
252 |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
253 // Open a new connection |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
254 s->fd = FtpOpenPort(p); |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
255 |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
256 if(s->fd < 0) return 0; |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
257 |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
258 if(newpos > 0) { |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
259 snprintf(str,255,"REST %"PRId64, (int64_t)newpos); |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
260 |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
261 resp = FtpSendCmd(str,p,rsp_txt); |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
262 if(resp != 3) { |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
263 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt); |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
264 newpos = 0; |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
265 } |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
266 } |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
267 |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
268 // Get the file |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
269 snprintf(str,255,"RETR %s",p->filename); |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
270 resp = FtpSendCmd(str,p,rsp_txt); |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
271 |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
272 if(resp != 1) { |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
273 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt); |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
274 return 0; |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
275 } |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
276 |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
277 s->pos = newpos; |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
278 return 1; |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
279 } |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
280 |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
281 static int fill_buffer(stream_t *s, char* buffer, int max_len){ |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
282 int r; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
283 |
17854
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
284 if(s->fd < 0 && !FtpOpenData(s,s->pos)) |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
285 return -1; |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
286 |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
287 if(!fd_can_read(s->fd, 15)) { |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
288 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] read timed out\n"); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
289 return -1; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
290 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
291 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
292 r = recv(s->fd,buffer,max_len,0); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
293 return (r <= 0) ? -1 : r; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
294 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
295 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
296 static int seek(stream_t *s,off_t newpos) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
297 struct stream_priv_s* p = s->priv; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
298 int resp; |
17854
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
299 char rsp_txt[256]; |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
300 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
301 if(s->pos > s->end_pos) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
302 s->eof=1; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
303 return 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
304 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
305 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
306 // Check to see if the server doesn't alredy terminated the transfert |
17854
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
307 if(fd_can_read(p->handle, 0)) { |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
308 if(readresp(p,rsp_txt) != 2) |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
309 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] Warning the server didn't finished the transfert correctly: %s\n",rsp_txt); |
10627 | 310 closesocket(s->fd); |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
311 s->fd = -1; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
312 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
313 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
314 // Close current download |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
315 if(s->fd >= 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
316 static const char pre_cmd[]={TELNET_IAC,TELNET_IP,TELNET_IAC,TELNET_SYNCH}; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
317 //int fl; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
318 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
319 // First close the fd |
10627 | 320 closesocket(s->fd); |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
321 s->fd = 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
322 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
323 // Send send the telnet sequence needed to make the server react |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
324 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
325 // Dunno if this is really needed, lftp have it. I let |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
326 // it here in case it turn out to be needed on some other OS |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
327 //fl=fcntl(p->handle,F_GETFL); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
328 //fcntl(p->handle,F_SETFL,fl&~O_NONBLOCK); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
329 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
330 // send only first byte as OOB due to OOB braindamage in many unices |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
331 send(p->handle,pre_cmd,1,MSG_OOB); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
332 send(p->handle,pre_cmd+1,sizeof(pre_cmd)-1,0); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
333 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
334 //fcntl(p->handle,F_SETFL,fl); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
335 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
336 // Get the 426 Transfer aborted |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
337 // Or the 226 Transfer complete |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
338 resp = readresp(p,rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
339 if(resp != 4 && resp != 2) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
340 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] Server didn't abort correctly: %s\n",rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
341 s->eof = 1; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
342 return 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
343 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
344 // Send the ABOR command |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
345 // Ignore the return code as sometimes it fail with "nothing to abort" |
16761
1eeb10c3e534
Make FtpSendCmd() function more user friendly by making it append the necessary "\r\n" line break (instead of the caller)
gpoirier
parents:
16750
diff
changeset
|
346 FtpSendCmd("ABOR",p,rsp_txt); |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
347 } |
17854
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
348 return FtpOpenData(s,newpos); |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
349 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
350 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
351 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
352 static void close_f(stream_t *s) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
353 struct stream_priv_s* p = s->priv; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
354 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
355 if(!p) return; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
356 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
357 if(s->fd > 0) { |
10627 | 358 closesocket(s->fd); |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
359 s->fd = 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
360 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
361 |
16761
1eeb10c3e534
Make FtpSendCmd() function more user friendly by making it append the necessary "\r\n" line break (instead of the caller)
gpoirier
parents:
16750
diff
changeset
|
362 FtpSendCmd("QUIT",p,NULL); |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
363 |
10627 | 364 if(p->handle) closesocket(p->handle); |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
365 if(p->buf) free(p->buf); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
366 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
367 m_struct_free(&stream_opts,p); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
368 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
369 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
370 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
371 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
372 static int open_f(stream_t *stream,int mode, void* opts, int* file_format) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
373 int len = 0,resp; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
374 struct stream_priv_s* p = (struct stream_priv_s*)opts; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
375 char str[256],rsp_txt[256]; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
376 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
377 if(mode != STREAM_READ) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
378 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] Unknown open mode %d\n",mode); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
379 m_struct_free(&stream_opts,opts); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
380 return STREAM_UNSUPORTED; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
381 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
382 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
383 if(!p->filename || !p->host) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
384 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] Bad url\n"); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
385 m_struct_free(&stream_opts,opts); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
386 return STREAM_ERROR; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
387 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
388 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
389 // Open the control connection |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
390 p->handle = connect2Server(p->host,p->port,1); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
391 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
392 if(p->handle < 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
393 m_struct_free(&stream_opts,opts); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
394 return STREAM_ERROR; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
395 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
396 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
397 // We got a connection, let's start serious things |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
398 stream->fd = -1; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
399 stream->priv = p; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
400 p->buf = malloc(BUFSIZE); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
401 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
402 if (readresp(p, NULL) == 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
403 close_f(stream); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
404 m_struct_free(&stream_opts,opts); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
405 return STREAM_ERROR; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
406 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
407 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
408 // Login |
16761
1eeb10c3e534
Make FtpSendCmd() function more user friendly by making it append the necessary "\r\n" line break (instead of the caller)
gpoirier
parents:
16750
diff
changeset
|
409 snprintf(str,255,"USER %s",p->user); |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
410 resp = FtpSendCmd(str,p,rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
411 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
412 // password needed |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
413 if(resp == 3) { |
16761
1eeb10c3e534
Make FtpSendCmd() function more user friendly by making it append the necessary "\r\n" line break (instead of the caller)
gpoirier
parents:
16750
diff
changeset
|
414 snprintf(str,255,"PASS %s",p->pass); |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
415 resp = FtpSendCmd(str,p,rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
416 if(resp != 2) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
417 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
418 close_f(stream); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
419 return STREAM_ERROR; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
420 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
421 } else if(resp != 2) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
422 mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
423 close_f(stream); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
424 return STREAM_ERROR; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
425 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
426 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
427 // Set the transfert type |
16761
1eeb10c3e534
Make FtpSendCmd() function more user friendly by making it append the necessary "\r\n" line break (instead of the caller)
gpoirier
parents:
16750
diff
changeset
|
428 resp = FtpSendCmd("TYPE I",p,rsp_txt); |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
429 if(resp != 2) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
430 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command 'TYPE I' failed: %s\n",rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
431 close_f(stream); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
432 return STREAM_ERROR; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
433 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
434 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
435 // Get the filesize |
16761
1eeb10c3e534
Make FtpSendCmd() function more user friendly by making it append the necessary "\r\n" line break (instead of the caller)
gpoirier
parents:
16750
diff
changeset
|
436 snprintf(str,255,"SIZE %s",p->filename); |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
437 resp = FtpSendCmd(str,p,rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
438 if(resp != 2) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
439 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
440 } else { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
441 int dummy; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
442 sscanf(rsp_txt,"%d %d",&dummy,&len); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
443 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
444 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
445 if(len > 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
446 stream->seek = seek; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
447 stream->end_pos = len; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
448 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
449 |
17854
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
450 // The data connection is really opened only at the first |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
451 // read/seek. This must be done when the cache is used |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
452 // because the connection would stay open in the main process, |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
453 // preventing correct abort with many servers. |
a63fa57ecbd7
Fix seeking with cache enabled on systems using fork().
albeu
parents:
17012
diff
changeset
|
454 stream->fd = -1; |
10625
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
455 stream->priv = p; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
456 stream->fill_buffer = fill_buffer; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
457 stream->close = close_f; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
458 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
459 return STREAM_OK; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
460 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
461 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
462 stream_info_t stream_info_ftp = { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
463 "File Transfer Protocol", |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
464 "ftp", |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
465 "Albeu", |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
466 "reuse a bit of code from ftplib written by Thomas Pfau", |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
467 open_f, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
468 { "ftp", NULL }, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
469 &stream_opts, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
470 1 // Urls are an option string |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
471 }; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
472 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
473 #endif |