2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // ftp-protocol - FTP protocol objects
22 #ifndef _H_FTP_PROTOCOL
23 #define _H_FTP_PROTOCOL
28 #include "netconnection.h"
30 #include <Security/ip++.h>
31 #include <Security/inetreply.h>
39 // The Protocol object for the FTP protocol
41 class FTPProtocol
: public Protocol
{
46 static const IPPort defaultFtpPort
= 21;
48 FTPProtocol(Manager
&mgr
);
51 FTPTransfer
*makeTransfer(const Target
&target
, Operation operation
);
54 // FTP-specific operation codes
56 downloadDirectory
= protocolSpecific
, // get filename list (NLST)
57 downloadListing
, // get host-specific listing (LIST)
58 makeDirectory
, // make a directory (MKD)
59 removeDirectory
, // remove a directory (RMD)
60 removeFile
, // remove a file (DELE)
61 genericCommand
// issue generic FTP command
66 // The data connection object manages a data pipe (for one upload/download)
68 class FTPDataConnection
: public TransferEngine::Client
, public TCPClientSocket
{
70 FTPDataConnection(FTPConnection
&conn
) : connection(conn
) { }
72 FTPConnection
&connection
; // the main Connection we belong to
74 void start(Sink
&sink
); // start download
75 void start(Source
&source
); // start upload
76 void close(); // unconditional close
77 void connectionDone(); // Connection is done
79 OSStatus
status() const { return mFailureStatus
; }
84 void transit(Event event
, char *input
, size_t inputLength
);
85 void transitError(const CssmCommonError
&error
);
90 OSStatus mFailureStatus
; // noErr unless something went wrong
91 bool mTransferDone
; // our transfer is all done
92 bool mConnectionDone
; // our Connection is ready to finish()
97 // This is the persistent connection object.
99 class FTPConnection
: public TCPConnection
{
100 friend class FTPDataConnection
;
102 FTPConnection(Protocol
&proto
, const HostTarget
&tgt
);
104 // state machine master state
106 errorState
, // invalid state marker (reset or fail)
109 loginInitial
, // just connected [want hello or need-login]
110 loginUserSent
, // USER command sent [want hello or need-pass]
111 loginPassSent
, // PASS command sent [want dispatch command]
114 idle
, // at command prompt, idle [nothing pending]
116 // data transfer states
117 typeCommandSent
, // sent TYPE command [want ok]
118 passiveSent
, // sent PASV [want contact address]
119 portSent
, // sent PORT [want port ok]
120 restartSent
, // sent REST [want 350 Restarting...]
121 transferSent
, // sent RETR et al [want transfer starting]
122 transferInProgress
, // download in progress [want transfer complete]
125 directCommandSent
, // sent non-transfer command, want success
130 FTPTransfer
&transfer() { return transferAs
<FTPTransfer
>(); }
132 void request(const char *path
);
136 void transit(Event event
, char *input
, size_t inputLength
);
137 void transitError(const CssmCommonError
&error
);
140 void startCommand(); // initiate mOperation, if any
141 void startTransfer(bool restarted
= false);
143 bool imageMode() const { return mImageMode
; }
144 void imageMode(bool mode
);
146 void fail(const char *reply
, OSStatus error
= Transfer::defaultOSStatusError
)
147 { setError(reply
, error
); Error::throwMe(error
); }
148 void fail() { retain(false); Connection::fail(); }
151 State state
; // state engine state
152 InetReply::Continuation replyContinuation
; // cotinued-reply marker
154 // state describing the ongoing connection
155 bool mImageMode
; // in image (vs. ascii) mode
156 bool mPassive
; // current transfer is in passive mode
158 string mOperationPath
; // remote path for operation
160 FTPDataConnection mDataPath
; // subsidiary (data transfer) connection
161 TCPServerSocket mReceiver
; // incoming listen socket for active mode transfers
166 // The official Transfer object (for all kinds of transfers)
168 class FTPTransfer
: public Transfer
{
170 FTPTransfer(Protocol
&proto
, const Target
&target
, Operation operation
);
172 ResultClass
resultClass() const;
174 string
&ftpResponse() { return mPrimaryResponseString
; }
175 unsigned int &ftpResponseCode() { return mPrimaryResponseCode
; }
176 unsigned int ftpResponseCode() const { return mPrimaryResponseCode
; }
179 void start(); // start me up
180 void abort(); // abort this Transfer
182 string mFailedReply
; // reply string that triggered failure
185 string mPrimaryResponseString
; //FTP protocol first response line.
186 unsigned int mPrimaryResponseCode
; // numeric response code.
191 unsigned int h1
, h2
, h3
, h4
, p1
, p2
;
194 FTPAddress(const IPSockAddress
&addr
);
195 operator IPSockAddress () const;
200 } // end namespace Network
201 } // end namespace Security
204 #endif //_H_FTP_PROTOCOL