]> git.saurik.com Git - apple/security.git/blob - Network/ftp-protocol.h
Security-176.tar.gz
[apple/security.git] / Network / ftp-protocol.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 //
20 // ftp-protocol - FTP protocol objects
21 //
22 #ifndef _H_FTP_PROTOCOL
23 #define _H_FTP_PROTOCOL
24
25 #include "xfercore.h"
26 #include "protocol.h"
27 #include "transfer.h"
28 #include "netconnection.h"
29 #include "neterror.h"
30 #include <Security/ip++.h>
31 #include <Security/inetreply.h>
32
33
34 namespace Security {
35 namespace Network {
36
37
38 //
39 // The Protocol object for the FTP protocol
40 //
41 class FTPProtocol : public Protocol {
42 protected:
43 class FTPConnection;
44 public:
45 class FTPTransfer;
46 static const IPPort defaultFtpPort = 21;
47
48 FTPProtocol(Manager &mgr);
49
50 public:
51 FTPTransfer *makeTransfer(const Target &target, Operation operation);
52
53 public:
54 // FTP-specific operation codes
55 enum {
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
62 };
63
64 private:
65 //
66 // The data connection object manages a data pipe (for one upload/download)
67 //
68 class FTPDataConnection : public TransferEngine::Client, public TCPClientSocket {
69 public:
70 FTPDataConnection(FTPConnection &conn) : connection(conn) { }
71
72 FTPConnection &connection; // the main Connection we belong to
73
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
78
79 OSStatus status() const { return mFailureStatus; }
80
81 int fileDesc() const;
82
83 protected:
84 void transit(Event event, char *input, size_t inputLength);
85 void transitError(const CssmCommonError &error);
86 void setup();
87 void finish();
88
89 private:
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()
93 };
94
95 protected:
96 //
97 // This is the persistent connection object.
98 //
99 class FTPConnection : public TCPConnection {
100 friend class FTPDataConnection;
101 public:
102 FTPConnection(Protocol &proto, const HostTarget &tgt);
103
104 // state machine master state
105 enum State {
106 errorState, // invalid state marker (reset or fail)
107
108 // login sub-engine
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]
112
113 // idle state
114 idle, // at command prompt, idle [nothing pending]
115
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]
123
124 // misc. states
125 directCommandSent, // sent non-transfer command, want success
126
127 START = loginInitial
128 };
129
130 FTPTransfer &transfer() { return transferAs<FTPTransfer>(); }
131
132 void request(const char *path);
133 void abort();
134
135 protected:
136 void transit(Event event, char *input, size_t inputLength);
137 void transitError(const CssmCommonError &error);
138 bool validate();
139
140 void startCommand(); // initiate mOperation, if any
141 void startTransfer(bool restarted = false);
142
143 bool imageMode() const { return mImageMode; }
144 void imageMode(bool mode);
145
146 void fail(const char *reply, OSStatus error = Transfer::defaultOSStatusError)
147 { setError(reply, error); Error::throwMe(error); }
148 void fail() { retain(false); Connection::fail(); }
149
150 protected:
151 State state; // state engine state
152 InetReply::Continuation replyContinuation; // cotinued-reply marker
153
154 // state describing the ongoing connection
155 bool mImageMode; // in image (vs. ascii) mode
156 bool mPassive; // current transfer is in passive mode
157
158 string mOperationPath; // remote path for operation
159
160 FTPDataConnection mDataPath; // subsidiary (data transfer) connection
161 TCPServerSocket mReceiver; // incoming listen socket for active mode transfers
162 };
163
164 public:
165 //
166 // The official Transfer object (for all kinds of transfers)
167 //
168 class FTPTransfer : public Transfer {
169 public:
170 FTPTransfer(Protocol &proto, const Target &target, Operation operation);
171
172 ResultClass resultClass() const;
173
174 string &ftpResponse() { return mPrimaryResponseString; }
175 unsigned int &ftpResponseCode() { return mPrimaryResponseCode; }
176 unsigned int ftpResponseCode() const { return mPrimaryResponseCode; }
177
178 protected:
179 void start(); // start me up
180 void abort(); // abort this Transfer
181
182 string mFailedReply; // reply string that triggered failure
183
184 private:
185 string mPrimaryResponseString; //FTP protocol first response line.
186 unsigned int mPrimaryResponseCode; // numeric response code.
187 };
188
189 private:
190 struct FTPAddress {
191 unsigned int h1, h2, h3, h4, p1, p2;
192
193 FTPAddress() { }
194 FTPAddress(const IPSockAddress &addr);
195 operator IPSockAddress () const;
196 };
197 };
198
199
200 } // end namespace Network
201 } // end namespace Security
202
203
204 #endif //_H_FTP_PROTOCOL