Mercurial > mplayer.hg
annotate libmpdemux/stream_ftp.c @ 10625:620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
different level of verbosity for control and data connections.
author | albeu |
---|---|
date | Fri, 15 Aug 2003 19:13:23 +0000 |
parents | |
children | f7df841c8b74 |
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> |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
14 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
15 #include "mp_msg.h" |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
16 #include "stream.h" |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
17 #include "help_mp.h" |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
18 #include "../m_option.h" |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
19 #include "../m_struct.h" |
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 static struct stream_priv_s { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
22 char* user; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
23 char* pass; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
24 char* host; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
25 int port; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
26 char* filename; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
27 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
28 char *cput,*cget; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
29 int handle; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
30 int cavail,cleft; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
31 char *buf; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
32 } stream_priv_dflts = { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
33 "anonymous","no@spam", |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
34 NULL, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
35 21, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
36 NULL, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
37 NULL, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
38 NULL, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
39 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
40 0, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
41 0,0, |
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 }; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
44 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
45 #define BUFSIZE 2048 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
46 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
47 #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
|
48 /// URL definition |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
49 static m_option_t stream_opts_fields[] = { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
50 {"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
|
51 {"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
|
52 {"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
|
53 {"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
|
54 {"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
|
55 { NULL, NULL, 0, 0, 0, 0, NULL } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
56 }; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
57 static struct m_struct_st stream_opts = { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
58 "ftp", |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
59 sizeof(struct stream_priv_s), |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
60 &stream_priv_dflts, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
61 stream_opts_fields |
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 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
64 #define TELNET_IAC 255 /* interpret as command: */ |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
65 #define TELNET_IP 244 /* interrupt process--permanently */ |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
66 #define TELNET_SYNCH 242 /* for telfunc calls */ |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
67 |
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 * read a line of text |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
70 * |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
71 * return -1 on error or bytecount |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
72 */ |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
73 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
|
74 { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
75 int x,retval = 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
76 char *end,*bp=buf; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
77 int eof = 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
78 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
79 do { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
80 if (ctl->cavail > 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
81 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
|
82 end = memccpy(bp,ctl->cget,'\n',x); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
83 if (end != NULL) |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
84 x = end - bp; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
85 retval += x; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
86 bp += x; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
87 *bp = '\0'; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
88 max -= x; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
89 ctl->cget += x; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
90 ctl->cavail -= x; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
91 if (end != NULL) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
92 bp -= 2; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
93 if (strcmp(bp,"\r\n") == 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
94 *bp++ = '\n'; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
95 *bp++ = '\0'; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
96 --retval; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
97 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
98 break; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
99 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
100 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
101 if (max == 1) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
102 *buf = '\0'; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
103 break; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
104 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
105 if (ctl->cput == ctl->cget) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
106 ctl->cput = ctl->cget = ctl->buf; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
107 ctl->cavail = 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
108 ctl->cleft = BUFSIZE; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
109 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
110 if(eof) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
111 if (retval == 0) |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
112 retval = -1; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
113 break; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
114 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
115 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
|
116 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
|
117 retval = -1; |
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 if (x == 0) |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
121 eof = 1; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
122 ctl->cleft -= x; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
123 ctl->cavail += x; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
124 ctl->cput += x; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
125 } while (1); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
126 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
127 return retval; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
128 } |
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 /* |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
131 * read a response from the server |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
132 * |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
133 * 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
|
134 * return 1 if first char matches |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
135 */ |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
136 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
|
137 { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
138 static char response[256]; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
139 char match[5]; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
140 int r; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
141 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
142 if (readline(response,256,ctl) == -1) |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
143 return 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
144 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
145 r = atoi(response)/100; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
146 if(rsp) strcpy(rsp,response); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
147 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
148 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
|
149 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
150 if (response[3] == '-') { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
151 strncpy(match,response,3); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
152 match[3] = ' '; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
153 match[4] = '\0'; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
154 do { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
155 if (readline(response,256,ctl) == -1) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
156 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
|
157 return 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
158 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
159 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
|
160 } while (strncmp(response,match,4)); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
161 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
162 return r; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
163 } |
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 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
166 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
|
167 { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
168 int l = strlen(cmd); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
169 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
170 mp_msg(MSGT_STREAM,MSGL_V, "[ftp] > %s",cmd); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
171 while(l > 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
172 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
|
173 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
174 if(s <= 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
175 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
|
176 return 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
177 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
178 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
179 cmd += s; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
180 l -= s; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
181 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
182 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
183 return readresp(nControl,rsp); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
184 } |
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 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
|
187 int resp,fd; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
188 char rsp_txt[256]; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
189 char* par,str[128]; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
190 int num[6]; |
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 resp = FtpSendCmd("PASV\r\n",p,rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
193 if(resp != 2) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
194 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
|
195 return 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
196 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
197 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
198 par = strchr(rsp_txt,'('); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
199 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
200 if(!par || !par[0] || !par[1]) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
201 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
|
202 return 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
203 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
204 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
205 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
|
206 &num[3],&num[4],&num[5]); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
207 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
|
208 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
|
209 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
210 if(fd <= 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
211 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
|
212 return 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
213 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
214 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
215 return fd; |
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 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
|
219 fd_set fds; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
220 struct timeval tv; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
221 int r; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
222 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
223 // Check if there is something to read. This avoid hang |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
224 // forever if the network stop responding. |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
225 FD_ZERO(&fds); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
226 FD_SET(s->fd,&fds); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
227 tv.tv_sec = 15; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
228 tv.tv_usec = 0; |
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 if(select(s->fd+1, &fds, NULL, NULL, &tv) < 1) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
231 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
|
232 return -1; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
233 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
234 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
235 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
|
236 return (r <= 0) ? -1 : r; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
237 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
238 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
239 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
|
240 struct stream_priv_s* p = s->priv; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
241 int resp; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
242 char str[256],rsp_txt[256]; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
243 fd_set fds; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
244 struct timeval tv; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
245 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
246 if(s->pos > s->end_pos) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
247 s->eof=1; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
248 return 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
249 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
250 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
251 FD_ZERO(&fds); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
252 FD_SET(p->handle,&fds); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
253 tv.tv_sec = tv.tv_usec = 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
254 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
255 // Check to see if the server doesn't alredy terminated the transfert |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
256 if(select(p->handle+1, &fds, NULL, NULL, &tv) > 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
257 if(readresp(p,rsp_txt) != 2) |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
258 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] Warning the server didn't finished the transfert correctly: %s\n",rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
259 close(s->fd); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
260 s->fd = -1; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
261 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
262 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
263 // Close current download |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
264 if(s->fd >= 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
265 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
|
266 //int fl; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
267 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
268 // First close the fd |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
269 close(s->fd); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
270 s->fd = 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
271 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
272 // 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
|
273 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
274 // 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
|
275 // 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
|
276 //fl=fcntl(p->handle,F_GETFL); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
277 //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
|
278 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
279 // 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
|
280 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
|
281 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
|
282 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
283 //fcntl(p->handle,F_SETFL,fl); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
284 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
285 // Get the 426 Transfer aborted |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
286 // Or the 226 Transfer complete |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
287 resp = readresp(p,rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
288 if(resp != 4 && resp != 2) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
289 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
|
290 s->eof = 1; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
291 return 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
292 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
293 // Send the ABOR command |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
294 // Ignore the return code as sometimes it fail with "nothing to abort" |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
295 FtpSendCmd("ABOR\n\r",p,rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
296 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
297 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
298 // Open a new connection |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
299 s->fd = FtpOpenPort(p); |
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->fd) return 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
302 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
303 if(newpos > 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
304 #ifdef _LARGEFILE_SOURCE |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
305 snprintf(str,255,"REST %lld\r\n",newpos); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
306 #else |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
307 snprintf(str,255,"REST %u\r\n",newpos); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
308 #endif |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
309 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
310 resp = FtpSendCmd(str,p,rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
311 if(resp != 3) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
312 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
|
313 newpos = 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
314 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
315 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
316 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
317 // Get the file |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
318 snprintf(str,255,"RETR %s\r\n",p->filename); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
319 resp = FtpSendCmd(str,p,rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
320 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
321 if(resp != 1) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
322 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
|
323 return 0; |
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 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
326 s->pos = newpos; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
327 return 1; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
328 } |
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 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
331 static void close_f(stream_t *s) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
332 struct stream_priv_s* p = s->priv; |
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 if(!p) return; |
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 if(s->fd > 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
337 close(s->fd); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
338 s->fd = 0; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
339 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
340 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
341 FtpSendCmd("QUIT\r\n",p,NULL); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
342 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
343 if(p->handle) close(p->handle); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
344 if(p->buf) free(p->buf); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
345 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
346 m_struct_free(&stream_opts,p); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
347 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
348 |
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 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
|
352 int len = 0,resp; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
353 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
|
354 char str[256],rsp_txt[256]; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
355 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
356 if(mode != STREAM_READ) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
357 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
|
358 m_struct_free(&stream_opts,opts); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
359 return STREAM_UNSUPORTED; |
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 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
362 if(!p->filename || !p->host) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
363 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
|
364 m_struct_free(&stream_opts,opts); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
365 return STREAM_ERROR; |
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 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
368 // Open the control connection |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
369 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
|
370 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
371 if(p->handle < 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
372 m_struct_free(&stream_opts,opts); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
373 return STREAM_ERROR; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
374 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
375 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
376 // 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
|
377 stream->fd = -1; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
378 stream->priv = p; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
379 p->buf = malloc(BUFSIZE); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
380 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
381 if (readresp(p, NULL) == 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
382 close_f(stream); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
383 m_struct_free(&stream_opts,opts); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
384 return STREAM_ERROR; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
385 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
386 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
387 // Login |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
388 snprintf(str,255,"USER %s\r\n",p->user); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
389 resp = FtpSendCmd(str,p,rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
390 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
391 // password needed |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
392 if(resp == 3) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
393 snprintf(str,255,"PASS %s\r\n",p->pass); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
394 resp = FtpSendCmd(str,p,rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
395 if(resp != 2) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
396 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
|
397 close_f(stream); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
398 return STREAM_ERROR; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
399 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
400 } else if(resp != 2) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
401 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
|
402 close_f(stream); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
403 return STREAM_ERROR; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
404 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
405 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
406 // Set the transfert type |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
407 resp = FtpSendCmd("TYPE I\r\n",p,rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
408 if(resp != 2) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
409 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
|
410 close_f(stream); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
411 return STREAM_ERROR; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
412 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
413 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
414 // Get the filesize |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
415 snprintf(str,255,"SIZE %s\r\n",p->filename); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
416 resp = FtpSendCmd(str,p,rsp_txt); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
417 if(resp != 2) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
418 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
|
419 } else { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
420 int dummy; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
421 sscanf(rsp_txt,"%d %d",&dummy,&len); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
422 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
423 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
424 // Start the data connection |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
425 stream->fd = FtpOpenPort(p); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
426 if(stream->fd <= 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
427 close_f(stream); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
428 return STREAM_ERROR; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
429 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
430 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
431 // Get the file |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
432 snprintf(str,255,"RETR %s\r\n",p->filename); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
433 resp = FtpSendCmd(str,p,rsp_txt); |
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 if(resp != 1) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
436 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
|
437 close_f(stream); |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
438 return STREAM_ERROR; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
439 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
440 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
441 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
442 if(len > 0) { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
443 stream->seek = seek; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
444 stream->end_pos = len; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
445 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
446 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
447 stream->priv = p; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
448 stream->fill_buffer = fill_buffer; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
449 stream->close = close_f; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
450 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
451 return STREAM_OK; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
452 } |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
453 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
454 stream_info_t stream_info_ftp = { |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
455 "File Transfer Protocol", |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
456 "ftp", |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
457 "Albeu", |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
458 "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
|
459 open_f, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
460 { "ftp", NULL }, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
461 &stream_opts, |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
462 1 // Urls are an option string |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
463 }; |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
464 |
620cc649f519
ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff
changeset
|
465 #endif |