]>
Commit | Line | Data |
---|---|---|
bac41a7b A |
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 | ||
ded8f8e2 A |
101 | void flushInput(); |
102 | ||
bac41a7b A |
103 | void tickle(); |
104 | ||
105 | private: | |
106 | void notify(int fd, Type type); | |
107 | ||
108 | private: | |
109 | void rawInputTransit(); | |
ded8f8e2 | 110 | bool lineInputTransit(); |
bac41a7b A |
111 | void autoReadInputTransit(); |
112 | ||
113 | void startOutput(); | |
114 | size_t autoCopy(); | |
115 | ||
116 | private: | |
117 | InputMode mMode; // current mode | |
118 | bool mAutoCopyOut; // auto-copyout overlay mode | |
119 | Sink *mSink; // sink for autoReadInput mode | |
120 | Source *mSource; // source for copyout overlay mode | |
121 | size_t mResidualReadCount; // bytes left to autoReadInput (zero => unlimited) | |
122 | size_t mResidualWriteCount; // bytes left to autoCopyOut (zero => unlimited) | |
123 | bool mAutoFlush; // output auto-flush mode | |
ded8f8e2 | 124 | bool mInputFlushed; // transit flushed input; do not complete buffer ops |
bac41a7b A |
125 | |
126 | FileDesc io; | |
127 | ||
128 | Buffer mReadBuffer; | |
129 | Buffer mWriteBuffer; | |
130 | }; | |
131 | ||
132 | public: | |
133 | void add(Client *client); | |
134 | void remove(Client *client); | |
135 | }; | |
136 | ||
137 | ||
138 | } // end namespace Network | |
139 | } // end namespace Security | |
140 | ||
141 | ||
142 | #endif //_H_XFERCORE |