~ubuntu-branches/ubuntu/utopic/kadu/utopic

« back to all changes in this revision

Viewing changes to plugins/jabber_protocol/3rdparty/libiris-win/src/irisnet/noncore/stunbinding.cpp

  • Committer: Package Import Robot
  • Author(s): Patryk Cisek
  • Date: 2014-10-02 11:55:19 UTC
  • mfrom: (0.48.1) (0.44.2) (43.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20141002115519-sxn21g16t596llc0
Tags: 1.0-2
* Using dpkg-buildflags to set compilation and linker flags.
* Added 03-no-CMAKE_CXX_FLAGS-overwrite.patch to make it possible to set
  compilation flags.
* Removed lintian override for RPATH in Kadu's plugins.
* Using chrpath to strip Kadu's plugins from RPATH, since they're not
  needed there.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2009  Barracuda Networks, Inc.
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2.1 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with this library; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
17
 * 02110-1301  USA
 
18
 *
 
19
 */
 
20
 
 
21
#include "stunbinding.h"
 
22
 
 
23
#include <QHostAddress>
 
24
#include "stunmessage.h"
 
25
#include "stuntypes.h"
 
26
#include "stuntransaction.h"
 
27
 
 
28
namespace XMPP {
 
29
 
 
30
class StunBinding::Private : public QObject
 
31
{
 
32
        Q_OBJECT
 
33
 
 
34
public:
 
35
        StunBinding *q;
 
36
        StunTransactionPool *pool;
 
37
        StunTransaction *trans;
 
38
        QHostAddress stunAddr;
 
39
        int stunPort;
 
40
        QHostAddress addr;
 
41
        int port;
 
42
        QString errorString;
 
43
        bool use_extPriority, use_extIceControlling, use_extIceControlled;
 
44
        quint32 extPriority;
 
45
        bool extUseCandidate;
 
46
        quint64 extIceControlling, extIceControlled;
 
47
        QString stuser, stpass;
 
48
        bool fpRequired;
 
49
 
 
50
        Private(StunBinding *_q) :
 
51
                QObject(_q),
 
52
                q(_q),
 
53
                pool(0),
 
54
                trans(0),
 
55
                use_extPriority(false),
 
56
                use_extIceControlling(false),
 
57
                use_extIceControlled(false),
 
58
                extUseCandidate(false),
 
59
                fpRequired(false)
 
60
        {
 
61
        }
 
62
 
 
63
        ~Private()
 
64
        {
 
65
                delete trans;
 
66
        }
 
67
 
 
68
        void start(const QHostAddress &_addr = QHostAddress(), int _port = -1)
 
69
        {
 
70
                Q_ASSERT(!trans);
 
71
 
 
72
                stunAddr = _addr;
 
73
                stunPort = _port;
 
74
 
 
75
                trans = new StunTransaction(this);
 
76
                connect(trans, SIGNAL(createMessage(const QByteArray &)), SLOT(trans_createMessage(const QByteArray &)));
 
77
                connect(trans, SIGNAL(finished(const XMPP::StunMessage &)), SLOT(trans_finished(const XMPP::StunMessage &)));
 
78
                connect(trans, SIGNAL(error(XMPP::StunTransaction::Error)), SLOT(trans_error(XMPP::StunTransaction::Error)));
 
79
 
 
80
                if(!stuser.isEmpty())
 
81
                {
 
82
                        trans->setShortTermUsername(stuser);
 
83
                        trans->setShortTermPassword(stpass);
 
84
                }
 
85
 
 
86
                trans->setFingerprintRequired(fpRequired);
 
87
 
 
88
                trans->start(pool, stunAddr, stunPort);
 
89
        }
 
90
 
 
91
private slots:
 
92
        void trans_createMessage(const QByteArray &transactionId)
 
93
        {
 
94
                StunMessage message;
 
95
                message.setMethod(StunTypes::Binding);
 
96
                message.setId((const quint8 *)transactionId.data());
 
97
 
 
98
                QList<StunMessage::Attribute> list;
 
99
 
 
100
                if(use_extPriority)
 
101
                {
 
102
                        StunMessage::Attribute a;
 
103
                        a.type = StunTypes::PRIORITY;
 
104
                        a.value = StunTypes::createPriority(extPriority);
 
105
                        list += a;
 
106
                }
 
107
 
 
108
                if(extUseCandidate)
 
109
                {
 
110
                        StunMessage::Attribute a;
 
111
                        a.type = StunTypes::USE_CANDIDATE;
 
112
                        list += a;
 
113
                }
 
114
 
 
115
                if(use_extIceControlling)
 
116
                {
 
117
                        StunMessage::Attribute a;
 
118
                        a.type = StunTypes::ICE_CONTROLLING;
 
119
                        a.value = StunTypes::createIceControlling(extIceControlling);
 
120
                        list += a;
 
121
                }
 
122
 
 
123
                if(use_extIceControlled)
 
124
                {
 
125
                        StunMessage::Attribute a;
 
126
                        a.type = StunTypes::ICE_CONTROLLED;
 
127
                        a.value = StunTypes::createIceControlled(extIceControlled);
 
128
                        list += a;
 
129
                }
 
130
 
 
131
                message.setAttributes(list);
 
132
 
 
133
                trans->setMessage(message);
 
134
        }
 
135
 
 
136
        void trans_finished(const XMPP::StunMessage &response)
 
137
        {
 
138
                delete trans;
 
139
                trans = 0;
 
140
 
 
141
                bool error = false;
 
142
                int code;
 
143
                QString reason;
 
144
                if(response.mclass() == StunMessage::ErrorResponse)
 
145
                {
 
146
                        if(!StunTypes::parseErrorCode(response.attribute(StunTypes::ERROR_CODE), &code, &reason))
 
147
                        {
 
148
                                errorString = "Unable to parse ERROR-CODE in error response.";
 
149
                                emit q->error(StunBinding::ErrorProtocol);
 
150
                                return;
 
151
                        }
 
152
 
 
153
                        error = true;
 
154
                }
 
155
 
 
156
                if(error)
 
157
                {
 
158
                        errorString = reason;
 
159
                        if(code == StunTypes::RoleConflict)
 
160
                                emit q->error(StunBinding::ErrorConflict);
 
161
                        else
 
162
                                emit q->error(StunBinding::ErrorRejected);
 
163
                        return;
 
164
                }
 
165
 
 
166
                QHostAddress saddr;
 
167
                quint16 sport = 0;
 
168
 
 
169
                QByteArray val;
 
170
                val = response.attribute(StunTypes::XOR_MAPPED_ADDRESS);
 
171
                if(!val.isNull())
 
172
                {
 
173
                        if(!StunTypes::parseXorMappedAddress(val, response.magic(), response.id(), &saddr, &sport))
 
174
                        {
 
175
                                errorString = "Unable to parse XOR-MAPPED-ADDRESS response.";
 
176
                                emit q->error(StunBinding::ErrorProtocol);
 
177
                                return;
 
178
                        }
 
179
                }
 
180
                else
 
181
                {
 
182
                        val = response.attribute(StunTypes::MAPPED_ADDRESS);
 
183
                        if(!val.isNull())
 
184
                        {
 
185
                                if(!StunTypes::parseMappedAddress(val, &saddr, &sport))
 
186
                                {
 
187
                                        errorString = "Unable to parse MAPPED-ADDRESS response.";
 
188
                                        emit q->error(StunBinding::ErrorProtocol);
 
189
                                        return;
 
190
                                }
 
191
                        }
 
192
                        else
 
193
                        {
 
194
                                errorString = "Response does not contain XOR-MAPPED-ADDRESS or MAPPED-ADDRESS.";
 
195
                                emit q->error(StunBinding::ErrorProtocol);
 
196
                                return;
 
197
                        }
 
198
                }
 
199
 
 
200
                addr = saddr;
 
201
                port = sport;
 
202
                emit q->success();
 
203
        }
 
204
 
 
205
        void trans_error(XMPP::StunTransaction::Error e)
 
206
        {
 
207
                delete trans;
 
208
                trans = 0;
 
209
 
 
210
                if(e == StunTransaction::ErrorTimeout)
 
211
                {
 
212
                        errorString = "Request timed out.";
 
213
                        emit q->error(StunBinding::ErrorTimeout);
 
214
                }
 
215
                else
 
216
                {
 
217
                        errorString = "Generic transaction error.";
 
218
                        emit q->error(StunBinding::ErrorGeneric);
 
219
                }
 
220
        }
 
221
};
 
222
 
 
223
StunBinding::StunBinding(StunTransactionPool *pool) :
 
224
        QObject(pool)
 
225
{
 
226
        d = new Private(this);
 
227
        d->pool = pool;
 
228
}
 
229
 
 
230
StunBinding::~StunBinding()
 
231
{
 
232
        delete d;
 
233
}
 
234
 
 
235
void StunBinding::setPriority(quint32 i)
 
236
{
 
237
        d->use_extPriority = true;
 
238
        d->extPriority = i;
 
239
}
 
240
 
 
241
void StunBinding::setUseCandidate(bool enabled)
 
242
{
 
243
        d->extUseCandidate = enabled;
 
244
}
 
245
 
 
246
void StunBinding::setIceControlling(quint64 i)
 
247
{
 
248
        d->use_extIceControlling = true;
 
249
        d->extIceControlling = i;
 
250
}
 
251
 
 
252
void StunBinding::setIceControlled(quint64 i)
 
253
{
 
254
        d->use_extIceControlled = true;
 
255
        d->extIceControlled = i;
 
256
}
 
257
 
 
258
void StunBinding::setShortTermUsername(const QString &username)
 
259
{
 
260
        d->stuser = username;
 
261
}
 
262
 
 
263
void StunBinding::setShortTermPassword(const QString &password)
 
264
{
 
265
        d->stpass = password;
 
266
}
 
267
 
 
268
void StunBinding::setFingerprintRequired(bool enabled)
 
269
{
 
270
        d->fpRequired = enabled;
 
271
}
 
272
 
 
273
void StunBinding::start()
 
274
{
 
275
        d->start();
 
276
}
 
277
 
 
278
void StunBinding::start(const QHostAddress &addr, int port)
 
279
{
 
280
        d->start(addr, port);
 
281
}
 
282
 
 
283
QHostAddress StunBinding::reflexiveAddress() const
 
284
{
 
285
        return d->addr;
 
286
}
 
287
 
 
288
int StunBinding::reflexivePort() const
 
289
{
 
290
        return d->port;
 
291
}
 
292
 
 
293
QString StunBinding::errorString() const
 
294
{
 
295
        return d->errorString;
 
296
}
 
297
 
 
298
}
 
299
 
 
300
#include "stunbinding.moc"