annotate libmpdemux/stream_ftp.c @ 17173:60a4b99ab1b7

punctuation fixes for the previous commit
author wanderer
date Sun, 11 Dec 2005 17:13:56 +0000
parents 6ff3379a0862
children a63fa57ecbd7
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 #ifdef HAVE_FTP
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
5
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
6 #include <stdlib.h>
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
7 #include <stdio.h>
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
8
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
9 #include <sys/types.h>
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
10 #include <sys/stat.h>
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
11 #include <fcntl.h>
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
12 #include <unistd.h>
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
13 #include <errno.h>
10627
f7df841c8b74 ftp support for mingw
faust3
parents: 10625
diff changeset
14 #ifndef HAVE_WINSOCK2
10639
10899318ddaf fix compilation on mingw
faust3
parents: 10635
diff changeset
15 #include <sys/socket.h>
10627
f7df841c8b74 ftp support for mingw
faust3
parents: 10625
diff changeset
16 #define closesocket close
f7df841c8b74 ftp support for mingw
faust3
parents: 10625
diff changeset
17 #else
f7df841c8b74 ftp support for mingw
faust3
parents: 10625
diff changeset
18 #include <winsock2.h>
f7df841c8b74 ftp support for mingw
faust3
parents: 10625
diff changeset
19 #endif
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
20
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
21 #include "mp_msg.h"
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
22 #include "stream.h"
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
23 #include "help_mp.h"
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16761
diff changeset
24 #include "m_option.h"
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16761
diff changeset
25 #include "m_struct.h"
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
26
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
27 static struct stream_priv_s {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
28 char* user;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
29 char* pass;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
30 char* host;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
31 int port;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
32 char* filename;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
33
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
34 char *cput,*cget;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
35 int handle;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
36 int cavail,cleft;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
37 char *buf;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
38 } stream_priv_dflts = {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
39 "anonymous","no@spam",
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
40 NULL,
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
41 21,
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
42 NULL,
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
43 NULL,
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
44 NULL,
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
45
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
46 0,
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
47 0,0,
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
48 NULL
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
49 };
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
50
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
51 #define BUFSIZE 2048
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
52
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
53 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
54 /// URL definition
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
55 static m_option_t stream_opts_fields[] = {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
56 {"username", ST_OFF(user), CONF_TYPE_STRING, 0, 0 ,0, NULL},
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
57 {"password", ST_OFF(pass), CONF_TYPE_STRING, 0, 0 ,0, NULL},
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
58 {"hostname", ST_OFF(host), CONF_TYPE_STRING, 0, 0 ,0, NULL},
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
59 {"port", ST_OFF(port), CONF_TYPE_INT, 0, 0 ,65635, NULL},
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
60 {"filename", ST_OFF(filename), CONF_TYPE_STRING, 0, 0 ,0, NULL},
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
61 { NULL, NULL, 0, 0, 0, 0, NULL }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
62 };
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
63 static struct m_struct_st stream_opts = {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
64 "ftp",
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
65 sizeof(struct stream_priv_s),
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
66 &stream_priv_dflts,
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
67 stream_opts_fields
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
68 };
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
69
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
70 #define TELNET_IAC 255 /* interpret as command: */
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
71 #define TELNET_IP 244 /* interrupt process--permanently */
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
72 #define TELNET_SYNCH 242 /* for telfunc calls */
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
73
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 * read a line of text
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
76 *
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
77 * return -1 on error or bytecount
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 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
80 {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
81 int x,retval = 0;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
82 char *end,*bp=buf;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
83 int eof = 0;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
84
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
85 do {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
86 if (ctl->cavail > 0) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
87 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
88 end = memccpy(bp,ctl->cget,'\n',x);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
89 if (end != NULL)
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
90 x = end - bp;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
91 retval += x;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
92 bp += x;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
93 *bp = '\0';
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
94 max -= x;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
95 ctl->cget += x;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
96 ctl->cavail -= x;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
97 if (end != NULL) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
98 bp -= 2;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
99 if (strcmp(bp,"\r\n") == 0) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
100 *bp++ = '\n';
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
101 *bp++ = '\0';
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
102 --retval;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
103 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
104 break;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
105 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
106 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
107 if (max == 1) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
108 *buf = '\0';
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
109 break;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
110 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
111 if (ctl->cput == ctl->cget) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
112 ctl->cput = ctl->cget = ctl->buf;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
113 ctl->cavail = 0;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
114 ctl->cleft = BUFSIZE;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
115 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
116 if(eof) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
117 if (retval == 0)
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
118 retval = -1;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
119 break;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
120 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
121 if ((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
122 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
123 retval = -1;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
124 break;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
125 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
126 if (x == 0)
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
127 eof = 1;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
128 ctl->cleft -= x;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
129 ctl->cavail += x;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
130 ctl->cput += x;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
131 } while (1);
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 retval;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
134 }
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 /*
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
137 * read a response from the server
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
138 *
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
139 * 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
140 * return 1 if first char matches
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 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
143 {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
144 static char response[256];
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
145 char match[5];
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
146 int r;
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 if (readline(response,256,ctl) == -1)
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
149 return 0;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
150
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
151 r = atoi(response)/100;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
152 if(rsp) strcpy(rsp,response);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
153
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
154 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
155
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
156 if (response[3] == '-') {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
157 strncpy(match,response,3);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
158 match[3] = ' ';
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
159 match[4] = '\0';
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
160 do {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
161 if (readline(response,256,ctl) == -1) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
162 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
163 return 0;
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 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
166 } while (strncmp(response,match,4));
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 return r;
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
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
171
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
172 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
173 {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
174 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
175 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
176
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
177 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
178 while(l > 0) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
179 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
180
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
181 if(s <= 0) {
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] write error: %s\n",strerror(errno));
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
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
186 cmd += s;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
187 l -= s;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
188 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
189
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
190 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
191 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
192 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
193 return FtpSendCmd("\r\n", nControl, rsp);
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
194 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
195
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
196 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
197 int resp,fd;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
198 char rsp_txt[256];
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
199 char* par,str[128];
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
200 int num[6];
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
201
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
202 resp = FtpSendCmd("PASV",p,rsp_txt);
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
203 if(resp != 2) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
204 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
205 return 0;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
206 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
207
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
208 par = strchr(rsp_txt,'(');
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(!par || !par[0] || !par[1]) {
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] invalid server response: %s ??\n",rsp_txt);
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 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
216 &num[3],&num[4],&num[5]);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
217 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
218 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
219
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
220 if(fd <= 0) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
221 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
222 return 0;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
223 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
224
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
225 return fd;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
226 }
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 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
229 fd_set fds;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
230 struct timeval tv;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
231 int r;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
232
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
233 // 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
234 // forever if the network stop responding.
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
235 FD_ZERO(&fds);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
236 FD_SET(s->fd,&fds);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
237 tv.tv_sec = 15;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
238 tv.tv_usec = 0;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
239
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
240 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
241 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
242 return -1;
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
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
245 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
246 return (r <= 0) ? -1 : r;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
247 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
248
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
249 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
250 struct stream_priv_s* p = s->priv;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
251 int resp;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
252 char str[256],rsp_txt[256];
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
253 fd_set fds;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
254 struct timeval tv;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
255
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
256 if(s->pos > s->end_pos) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
257 s->eof=1;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
258 return 0;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
259 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
260
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
261 FD_ZERO(&fds);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
262 FD_SET(p->handle,&fds);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
263 tv.tv_sec = tv.tv_usec = 0;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
264
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
265 // 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
266 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
267 if(readresp(p,rsp_txt) != 2)
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
268 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
269 closesocket(s->fd);
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
270 s->fd = -1;
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
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
273 // Close current download
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
274 if(s->fd >= 0) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
275 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
276 //int fl;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
277
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
278 // First close the fd
10627
f7df841c8b74 ftp support for mingw
faust3
parents: 10625
diff changeset
279 closesocket(s->fd);
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
280 s->fd = 0;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
281
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
282 // 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
283
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
284 // 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
285 // 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
286 //fl=fcntl(p->handle,F_GETFL);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
287 //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
288
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
289 // 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
290 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
291 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
292
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
293 //fcntl(p->handle,F_SETFL,fl);
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 // Get the 426 Transfer aborted
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
296 // Or the 226 Transfer complete
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
297 resp = readresp(p,rsp_txt);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
298 if(resp != 4 && resp != 2) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
299 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
300 s->eof = 1;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
301 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 // Send the ABOR command
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
304 // 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
305 FtpSendCmd("ABOR",p,rsp_txt);
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
306 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
307
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
308 // Open a new connection
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
309 s->fd = FtpOpenPort(p);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
310
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
311 if(!s->fd) return 0;
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 if(newpos > 0) {
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
314 snprintf(str,255,"REST %"PRId64, (int64_t)newpos);
10625
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 resp = FtpSendCmd(str,p,rsp_txt);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
317 if(resp != 3) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
318 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
319 newpos = 0;
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 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
322
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
323 // Get the file
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
324 snprintf(str,255,"RETR %s",p->filename);
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
325 resp = FtpSendCmd(str,p,rsp_txt);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
326
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
327 if(resp != 1) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
328 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
329 return 0;
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
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
332 s->pos = newpos;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
333 return 1;
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
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
336
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
337 static void close_f(stream_t *s) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
338 struct stream_priv_s* p = s->priv;
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 if(!p) return;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
341
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
342 if(s->fd > 0) {
10627
f7df841c8b74 ftp support for mingw
faust3
parents: 10625
diff changeset
343 closesocket(s->fd);
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
344 s->fd = 0;
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
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
347 FtpSendCmd("QUIT",p,NULL);
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
348
10627
f7df841c8b74 ftp support for mingw
faust3
parents: 10625
diff changeset
349 if(p->handle) closesocket(p->handle);
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
350 if(p->buf) free(p->buf);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
351
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
352 m_struct_free(&stream_opts,p);
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
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
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
357 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
358 int len = 0,resp;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
359 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
360 char str[256],rsp_txt[256];
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(mode != STREAM_READ) {
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] Unknown open mode %d\n",mode);
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_UNSUPORTED;
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 if(!p->filename || !p->host) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
369 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
370 m_struct_free(&stream_opts,opts);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
371 return STREAM_ERROR;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
372 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
373
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
374 // Open the control connection
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
375 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
376
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
377 if(p->handle < 0) {
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_ERROR;
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 // 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
383 stream->fd = -1;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
384 stream->priv = p;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
385 p->buf = malloc(BUFSIZE);
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 if (readresp(p, NULL) == 0) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
388 close_f(stream);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
389 m_struct_free(&stream_opts,opts);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
390 return STREAM_ERROR;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
391 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
392
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
393 // 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
394 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
395 resp = FtpSendCmd(str,p,rsp_txt);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
396
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
397 // password needed
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
398 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
399 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
400 resp = FtpSendCmd(str,p,rsp_txt);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
401 if(resp != 2) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
402 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
403 close_f(stream);
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 } else if(resp != 2) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
407 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
408 close_f(stream);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
409 return STREAM_ERROR;
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
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
412 // 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
413 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
414 if(resp != 2) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
415 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
416 close_f(stream);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
417 return STREAM_ERROR;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
418 }
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 // 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
421 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
422 resp = FtpSendCmd(str,p,rsp_txt);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
423 if(resp != 2) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
424 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
425 } else {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
426 int dummy;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
427 sscanf(rsp_txt,"%d %d",&dummy,&len);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
428 }
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 // Start the data connection
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
431 stream->fd = FtpOpenPort(p);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
432 if(stream->fd <= 0) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
433 close_f(stream);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
434 return STREAM_ERROR;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
435 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
436
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
437 // Get the file
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
438 snprintf(str,255,"RETR %s",p->filename);
10625
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
439 resp = FtpSendCmd(str,p,rsp_txt);
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 if(resp != 1) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
442 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
443 close_f(stream);
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
444 return STREAM_ERROR;
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
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
448 if(len > 0) {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
449 stream->seek = seek;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
450 stream->end_pos = len;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
451 }
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 stream->priv = p;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
454 stream->fill_buffer = fill_buffer;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
455 stream->close = close_f;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
456
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
457 return STREAM_OK;
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
458 }
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
459
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
460 stream_info_t stream_info_ftp = {
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
461 "File Transfer Protocol",
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
462 "ftp",
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
463 "Albeu",
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
464 "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
465 open_f,
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
466 { "ftp", NULL },
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
467 &stream_opts,
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
468 1 // Urls are an option string
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
469 };
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
470
620cc649f519 ftp support. The change on connect2Server is needed bcs we need 2
albeu
parents:
diff changeset
471 #endif