]> git.ipfire.org Git - thirdparty/squid.git/blame - src/clients/FtpClient.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / clients / FtpClient.h
CommitLineData
434a79b0 1/*
bbc27441 2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
434a79b0 3 *
bbc27441
AJ
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
434a79b0
DK
7 */
8
bbc27441
AJ
9/* DEBUG: section 09 File Transfer Protocol (FTP) */
10
5517260a
AR
11#ifndef SQUID_FTP_CLIENT_H
12#define SQUID_FTP_CLIENT_H
434a79b0 13
fccd4a86 14#include "clients/Client.h"
434a79b0 15
54a6c0cd 16class String;
27c841f6
AR
17namespace Ftp
18{
434a79b0
DK
19
20extern const char *const crlf;
21
e7ce227f
AR
22/// Common code for FTP server control and data channels.
23/// Does not own the channel descriptor, which is managed by Ftp::Client.
5517260a 24class Channel
434a79b0
DK
25{
26public:
27 /// called after the socket is opened, sets up close handler
28 void opened(const Comm::ConnectionPointer &conn, const AsyncCall::Pointer &aCloser);
29
30 /** Handles all operations needed to properly close the active channel FD.
31 * clearing the close handler, clearing the listen socket properly, and calling comm_close
32 */
33 void close();
34
35 void forget(); /// remove the close handler, leave connection open
36
37 void clear(); ///< just drops conn and close handler. does not close active connections.
38
39 Comm::ConnectionPointer conn; ///< channel descriptor
40
41 /** A temporary handle to the connection being listened on.
42 * Closing this will also close the waiting Data channel acceptor.
43 * If a data connection has already been accepted but is still waiting in the event queue
44 * the callback will still happen and needs to be handled (usually dropped).
45 */
46 Comm::ConnectionPointer listenConn;
47
48 AsyncCall::Pointer opener; ///< Comm opener handler callback.
49private:
50 AsyncCall::Pointer closer; ///< Comm close handler callback
51};
52
e7ce227f
AR
53/// FTP channel for control commands.
54/// This channel is opened once per transaction.
55class CtrlChannel: public Ftp::Channel
56{
57public:
58 CtrlChannel();
59 ~CtrlChannel();
60
61 char *buf;
62 size_t size;
63 size_t offset;
64 wordlist *message;
65 char *last_command;
66 char *last_reply;
67 int replycode;
68
69private:
70 CtrlChannel(const CtrlChannel &); // not implemented
71 CtrlChannel &operator =(const CtrlChannel &); // not implemented
72};
73
74/// FTP channel for data exchanges.
75/// This channel may be opened/closed a few times.
76class DataChannel: public Ftp::Channel
77{
78public:
79 DataChannel();
80 ~DataChannel();
81
82 void addr(const Ip::Address &addr); ///< import host and port
83
84public:
85 MemBuf *readBuf;
86 char *host;
87 unsigned short port;
88 bool read_pending;
89};
90
43446566 91/// FTP client functionality shared among FTP Gateway and Relay clients.
fccd4a86 92class Client: public ::Client
434a79b0 93{
5c2f68b7
AJ
94 CBDATA_CLASS(Client);
95
434a79b0 96public:
5517260a
AR
97 explicit Client(FwdState *fwdState);
98 virtual ~Client();
434a79b0 99
5517260a 100 /// handle a fatal transaction error, closing the control connection
434a79b0 101 virtual void failed(err_type error = ERR_NONE, int xerrno = 0);
5517260a
AR
102
103 /// read timeout handler
434a79b0 104 virtual void timeout(const CommTimeoutCbParams &io);
5517260a 105
fccd4a86 106 /* Client API */
5517260a
AR
107 virtual void maybeReadVirginBody();
108
434a79b0 109 void writeCommand(const char *buf);
73950ceb
AR
110
111 /// extracts remoteAddr from PASV response, validates it,
112 /// sets data address details, and returns true on success
113 bool handlePasvReply(Ip::Address &remoteAddr);
000e664b
AR
114 bool handleEpsvReply(Ip::Address &remoteAddr);
115
116 bool sendEprt();
117 bool sendPort();
118 bool sendPassive();
434a79b0 119 void connectDataChannel();
000e664b 120 bool openListenSocket();
434a79b0
DK
121 void switchTimeoutToDataChannel();
122
e7ce227f
AR
123 CtrlChannel ctrl; ///< FTP control channel state
124 DataChannel data; ///< FTP data channel state
434a79b0 125
000e664b
AR
126 enum {
127 BEGIN,
128 SENT_USER,
129 SENT_PASS,
130 SENT_TYPE,
131 SENT_MDTM,
132 SENT_SIZE,
133 SENT_EPRT,
134 SENT_PORT,
135 SENT_EPSV_ALL,
136 SENT_EPSV_1,
137 SENT_EPSV_2,
138 SENT_PASV,
139 SENT_CWD,
140 SENT_LIST,
141 SENT_NLST,
142 SENT_REST,
143 SENT_RETR,
144 SENT_STOR,
145 SENT_QUIT,
146 READING_DATA,
147 WRITING_DATA,
148 SENT_MKDIR,
149 SENT_FEAT,
54a6c0cd
CT
150 SENT_PWD,
151 SENT_CDUP,
000e664b
AR
152 SENT_DATA_REQUEST, // LIST, NLST or RETR requests..
153 SENT_COMMAND, // General command
154 END
155 } ftp_state_t;
156
434a79b0
DK
157 int state;
158 char *old_request;
159 char *old_reply;
160
161protected:
5517260a 162 /* AsyncJob API */
434a79b0
DK
163 virtual void start();
164
fccd4a86 165 /* Client API */
434a79b0
DK
166 virtual void closeServer();
167 virtual bool doneWithServer() const;
5517260a
AR
168 virtual const Comm::ConnectionPointer & dataConnection() const;
169 virtual void abortTransaction(const char *reason);
170
434a79b0
DK
171 virtual Http::StatusCode failedHttpStatus(err_type &error);
172 void ctrlClosed(const CommCloseCbParams &io);
173 void scheduleReadControlReply(int buffered_ok);
174 void readControlReply(const CommIoCbParams &io);
175 virtual void handleControlReply();
176 void writeCommandCallback(const CommIoCbParams &io);
43446566 177 virtual void dataChannelConnected(const CommConnectCbParams &io) = 0;
434a79b0
DK
178 void dataRead(const CommIoCbParams &io);
179 void dataComplete();
180 AsyncCall::Pointer dataCloser();
181 virtual void dataClosed(const CommCloseCbParams &io);
5517260a 182 void initReadBuf();
434a79b0
DK
183
184 // sending of the request body to the server
185 virtual void sentRequestBody(const CommIoCbParams &io);
186 virtual void doneSendingRequestBody();
187
188private:
a2c7f09a 189 bool parseControlReply(size_t &bytesUsed);
434a79b0 190
e7ce227f
AR
191 /// XXX: An old hack for FTP servers like ftp.netscape.com that may not
192 /// respond to PASV. Use faster connect timeout instead of read timeout.
193 bool shortenReadTimeout;
434a79b0
DK
194};
195
92ae4c86 196} // namespace Ftp
434a79b0 197
5517260a 198#endif /* SQUID_FTP_CLIENT_H */
f53969cc 199