annotate stream/stream_ftp.c @ 21452:1f42a6aba8c9

Do not call usec_sleep when we are not supposed to sleep.
author reimar
date Sun, 03 Dec 2006 14:01:22 +0000
parents 2a9d669e5ff6
children 49a433e2e78f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 #include <stdlib.h>
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
5 #include <stdio.h>
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
6
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
7 #include <sys/types.h>
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
8 #include <sys/stat.h>
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
9 #include <fcntl.h>
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
10 #include <unistd.h>
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
11 #include <errno.h>
10627
f7df841c8b74 ftp support for mingw
faust3
parents: 10625
diff changeset
12 #ifndef HAVE_WINSOCK2
10639
10899318ddaf fix compilation on mingw
faust3
parents: 10635
diff changeset
13 #include <sys/socket.h>
10627
f7df841c8b74 ftp support for mingw
faust3
parents: 10625
diff changeset
14 #define closesocket close
f7df841c8b74 ftp support for mingw
faust3
parents: 10625
diff changeset
15 #else
f7df841c8b74 ftp support for mingw
faust3
parents: 10625
diff changeset
16 #include <winsock2.h>
f7df841c8b74 ftp support for mingw
faust3
parents: 10625
diff changeset
17 #endif
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
18
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
19 #include "mp_msg.h"
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
20 #include "stream.h"
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
21 #include "help_mp.h"
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16761
diff changeset
22 #include "m_option.h"
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16761
diff changeset
23 #include "m_struct.h"
19335
2a9d669e5ff6 isolated tcp socket code from network.c to a dedicated file
ben
parents: 19271
diff changeset
24 #include "tcp.h"
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
25
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
26 static struct stream_priv_s {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
27 char* user;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
28 char* pass;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
29 char* host;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
30 int port;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
31 char* filename;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
32
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
33 char *cput,*cget;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
34 int handle;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
35 int cavail,cleft;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
36 char *buf;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
37 } stream_priv_dflts = {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
38 "anonymous","no@spam",
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
39 NULL,
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
40 21,
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
41 NULL,
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
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
45 0,
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
46 0,0,
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
47 NULL
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
48 };
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 #define BUFSIZE 2048
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
51
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
52 #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
53 /// URL definition
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
54 static m_option_t stream_opts_fields[] = {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
55 {"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
56 {"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
57 {"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
58 {"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
59 {"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
60 { NULL, NULL, 0, 0, 0, 0, NULL }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
61 };
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
62 static struct m_struct_st stream_opts = {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
63 "ftp",
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
64 sizeof(struct stream_priv_s),
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
65 &stream_priv_dflts,
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
66 stream_opts_fields
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 #define TELNET_IAC 255 /* interpret as command: */
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
70 #define TELNET_IP 244 /* interrupt process--permanently */
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
71 #define TELNET_SYNCH 242 /* for telfunc calls */
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
72
17854
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
73 // 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
74 // forever if the network stop responding.
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
75 static int fd_can_read(int fd,int timeout) {
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
76 fd_set fds;
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
77 struct timeval tv;
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
78
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
79 FD_ZERO(&fds);
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
80 FD_SET(fd,&fds);
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
81 tv.tv_sec = timeout;
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
82 tv.tv_usec = 0;
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
83
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
84 return (select(fd+1, &fds, NULL, NULL, &tv) > 0);
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
85 }
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
86
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
87 /*
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
88 * read a line of text
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
89 *
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
90 * return -1 on error or bytecount
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
91 */
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
92 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
93 {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
94 int x,retval = 0;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
95 char *end,*bp=buf;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
96 int eof = 0;
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 do {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
99 if (ctl->cavail > 0) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
100 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
101 end = memccpy(bp,ctl->cget,'\n',x);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
102 if (end != NULL)
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
103 x = end - bp;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
104 retval += x;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
105 bp += x;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
106 *bp = '\0';
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
107 max -= x;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
108 ctl->cget += x;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
109 ctl->cavail -= x;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
110 if (end != NULL) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
111 bp -= 2;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
112 if (strcmp(bp,"\r\n") == 0) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
113 *bp++ = '\n';
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
114 *bp++ = '\0';
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
115 --retval;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
116 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
117 break;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
118 }
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 (max == 1) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
121 *buf = '\0';
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
122 break;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
123 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
124 if (ctl->cput == ctl->cget) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
125 ctl->cput = ctl->cget = ctl->buf;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
126 ctl->cavail = 0;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
127 ctl->cleft = BUFSIZE;
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 if(eof) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
130 if (retval == 0)
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
131 retval = -1;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
132 break;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
133 }
17854
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
134
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
135 if(!fd_can_read(ctl->handle, 15)) {
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
136 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
137 retval = -1;
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
138 break;
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
139 }
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
140
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
141 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
142 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
143 retval = -1;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
144 break;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
145 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
146 if (x == 0)
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
147 eof = 1;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
148 ctl->cleft -= x;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
149 ctl->cavail += x;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
150 ctl->cput += x;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
151 } while (1);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
152
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
153 return retval;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
154 }
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 * read a response from the server
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 * 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
160 * return 1 if first char matches
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 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
163 {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
164 static char response[256];
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
165 char match[5];
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
166 int r;
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 if (readline(response,256,ctl) == -1)
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
169 return 0;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
170
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
171 r = atoi(response)/100;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
172 if(rsp) strcpy(rsp,response);
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 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
175
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
176 if (response[3] == '-') {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
177 strncpy(match,response,3);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
178 match[3] = ' ';
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
179 match[4] = '\0';
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
180 do {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
181 if (readline(response,256,ctl) == -1) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
182 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
183 return 0;
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 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
186 } while (strncmp(response,match,4));
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
187 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
188 return r;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
189 }
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 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
193 {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
194 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
195 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
196
17854
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
197 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
198 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
199 while(l > 0) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
200 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
201
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
202 if(s <= 0) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
203 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
204 return 0;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
205 }
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 cmd += s;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
208 l -= s;
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
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
211 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
212 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
213 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
214 return FtpSendCmd("\r\n", nControl, rsp);
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
215 }
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 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
218 int resp,fd;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
219 char rsp_txt[256];
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
220 char* par,str[128];
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
221 int num[6];
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
222
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
223 resp = FtpSendCmd("PASV",p,rsp_txt);
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
224 if(resp != 2) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
225 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
226 return 0;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
227 }
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 par = strchr(rsp_txt,'(');
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
230
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
231 if(!par || !par[0] || !par[1]) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
232 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
233 return 0;
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
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
236 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
237 &num[3],&num[4],&num[5]);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
238 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
239 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
240
17854
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
241 if(fd < 0)
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
242 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
243
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
244 return fd;
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
17854
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
247 static int FtpOpenData(stream_t* s,size_t newpos) {
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
248 struct stream_priv_s* p = s->priv;
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
249 int resp;
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
250 char str[256],rsp_txt[256];
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
251
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
252 // Open a new connection
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
253 s->fd = FtpOpenPort(p);
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
254
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
255 if(s->fd < 0) return 0;
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
256
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
257 if(newpos > 0) {
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
258 snprintf(str,255,"REST %"PRId64, (int64_t)newpos);
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
259
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
260 resp = FtpSendCmd(str,p,rsp_txt);
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
261 if(resp != 3) {
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
262 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
263 newpos = 0;
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
264 }
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 // Get the file
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
268 snprintf(str,255,"RETR %s",p->filename);
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
269 resp = FtpSendCmd(str,p,rsp_txt);
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
270
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
271 if(resp != 1) {
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
272 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
273 return 0;
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
274 }
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 s->pos = newpos;
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
277 return 1;
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
278 }
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
279
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
280 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
281 int r;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
282
17854
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
283 if(s->fd < 0 && !FtpOpenData(s,s->pos))
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
284 return -1;
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
285
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
286 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
287 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
288 return -1;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
289 }
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 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
292 return (r <= 0) ? -1 : r;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
293 }
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 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
296 struct stream_priv_s* p = s->priv;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
297 int resp;
17854
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
298 char rsp_txt[256];
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
299
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
300 if(s->pos > s->end_pos) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
301 s->eof=1;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
302 return 0;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
303 }
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 // 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
306 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
307 if(readresp(p,rsp_txt) != 2)
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
308 mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] Warning the server didn't finished the transfert correctly: %s\n",rsp_txt);
10627
f7df841c8b74 ftp support for mingw
faust3
parents: 10625
diff changeset
309 closesocket(s->fd);
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
310 s->fd = -1;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
311 }
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 // Close current download
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
314 if(s->fd >= 0) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
315 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
316 //int fl;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
317
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
318 // First close the fd
10627
f7df841c8b74 ftp support for mingw
faust3
parents: 10625
diff changeset
319 closesocket(s->fd);
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
320 s->fd = 0;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
321
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
322 // 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
323
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
324 // 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
325 // 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
326 //fl=fcntl(p->handle,F_GETFL);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
327 //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
328
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
329 // 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
330 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
331 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
332
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
333 //fcntl(p->handle,F_SETFL,fl);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
334
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
335 // Get the 426 Transfer aborted
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
336 // Or the 226 Transfer complete
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
337 resp = readresp(p,rsp_txt);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
338 if(resp != 4 && resp != 2) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
339 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
340 s->eof = 1;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
341 return 0;
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 // Send the ABOR command
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
344 // 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
345 FtpSendCmd("ABOR",p,rsp_txt);
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
346 }
17854
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
347 return FtpOpenData(s,newpos);
10625
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 void close_f(stream_t *s) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
352 struct stream_priv_s* p = s->priv;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
353
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
354 if(!p) return;
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(s->fd > 0) {
10627
f7df841c8b74 ftp support for mingw
faust3
parents: 10625
diff changeset
357 closesocket(s->fd);
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
358 s->fd = 0;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
359 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
360
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
361 FtpSendCmd("QUIT",p,NULL);
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
362
10627
f7df841c8b74 ftp support for mingw
faust3
parents: 10625
diff changeset
363 if(p->handle) closesocket(p->handle);
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
364 if(p->buf) free(p->buf);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
365
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
366 m_struct_free(&stream_opts,p);
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
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 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
372 int len = 0,resp;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
373 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
374 char str[256],rsp_txt[256];
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 if(mode != STREAM_READ) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
377 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
378 m_struct_free(&stream_opts,opts);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
379 return STREAM_UNSUPORTED;
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
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
382 if(!p->filename || !p->host) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
383 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
384 m_struct_free(&stream_opts,opts);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
385 return STREAM_ERROR;
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
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
388 // Open the control connection
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
389 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
390
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
391 if(p->handle < 0) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
392 m_struct_free(&stream_opts,opts);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
393 return STREAM_ERROR;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
394 }
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 // 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
397 stream->fd = -1;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
398 stream->priv = p;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
399 p->buf = malloc(BUFSIZE);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
400
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
401 if (readresp(p, NULL) == 0) {
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 m_struct_free(&stream_opts,opts);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
404 return STREAM_ERROR;
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
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
407 // 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
408 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
409 resp = FtpSendCmd(str,p,rsp_txt);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
410
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
411 // password needed
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
412 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
413 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
414 resp = FtpSendCmd(str,p,rsp_txt);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
415 if(resp != 2) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
416 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
417 close_f(stream);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
418 return STREAM_ERROR;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
419 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
420 } else if(resp != 2) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
421 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
422 close_f(stream);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
423 return STREAM_ERROR;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
424 }
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 // 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
427 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
428 if(resp != 2) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
429 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
430 close_f(stream);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
431 return STREAM_ERROR;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
432 }
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 // 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
435 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
436 resp = FtpSendCmd(str,p,rsp_txt);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
437 if(resp != 2) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
438 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
439 } else {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
440 int dummy;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
441 sscanf(rsp_txt,"%d %d",&dummy,&len);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
442 }
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 if(len > 0) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
445 stream->seek = seek;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
446 stream->end_pos = len;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
447 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
448
17854
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
449 // 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
450 // 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
451 // 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
452 // preventing correct abort with many servers.
a63fa57ecbd7 Fix seeking with cache enabled on systems using fork().
albeu
parents: 17012
diff changeset
453 stream->fd = -1;
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
454 stream->priv = p;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
455 stream->fill_buffer = fill_buffer;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
456 stream->close = close_f;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
457
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
458 return STREAM_OK;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
459 }
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 stream_info_t stream_info_ftp = {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
462 "File Transfer Protocol",
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
463 "ftp",
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
464 "Albeu",
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
465 "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
466 open_f,
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
467 { "ftp", NULL },
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
468 &stream_opts,
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
469 1 // Urls are an option string
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
470 };