2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 09 File Transfer Protocol (FTP) */
11 #ifndef SQUID_FTP_CLIENT_H
12 #define SQUID_FTP_CLIENT_H
14 #include "clients/Client.h"
20 extern const char *const crlf
;
22 /// Common code for FTP server control and data channels.
23 /// Does not own the channel descriptor, which is managed by Ftp::Client.
27 /// called after the socket is opened, sets up close handler
28 void opened(const Comm::ConnectionPointer
&conn
, const AsyncCall::Pointer
&aCloser
);
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
35 void forget(); /// remove the close handler, leave connection open
37 void clear(); ///< just drops conn and close handler. does not close active connections.
39 Comm::ConnectionPointer conn
; ///< channel descriptor
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).
46 Comm::ConnectionPointer listenConn
;
48 AsyncCall::Pointer opener
; ///< Comm opener handler callback.
50 AsyncCall::Pointer closer
; ///< Comm close handler callback
53 /// FTP channel for control commands.
54 /// This channel is opened once per transaction.
55 class CtrlChannel
: public Ftp::Channel
70 CtrlChannel(const CtrlChannel
&); // not implemented
71 CtrlChannel
&operator =(const CtrlChannel
&); // not implemented
74 /// FTP channel for data exchanges.
75 /// This channel may be opened/closed a few times.
76 class DataChannel
: public Ftp::Channel
82 void addr(const Ip::Address
&addr
); ///< import host and port
91 /// FTP client functionality shared among FTP Gateway and Relay clients.
92 class Client
: public ::Client
97 explicit Client(FwdState
*fwdState
);
100 /// handle a fatal transaction error, closing the control connection
101 virtual void failed(err_type error
= ERR_NONE
, int xerrno
= 0,
102 ErrorState
*ftperr
= nullptr);
104 /// read timeout handler
105 virtual void timeout(const CommTimeoutCbParams
&io
);
108 virtual void maybeReadVirginBody();
110 void writeCommand(const char *buf
);
112 /// extracts remoteAddr from PASV response, validates it,
113 /// sets data address details, and returns true on success
114 bool handlePasvReply(Ip::Address
&remoteAddr
);
115 bool handleEpsvReply(Ip::Address
&remoteAddr
);
120 void connectDataChannel();
121 bool openListenSocket();
122 void switchTimeoutToDataChannel();
124 CtrlChannel ctrl
; ///< FTP control channel state
125 DataChannel data
; ///< FTP data channel state
153 SENT_DATA_REQUEST
, // LIST, NLST or RETR requests..
154 SENT_COMMAND
, // General command
164 virtual void start();
167 virtual void closeServer();
168 virtual bool doneWithServer() const;
169 virtual const Comm::ConnectionPointer
& dataConnection() const;
170 virtual void abortAll(const char *reason
);
172 virtual Http::StatusCode
failedHttpStatus(err_type
&error
);
173 void ctrlClosed(const CommCloseCbParams
&io
);
174 void scheduleReadControlReply(int buffered_ok
);
175 void readControlReply(const CommIoCbParams
&io
);
176 virtual void handleControlReply();
177 void writeCommandCallback(const CommIoCbParams
&io
);
178 virtual void dataChannelConnected(const CommConnectCbParams
&io
) = 0;
179 void dataRead(const CommIoCbParams
&io
);
181 AsyncCall::Pointer
dataCloser();
182 virtual void dataClosed(const CommCloseCbParams
&io
);
185 // sending of the request body to the server
186 virtual void sentRequestBody(const CommIoCbParams
&io
);
187 virtual void doneSendingRequestBody();
190 bool parseControlReply(size_t &bytesUsed
);
192 /// XXX: An old hack for FTP servers like ftp.netscape.com that may not
193 /// respond to PASV. Use faster connect timeout instead of read timeout.
194 bool shortenReadTimeout
;
199 #endif /* SQUID_FTP_CLIENT_H */