1 | /*
|
---|
2 | * s5b.h - direct connection protocol via tcp
|
---|
3 | * Copyright (C) 2003 Justin Karneges
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU Lesser General Public
|
---|
7 | * License as published by the Free Software Foundation; either
|
---|
8 | * version 2.1 of the License, or (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This library is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | * Lesser General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU Lesser General Public
|
---|
16 | * License along with this library; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
18 | *
|
---|
19 | */
|
---|
20 |
|
---|
21 | #ifndef XMPP_S5B_H
|
---|
22 | #define XMPP_S5B_H
|
---|
23 |
|
---|
24 | #include<qobject.h>
|
---|
25 | #include<qcstring.h>
|
---|
26 | #include<qptrlist.h>
|
---|
27 | #include<qvaluelist.h>
|
---|
28 | #include"im.h"
|
---|
29 | #include"bytestream.h"
|
---|
30 |
|
---|
31 | class SocksClient;
|
---|
32 | class SocksUDP;
|
---|
33 |
|
---|
34 | namespace XMPP
|
---|
35 | {
|
---|
36 | class StreamHost;
|
---|
37 | class S5BConnection;
|
---|
38 | class S5BManager;
|
---|
39 | class S5BServer;
|
---|
40 | struct S5BRequest;
|
---|
41 | typedef QValueList<StreamHost> StreamHostList;
|
---|
42 | typedef QPtrList<S5BConnection> S5BConnectionList;
|
---|
43 | typedef QPtrListIterator<S5BConnection> S5BConnectionListIt;
|
---|
44 |
|
---|
45 | class S5BDatagram
|
---|
46 | {
|
---|
47 | public:
|
---|
48 | S5BDatagram();
|
---|
49 | S5BDatagram(int source, int dest, const QByteArray &data);
|
---|
50 |
|
---|
51 | int sourcePort() const;
|
---|
52 | int destPort() const;
|
---|
53 | QByteArray data() const;
|
---|
54 |
|
---|
55 | private:
|
---|
56 | int _source, _dest;
|
---|
57 | QByteArray _buf;
|
---|
58 | };
|
---|
59 |
|
---|
60 | class S5BConnection : public ByteStream
|
---|
61 | {
|
---|
62 | Q_OBJECT
|
---|
63 | public:
|
---|
64 | enum Mode { Stream, Datagram };
|
---|
65 | enum Error { ErrRefused, ErrConnect, ErrProxy, ErrSocket };
|
---|
66 | enum State { Idle, Requesting, Connecting, WaitingForAccept, Active };
|
---|
67 | ~S5BConnection();
|
---|
68 |
|
---|
69 | Jid proxy() const;
|
---|
70 | void setProxy(const Jid &proxy);
|
---|
71 |
|
---|
72 | void connectToJid(const Jid &peer, const QString &sid, Mode m = Stream);
|
---|
73 | void accept();
|
---|
74 | void close();
|
---|
75 |
|
---|
76 | Jid peer() const;
|
---|
77 | QString sid() const;
|
---|
78 | bool isRemote() const;
|
---|
79 | Mode mode() const;
|
---|
80 | int state() const;
|
---|
81 |
|
---|
82 | bool isOpen() const;
|
---|
83 | void write(const QByteArray &);
|
---|
84 | QByteArray read(int bytes=0);
|
---|
85 | int bytesAvailable() const;
|
---|
86 | int bytesToWrite() const;
|
---|
87 |
|
---|
88 | void writeDatagram(const S5BDatagram &);
|
---|
89 | S5BDatagram readDatagram();
|
---|
90 | int datagramsAvailable() const;
|
---|
91 |
|
---|
92 | signals:
|
---|
93 | void proxyQuery(); // querying proxy for streamhost information
|
---|
94 | void proxyResult(bool b); // query success / fail
|
---|
95 | void requesting(); // sent actual S5B request (initiator only)
|
---|
96 | void accepted(); // target accepted (initiator only
|
---|
97 | void tryingHosts(const StreamHostList &hosts); // currently connecting to these hosts
|
---|
98 | void proxyConnect(); // connecting to proxy
|
---|
99 | void waitingForActivation(); // waiting for activation (target only)
|
---|
100 | void connected(); // connection active
|
---|
101 | void datagramReady();
|
---|
102 |
|
---|
103 | private slots:
|
---|
104 | void doPending();
|
---|
105 |
|
---|
106 | void sc_connectionClosed();
|
---|
107 | void sc_delayedCloseFinished();
|
---|
108 | void sc_readyRead();
|
---|
109 | void sc_bytesWritten(int);
|
---|
110 | void sc_error(int);
|
---|
111 |
|
---|
112 | void su_packetReady(const QByteArray &buf);
|
---|
113 |
|
---|
114 | private:
|
---|
115 | class Private;
|
---|
116 | Private *d;
|
---|
117 |
|
---|
118 | void reset(bool clear=false);
|
---|
119 | void handleUDP(const QByteArray &buf);
|
---|
120 | void sendUDP(const QByteArray &buf);
|
---|
121 |
|
---|
122 | friend class S5BManager;
|
---|
123 | void man_waitForAccept(const S5BRequest &r);
|
---|
124 | void man_clientReady(SocksClient *, SocksUDP *);
|
---|
125 | void man_udpReady(const QByteArray &buf);
|
---|
126 | void man_failed(int);
|
---|
127 | S5BConnection(S5BManager *, QObject *parent=0);
|
---|
128 | };
|
---|
129 |
|
---|
130 | class S5BManager : public QObject
|
---|
131 | {
|
---|
132 | Q_OBJECT
|
---|
133 | public:
|
---|
134 | S5BManager(Client *);
|
---|
135 | ~S5BManager();
|
---|
136 |
|
---|
137 | Client *client() const;
|
---|
138 | S5BServer *server() const;
|
---|
139 | void setServer(S5BServer *s);
|
---|
140 |
|
---|
141 | bool isAcceptableSID(const Jid &peer, const QString &sid) const;
|
---|
142 | QString genUniqueSID(const Jid &peer) const;
|
---|
143 |
|
---|
144 | S5BConnection *createConnection();
|
---|
145 | S5BConnection *takeIncoming();
|
---|
146 |
|
---|
147 | class Item;
|
---|
148 | class Entry;
|
---|
149 |
|
---|
150 | signals:
|
---|
151 | void incomingReady();
|
---|
152 |
|
---|
153 | private slots:
|
---|
154 | void ps_incoming(const S5BRequest &req);
|
---|
155 | void ps_incomingUDPSuccess(const Jid &from, const QString &dstaddr);
|
---|
156 | void ps_incomingActivate(const Jid &from, const QString &sid, const Jid &streamHost);
|
---|
157 | void item_accepted();
|
---|
158 | void item_tryingHosts(const StreamHostList &list);
|
---|
159 | void item_proxyConnect();
|
---|
160 | void item_waitingForActivation();
|
---|
161 | void item_connected();
|
---|
162 | void item_error(int);
|
---|
163 | void query_finished();
|
---|
164 |
|
---|
165 | private:
|
---|
166 | class Private;
|
---|
167 | Private *d;
|
---|
168 |
|
---|
169 | S5BConnection *findIncoming(const Jid &from, const QString &sid) const;
|
---|
170 | Entry *findEntry(S5BConnection *) const;
|
---|
171 | Entry *findEntry(Item *) const;
|
---|
172 | Entry *findEntryByHash(const QString &key) const;
|
---|
173 | Entry *findEntryBySID(const Jid &peer, const QString &sid) const;
|
---|
174 | Entry *findServerEntryByHash(const QString &key) const;
|
---|
175 |
|
---|
176 | void entryContinue(Entry *e);
|
---|
177 | void queryProxy(Entry *e);
|
---|
178 | bool targetShouldOfferProxy(Entry *e);
|
---|
179 |
|
---|
180 | friend class S5BConnection;
|
---|
181 | void con_connect(S5BConnection *);
|
---|
182 | void con_accept(S5BConnection *);
|
---|
183 | void con_reject(S5BConnection *);
|
---|
184 | void con_unlink(S5BConnection *);
|
---|
185 | void con_sendUDP(S5BConnection *, const QByteArray &buf);
|
---|
186 |
|
---|
187 | friend class S5BServer;
|
---|
188 | bool srv_ownsHash(const QString &key) const;
|
---|
189 | void srv_incomingReady(SocksClient *sc, const QString &key);
|
---|
190 | void srv_incomingUDP(bool init, const QHostAddress &addr, int port, const QString &key, const QByteArray &data);
|
---|
191 | void srv_unlink();
|
---|
192 |
|
---|
193 | friend class Item;
|
---|
194 | void doSuccess(const Jid &peer, const QString &id, const Jid &streamHost);
|
---|
195 | void doError(const Jid &peer, const QString &id, int, const QString &);
|
---|
196 | void doActivate(const Jid &peer, const QString &sid, const Jid &streamHost);
|
---|
197 | };
|
---|
198 |
|
---|
199 | class S5BConnector : public QObject
|
---|
200 | {
|
---|
201 | Q_OBJECT
|
---|
202 | public:
|
---|
203 | S5BConnector(QObject *parent=0);
|
---|
204 | ~S5BConnector();
|
---|
205 |
|
---|
206 | void reset();
|
---|
207 | void start(const Jid &self, const StreamHostList &hosts, const QString &key, bool udp, int timeout);
|
---|
208 | SocksClient *takeClient();
|
---|
209 | SocksUDP *takeUDP();
|
---|
210 | StreamHost streamHostUsed() const;
|
---|
211 |
|
---|
212 | class Item;
|
---|
213 |
|
---|
214 | signals:
|
---|
215 | void result(bool);
|
---|
216 |
|
---|
217 | private slots:
|
---|
218 | void item_result(bool);
|
---|
219 | void t_timeout();
|
---|
220 |
|
---|
221 | private:
|
---|
222 | class Private;
|
---|
223 | Private *d;
|
---|
224 |
|
---|
225 | friend class S5BManager;
|
---|
226 | void man_udpSuccess(const Jid &streamHost);
|
---|
227 | };
|
---|
228 |
|
---|
229 | // listens on a port for serving
|
---|
230 | class S5BServer : public QObject
|
---|
231 | {
|
---|
232 | Q_OBJECT
|
---|
233 | public:
|
---|
234 | S5BServer(QObject *par=0);
|
---|
235 | ~S5BServer();
|
---|
236 |
|
---|
237 | bool isActive() const;
|
---|
238 | bool start(int port);
|
---|
239 | void stop();
|
---|
240 | int port() const;
|
---|
241 | void setHostList(const QStringList &);
|
---|
242 | QStringList hostList() const;
|
---|
243 |
|
---|
244 | class Item;
|
---|
245 |
|
---|
246 | private slots:
|
---|
247 | void ss_incomingReady();
|
---|
248 | void ss_incomingUDP(const QString &host, int port, const QHostAddress &addr, int sourcePort, const QByteArray &data);
|
---|
249 | void item_result(bool);
|
---|
250 |
|
---|
251 | private:
|
---|
252 | class Private;
|
---|
253 | Private *d;
|
---|
254 |
|
---|
255 | friend class S5BManager;
|
---|
256 | void link(S5BManager *);
|
---|
257 | void unlink(S5BManager *);
|
---|
258 | void unlinkAll();
|
---|
259 | const QPtrList<S5BManager> & managerList() const;
|
---|
260 | void writeUDP(const QHostAddress &addr, int port, const QByteArray &data);
|
---|
261 | };
|
---|
262 |
|
---|
263 | class JT_S5B : public Task
|
---|
264 | {
|
---|
265 | Q_OBJECT
|
---|
266 | public:
|
---|
267 | JT_S5B(Task *);
|
---|
268 | ~JT_S5B();
|
---|
269 |
|
---|
270 | void request(const Jid &to, const QString &sid, const StreamHostList &hosts, bool fast, bool udp=false);
|
---|
271 | void requestProxyInfo(const Jid &to);
|
---|
272 | void requestActivation(const Jid &to, const QString &sid, const Jid &target);
|
---|
273 |
|
---|
274 | void onGo();
|
---|
275 | void onDisconnect();
|
---|
276 | bool take(const QDomElement &);
|
---|
277 |
|
---|
278 | Jid streamHostUsed() const;
|
---|
279 | StreamHost proxyInfo() const;
|
---|
280 |
|
---|
281 | private slots:
|
---|
282 | void t_timeout();
|
---|
283 |
|
---|
284 | private:
|
---|
285 | class Private;
|
---|
286 | Private *d;
|
---|
287 | };
|
---|
288 |
|
---|
289 | struct S5BRequest
|
---|
290 | {
|
---|
291 | Jid from;
|
---|
292 | QString id, sid;
|
---|
293 | StreamHostList hosts;
|
---|
294 | bool fast;
|
---|
295 | bool udp;
|
---|
296 | };
|
---|
297 | class JT_PushS5B : public Task
|
---|
298 | {
|
---|
299 | Q_OBJECT
|
---|
300 | public:
|
---|
301 | JT_PushS5B(Task *);
|
---|
302 | ~JT_PushS5B();
|
---|
303 |
|
---|
304 | int priority() const;
|
---|
305 |
|
---|
306 | void respondSuccess(const Jid &to, const QString &id, const Jid &streamHost);
|
---|
307 | void respondError(const Jid &to, const QString &id, int code, const QString &str);
|
---|
308 | void sendUDPSuccess(const Jid &to, const QString &dstaddr);
|
---|
309 | void sendActivate(const Jid &to, const QString &sid, const Jid &streamHost);
|
---|
310 |
|
---|
311 | bool take(const QDomElement &);
|
---|
312 |
|
---|
313 | signals:
|
---|
314 | void incoming(const S5BRequest &req);
|
---|
315 | void incomingUDPSuccess(const Jid &from, const QString &dstaddr);
|
---|
316 | void incomingActivate(const Jid &from, const QString &sid, const Jid &streamHost);
|
---|
317 | };
|
---|
318 |
|
---|
319 | class StreamHost
|
---|
320 | {
|
---|
321 | public:
|
---|
322 | StreamHost();
|
---|
323 |
|
---|
324 | const Jid & jid() const;
|
---|
325 | const QString & host() const;
|
---|
326 | int port() const;
|
---|
327 | bool isProxy() const;
|
---|
328 | void setJid(const Jid &);
|
---|
329 | void setHost(const QString &);
|
---|
330 | void setPort(int);
|
---|
331 | void setIsProxy(bool);
|
---|
332 |
|
---|
333 | private:
|
---|
334 | Jid j;
|
---|
335 | QString v_host;
|
---|
336 | int v_port;
|
---|
337 | bool proxy;
|
---|
338 | };
|
---|
339 | }
|
---|
340 |
|
---|
341 | #endif
|
---|