2
* s5b.h - direct connection protocol via tcp
3
* Copyright (C) 2003 Justin Karneges
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.
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.
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
27
#include<qvaluelist.h>
29
#include"bytestream.h"
42
typedef QValueList<StreamHost> StreamHostList;
43
typedef QPtrList<S5BConnection> S5BConnectionList;
44
typedef QPtrListIterator<S5BConnection> S5BConnectionListIt;
50
S5BDatagram(int source, int dest, const QByteArray &data);
52
int sourcePort() const;
54
QByteArray data() const;
61
class S5BConnection : public ByteStream
65
enum Mode { Stream, Datagram };
66
enum Error { ErrRefused, ErrConnect, ErrProxy, ErrSocket };
67
enum State { Idle, Requesting, Connecting, WaitingForAccept, Active };
71
void setProxy(const Jid &proxy);
73
void connectToJid(const Jid &peer, const QString &sid, Mode m = Stream);
79
bool isRemote() const;
84
void write(const QByteArray &);
85
QByteArray read(int bytes=0);
86
int bytesAvailable() const;
87
int bytesToWrite() const;
89
void writeDatagram(const S5BDatagram &);
90
S5BDatagram readDatagram();
91
int datagramsAvailable() const;
94
void proxyQuery(); // querying proxy for streamhost information
95
void proxyResult(bool b); // query success / fail
96
void requesting(); // sent actual S5B request (initiator only)
97
void accepted(); // target accepted (initiator only
98
void tryingHosts(const StreamHostList &hosts); // currently connecting to these hosts
99
void proxyConnect(); // connecting to proxy
100
void waitingForActivation(); // waiting for activation (target only)
101
void connected(); // connection active
102
void datagramReady();
107
void sc_connectionClosed();
108
void sc_delayedCloseFinished();
110
void sc_bytesWritten(int);
113
void su_packetReady(const QByteArray &buf);
119
void reset(bool clear=false);
120
void handleUDP(const QByteArray &buf);
121
void sendUDP(const QByteArray &buf);
123
friend class S5BManager;
124
void man_waitForAccept(const S5BRequest &r);
125
void man_clientReady(SocksClient *, SocksUDP *);
126
void man_udpReady(const QByteArray &buf);
127
void man_failed(int);
128
S5BConnection(S5BManager *, QObject *parent=0);
131
class S5BManager : public QObject
135
S5BManager(Client *);
138
Client *client() const;
139
S5BServer *server() const;
140
void setServer(S5BServer *s);
142
bool isAcceptableSID(const Jid &peer, const QString &sid) const;
143
QString genUniqueSID(const Jid &peer) const;
145
S5BConnection *createConnection();
146
S5BConnection *takeIncoming();
152
void incomingReady();
155
void ps_incoming(const S5BRequest &req);
156
void ps_incomingUDPSuccess(const Jid &from, const QString &dstaddr);
157
void ps_incomingActivate(const Jid &from, const QString &sid, const Jid &streamHost);
158
void item_accepted();
159
void item_tryingHosts(const StreamHostList &list);
160
void item_proxyConnect();
161
void item_waitingForActivation();
162
void item_connected();
163
void item_error(int);
164
void query_finished();
170
S5BConnection *findIncoming(const Jid &from, const QString &sid) const;
171
Entry *findEntry(S5BConnection *) const;
172
Entry *findEntry(Item *) const;
173
Entry *findEntryByHash(const QString &key) const;
174
Entry *findEntryBySID(const Jid &peer, const QString &sid) const;
175
Entry *findServerEntryByHash(const QString &key) const;
177
void entryContinue(Entry *e);
178
void queryProxy(Entry *e);
179
bool targetShouldOfferProxy(Entry *e);
181
friend class S5BConnection;
182
void con_connect(S5BConnection *);
183
void con_accept(S5BConnection *);
184
void con_reject(S5BConnection *);
185
void con_unlink(S5BConnection *);
186
void con_sendUDP(S5BConnection *, const QByteArray &buf);
188
friend class S5BServer;
189
bool srv_ownsHash(const QString &key) const;
190
void srv_incomingReady(SocksClient *sc, const QString &key);
191
void srv_incomingUDP(bool init, const QHostAddress &addr, int port, const QString &key, const QByteArray &data);
195
void doSuccess(const Jid &peer, const QString &id, const Jid &streamHost);
196
void doError(const Jid &peer, const QString &id, int, const QString &);
197
void doActivate(const Jid &peer, const QString &sid, const Jid &streamHost);
200
class S5BConnector : public QObject
204
S5BConnector(QObject *parent=0);
208
void start(const Jid &self, const StreamHostList &hosts, const QString &key, bool udp, int timeout);
209
SocksClient *takeClient();
211
StreamHost streamHostUsed() const;
219
void item_result(bool);
226
friend class S5BManager;
227
void man_udpSuccess(const Jid &streamHost);
230
// listens on a port for serving
231
class S5BServer : public QObject
235
S5BServer(QObject *par=0);
238
bool isActive() const;
239
bool start(int port);
242
void setHostList(const QStringList &);
243
QStringList hostList() const;
248
void ss_incomingReady();
249
void ss_incomingUDP(const QString &host, int port, const QHostAddress &addr, int sourcePort, const QByteArray &data);
250
void item_result(bool);
256
friend class S5BManager;
257
void link(S5BManager *);
258
void unlink(S5BManager *);
260
const QPtrList<S5BManager> & managerList() const;
261
void writeUDP(const QHostAddress &addr, int port, const QByteArray &data);
264
class JT_S5B : public Task
271
void request(const Jid &to, const QString &sid, const StreamHostList &hosts, bool fast, bool udp=false);
272
void requestProxyInfo(const Jid &to);
273
void requestActivation(const Jid &to, const QString &sid, const Jid &target);
277
bool take(const QDomElement &);
279
Jid streamHostUsed() const;
280
StreamHost proxyInfo() const;
294
StreamHostList hosts;
298
class JT_PushS5B : public Task
305
int priority() const;
307
void respondSuccess(const Jid &to, const QString &id, const Jid &streamHost);
308
void respondError(const Jid &to, const QString &id, int code, const QString &str);
309
void sendUDPSuccess(const Jid &to, const QString &dstaddr);
310
void sendActivate(const Jid &to, const QString &sid, const Jid &streamHost);
312
bool take(const QDomElement &);
315
void incoming(const S5BRequest &req);
316
void incomingUDPSuccess(const Jid &from, const QString &dstaddr);
317
void incomingActivate(const Jid &from, const QString &sid, const Jid &streamHost);
325
const Jid & jid() const;
326
const QString & host() const;
328
bool isProxy() const;
329
void setJid(const Jid &);
330
void setHost(const QString &);
332
void setIsProxy(bool);