2 * Copyright (C) 1996-2015 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 33 Client-side Routines */
11 #ifndef SQUID_SERVERS_FTP_SERVER_H
12 #define SQUID_SERVERS_FTP_SERVER_H
14 #include "base/Lock.h"
15 #include "client_side.h"
27 fssHandleUploadRequest
,
36 // TODO: This should become a part of MasterXaction when we start sending
37 // master transactions to the clients/ code.
38 /// Transaction information shared among our FTP client and server jobs.
39 class MasterState
: public RefCountable
42 typedef RefCount
<MasterState
> Pointer
;
44 MasterState(): serverState(fssBegin
), clientReadGreeting(false) {}
46 Ip::Address clientDataAddr
; ///< address of our FTP client data connection
47 SBuf workingDir
; ///< estimated current working directory for URI formation
48 ServerState serverState
; ///< what our FTP server is doing
49 bool clientReadGreeting
; ///< whether our FTP client read their FTP server greeting
52 /// Manages a control connection from an FTP client.
53 class Server
: public ConnStateData
56 // XXX CBDATA_CLASS expands to nonvirtual toCbdata, AsyncJob::toCbdata
57 // is pure virtual. breaks build on clang if override is used
60 explicit Server(const MasterXaction::Pointer
&xact
);
63 virtual void callException(const std::exception
&e
);
65 // This is a pointer in hope to minimize future changes when MasterState
66 // becomes a part of MasterXaction. Guaranteed not to be nil.
67 MasterState::Pointer master
; ///< info shared among our FTP client and server jobs
70 friend void StartListening();
72 // errors detected before it is possible to create an HTTP request wrapper
73 enum class EarlyErrorKind
{
83 /* ConnStateData API */
84 virtual ClientSocketContext
*parseOneRequest();
85 virtual void processParsedRequest(ClientSocketContext
*context
);
86 virtual void notePeerConnection(Comm::ConnectionPointer conn
);
87 virtual void clientPinnedConnectionClosed(const CommCloseCbParams
&io
);
88 virtual void handleReply(HttpReply
*header
, StoreIOBuffer receivedData
);
89 virtual int pipelinePrefetchMax() const;
90 virtual void writeControlMsgAndCall(HttpReply
*rep
, AsyncCall::Pointer
&call
);
91 virtual time_t idleTimeout() const;
94 virtual void noteMoreBodySpaceAvailable(BodyPipe::Pointer
);
95 virtual void noteBodyConsumerAborted(BodyPipe::Pointer ptr
);
101 static void AcceptCtrlConnection(const CommAcceptCbParams
¶ms
);
102 void acceptDataConnection(const CommAcceptCbParams
¶ms
);
103 void readUploadData(const CommIoCbParams
&io
);
104 void wroteEarlyReply(const CommIoCbParams
&io
);
105 void wroteReply(const CommIoCbParams
&io
);
106 void wroteReplyData(const CommIoCbParams
&io
);
107 void connectedForData(const CommConnectCbParams
¶ms
);
109 unsigned int listenForDataConnection();
110 bool createDataConnection(Ip::Address cltAddr
);
111 void closeDataConnection();
113 void calcUri(const SBuf
*file
);
114 void changeState(const Ftp::ServerState newState
, const char *reason
);
115 ClientSocketContext
*handleUserRequest(const SBuf
&cmd
, SBuf
¶ms
);
116 bool checkDataConnPost() const;
117 void replyDataWritingCheckpoint();
118 void maybeReadUploadData();
120 void setReply(const int code
, const char *msg
);
121 void writeCustomReply(const int code
, const char *msg
, const HttpReply
*reply
= NULL
);
122 void writeEarlyReply(const int code
, const char *msg
);
123 void writeErrorReply(const HttpReply
*reply
, const int status
);
124 void writeForwardedForeign(const HttpReply
*reply
);
125 void writeForwardedReply(const HttpReply
*reply
);
126 void writeForwardedReplyAndCall(const HttpReply
*reply
, AsyncCall::Pointer
&call
);
127 void writeReply(MemBuf
&mb
);
129 ClientSocketContext
*earlyError(const EarlyErrorKind eek
);
130 bool handleRequest(HttpRequest
*);
131 void setDataCommand();
132 bool checkDataConnPre();
134 /// a method handling an FTP command; selected by handleRequest()
135 typedef bool (Ftp::Server::*RequestHandler
)(String
&cmd
, String
¶ms
);
136 bool handleFeatRequest(String
&cmd
, String
¶ms
);
137 bool handlePasvRequest(String
&cmd
, String
¶ms
);
138 bool handlePortRequest(String
&cmd
, String
¶ms
);
139 bool handleDataRequest(String
&cmd
, String
¶ms
);
140 bool handleUploadRequest(String
&cmd
, String
¶ms
);
141 bool handleEprtRequest(String
&cmd
, String
¶ms
);
142 bool handleEpsvRequest(String
&cmd
, String
¶ms
);
143 bool handleCwdRequest(String
&cmd
, String
¶ms
);
144 bool handlePassRequest(String
&cmd
, String
¶ms
);
145 bool handleCdupRequest(String
&cmd
, String
¶ms
);
147 /// a method handling an FTP response; selected by handleReply()
148 typedef void (Ftp::Server::*ReplyHandler
)(const HttpReply
*reply
, StoreIOBuffer data
);
149 void handleFeatReply(const HttpReply
*header
, StoreIOBuffer receivedData
);
150 void handlePasvReply(const HttpReply
*header
, StoreIOBuffer receivedData
);
151 void handlePortReply(const HttpReply
*header
, StoreIOBuffer receivedData
);
152 void handleErrorReply(const HttpReply
*header
, StoreIOBuffer receivedData
);
153 void handleDataReply(const HttpReply
*header
, StoreIOBuffer receivedData
);
154 void handleUploadReply(const HttpReply
*header
, StoreIOBuffer receivedData
);
155 void handleEprtReply(const HttpReply
*header
, StoreIOBuffer receivedData
);
156 void handleEpsvReply(const HttpReply
*header
, StoreIOBuffer receivedData
);
159 void doProcessRequest();
160 void shovelUploadData();
161 void resetLogin(const char *reason
);
163 SBuf uri
; ///< a URI reconstructed from various FTP message details
164 SBuf host
; ///< intended dest. of a transparently intercepted FTP conn
165 bool gotEpsvAll
; ///< restrict data conn setup commands to just EPSV
166 AsyncCall::Pointer onDataAcceptCall
; ///< who to call upon data conn acceptance
167 Comm::ConnectionPointer dataListenConn
; ///< data connection listening socket
168 Comm::ConnectionPointer dataConn
; ///< data connection
169 char uploadBuf
[CLIENT_REQ_BUF_SZ
]; ///< data connection input buffer
170 size_t uploadAvailSize
; ///< number of yet unused uploadBuf bytes
172 AsyncCall::Pointer listener
; ///< set when we are passively listening
173 AsyncCall::Pointer connector
; ///< set when we are actively connecting
174 AsyncCall::Pointer reader
; ///< set when we are reading FTP data
179 #endif /* SQUID_SERVERS_FTP_SERVER_H */