]> git.saurik.com Git - apple/security.git/blob - Network/xfercore.h
Security-28.tar.gz
[apple/security.git] / Network / xfercore.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 // xfercore - core data transfer engine
21 //
22 #ifndef _H_XFERCORE
23 #define _H_XFERCORE
24
25 #include <Security/ip++.h>
26 #include <Security/selector.h>
27 #include <Security/buffers.h>
28 #include <Security/streams.h>
29 #include <cstdarg>
30
31 #if defined(SOCKS_SUPPORT)
32 # include <Security/socks++.h>
33 # define TCPClientSocket SocksClientSocket
34 # define TCPServerSocket SocksServerSocket
35 #endif
36
37 using Security::Buffer;
38 using namespace IPPlusPlus;
39
40
41 namespace Security {
42 namespace Network {
43
44
45 class TransferEngine : public Selector {
46 public:
47 TransferEngine() { }
48 virtual ~TransferEngine() { }
49
50 public:
51 class Client : public Selector::Client {
52 friend class TransferEngine;
53 public:
54 Client();
55 virtual ~Client();
56
57 public:
58 enum InputMode {
59 invalidInput, // error mode (invalid)
60 connecting, // working on TCP connection
61 rawInput, // raw chunk input (whatever's on the wire)
62 lineInput, // Internet lines input (\r\n)
63 autoReadInput, // bulk read to docked sink
64
65 autoIODone // transiition marker
66 };
67 InputMode mode() const { return mMode; }
68 void mode(InputMode m); // set (switch) mode
69 void mode(Sink &sink, size_t byteCount = 0);
70
71 void mode(Source &source, size_t byteCount = 0);
72 bool autoWriteActive() const { return mSource; }
73
74 enum Event { // event type: (input, length) arguments
75 inputAvailable, // input available in current mode: (data, length)
76 connectionDone, // TCP connection event: (NULL, errno)
77 autoReadDone, // autoReadInput has completed: (NULL, 0)
78 autoWriteDone, // autoWriteOutput has completed: (NULL, 0)
79 endOfInput, // end of data stream from remote end: (NULL, 0)
80 ioError // I/O failed: (CssmCommonError *, 0)
81 };
82
83 virtual void transit(Event event, char *data = NULL, size_t length = 0) = 0;
84 virtual void transitError(const CssmCommonError &error) = 0;
85 virtual int fileDesc() const = 0;
86
87 public:
88 // override this to implement I/O filters - default is pass-through
89 virtual size_t read(void *data, size_t size);
90 virtual size_t write(const void *data, size_t size);
91 virtual bool atEnd() const;
92
93 protected:
94 void printf(const char *format, ...);
95 void printfe(const char *format, ...);
96 void vprintf(const char *format, va_list args);
97 void vprintfe(const char *format, va_list args);
98
99 void flushOutput(bool autoFlush = true);
100
101 void tickle();
102
103 private:
104 void notify(int fd, Type type);
105
106 private:
107 void rawInputTransit();
108 void lineInputTransit();
109 void autoReadInputTransit();
110
111 void startOutput();
112 size_t autoCopy();
113
114 private:
115 InputMode mMode; // current mode
116 bool mAutoCopyOut; // auto-copyout overlay mode
117 Sink *mSink; // sink for autoReadInput mode
118 Source *mSource; // source for copyout overlay mode
119 size_t mResidualReadCount; // bytes left to autoReadInput (zero => unlimited)
120 size_t mResidualWriteCount; // bytes left to autoCopyOut (zero => unlimited)
121 bool mAutoFlush; // output auto-flush mode
122
123 FileDesc io;
124
125 Buffer mReadBuffer;
126 Buffer mWriteBuffer;
127 };
128
129 public:
130 void add(Client *client);
131 void remove(Client *client);
132 };
133
134
135 } // end namespace Network
136 } // end namespace Security
137
138
139 #endif //_H_XFERCORE