~ubuntu-branches/ubuntu/quantal/psi/quantal

« back to all changes in this revision

Viewing changes to iris/xmpp-core/protocol.h

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2009-09-25 17:49:51 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090925174951-lvm7kdap82o8xhn3
Tags: 0.13-1
* Updated to upstream version 0.13
* Set Standards-Version to 3.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * protocol.h - XMPP-Core protocol state machine
3
 
 * Copyright (C) 2004  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 PROTOCOL_H
22
 
#define PROTOCOL_H
23
 
 
24
 
#include <qpair.h>
25
 
//Added by qt3to4:
26
 
#include <QList>
27
 
#include "xmlprotocol.h"
28
 
#include "xmpp.h"
29
 
 
30
 
#define NS_ETHERX   "http://etherx.jabber.org/streams"
31
 
#define NS_CLIENT   "jabber:client"
32
 
#define NS_SERVER   "jabber:server"
33
 
#define NS_DIALBACK "jabber:server:dialback"
34
 
#define NS_STREAMS  "urn:ietf:params:xml:ns:xmpp-streams"
35
 
#define NS_TLS      "urn:ietf:params:xml:ns:xmpp-tls"
36
 
#define NS_SASL     "urn:ietf:params:xml:ns:xmpp-sasl"
37
 
#define NS_SESSION  "urn:ietf:params:xml:ns:xmpp-session"
38
 
#define NS_STANZAS  "urn:ietf:params:xml:ns:xmpp-stanzas"
39
 
#define NS_BIND     "urn:ietf:params:xml:ns:xmpp-bind"
40
 
#define NS_COMPRESS_FEATURE "http://jabber.org/features/compress"
41
 
#define NS_COMPRESS_PROTOCOL "http://jabber.org/protocol/compress"
42
 
 
43
 
namespace XMPP
44
 
{
45
 
        class Version
46
 
        {
47
 
        public:
48
 
                Version(int maj=0, int min=0);
49
 
 
50
 
                int major, minor;
51
 
        };
52
 
 
53
 
        class StreamFeatures
54
 
        {
55
 
        public:
56
 
                StreamFeatures();
57
 
 
58
 
                bool tls_supported, sasl_supported, bind_supported, compress_supported;
59
 
                bool tls_required;
60
 
                QStringList sasl_mechs;
61
 
                QStringList compression_mechs;
62
 
        };
63
 
 
64
 
        class BasicProtocol : public XmlProtocol
65
 
        {
66
 
        public:
67
 
                // xmpp 1.0 error conditions
68
 
                enum SASLCond {
69
 
                        Aborted,
70
 
                        IncorrectEncoding,
71
 
                        InvalidAuthzid,
72
 
                        InvalidMech,
73
 
                        MechTooWeak,
74
 
                        NotAuthorized,
75
 
                        TemporaryAuthFailure
76
 
                };
77
 
                enum StreamCond {
78
 
                        BadFormat,
79
 
                        BadNamespacePrefix,
80
 
                        Conflict,
81
 
                        ConnectionTimeout,
82
 
                        HostGone,
83
 
                        HostUnknown,
84
 
                        ImproperAddressing,
85
 
                        InternalServerError,
86
 
                        InvalidFrom,
87
 
                        InvalidId,
88
 
                        InvalidNamespace,
89
 
                        InvalidXml,
90
 
                        StreamNotAuthorized,
91
 
                        PolicyViolation,
92
 
                        RemoteConnectionFailed,
93
 
                        ResourceConstraint,
94
 
                        RestrictedXml,
95
 
                        SeeOtherHost,
96
 
                        SystemShutdown,
97
 
                        UndefinedCondition,
98
 
                        UnsupportedEncoding,
99
 
                        UnsupportedStanzaType,
100
 
                        UnsupportedVersion,
101
 
                        XmlNotWellFormed
102
 
                };
103
 
                enum BindCond {
104
 
                        BindBadRequest,
105
 
                        BindNotAllowed,
106
 
                        BindConflict
107
 
                };
108
 
 
109
 
                // extend the XmlProtocol enums
110
 
                enum Need {
111
 
                        NSASLMechs = XmlProtocol::NCustom, // need SASL mechlist
112
 
                        NStartTLS,  // need to switch on TLS layer
113
 
                        NCompress,  // need to switch on compression layer
114
 
                        NSASLFirst, // need SASL first step
115
 
                        NSASLNext,  // need SASL next step
116
 
                        NSASLLayer, // need to switch on SASL layer
117
 
                        NCustom = XmlProtocol::NCustom+10
118
 
                };
119
 
                enum Event {
120
 
                        EFeatures = XmlProtocol::ECustom, // breakpoint after features packet is received
121
 
                        ESASLSuccess, // breakpoint after successful sasl auth
122
 
                        EStanzaReady, // a stanza was received
123
 
                        EStanzaSent,  // a stanza was sent
124
 
                        EReady,       // stream is ready for stanza use
125
 
                        ECustom = XmlProtocol::ECustom+10
126
 
                };
127
 
                enum Error {
128
 
                        ErrProtocol = XmlProtocol::ErrCustom, // there was an error in the xmpp-core protocol exchange
129
 
                        ErrStream,   // <stream:error>, see errCond, errText, and errAppSpec for details
130
 
                        ErrStartTLS, // server refused starttls
131
 
                        ErrCompress, // server refused compression
132
 
                        ErrAuth,     // authorization error.  errCond holds sasl condition (or numeric code for old-protocol)
133
 
                        ErrBind,     // server refused resource bind
134
 
                        ErrCustom = XmlProtocol::ErrCustom+10
135
 
                };
136
 
 
137
 
                BasicProtocol();
138
 
                ~BasicProtocol();
139
 
 
140
 
                void reset();
141
 
 
142
 
                // for outgoing xml
143
 
                QDomDocument doc;
144
 
 
145
 
                // sasl-related
146
 
                QString saslMech() const;
147
 
                QByteArray saslStep() const;
148
 
                void setSASLMechList(const QStringList &list);
149
 
                void setSASLFirst(const QString &mech, const QByteArray &step);
150
 
                void setSASLNext(const QByteArray &step);
151
 
                void setSASLAuthed();
152
 
 
153
 
                // send / recv
154
 
                void sendStanza(const QDomElement &e);
155
 
                void sendDirect(const QString &s);
156
 
                void sendWhitespace();
157
 
                QDomElement recvStanza();
158
 
 
159
 
                // shutdown
160
 
                void shutdown();
161
 
                void shutdownWithError(int cond, const QString &otherHost="");
162
 
 
163
 
                // <stream> information
164
 
                QString to, from, id, lang;
165
 
                Version version;
166
 
 
167
 
                // error output
168
 
                int errCond;
169
 
                QString errText;
170
 
                QDomElement errAppSpec;
171
 
                QString otherHost;
172
 
 
173
 
                QByteArray spare; // filled with unprocessed data on NStartTLS and NSASLLayer
174
 
 
175
 
                bool isReady() const;
176
 
 
177
 
                enum { TypeElement, TypeStanza, TypeDirect, TypePing };
178
 
 
179
 
        protected:
180
 
                static int stringToSASLCond(const QString &s);
181
 
                static int stringToStreamCond(const QString &s);
182
 
                static QString saslCondToString(int);
183
 
                static QString streamCondToString(int);
184
 
 
185
 
                void send(const QDomElement &e, bool clip=false);
186
 
                void sendStreamError(int cond, const QString &text="", const QDomElement &appSpec=QDomElement());
187
 
                void sendStreamError(const QString &text); // old-style
188
 
 
189
 
                bool errorAndClose(int cond, const QString &text="", const QDomElement &appSpec=QDomElement());
190
 
                bool error(int code);
191
 
                void delayErrorAndClose(int cond, const QString &text="", const QDomElement &appSpec=QDomElement());
192
 
                void delayError(int code);
193
 
 
194
 
                // reimplemented
195
 
                QDomElement docElement();
196
 
                void handleDocOpen(const Parser::Event &pe);
197
 
                bool handleError();
198
 
                bool handleCloseFinished();
199
 
                bool doStep(const QDomElement &e);
200
 
                void itemWritten(int id, int size);
201
 
 
202
 
                virtual QString defaultNamespace();
203
 
                virtual QStringList extraNamespaces(); // stringlist: prefix,uri,prefix,uri, [...]
204
 
                virtual void handleStreamOpen(const Parser::Event &pe);
205
 
                virtual bool doStep2(const QDomElement &e)=0;
206
 
 
207
 
                void setReady(bool b);
208
 
 
209
 
                QString sasl_mech;
210
 
                QStringList sasl_mechlist;
211
 
                QByteArray sasl_step;
212
 
                bool sasl_authed;
213
 
 
214
 
                QDomElement stanzaToRecv;
215
 
 
216
 
        private:
217
 
                struct SASLCondEntry
218
 
                {
219
 
                        const char *str;
220
 
                        int cond;
221
 
                };
222
 
                static SASLCondEntry saslCondTable[];
223
 
 
224
 
                struct StreamCondEntry
225
 
                {
226
 
                        const char *str;
227
 
                        int cond;
228
 
                };
229
 
                static StreamCondEntry streamCondTable[];
230
 
 
231
 
                struct SendItem
232
 
                {
233
 
                        QDomElement stanzaToSend;
234
 
                        QString stringToSend;
235
 
                        bool doWhitespace;
236
 
                };
237
 
                QList<SendItem> sendList;
238
 
 
239
 
                bool doShutdown, delayedError, closeError, ready;
240
 
                int stanzasPending, stanzasWritten;
241
 
 
242
 
                void init();
243
 
                void extractStreamError(const QDomElement &e);
244
 
        };
245
 
 
246
 
        class CoreProtocol : public BasicProtocol
247
 
        {
248
 
        public:
249
 
                enum {
250
 
                        NPassword = NCustom,  // need password for old-mode
251
 
                        EDBVerify = ECustom,  // breakpoint after db:verify request
252
 
                        ErrPlain = ErrCustom  // server only supports plain, but allowPlain is false locally
253
 
                };
254
 
 
255
 
                CoreProtocol();
256
 
                ~CoreProtocol();
257
 
 
258
 
                void reset();
259
 
 
260
 
                void startClientOut(const Jid &jid, bool oldOnly, bool tlsActive, bool doAuth, bool doCompression);
261
 
                void startServerOut(const QString &to);
262
 
                void startDialbackOut(const QString &to, const QString &from);
263
 
                void startDialbackVerifyOut(const QString &to, const QString &from, const QString &id, const QString &key);
264
 
                void startClientIn(const QString &id);
265
 
                void startServerIn(const QString &id);
266
 
 
267
 
                void setLang(const QString &s);
268
 
                void setAllowTLS(bool b);
269
 
                void setAllowBind(bool b);
270
 
                void setAllowPlain(bool b); // old-mode
271
 
                const Jid& jid() const;
272
 
 
273
 
                void setPassword(const QString &s);
274
 
                void setFrom(const QString &s);
275
 
                void setDialbackKey(const QString &s);
276
 
 
277
 
                // input
278
 
                QString user, host;
279
 
 
280
 
                // status
281
 
                bool old;
282
 
 
283
 
                StreamFeatures features;
284
 
 
285
 
                //static QString xmlToString(const QDomElement &e, bool clip=false);
286
 
 
287
 
                class DBItem
288
 
                {
289
 
                public:
290
 
                        enum { ResultRequest, ResultGrant, VerifyRequest, VerifyGrant, Validated };
291
 
                        int type;
292
 
                        Jid to, from;
293
 
                        QString key, id;
294
 
                        bool ok;
295
 
                };
296
 
 
297
 
        private:
298
 
                enum Step {
299
 
                        Start,
300
 
                        Done,
301
 
                        SendFeatures,
302
 
                        GetRequest,
303
 
                        HandleTLS,
304
 
                        GetSASLResponse,
305
 
                        IncHandleSASLSuccess,
306
 
                        GetFeatures,        // read features packet
307
 
                        HandleFeatures,     // act on features, by initiating tls, sasl, or bind
308
 
                        GetTLSProceed,      // read <proceed/> tls response
309
 
                        GetCompressProceed, // read <compressed/> compression response
310
 
                        GetSASLFirst,       // perform sasl first step using provided data
311
 
                        GetSASLChallenge,   // read server sasl challenge
312
 
                        GetSASLNext,        // perform sasl next step using provided data
313
 
                        HandleSASLSuccess,  // handle what must be done after reporting sasl success
314
 
                        GetBindResponse,    // read bind response
315
 
                        HandleAuthGet,      // send old-protocol auth-get
316
 
                        GetAuthGetResponse, // read auth-get response
317
 
                        HandleAuthSet,      // send old-protocol auth-set
318
 
                        GetAuthSetResponse  // read auth-set response
319
 
                };
320
 
 
321
 
                QList<DBItem> dbrequests, dbpending, dbvalidated;
322
 
 
323
 
                bool server, dialback, dialback_verify;
324
 
                int step;
325
 
 
326
 
                bool digest;
327
 
                bool tls_started, sasl_started, compress_started;
328
 
 
329
 
                Jid jid_;
330
 
                bool oldOnly;
331
 
                bool allowPlain;
332
 
                bool doTLS, doAuth, doBinding, doCompress;
333
 
                QString password;
334
 
 
335
 
                QString dialback_id, dialback_key;
336
 
                QString self_from;
337
 
 
338
 
                void init();
339
 
                static int getOldErrorCode(const QDomElement &e);
340
 
                bool loginComplete();
341
 
 
342
 
                bool isValidStanza(const QDomElement &e) const;
343
 
                bool grabPendingItem(const Jid &to, const Jid &from, int type, DBItem *item);
344
 
                bool normalStep(const QDomElement &e);
345
 
                bool dialbackStep(const QDomElement &e);
346
 
 
347
 
                // reimplemented
348
 
                bool stepAdvancesParser() const;
349
 
                bool stepRequiresElement() const;
350
 
                void stringSend(const QString &s);
351
 
                void stringRecv(const QString &s);
352
 
                QString defaultNamespace();
353
 
                QStringList extraNamespaces();
354
 
                void handleStreamOpen(const Parser::Event &pe);
355
 
                bool doStep2(const QDomElement &e);
356
 
                void elementSend(const QDomElement &e);
357
 
                void elementRecv(const QDomElement &e);
358
 
        };
359
 
}
360
 
 
361
 
#endif