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
26 #include <QHostAddress>
28 #include "bytestream.h"
29 #include "xmpp_bytestream.h"
30 #include "xmpp/jid/jid.h"
31 #include "xmpp_task.h"
32 #include "xmpp_stanza.h"
45 typedef QList
<StreamHost
> StreamHostList
;
46 typedef QList
<S5BConnection
*> S5BConnectionList
;
52 S5BDatagram(int source
, int dest
, const QByteArray
&data
);
54 int sourcePort() const;
56 QByteArray
data() const;
63 class S5BConnection
: public BSConnection
67 enum Mode
{ Stream
, Datagram
};
72 void setProxy(const Jid
&proxy
);
74 void connectToJid(const Jid
&peer
, const QString
&sid
, Mode m
= Stream
);
75 void connectToJid(const Jid
&peer
, const QString
&sid
) {
76 connectToJid(peer
, sid
, Stream
);
84 BytestreamManager
* manager() const;
85 bool isRemote() const;
90 void write(const QByteArray
&);
91 QByteArray
read(int bytes
=0);
92 int bytesAvailable() const;
93 int bytesToWrite() const;
95 void writeDatagram(const S5BDatagram
&);
96 S5BDatagram
readDatagram();
97 int datagramsAvailable() const;
100 void proxyQuery(); // querying proxy for streamhost information
101 void proxyResult(bool b
); // query success / fail
102 void requesting(); // sent actual S5B request (initiator only)
103 void accepted(); // target accepted (initiator only
104 void tryingHosts(const StreamHostList
&hosts
); // currently connecting to these hosts
105 void proxyConnect(); // connecting to proxy
106 void waitingForActivation(); // waiting for activation (target only)
107 void connected(); // connection active
108 void datagramReady();
113 void sc_connectionClosed();
114 void sc_delayedCloseFinished();
116 void sc_bytesWritten(int);
119 void su_packetReady(const QByteArray
&buf
);
125 void reset(bool clear
=false);
126 void handleUDP(const QByteArray
&buf
);
127 void sendUDP(const QByteArray
&buf
);
129 friend class S5BManager
;
130 void man_waitForAccept(const S5BRequest
&r
);
131 void man_clientReady(SocksClient
*, SocksUDP
*);
132 void man_udpReady(const QByteArray
&buf
);
133 void man_failed(int);
134 S5BConnection(S5BManager
*, QObject
*parent
=0);
137 class S5BManager
: public BytestreamManager
141 S5BManager(Client
*);
144 static const char* ns();
145 Client
*client() const;
146 S5BServer
*server() const;
147 void setServer(S5BServer
*s
);
149 Jid
localPeer(const Jid
&peer
) const;
150 bool isAcceptableSID(const Jid
&peer
, const QString
&sid
) const;
152 BSConnection
*createConnection();
153 S5BConnection
*takeIncoming();
159 const char* sidPrefix() const;
162 void ps_incoming(const S5BRequest
&req
);
163 void ps_incomingUDPSuccess(const Jid
&from
, const QString
&dstaddr
);
164 void ps_incomingActivate(const Jid
&from
, const QString
&sid
, const Jid
&streamHost
);
165 void item_accepted();
166 void item_tryingHosts(const StreamHostList
&list
);
167 void item_proxyConnect();
168 void item_waitingForActivation();
169 void item_connected();
170 void item_error(int);
171 void query_finished();
177 S5BConnection
*findIncoming(const Jid
&from
, const QString
&sid
) const;
178 Entry
*findEntry(S5BConnection
*) const;
179 Entry
*findEntry(Item
*) const;
180 Entry
*findEntryByHash(const QString
&key
) const;
181 Entry
*findEntryBySID(const Jid
&peer
, const QString
&sid
) const;
182 Entry
*findServerEntryByHash(const QString
&key
) const;
184 void entryContinue(Entry
*e
);
185 void queryProxy(Entry
*e
);
186 bool targetShouldOfferProxy(Entry
*e
);
188 friend class S5BConnection
;
189 void con_connect(S5BConnection
*);
190 void con_accept(S5BConnection
*);
191 void con_reject(S5BConnection
*);
192 void con_unlink(S5BConnection
*);
193 void con_sendUDP(S5BConnection
*, const QByteArray
&buf
);
195 friend class S5BServer
;
196 bool srv_ownsHash(const QString
&key
) const;
197 void srv_incomingReady(SocksClient
*sc
, const QString
&key
);
198 void srv_incomingUDP(bool init
, const QHostAddress
&addr
, int port
, const QString
&key
, const QByteArray
&data
);
202 void doSuccess(const Jid
&peer
, const QString
&id
, const Jid
&streamHost
);
203 void doError(const Jid
&peer
, const QString
&id
, Stanza::Error::ErrorCond
, const QString
&);
204 void doActivate(const Jid
&peer
, const QString
&sid
, const Jid
&streamHost
);
207 class S5BConnector
: public QObject
211 S5BConnector(QObject
*parent
=0);
215 void start(const Jid
&self
, const StreamHostList
&hosts
, const QString
&key
, bool udp
, int timeout
);
216 SocksClient
*takeClient();
218 StreamHost
streamHostUsed() const;
226 void item_result(bool);
233 friend class S5BManager
;
234 void man_udpSuccess(const Jid
&streamHost
);
237 // listens on a port for serving
238 class S5BServer
: public QObject
242 S5BServer(QObject
*par
=0);
245 bool isActive() const;
246 bool start(int port
);
249 void setHostList(const QStringList
&);
250 QStringList
hostList() const;
255 void ss_incomingReady();
256 void ss_incomingUDP(const QString
&host
, int port
, const QHostAddress
&addr
, int sourcePort
, const QByteArray
&data
);
257 void item_result(bool);
263 friend class S5BManager
;
264 void link(S5BManager
*);
265 void unlink(S5BManager
*);
267 const QList
<S5BManager
*> & managerList() const;
268 void writeUDP(const QHostAddress
&addr
, int port
, const QByteArray
&data
);
271 class JT_S5B
: public Task
278 void request(const Jid
&to
, const QString
&sid
, const StreamHostList
&hosts
, bool fast
, bool udp
=false);
279 void requestProxyInfo(const Jid
&to
);
280 void requestActivation(const Jid
&to
, const QString
&sid
, const Jid
&target
);
284 bool take(const QDomElement
&);
286 Jid
streamHostUsed() const;
287 StreamHost
proxyInfo() const;
301 StreamHostList hosts
;
305 class JT_PushS5B
: public Task
312 int priority() const;
314 void respondSuccess(const Jid
&to
, const QString
&id
, const Jid
&streamHost
);
315 void respondError(const Jid
&to
, const QString
&id
,
316 Stanza::Error::ErrorCond cond
, const QString
&str
);
317 void sendUDPSuccess(const Jid
&to
, const QString
&dstaddr
);
318 void sendActivate(const Jid
&to
, const QString
&sid
, const Jid
&streamHost
);
320 bool take(const QDomElement
&);
323 void incoming(const S5BRequest
&req
);
324 void incomingUDPSuccess(const Jid
&from
, const QString
&dstaddr
);
325 void incomingActivate(const Jid
&from
, const QString
&sid
, const Jid
&streamHost
);
333 const Jid
& jid() const;
334 const QString
& host() const;
336 bool isProxy() const;
337 void setJid(const Jid
&);
338 void setHost(const QString
&);
340 void setIsProxy(bool);