QXmppStun.cpp 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578
  1. /*
  2. * Copyright (C) 2008-2012 The QXmpp developers
  3. *
  4. * Author:
  5. * Jeremy Lainé
  6. *
  7. * Source:
  8. * http://code.google.com/p/qxmpp
  9. *
  10. * This file is a part of QXmpp library.
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. */
  23. #define QXMPP_DEBUG_STUN
  24. #include <QCryptographicHash>
  25. #include <QHostInfo>
  26. #include <QNetworkInterface>
  27. #include <QUdpSocket>
  28. #include <QTimer>
  29. #include "QXmppStun.h"
  30. #include "QXmppUtils.h"
  31. #define ID_SIZE 12
  32. #define STUN_RTO_INTERVAL 500
  33. #define STUN_RTO_MAX 7
  34. static const quint32 STUN_MAGIC = 0x2112A442;
  35. static const quint16 STUN_HEADER = 20;
  36. static const quint8 STUN_IPV4 = 0x01;
  37. static const quint8 STUN_IPV6 = 0x02;
  38. enum AttributeType {
  39. MappedAddress = 0x0001, // RFC5389
  40. ChangeRequest = 0x0003, // RFC5389
  41. SourceAddress = 0x0004, // RFC5389
  42. ChangedAddress = 0x0005, // RFC5389
  43. Username = 0x0006, // RFC5389
  44. MessageIntegrity = 0x0008, // RFC5389
  45. ErrorCode = 0x0009, // RFC5389
  46. ChannelNumber = 0x000c, // RFC5766 : TURN
  47. Lifetime = 0x000d, // RFC5766 : TURN
  48. XorPeerAddress = 0x0012, // RFC5766 : TURN
  49. DataAttr = 0x0013, // RFC5766 : TURN
  50. Realm = 0x0014, // RFC5389
  51. Nonce = 0x0015, // RFC5389
  52. XorRelayedAddress= 0x0016, // RFC5766 : TURN
  53. EvenPort = 0x0018, // RFC5766 : TURN
  54. RequestedTransport=0x0019, // RFC5766 : TURN
  55. XorMappedAddress = 0x0020, // RFC5389
  56. ReservationToken = 0x0022, // RFC5766 : TURN
  57. Priority = 0x0024, // RFC5245
  58. UseCandidate = 0x0025, // RFC5245
  59. Software = 0x8022, // RFC5389
  60. Fingerprint = 0x8028, // RFC5389
  61. IceControlled = 0x8029, // RFC5245
  62. IceControlling = 0x802a, // RFC5245
  63. OtherAddress = 0x802c, // RFC5780
  64. };
  65. // FIXME : we need to set local preference to discriminate between
  66. // multiple IP addresses
  67. static quint32 candidatePriority(const QXmppJingleCandidate &candidate, int localPref = 65535)
  68. {
  69. int typePref;
  70. switch (candidate.type())
  71. {
  72. case QXmppJingleCandidate::HostType:
  73. typePref = 126;
  74. break;
  75. case QXmppJingleCandidate::PeerReflexiveType:
  76. typePref = 110;
  77. break;
  78. case QXmppJingleCandidate::ServerReflexiveType:
  79. typePref = 100;
  80. break;
  81. default:
  82. typePref = 0;
  83. }
  84. return (1 << 24) * typePref + \
  85. (1 << 8) * localPref + \
  86. (256 - candidate.component());
  87. }
  88. static bool isIPv6LinkLocalAddress(const QHostAddress &addr)
  89. {
  90. if (addr.protocol() != QAbstractSocket::IPv6Protocol)
  91. return false;
  92. Q_IPV6ADDR ipv6addr = addr.toIPv6Address();
  93. return (((ipv6addr[0] << 8) + ipv6addr[1]) & 0xffc0) == 0xfe80;
  94. }
  95. static bool decodeAddress(QDataStream &stream, quint16 a_length, QHostAddress &address, quint16 &port, const QByteArray &xorId = QByteArray())
  96. {
  97. if (a_length < 4)
  98. return false;
  99. quint8 reserved, protocol;
  100. quint16 rawPort;
  101. stream >> reserved;
  102. stream >> protocol;
  103. stream >> rawPort;
  104. if (xorId.isEmpty())
  105. port = rawPort;
  106. else
  107. port = rawPort ^ (STUN_MAGIC >> 16);
  108. if (protocol == STUN_IPV4)
  109. {
  110. if (a_length != 8)
  111. return false;
  112. quint32 addr;
  113. stream >> addr;
  114. if (xorId.isEmpty())
  115. address = QHostAddress(addr);
  116. else
  117. address = QHostAddress(addr ^ STUN_MAGIC);
  118. } else if (protocol == STUN_IPV6) {
  119. if (a_length != 20)
  120. return false;
  121. Q_IPV6ADDR addr;
  122. stream.readRawData((char*)&addr, sizeof(addr));
  123. if (!xorId.isEmpty())
  124. {
  125. QByteArray xpad;
  126. QDataStream(&xpad, QIODevice::WriteOnly) << STUN_MAGIC;
  127. xpad += xorId;
  128. for (int i = 0; i < 16; i++)
  129. addr[i] ^= xpad[i];
  130. }
  131. address = QHostAddress(addr);
  132. } else {
  133. return false;
  134. }
  135. return true;
  136. }
  137. static void encodeAddress(QDataStream &stream, quint16 type, const QHostAddress &address, quint16 port, const QByteArray &xorId = QByteArray())
  138. {
  139. const quint8 reserved = 0;
  140. if (address.protocol() == QAbstractSocket::IPv4Protocol)
  141. {
  142. stream << type;
  143. stream << quint16(8);
  144. stream << reserved;
  145. stream << quint8(STUN_IPV4);
  146. quint32 addr = address.toIPv4Address();
  147. if (!xorId.isEmpty())
  148. {
  149. port ^= (STUN_MAGIC >> 16);
  150. addr ^= STUN_MAGIC;
  151. }
  152. stream << port;
  153. stream << addr;
  154. } else if (address.protocol() == QAbstractSocket::IPv6Protocol) {
  155. stream << type;
  156. stream << quint16(20);
  157. stream << reserved;
  158. stream << quint8(STUN_IPV6);
  159. Q_IPV6ADDR addr = address.toIPv6Address();
  160. if (!xorId.isEmpty())
  161. {
  162. port ^= (STUN_MAGIC >> 16);
  163. QByteArray xpad;
  164. QDataStream(&xpad, QIODevice::WriteOnly) << STUN_MAGIC;
  165. xpad += xorId;
  166. for (int i = 0; i < 16; i++)
  167. addr[i] ^= xpad[i];
  168. }
  169. stream << port;
  170. stream.writeRawData((char*)&addr, sizeof(addr));
  171. } else {
  172. qWarning("Cannot write STUN attribute for unknown IP version");
  173. }
  174. }
  175. static void addAddress(QDataStream &stream, quint16 type, const QHostAddress &host, quint16 port, const QByteArray &xorId = QByteArray())
  176. {
  177. if (port && !host.isNull() &&
  178. (host.protocol() == QAbstractSocket::IPv4Protocol ||
  179. host.protocol() == QAbstractSocket::IPv6Protocol))
  180. {
  181. encodeAddress(stream, type, host, port, xorId);
  182. }
  183. }
  184. static void encodeString(QDataStream &stream, quint16 type, const QString &string)
  185. {
  186. const QByteArray utf8string = string.toUtf8();
  187. stream << type;
  188. stream << quint16(utf8string.size());
  189. stream.writeRawData(utf8string.data(), utf8string.size());
  190. if (utf8string.size() % 4)
  191. {
  192. const QByteArray padding(4 - (utf8string.size() % 4), 0);
  193. stream.writeRawData(padding.data(), padding.size());
  194. }
  195. }
  196. static void setBodyLength(QByteArray &buffer, qint16 length)
  197. {
  198. QDataStream stream(&buffer, QIODevice::WriteOnly);
  199. stream.device()->seek(2);
  200. stream << length;
  201. }
  202. /// Constructs a new QXmppStunMessage.
  203. QXmppStunMessage::QXmppStunMessage()
  204. : errorCode(0),
  205. changedPort(0),
  206. mappedPort(0),
  207. otherPort(0),
  208. sourcePort(0),
  209. xorMappedPort(0),
  210. xorPeerPort(0),
  211. xorRelayedPort(0),
  212. useCandidate(false),
  213. m_cookie(STUN_MAGIC),
  214. m_type(0),
  215. m_changeRequest(0),
  216. m_channelNumber(0),
  217. m_lifetime(0),
  218. m_priority(0)
  219. {
  220. m_id = QByteArray(ID_SIZE, 0);
  221. }
  222. quint32 QXmppStunMessage::cookie() const
  223. {
  224. return m_cookie;
  225. }
  226. void QXmppStunMessage::setCookie(quint32 cookie)
  227. {
  228. m_cookie = cookie;
  229. }
  230. QByteArray QXmppStunMessage::id() const
  231. {
  232. return m_id;
  233. }
  234. void QXmppStunMessage::setId(const QByteArray &id)
  235. {
  236. Q_ASSERT(id.size() == ID_SIZE);
  237. m_id = id;
  238. }
  239. quint16 QXmppStunMessage::messageClass() const
  240. {
  241. return m_type & 0x0110;
  242. }
  243. quint16 QXmppStunMessage::messageMethod() const
  244. {
  245. return m_type & 0x3eef;
  246. }
  247. quint16 QXmppStunMessage::type() const
  248. {
  249. return m_type;
  250. }
  251. void QXmppStunMessage::setType(quint16 type)
  252. {
  253. m_type = type;
  254. }
  255. /// Returns the CHANGE-REQUEST attribute, indicating whether to change
  256. /// the IP and / or port from which the response is sent.
  257. quint32 QXmppStunMessage::changeRequest() const
  258. {
  259. return m_changeRequest;
  260. }
  261. /// Sets the CHANGE-REQUEST attribute, indicating whether to change
  262. /// the IP and / or port from which the response is sent.
  263. ///
  264. /// \param changeRequest
  265. void QXmppStunMessage::setChangeRequest(quint32 changeRequest)
  266. {
  267. m_changeRequest = changeRequest;
  268. m_attributes << ChangeRequest;
  269. }
  270. /// Returns the CHANNEL-NUMBER attribute.
  271. quint16 QXmppStunMessage::channelNumber() const
  272. {
  273. return m_channelNumber;
  274. }
  275. /// Sets the CHANNEL-NUMBER attribute.
  276. ///
  277. /// \param channelNumber
  278. void QXmppStunMessage::setChannelNumber(quint16 channelNumber)
  279. {
  280. m_channelNumber = channelNumber;
  281. m_attributes << ChannelNumber;
  282. }
  283. /// Returns the DATA attribute.
  284. QByteArray QXmppStunMessage::data() const
  285. {
  286. return m_data;
  287. }
  288. /// Sets the DATA attribute.
  289. void QXmppStunMessage::setData(const QByteArray &data)
  290. {
  291. m_data = data;
  292. m_attributes << DataAttr;
  293. }
  294. /// Returns the LIFETIME attribute, indicating the duration in seconds for
  295. /// which the server will maintain an allocation.
  296. quint32 QXmppStunMessage::lifetime() const
  297. {
  298. return m_lifetime;
  299. }
  300. /// Sets the LIFETIME attribute, indicating the duration in seconds for
  301. /// which the server will maintain an allocation.
  302. ///
  303. /// \param lifetime
  304. void QXmppStunMessage::setLifetime(quint32 lifetime)
  305. {
  306. m_lifetime = lifetime;
  307. m_attributes << Lifetime;
  308. }
  309. /// Returns the NONCE attribute.
  310. QByteArray QXmppStunMessage::nonce() const
  311. {
  312. return m_nonce;
  313. }
  314. /// Sets the NONCE attribute.
  315. ///
  316. /// \param nonce
  317. void QXmppStunMessage::setNonce(const QByteArray &nonce)
  318. {
  319. m_nonce = nonce;
  320. m_attributes << Nonce;
  321. }
  322. /// Returns the PRIORITY attribute, the priority that would be assigned to
  323. /// a peer reflexive candidate discovered during the ICE check.
  324. quint32 QXmppStunMessage::priority() const
  325. {
  326. return m_priority;
  327. }
  328. /// Sets the PRIORITY attribute, the priority that would be assigned to
  329. /// a peer reflexive candidate discovered during the ICE check.
  330. ///
  331. /// \param priority
  332. void QXmppStunMessage::setPriority(quint32 priority)
  333. {
  334. m_priority = priority;
  335. m_attributes << Priority;
  336. }
  337. /// Returns the REALM attribute.
  338. QString QXmppStunMessage::realm() const
  339. {
  340. return m_realm;
  341. }
  342. /// Sets the REALM attribute.
  343. ///
  344. /// \param realm
  345. void QXmppStunMessage::setRealm(const QString &realm)
  346. {
  347. m_realm = realm;
  348. m_attributes << Realm;
  349. }
  350. /// Returns the REQUESTED-TRANSPORT attribute.
  351. quint8 QXmppStunMessage::requestedTransport() const
  352. {
  353. return m_requestedTransport;
  354. }
  355. /// Sets the REQUESTED-TRANSPORT attribute.
  356. ///
  357. /// \param requestedTransport
  358. void QXmppStunMessage::setRequestedTransport(quint8 requestedTransport)
  359. {
  360. m_requestedTransport = requestedTransport;
  361. m_attributes << RequestedTransport;
  362. }
  363. /// Returns the RESERVATION-TOKEN attribute.
  364. QByteArray QXmppStunMessage::reservationToken() const
  365. {
  366. return m_reservationToken;
  367. }
  368. /// Sets the RESERVATION-TOKEN attribute.
  369. ///
  370. /// \param reservationToken
  371. void QXmppStunMessage::setReservationToken(const QByteArray &reservationToken)
  372. {
  373. m_reservationToken = reservationToken;
  374. m_reservationToken.resize(8);
  375. m_attributes << ReservationToken;
  376. }
  377. /// Returns the SOFTWARE attribute, containing a textual description of the
  378. /// software being used.
  379. QString QXmppStunMessage::software() const
  380. {
  381. return m_software;
  382. }
  383. /// Sets the SOFTWARE attribute, containing a textual description of the
  384. /// software being used.
  385. ///
  386. /// \param software
  387. void QXmppStunMessage::setSoftware(const QString &software)
  388. {
  389. m_software = software;
  390. m_attributes << Software;
  391. }
  392. /// Returns the USERNAME attribute, containing the username to use for
  393. /// authentication.
  394. QString QXmppStunMessage::username() const
  395. {
  396. return m_username;
  397. }
  398. /// Sets the USERNAME attribute, containing the username to use for
  399. /// authentication.
  400. ///
  401. /// \param username
  402. void QXmppStunMessage::setUsername(const QString &username)
  403. {
  404. m_username = username;
  405. m_attributes << Username;
  406. }
  407. /// Decodes a QXmppStunMessage and checks its integrity using the given key.
  408. ///
  409. /// \param buffer
  410. /// \param key
  411. /// \param errors
  412. bool QXmppStunMessage::decode(const QByteArray &buffer, const QByteArray &key, QStringList *errors)
  413. {
  414. QStringList silent;
  415. if (!errors)
  416. errors = &silent;
  417. if (buffer.size() < STUN_HEADER)
  418. {
  419. *errors << QLatin1String("Received a truncated STUN packet");
  420. return false;
  421. }
  422. // parse STUN header
  423. QDataStream stream(buffer);
  424. quint16 length;
  425. stream >> m_type;
  426. stream >> length;
  427. stream >> m_cookie;
  428. stream.readRawData(m_id.data(), m_id.size());
  429. if (length != buffer.size() - STUN_HEADER)
  430. {
  431. *errors << QLatin1String("Received an invalid STUN packet");
  432. return false;
  433. }
  434. // parse STUN attributes
  435. int done = 0;
  436. bool after_integrity = false;
  437. while (done < length)
  438. {
  439. quint16 a_type, a_length;
  440. stream >> a_type;
  441. stream >> a_length;
  442. const int pad_length = 4 * ((a_length + 3) / 4) - a_length;
  443. // only FINGERPRINT is allowed after MESSAGE-INTEGRITY
  444. if (after_integrity && a_type != Fingerprint)
  445. {
  446. *errors << QString("Skipping attribute %1 after MESSAGE-INTEGRITY").arg(QString::number(a_type));
  447. stream.skipRawData(a_length + pad_length);
  448. done += 4 + a_length + pad_length;
  449. continue;
  450. }
  451. if (a_type == Priority)
  452. {
  453. // PRIORITY
  454. if (a_length != sizeof(m_priority))
  455. return false;
  456. stream >> m_priority;
  457. m_attributes << Priority;
  458. } else if (a_type == ErrorCode) {
  459. // ERROR-CODE
  460. if (a_length < 4)
  461. return false;
  462. quint16 reserved;
  463. quint8 errorCodeHigh, errorCodeLow;
  464. stream >> reserved;
  465. stream >> errorCodeHigh;
  466. stream >> errorCodeLow;
  467. errorCode = errorCodeHigh * 100 + errorCodeLow;
  468. QByteArray phrase(a_length - 4, 0);
  469. stream.readRawData(phrase.data(), phrase.size());
  470. errorPhrase = QString::fromUtf8(phrase);
  471. } else if (a_type == UseCandidate) {
  472. // USE-CANDIDATE
  473. if (a_length != 0)
  474. return false;
  475. useCandidate = true;
  476. } else if (a_type == ChannelNumber) {
  477. // CHANNEL-NUMBER
  478. if (a_length != 4)
  479. return false;
  480. stream >> m_channelNumber;
  481. stream.skipRawData(2);
  482. m_attributes << ChannelNumber;
  483. } else if (a_type == DataAttr) {
  484. // DATA
  485. m_data.resize(a_length);
  486. stream.readRawData(m_data.data(), m_data.size());
  487. m_attributes << DataAttr;
  488. } else if (a_type == Lifetime) {
  489. // LIFETIME
  490. if (a_length != sizeof(m_lifetime))
  491. return false;
  492. stream >> m_lifetime;
  493. m_attributes << Lifetime;
  494. } else if (a_type == Nonce) {
  495. // NONCE
  496. m_nonce.resize(a_length);
  497. stream.readRawData(m_nonce.data(), m_nonce.size());
  498. m_attributes << Nonce;
  499. } else if (a_type == Realm) {
  500. // REALM
  501. QByteArray utf8Realm(a_length, 0);
  502. stream.readRawData(utf8Realm.data(), utf8Realm.size());
  503. m_realm = QString::fromUtf8(utf8Realm);
  504. m_attributes << Realm;
  505. } else if (a_type == RequestedTransport) {
  506. // REQUESTED-TRANSPORT
  507. if (a_length != 4)
  508. return false;
  509. stream >> m_requestedTransport;
  510. stream.skipRawData(3);
  511. m_attributes << RequestedTransport;
  512. } else if (a_type == ReservationToken) {
  513. // RESERVATION-TOKEN
  514. if (a_length != 8)
  515. return false;
  516. m_reservationToken.resize(a_length);
  517. stream.readRawData(m_reservationToken.data(), m_reservationToken.size());
  518. m_attributes << ReservationToken;
  519. } else if (a_type == Software) {
  520. // SOFTWARE
  521. QByteArray utf8Software(a_length, 0);
  522. stream.readRawData(utf8Software.data(), utf8Software.size());
  523. m_software = QString::fromUtf8(utf8Software);
  524. m_attributes << Software;
  525. } else if (a_type == Username) {
  526. // USERNAME
  527. QByteArray utf8Username(a_length, 0);
  528. stream.readRawData(utf8Username.data(), utf8Username.size());
  529. m_username = QString::fromUtf8(utf8Username);
  530. m_attributes << Username;
  531. } else if (a_type == MappedAddress) {
  532. // MAPPED-ADDRESS
  533. if (!decodeAddress(stream, a_length, mappedHost, mappedPort))
  534. {
  535. *errors << QLatin1String("Bad MAPPED-ADDRESS");
  536. return false;
  537. }
  538. } else if (a_type == ChangeRequest) {
  539. // CHANGE-REQUEST
  540. if (a_length != sizeof(m_changeRequest))
  541. return false;
  542. stream >> m_changeRequest;
  543. m_attributes << ChangeRequest;
  544. } else if (a_type == SourceAddress) {
  545. // SOURCE-ADDRESS
  546. if (!decodeAddress(stream, a_length, sourceHost, sourcePort))
  547. {
  548. *errors << QLatin1String("Bad SOURCE-ADDRESS");
  549. return false;
  550. }
  551. } else if (a_type == ChangedAddress) {
  552. // CHANGED-ADDRESS
  553. if (!decodeAddress(stream, a_length, changedHost, changedPort))
  554. {
  555. *errors << QLatin1String("Bad CHANGED-ADDRESS");
  556. return false;
  557. }
  558. } else if (a_type == OtherAddress) {
  559. // OTHER-ADDRESS
  560. if (!decodeAddress(stream, a_length, otherHost, otherPort))
  561. {
  562. *errors << QLatin1String("Bad OTHER-ADDRESS");
  563. return false;
  564. }
  565. } else if (a_type == XorMappedAddress) {
  566. // XOR-MAPPED-ADDRESS
  567. if (!decodeAddress(stream, a_length, xorMappedHost, xorMappedPort, m_id))
  568. {
  569. *errors << QLatin1String("Bad XOR-MAPPED-ADDRESS");
  570. return false;
  571. }
  572. } else if (a_type == XorPeerAddress) {
  573. // XOR-PEER-ADDRESS
  574. if (!decodeAddress(stream, a_length, xorPeerHost, xorPeerPort, m_id))
  575. {
  576. *errors << QLatin1String("Bad XOR-PEER-ADDRESS");
  577. return false;
  578. }
  579. } else if (a_type == XorRelayedAddress) {
  580. // XOR-RELAYED-ADDRESS
  581. if (!decodeAddress(stream, a_length, xorRelayedHost, xorRelayedPort, m_id))
  582. {
  583. *errors << QLatin1String("Bad XOR-RELAYED-ADDRESS");
  584. return false;
  585. }
  586. } else if (a_type == MessageIntegrity) {
  587. // MESSAGE-INTEGRITY
  588. if (a_length != 20)
  589. return false;
  590. QByteArray integrity(20, 0);
  591. stream.readRawData(integrity.data(), integrity.size());
  592. // check HMAC-SHA1
  593. if (!key.isEmpty())
  594. {
  595. QByteArray copy = buffer.left(STUN_HEADER + done);
  596. setBodyLength(copy, done + 24);
  597. if (integrity != QXmppUtils::generateHmacSha1(key, copy))
  598. {
  599. *errors << QLatin1String("Bad message integrity");
  600. return false;
  601. }
  602. }
  603. // from here onwards, only FINGERPRINT is allowed
  604. after_integrity = true;
  605. } else if (a_type == Fingerprint) {
  606. // FINGERPRINT
  607. if (a_length != 4)
  608. return false;
  609. quint32 fingerprint;
  610. stream >> fingerprint;
  611. // check CRC32
  612. QByteArray copy = buffer.left(STUN_HEADER + done);
  613. setBodyLength(copy, done + 8);
  614. const quint32 expected = QXmppUtils::generateCrc32(copy) ^ 0x5354554eL;
  615. if (fingerprint != expected)
  616. {
  617. *errors << QLatin1String("Bad fingerprint");
  618. return false;
  619. }
  620. // stop parsing, no more attributes are allowed
  621. return true;
  622. } else if (a_type == IceControlling) {
  623. /// ICE-CONTROLLING
  624. if (a_length != 8)
  625. return false;
  626. iceControlling.resize(a_length);
  627. stream.readRawData(iceControlling.data(), iceControlling.size());
  628. } else if (a_type == IceControlled) {
  629. /// ICE-CONTROLLED
  630. if (a_length != 8)
  631. return false;
  632. iceControlled.resize(a_length);
  633. stream.readRawData(iceControlled.data(), iceControlled.size());
  634. } else {
  635. // Unknown attribute
  636. stream.skipRawData(a_length);
  637. *errors << QString("Skipping unknown attribute %1").arg(QString::number(a_type));
  638. }
  639. stream.skipRawData(pad_length);
  640. done += 4 + a_length + pad_length;
  641. }
  642. return true;
  643. }
  644. /// Encodes the current QXmppStunMessage, optionally calculating the
  645. /// message integrity attribute using the given key.
  646. ///
  647. /// \param key
  648. /// \param addFingerprint
  649. QByteArray QXmppStunMessage::encode(const QByteArray &key, bool addFingerprint) const
  650. {
  651. QByteArray buffer;
  652. QDataStream stream(&buffer, QIODevice::WriteOnly);
  653. // encode STUN header
  654. quint16 length = 0;
  655. stream << m_type;
  656. stream << length;
  657. stream << m_cookie;
  658. stream.writeRawData(m_id.data(), m_id.size());
  659. // MAPPED-ADDRESS
  660. addAddress(stream, MappedAddress, mappedHost, mappedPort);
  661. // CHANGE-REQUEST
  662. if (m_attributes.contains(ChangeRequest)) {
  663. stream << quint16(ChangeRequest);
  664. stream << quint16(sizeof(m_changeRequest));
  665. stream << m_changeRequest;
  666. }
  667. // SOURCE-ADDRESS
  668. addAddress(stream, SourceAddress, sourceHost, sourcePort);
  669. // CHANGED-ADDRESS
  670. addAddress(stream, ChangedAddress, changedHost, changedPort);
  671. // OTHER-ADDRESS
  672. addAddress(stream, OtherAddress, otherHost, otherPort);
  673. // XOR-MAPPED-ADDRESS
  674. addAddress(stream, XorMappedAddress, xorMappedHost, xorMappedPort, m_id);
  675. // XOR-PEER-ADDRESS
  676. addAddress(stream, XorPeerAddress, xorPeerHost, xorPeerPort, m_id);
  677. // XOR-RELAYED-ADDRESS
  678. addAddress(stream, XorRelayedAddress, xorRelayedHost, xorRelayedPort, m_id);
  679. // ERROR-CODE
  680. if (errorCode)
  681. {
  682. const quint16 reserved = 0;
  683. const quint8 errorCodeHigh = errorCode / 100;
  684. const quint8 errorCodeLow = errorCode % 100;
  685. const QByteArray phrase = errorPhrase.toUtf8();
  686. stream << quint16(ErrorCode);
  687. stream << quint16(phrase.size() + 4);
  688. stream << reserved;
  689. stream << errorCodeHigh;
  690. stream << errorCodeLow;
  691. stream.writeRawData(phrase.data(), phrase.size());
  692. if (phrase.size() % 4)
  693. {
  694. const QByteArray padding(4 - (phrase.size() % 4), 0);
  695. stream.writeRawData(padding.data(), padding.size());
  696. }
  697. }
  698. // PRIORITY
  699. if (m_attributes.contains(Priority))
  700. {
  701. stream << quint16(Priority);
  702. stream << quint16(sizeof(m_priority));
  703. stream << m_priority;
  704. }
  705. // USE-CANDIDATE
  706. if (useCandidate)
  707. {
  708. stream << quint16(UseCandidate);
  709. stream << quint16(0);
  710. }
  711. // CHANNEL-NUMBER
  712. if (m_attributes.contains(ChannelNumber)) {
  713. stream << quint16(ChannelNumber);
  714. stream << quint16(4);
  715. stream << m_channelNumber;
  716. stream << quint16(0);
  717. }
  718. // DATA
  719. if (m_attributes.contains(DataAttr)) {
  720. stream << quint16(DataAttr);
  721. stream << quint16(m_data.size());
  722. stream.writeRawData(m_data.data(), m_data.size());
  723. if (m_data.size() % 4) {
  724. const QByteArray padding(4 - (m_data.size() % 4), 0);
  725. stream.writeRawData(padding.data(), padding.size());
  726. }
  727. }
  728. // LIFETIME
  729. if (m_attributes.contains(Lifetime)) {
  730. stream << quint16(Lifetime);
  731. stream << quint16(sizeof(m_lifetime));
  732. stream << m_lifetime;
  733. }
  734. // NONCE
  735. if (m_attributes.contains(Nonce)) {
  736. stream << quint16(Nonce);
  737. stream << quint16(m_nonce.size());
  738. stream.writeRawData(m_nonce.data(), m_nonce.size());
  739. }
  740. // REALM
  741. if (m_attributes.contains(Realm))
  742. encodeString(stream, Realm, m_realm);
  743. // REQUESTED-TRANSPORT
  744. if (m_attributes.contains(RequestedTransport)) {
  745. const QByteArray reserved(3, 0);
  746. stream << quint16(RequestedTransport);
  747. stream << quint16(4);
  748. stream << m_requestedTransport;
  749. stream.writeRawData(reserved.data(), reserved.size());
  750. }
  751. // RESERVATION-TOKEN
  752. if (m_attributes.contains(ReservationToken)) {
  753. stream << quint16(ReservationToken);
  754. stream << quint16(m_reservationToken.size());
  755. stream.writeRawData(m_reservationToken.data(), m_reservationToken.size());
  756. }
  757. // SOFTWARE
  758. if (m_attributes.contains(Software))
  759. encodeString(stream, Software, m_software);
  760. // USERNAME
  761. if (m_attributes.contains(Username))
  762. encodeString(stream, Username, m_username);
  763. // ICE-CONTROLLING or ICE-CONTROLLED
  764. if (!iceControlling.isEmpty())
  765. {
  766. stream << quint16(IceControlling);
  767. stream << quint16(iceControlling.size());
  768. stream.writeRawData(iceControlling.data(), iceControlling.size());
  769. } else if (!iceControlled.isEmpty()) {
  770. stream << quint16(IceControlled);
  771. stream << quint16(iceControlled.size());
  772. stream.writeRawData(iceControlled.data(), iceControlled.size());
  773. }
  774. // set body length
  775. setBodyLength(buffer, buffer.size() - STUN_HEADER);
  776. // MESSAGE-INTEGRITY
  777. if (!key.isEmpty())
  778. {
  779. setBodyLength(buffer, buffer.size() - STUN_HEADER + 24);
  780. QByteArray integrity = QXmppUtils::generateHmacSha1(key, buffer);
  781. stream << quint16(MessageIntegrity);
  782. stream << quint16(integrity.size());
  783. stream.writeRawData(integrity.data(), integrity.size());
  784. }
  785. // FINGERPRINT
  786. if (addFingerprint)
  787. {
  788. setBodyLength(buffer, buffer.size() - STUN_HEADER + 8);
  789. quint32 fingerprint = QXmppUtils::generateCrc32(buffer) ^ 0x5354554eL;
  790. stream << quint16(Fingerprint);
  791. stream << quint16(sizeof(fingerprint));
  792. stream << fingerprint;
  793. }
  794. return buffer;
  795. }
  796. /// If the given packet looks like a STUN message, returns the message
  797. /// type, otherwise returns 0.
  798. ///
  799. /// \param buffer
  800. /// \param cookie
  801. /// \param id
  802. quint16 QXmppStunMessage::peekType(const QByteArray &buffer, quint32 &cookie, QByteArray &id)
  803. {
  804. if (buffer.size() < STUN_HEADER)
  805. return 0;
  806. // parse STUN header
  807. QDataStream stream(buffer);
  808. quint16 type;
  809. quint16 length;
  810. stream >> type;
  811. stream >> length;
  812. stream >> cookie;
  813. if (length != buffer.size() - STUN_HEADER)
  814. return 0;
  815. id.resize(ID_SIZE);
  816. stream.readRawData(id.data(), id.size());
  817. return type;
  818. }
  819. QString QXmppStunMessage::toString() const
  820. {
  821. QStringList dumpLines;
  822. QString typeName;
  823. switch (messageMethod())
  824. {
  825. case Binding: typeName = "Binding"; break;
  826. case SharedSecret: typeName = "Shared Secret"; break;
  827. case Allocate: typeName = "Allocate"; break;
  828. case Refresh: typeName = "Refresh"; break;
  829. case Send: typeName = "Send"; break;
  830. case Data: typeName = "Data"; break;
  831. case CreatePermission: typeName = "CreatePermission"; break;
  832. case ChannelBind: typeName = "ChannelBind"; break;
  833. default: typeName = "Unknown"; break;
  834. }
  835. switch (messageClass())
  836. {
  837. case Request: typeName += " Request"; break;
  838. case Indication: typeName += " Indication"; break;
  839. case Response: typeName += " Response"; break;
  840. case Error: typeName += " Error"; break;
  841. default: break;
  842. }
  843. dumpLines << QString(" type %1 (%2)")
  844. .arg(typeName)
  845. .arg(QString::number(m_type));
  846. dumpLines << QString(" id %1").arg(QString::fromAscii(m_id.toHex()));
  847. // attributes
  848. if (m_attributes.contains(ChannelNumber))
  849. dumpLines << QString(" * CHANNEL-NUMBER %1").arg(QString::number(m_channelNumber));
  850. if (errorCode)
  851. dumpLines << QString(" * ERROR-CODE %1 %2")
  852. .arg(QString::number(errorCode), errorPhrase);
  853. if (m_attributes.contains(Lifetime))
  854. dumpLines << QString(" * LIFETIME %1").arg(QString::number(m_lifetime));
  855. if (m_attributes.contains(Nonce))
  856. dumpLines << QString(" * NONCE %1").arg(QString::fromLatin1(m_nonce));
  857. if (m_attributes.contains(Realm))
  858. dumpLines << QString(" * REALM %1").arg(m_realm);
  859. if (m_attributes.contains(RequestedTransport))
  860. dumpLines << QString(" * REQUESTED-TRANSPORT 0x%1").arg(QString::number(m_requestedTransport, 16));
  861. if (m_attributes.contains(ReservationToken))
  862. dumpLines << QString(" * RESERVATION-TOKEN %1").arg(QString::fromAscii(m_reservationToken.toHex()));
  863. if (m_attributes.contains(Software))
  864. dumpLines << QString(" * SOFTWARE %1").arg(m_software);
  865. if (m_attributes.contains(Username))
  866. dumpLines << QString(" * USERNAME %1").arg(m_username);
  867. if (mappedPort)
  868. dumpLines << QString(" * MAPPED-ADDRESS %1 %2")
  869. .arg(mappedHost.toString(), QString::number(mappedPort));
  870. if (m_attributes.contains(ChangeRequest))
  871. dumpLines << QString(" * CHANGE-REQUEST %1")
  872. .arg(QString::number(m_changeRequest));
  873. if (sourcePort)
  874. dumpLines << QString(" * SOURCE-ADDRESS %1 %2")
  875. .arg(sourceHost.toString(), QString::number(sourcePort));
  876. if (changedPort)
  877. dumpLines << QString(" * CHANGED-ADDRESS %1 %2")
  878. .arg(changedHost.toString(), QString::number(changedPort));
  879. if (otherPort)
  880. dumpLines << QString(" * OTHER-ADDRESS %1 %2")
  881. .arg(otherHost.toString(), QString::number(otherPort));
  882. if (xorMappedPort)
  883. dumpLines << QString(" * XOR-MAPPED-ADDRESS %1 %2")
  884. .arg(xorMappedHost.toString(), QString::number(xorMappedPort));
  885. if (xorPeerPort)
  886. dumpLines << QString(" * XOR-PEER-ADDRESS %1 %2")
  887. .arg(xorPeerHost.toString(), QString::number(xorPeerPort));
  888. if (xorRelayedPort)
  889. dumpLines << QString(" * XOR-RELAYED-ADDRESS %1 %2")
  890. .arg(xorRelayedHost.toString(), QString::number(xorRelayedPort));
  891. if (m_attributes.contains(Priority))
  892. dumpLines << QString(" * PRIORITY %1").arg(QString::number(m_priority));
  893. if (!iceControlling.isEmpty())
  894. dumpLines << QString(" * ICE-CONTROLLING %1")
  895. .arg(QString::fromAscii(iceControlling.toHex()));
  896. if (!iceControlled.isEmpty())
  897. dumpLines << QString(" * ICE-CONTROLLED %1")
  898. .arg(QString::fromAscii(iceControlled.toHex()));
  899. return dumpLines.join("\n");
  900. }
  901. /// Constructs a new QXmppStunTransaction.
  902. ///
  903. /// \param request
  904. /// \param receiver
  905. QXmppStunTransaction::QXmppStunTransaction(const QXmppStunMessage &request, QObject *receiver)
  906. : QXmppLoggable(receiver),
  907. m_request(request),
  908. m_tries(0)
  909. {
  910. bool check;
  911. Q_UNUSED(check);
  912. check = connect(this, SIGNAL(writeStun(QXmppStunMessage)),
  913. receiver, SLOT(writeStun(QXmppStunMessage)));
  914. Q_ASSERT(check);
  915. check = connect(this, SIGNAL(finished()),
  916. receiver, SLOT(transactionFinished()));
  917. Q_ASSERT(check);
  918. // RTO timer
  919. m_retryTimer = new QTimer(this);
  920. m_retryTimer->setSingleShot(true);
  921. check = connect(m_retryTimer, SIGNAL(timeout()),
  922. this, SLOT(retry()));
  923. // send packet immediately
  924. m_tries++;
  925. emit writeStun(m_request);
  926. m_retryTimer->start(STUN_RTO_INTERVAL);
  927. }
  928. void QXmppStunTransaction::readStun(const QXmppStunMessage &response)
  929. {
  930. if (response.messageClass() == QXmppStunMessage::Error ||
  931. response.messageClass() == QXmppStunMessage::Response) {
  932. m_response = response;
  933. emit finished();
  934. }
  935. }
  936. /// Returns the STUN request.
  937. QXmppStunMessage QXmppStunTransaction::request() const
  938. {
  939. return m_request;
  940. }
  941. /// Returns the STUN response.
  942. QXmppStunMessage QXmppStunTransaction::response() const
  943. {
  944. return m_response;
  945. }
  946. void QXmppStunTransaction::retry()
  947. {
  948. if (m_tries >= STUN_RTO_MAX) {
  949. m_response.setType(QXmppStunMessage::Error);
  950. m_response.errorPhrase = QLatin1String("Request timed out");
  951. emit finished();
  952. return;
  953. }
  954. // resend request
  955. m_tries++;
  956. emit writeStun(m_request);
  957. m_retryTimer->start(2 * m_retryTimer->interval());
  958. }
  959. /// Constructs a new QXmppTurnAllocation.
  960. ///
  961. /// \param parent
  962. QXmppTurnAllocation::QXmppTurnAllocation(QObject *parent)
  963. : QXmppLoggable(parent),
  964. m_relayedPort(0),
  965. m_turnPort(0),
  966. m_channelNumber(0x4000),
  967. m_lifetime(600),
  968. m_state(UnconnectedState)
  969. {
  970. bool check;
  971. Q_UNUSED(check);
  972. socket = new QUdpSocket(this);
  973. check = connect(socket, SIGNAL(readyRead()),
  974. this, SLOT(readyRead()));
  975. Q_ASSERT(check);
  976. m_timer = new QTimer(this);
  977. m_timer->setSingleShot(true);
  978. check = connect(m_timer, SIGNAL(timeout()),
  979. this, SLOT(refresh()));
  980. Q_ASSERT(check);
  981. // channels are valid 600s, we refresh every 500s
  982. m_channelTimer = new QTimer(this);
  983. m_channelTimer->setInterval(500 * 1000);
  984. check = connect(m_channelTimer, SIGNAL(timeout()),
  985. this, SLOT(refreshChannels()));
  986. Q_ASSERT(check);
  987. }
  988. /// Destroys the TURN allocation.
  989. QXmppTurnAllocation::~QXmppTurnAllocation()
  990. {
  991. if (m_state == ConnectedState)
  992. disconnectFromHost();
  993. }
  994. /// Allocates the TURN allocation.
  995. void QXmppTurnAllocation::connectToHost()
  996. {
  997. if (m_state != UnconnectedState)
  998. return;
  999. // start listening for UDP
  1000. if (socket->state() == QAbstractSocket::UnconnectedState) {
  1001. if (!socket->bind()) {
  1002. warning("Could not start listening for TURN");
  1003. return;
  1004. }
  1005. }
  1006. // send allocate request
  1007. QXmppStunMessage request;
  1008. request.setType(QXmppStunMessage::Allocate | QXmppStunMessage::Request);
  1009. request.setId(QXmppUtils::generateRandomBytes(12));
  1010. request.setLifetime(m_lifetime);
  1011. request.setRequestedTransport(0x11);
  1012. m_transactions << new QXmppStunTransaction(request, this);
  1013. // update state
  1014. setState(ConnectingState);
  1015. }
  1016. /// Releases the TURN allocation.
  1017. void QXmppTurnAllocation::disconnectFromHost()
  1018. {
  1019. m_channelTimer->stop();
  1020. m_timer->stop();
  1021. // clear channels and any outstanding transactions
  1022. m_channels.clear();
  1023. foreach (QXmppStunTransaction *transaction, m_transactions)
  1024. delete transaction;
  1025. m_transactions.clear();
  1026. // end allocation
  1027. if (m_state == ConnectedState) {
  1028. QXmppStunMessage request;
  1029. request.setType(QXmppStunMessage::Refresh | QXmppStunMessage::Request);
  1030. request.setId(QXmppUtils::generateRandomBytes(12));
  1031. request.setNonce(m_nonce);
  1032. request.setRealm(m_realm);
  1033. request.setUsername(m_username);
  1034. request.setLifetime(0);
  1035. m_transactions << new QXmppStunTransaction(request, this);
  1036. setState(ClosingState);
  1037. } else {
  1038. setState(UnconnectedState);
  1039. }
  1040. }
  1041. void QXmppTurnAllocation::readyRead()
  1042. {
  1043. QByteArray buffer;
  1044. QHostAddress remoteHost;
  1045. quint16 remotePort;
  1046. while (socket->hasPendingDatagrams()) {
  1047. const qint64 size = socket->pendingDatagramSize();
  1048. buffer.resize(size);
  1049. socket->readDatagram(buffer.data(), buffer.size(), &remoteHost, &remotePort);
  1050. handleDatagram(buffer, remoteHost, remotePort);
  1051. }
  1052. }
  1053. void QXmppTurnAllocation::handleDatagram(const QByteArray &buffer, const QHostAddress &remoteHost, quint16 remotePort)
  1054. {
  1055. // demultiplex channel data
  1056. if (buffer.size() >= 4 && (buffer[0] & 0xc0) == 0x40) {
  1057. QDataStream stream(buffer);
  1058. quint16 channel, length;
  1059. stream >> channel;
  1060. stream >> length;
  1061. if (m_state == ConnectedState && m_channels.contains(channel) && length <= buffer.size() - 4) {
  1062. emit datagramReceived(buffer.mid(4, length), m_channels[channel].first,
  1063. m_channels[channel].second);
  1064. }
  1065. return;
  1066. }
  1067. // parse STUN message
  1068. QXmppStunMessage message;
  1069. QStringList errors;
  1070. if (!message.decode(buffer, QByteArray(), &errors)) {
  1071. foreach (const QString &error, errors)
  1072. warning(error);
  1073. return;
  1074. }
  1075. #ifdef QXMPP_DEBUG_STUN
  1076. logReceived(QString("TURN packet from %1 port %2\n%3").arg(
  1077. remoteHost.toString(),
  1078. QString::number(remotePort),
  1079. message.toString()));
  1080. #endif
  1081. // find transaction
  1082. foreach (QXmppStunTransaction *transaction, m_transactions) {
  1083. if (transaction->request().id() == message.id() &&
  1084. transaction->request().messageMethod() == message.messageMethod()) {
  1085. transaction->readStun(message);
  1086. return;
  1087. }
  1088. }
  1089. }
  1090. /// Refresh allocation.
  1091. void QXmppTurnAllocation::refresh()
  1092. {
  1093. QXmppStunMessage request;
  1094. request.setType(QXmppStunMessage::Refresh | QXmppStunMessage::Request);
  1095. request.setId(QXmppUtils::generateRandomBytes(12));
  1096. request.setNonce(m_nonce);
  1097. request.setRealm(m_realm);
  1098. request.setUsername(m_username);
  1099. m_transactions << new QXmppStunTransaction(request, this);
  1100. }
  1101. /// Refresh channel bindings.
  1102. void QXmppTurnAllocation::refreshChannels()
  1103. {
  1104. foreach (quint16 channel, m_channels.keys()) {
  1105. QXmppStunMessage request;
  1106. request.setType(QXmppStunMessage::ChannelBind | QXmppStunMessage::Request);
  1107. request.setId(QXmppUtils::generateRandomBytes(12));
  1108. request.setNonce(m_nonce);
  1109. request.setRealm(m_realm);
  1110. request.setUsername(m_username);
  1111. request.setChannelNumber(channel);
  1112. request.xorPeerHost = m_channels[channel].first;
  1113. request.xorPeerPort = m_channels[channel].second;
  1114. m_transactions << new QXmppStunTransaction(request, this);
  1115. }
  1116. }
  1117. /// Returns the relayed host address, i.e. the address on the server
  1118. /// used to communicate with peers.
  1119. QHostAddress QXmppTurnAllocation::relayedHost() const
  1120. {
  1121. return m_relayedHost;
  1122. }
  1123. /// Returns the relayed port, i.e. the port on the server used to communicate
  1124. /// with peers.
  1125. quint16 QXmppTurnAllocation::relayedPort() const
  1126. {
  1127. return m_relayedPort;
  1128. }
  1129. /// Sets the password used to authenticate with the TURN server.
  1130. ///
  1131. /// \param password
  1132. void QXmppTurnAllocation::setPassword(const QString &password)
  1133. {
  1134. m_password = password;
  1135. }
  1136. /// Sets the TURN server to use.
  1137. ///
  1138. /// \param host The address of the TURN server.
  1139. /// \param port The port of the TURN server.
  1140. void QXmppTurnAllocation::setServer(const QHostAddress &host, quint16 port)
  1141. {
  1142. m_turnHost = host;
  1143. m_turnPort = port;
  1144. }
  1145. /// Sets the \a user used for authentication with the TURN server.
  1146. ///
  1147. /// \param user
  1148. void QXmppTurnAllocation::setUser(const QString &user)
  1149. {
  1150. m_username = user;
  1151. }
  1152. /// Returns the current state of the allocation.
  1153. ///
  1154. QXmppTurnAllocation::AllocationState QXmppTurnAllocation::state() const
  1155. {
  1156. return m_state;
  1157. }
  1158. void QXmppTurnAllocation::setState(AllocationState state)
  1159. {
  1160. if (state == m_state)
  1161. return;
  1162. m_state = state;
  1163. if (m_state == ConnectedState) {
  1164. emit connected();
  1165. } else if (m_state == UnconnectedState) {
  1166. m_timer->stop();
  1167. emit disconnected();
  1168. }
  1169. }
  1170. void QXmppTurnAllocation::transactionFinished()
  1171. {
  1172. QXmppStunTransaction *transaction = qobject_cast<QXmppStunTransaction*>(sender());
  1173. if (!transaction || !m_transactions.removeAll(transaction))
  1174. return;
  1175. transaction->deleteLater();
  1176. // handle authentication
  1177. const QXmppStunMessage reply = transaction->response();
  1178. if (reply.messageClass() == QXmppStunMessage::Error &&
  1179. reply.errorCode == 401 &&
  1180. (reply.nonce() != m_nonce && reply.realm() != m_realm))
  1181. {
  1182. // update long-term credentials
  1183. m_nonce = reply.nonce();
  1184. m_realm = reply.realm();
  1185. QCryptographicHash hash(QCryptographicHash::Md5);
  1186. hash.addData((m_username + ":" + m_realm + ":" + m_password).toUtf8());
  1187. m_key = hash.result();
  1188. // retry request
  1189. QXmppStunMessage request(transaction->request());
  1190. request.setId(QXmppUtils::generateRandomBytes(12));
  1191. request.setNonce(m_nonce);
  1192. request.setRealm(m_realm);
  1193. request.setUsername(m_username);
  1194. m_transactions << new QXmppStunTransaction(request, this);
  1195. return;
  1196. }
  1197. const quint16 method = transaction->request().messageMethod();
  1198. if (method == QXmppStunMessage::Allocate) {
  1199. if (reply.messageClass() == QXmppStunMessage::Error) {
  1200. warning(QString("Allocation failed: %1 %2").arg(
  1201. QString::number(reply.errorCode), reply.errorPhrase));
  1202. setState(UnconnectedState);
  1203. return;
  1204. }
  1205. if (reply.xorRelayedHost.isNull() ||
  1206. reply.xorRelayedHost.protocol() != QAbstractSocket::IPv4Protocol ||
  1207. !reply.xorRelayedPort) {
  1208. warning("Allocation did not yield a valid relayed address");
  1209. setState(UnconnectedState);
  1210. return;
  1211. }
  1212. // store relayed address
  1213. m_relayedHost = reply.xorRelayedHost;
  1214. m_relayedPort = reply.xorRelayedPort;
  1215. // schedule refresh
  1216. m_lifetime = reply.lifetime();
  1217. m_timer->start((m_lifetime - 60) * 1000);
  1218. setState(ConnectedState);
  1219. } else if (method == QXmppStunMessage::ChannelBind) {
  1220. if (reply.messageClass() == QXmppStunMessage::Error) {
  1221. warning(QString("ChannelBind failed: %1 %2").arg(
  1222. QString::number(reply.errorCode), reply.errorPhrase));
  1223. // remove channel
  1224. m_channels.remove(transaction->request().channelNumber());
  1225. if (m_channels.isEmpty())
  1226. m_channelTimer->stop();
  1227. return;
  1228. }
  1229. } else if (method == QXmppStunMessage::Refresh) {
  1230. if (reply.messageClass() == QXmppStunMessage::Error) {
  1231. warning(QString("Refresh failed: %1 %2").arg(
  1232. QString::number(reply.errorCode), reply.errorPhrase));
  1233. setState(UnconnectedState);
  1234. return;
  1235. }
  1236. if (m_state == ClosingState) {
  1237. setState(UnconnectedState);
  1238. return;
  1239. }
  1240. // schedule refresh
  1241. m_lifetime = reply.lifetime();
  1242. m_timer->start((m_lifetime - 60) * 1000);
  1243. }
  1244. }
  1245. qint64 QXmppTurnAllocation::writeDatagram(const QByteArray &data, const QHostAddress &host, quint16 port)
  1246. {
  1247. if (m_state != ConnectedState)
  1248. return -1;
  1249. const Address addr = qMakePair(host, port);
  1250. quint16 channel = m_channels.key(addr);
  1251. if (!channel) {
  1252. channel = m_channelNumber++;
  1253. m_channels.insert(channel, addr);
  1254. // bind channel
  1255. QXmppStunMessage request;
  1256. request.setType(QXmppStunMessage::ChannelBind | QXmppStunMessage::Request);
  1257. request.setId(QXmppUtils::generateRandomBytes(12));
  1258. request.setNonce(m_nonce);
  1259. request.setRealm(m_realm);
  1260. request.setUsername(m_username);
  1261. request.setChannelNumber(channel);
  1262. request.xorPeerHost = host;
  1263. request.xorPeerPort = port;
  1264. m_transactions << new QXmppStunTransaction(request, this);
  1265. // schedule refresh
  1266. if (!m_channelTimer->isActive())
  1267. m_channelTimer->start();
  1268. }
  1269. // send data
  1270. QByteArray channelData;
  1271. channelData.reserve(4 + data.size());
  1272. QDataStream stream(&channelData, QIODevice::WriteOnly);
  1273. stream << channel;
  1274. stream << quint16(data.size());
  1275. stream.writeRawData(data.data(), data.size());
  1276. if (socket->writeDatagram(channelData, m_turnHost, m_turnPort) == channelData.size())
  1277. return data.size();
  1278. else
  1279. return -1;
  1280. }
  1281. void QXmppTurnAllocation::writeStun(const QXmppStunMessage &message)
  1282. {
  1283. socket->writeDatagram(message.encode(m_key), m_turnHost, m_turnPort);
  1284. #ifdef QXMPP_DEBUG_STUN
  1285. logSent(QString("TURN packet to %1 port %2\n%3").arg(
  1286. m_turnHost.toString(),
  1287. QString::number(m_turnPort),
  1288. message.toString()));
  1289. #endif
  1290. }
  1291. QXmppIceComponent::Pair::Pair(int component, bool controlling)
  1292. : checked(QIODevice::NotOpen),
  1293. socket(0),
  1294. m_component(component),
  1295. m_controlling(controlling)
  1296. {
  1297. transaction = QXmppUtils::generateRandomBytes(ID_SIZE);
  1298. }
  1299. quint64 QXmppIceComponent::Pair::priority() const
  1300. {
  1301. QXmppJingleCandidate local;
  1302. local.setComponent(m_component);
  1303. local.setType(socket ? QXmppJingleCandidate::HostType : QXmppJingleCandidate::RelayedType);
  1304. local.setPriority(candidatePriority(local));
  1305. // see RFC 5245 - 5.7.2. Computing Pair Priority and Ordering Pairs
  1306. const quint32 G = m_controlling ? local.priority() : remote.priority();
  1307. const quint32 D = m_controlling ? remote.priority() : local.priority();
  1308. return (quint64(1) << 32) * qMin(G, D) + 2 * qMax(G, D) + (G > D ? 1 : 0);
  1309. }
  1310. QString QXmppIceComponent::Pair::toString() const
  1311. {
  1312. QString str = QString("%1 port %2").arg(remote.host().toString(), QString::number(remote.port()));
  1313. if (socket)
  1314. str += QString(" (local %1 port %2)").arg(socket->localAddress().toString(), QString::number(socket->localPort()));
  1315. else
  1316. str += QString(" (relayed)");
  1317. if (!reflexive.host().isNull() && reflexive.port())
  1318. str += QString(" (reflexive %1 port %2)").arg(reflexive.host().toString(), QString::number(reflexive.port()));
  1319. return str;
  1320. }
  1321. /// Constructs a new QXmppIceComponent.
  1322. ///
  1323. /// \param parent
  1324. QXmppIceComponent::QXmppIceComponent(QObject *parent)
  1325. : QXmppLoggable(parent),
  1326. m_component(0),
  1327. m_activePair(0),
  1328. m_fallbackPair(0),
  1329. m_iceControlling(false),
  1330. m_peerReflexivePriority(0),
  1331. m_stunPort(0),
  1332. m_stunTries(0),
  1333. m_turnConfigured(false)
  1334. {
  1335. bool check;
  1336. Q_UNUSED(check);
  1337. m_localUser = QXmppUtils::generateStanzaHash(4);
  1338. m_localPassword = QXmppUtils::generateStanzaHash(22);
  1339. m_timer = new QTimer(this);
  1340. m_timer->setInterval(500);
  1341. check = connect(m_timer, SIGNAL(timeout()),
  1342. this, SLOT(checkCandidates()));
  1343. Q_ASSERT(check);
  1344. m_stunTimer = new QTimer(this);
  1345. m_stunTimer->setInterval(500);
  1346. check = connect(m_stunTimer, SIGNAL(timeout()),
  1347. this, SLOT(checkStun()));
  1348. Q_ASSERT(check);
  1349. m_turnAllocation = new QXmppTurnAllocation(this);
  1350. check = connect(m_turnAllocation, SIGNAL(connected()),
  1351. this, SLOT(turnConnected()));
  1352. Q_ASSERT(check);
  1353. check = connect(m_turnAllocation, SIGNAL(datagramReceived(QByteArray,QHostAddress,quint16)),
  1354. this, SLOT(handleDatagram(QByteArray,QHostAddress,quint16)));
  1355. Q_ASSERT(check);
  1356. }
  1357. /// Destroys the QXmppIceComponent.
  1358. QXmppIceComponent::~QXmppIceComponent()
  1359. {
  1360. foreach (Pair *pair, m_pairs)
  1361. delete pair;
  1362. }
  1363. /// Returns the component id for the current socket, e.g. 1 for RTP
  1364. /// and 2 for RTCP.
  1365. int QXmppIceComponent::component() const
  1366. {
  1367. return m_component;
  1368. }
  1369. /// Sets the component id for the current socket, e.g. 1 for RTP
  1370. /// and 2 for RTCP.
  1371. ///
  1372. /// \param component
  1373. void QXmppIceComponent::setComponent(int component)
  1374. {
  1375. m_component = component;
  1376. // calculate peer-reflexive candidate priority
  1377. // see RFC 5245 - 7.1.2.1. PRIORITY and USE-CANDIDATE
  1378. QXmppJingleCandidate reflexive;
  1379. reflexive.setComponent(m_component);
  1380. reflexive.setType(QXmppJingleCandidate::PeerReflexiveType);
  1381. m_peerReflexivePriority = candidatePriority(reflexive);
  1382. setObjectName(QString("STUN(%1)").arg(QString::number(m_component)));
  1383. }
  1384. void QXmppIceComponent::checkCandidates()
  1385. {
  1386. debug("Checking remote candidates");
  1387. foreach (Pair *pair, m_pairs)
  1388. {
  1389. if (m_remoteUser.isEmpty())
  1390. continue;
  1391. // send a binding request
  1392. QXmppStunMessage message;
  1393. message.setId(pair->transaction);
  1394. message.setType(QXmppStunMessage::Binding | QXmppStunMessage::Request);
  1395. message.setPriority(m_peerReflexivePriority);
  1396. message.setUsername(QString("%1:%2").arg(m_remoteUser, m_localUser));
  1397. if (m_iceControlling)
  1398. {
  1399. message.iceControlling = QByteArray(8, 0);
  1400. message.useCandidate = true;
  1401. } else {
  1402. message.iceControlled = QByteArray(8, 0);
  1403. }
  1404. writeStun(message, pair);
  1405. }
  1406. }
  1407. void QXmppIceComponent::checkStun()
  1408. {
  1409. if (m_stunHost.isNull() || !m_stunPort || m_stunTries > 10) {
  1410. m_stunTimer->stop();
  1411. return;
  1412. }
  1413. // Send a request to STUN server to determine server-reflexive candidate
  1414. foreach (QUdpSocket *socket, m_sockets)
  1415. {
  1416. QXmppStunMessage msg;
  1417. msg.setType(QXmppStunMessage::Binding | QXmppStunMessage::Request);
  1418. msg.setId(m_stunId);
  1419. #ifdef QXMPP_DEBUG_STUN
  1420. logSent(QString("STUN packet to %1 port %2\n%3").arg(m_stunHost.toString(),
  1421. QString::number(m_stunPort), msg.toString()));
  1422. #endif
  1423. socket->writeDatagram(msg.encode(), m_stunHost, m_stunPort);
  1424. }
  1425. m_stunTries++;
  1426. }
  1427. /// Stops ICE connectivity checks and closes the underlying sockets.
  1428. void QXmppIceComponent::close()
  1429. {
  1430. foreach (QUdpSocket *socket, m_sockets)
  1431. socket->close();
  1432. m_turnAllocation->disconnectFromHost();
  1433. m_timer->stop();
  1434. m_stunTimer->stop();
  1435. m_activePair = 0;
  1436. }
  1437. /// Starts ICE connectivity checks.
  1438. void QXmppIceComponent::connectToHost()
  1439. {
  1440. if (m_activePair)
  1441. return;
  1442. checkCandidates();
  1443. m_timer->start();
  1444. }
  1445. /// Returns true if ICE negotiation completed, false otherwise.
  1446. bool QXmppIceComponent::isConnected() const
  1447. {
  1448. return m_activePair != 0;
  1449. }
  1450. /// Sets whether the local party has the ICE controlling role.
  1451. void QXmppIceComponent::setIceControlling(bool controlling)
  1452. {
  1453. m_iceControlling = controlling;
  1454. }
  1455. /// Returns the list of local candidates.
  1456. QList<QXmppJingleCandidate> QXmppIceComponent::localCandidates() const
  1457. {
  1458. return m_localCandidates;
  1459. }
  1460. /// Sets the local user fragment.
  1461. ///
  1462. /// \param user
  1463. void QXmppIceComponent::setLocalUser(const QString &user)
  1464. {
  1465. m_localUser = user;
  1466. }
  1467. /// Sets the local password.
  1468. ///
  1469. /// \param password
  1470. void QXmppIceComponent::setLocalPassword(const QString &password)
  1471. {
  1472. m_localPassword = password;
  1473. }
  1474. /// Adds a remote STUN candidate.
  1475. bool QXmppIceComponent::addRemoteCandidate(const QXmppJingleCandidate &candidate)
  1476. {
  1477. if (candidate.component() != m_component ||
  1478. (candidate.type() != QXmppJingleCandidate::HostType &&
  1479. candidate.type() != QXmppJingleCandidate::RelayedType &&
  1480. candidate.type() != QXmppJingleCandidate::ServerReflexiveType) ||
  1481. candidate.protocol() != "udp" ||
  1482. (candidate.host().protocol() != QAbstractSocket::IPv4Protocol &&
  1483. candidate.host().protocol() != QAbstractSocket::IPv6Protocol))
  1484. return false;
  1485. foreach (Pair *pair, m_pairs)
  1486. if (pair->remote.host() == candidate.host() &&
  1487. pair->remote.port() == candidate.port())
  1488. return false;
  1489. foreach (QUdpSocket *socket, m_sockets)
  1490. {
  1491. // do not pair IPv4 with IPv6 or global with link-local addresses
  1492. if (socket->localAddress().protocol() != candidate.host().protocol() ||
  1493. isIPv6LinkLocalAddress(socket->localAddress()) != isIPv6LinkLocalAddress(candidate.host()))
  1494. continue;
  1495. Pair *pair = new Pair(m_component, m_iceControlling);
  1496. pair->remote = candidate;
  1497. if (isIPv6LinkLocalAddress(pair->remote.host()))
  1498. {
  1499. QHostAddress remoteHost = pair->remote.host();
  1500. remoteHost.setScopeId(socket->localAddress().scopeId());
  1501. pair->remote.setHost(remoteHost);
  1502. }
  1503. pair->socket = socket;
  1504. m_pairs << pair;
  1505. if (!m_fallbackPair)
  1506. m_fallbackPair = pair;
  1507. }
  1508. // only use relaying for IPv4 candidates
  1509. if (m_turnConfigured && candidate.host().protocol() == QAbstractSocket::IPv4Protocol) {
  1510. Pair *pair = new Pair(m_component, m_iceControlling);
  1511. pair->remote = candidate;
  1512. pair->socket = 0;
  1513. m_pairs << pair;
  1514. }
  1515. return true;
  1516. }
  1517. /// Adds a discovered peer-reflexive STUN candidate.
  1518. QXmppIceComponent::Pair *QXmppIceComponent::addRemoteCandidate(QUdpSocket *socket, const QHostAddress &host, quint16 port, quint32 priority)
  1519. {
  1520. foreach (Pair *pair, m_pairs)
  1521. if (pair->remote.host() == host &&
  1522. pair->remote.port() == port &&
  1523. pair->socket == socket)
  1524. return pair;
  1525. QXmppJingleCandidate candidate;
  1526. candidate.setComponent(m_component);
  1527. candidate.setHost(host);
  1528. candidate.setId(QXmppUtils::generateStanzaHash(10));
  1529. candidate.setPort(port);
  1530. candidate.setPriority(priority);
  1531. candidate.setProtocol("udp");
  1532. candidate.setType(QXmppJingleCandidate::PeerReflexiveType);
  1533. Pair *pair = new Pair(m_component, m_iceControlling);
  1534. pair->remote = candidate;
  1535. pair->socket = socket;
  1536. m_pairs << pair;
  1537. debug(QString("Added candidate %1").arg(pair->toString()));
  1538. return pair;
  1539. }
  1540. /// Sets the remote user fragment.
  1541. ///
  1542. /// \param user
  1543. void QXmppIceComponent::setRemoteUser(const QString &user)
  1544. {
  1545. m_remoteUser = user;
  1546. }
  1547. /// Sets the remote password.
  1548. ///
  1549. /// \param password
  1550. void QXmppIceComponent::setRemotePassword(const QString &password)
  1551. {
  1552. m_remotePassword = password;
  1553. }
  1554. /// Sets the list of sockets to use for this component.
  1555. ///
  1556. /// \param sockets
  1557. void QXmppIceComponent::setSockets(QList<QUdpSocket*> sockets)
  1558. {
  1559. // clear previous candidates and sockets
  1560. m_localCandidates.clear();
  1561. foreach (QUdpSocket *socket, m_sockets)
  1562. delete socket;
  1563. m_sockets.clear();
  1564. // store candidates
  1565. int foundation = 0;
  1566. foreach (QUdpSocket *socket, sockets)
  1567. {
  1568. socket->setParent(this);
  1569. connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
  1570. QXmppJingleCandidate candidate;
  1571. candidate.setComponent(m_component);
  1572. candidate.setFoundation(foundation++);
  1573. // remove scope ID from IPv6 non-link local addresses
  1574. QHostAddress addr(socket->localAddress());
  1575. if (addr.protocol() == QAbstractSocket::IPv6Protocol &&
  1576. !isIPv6LinkLocalAddress(addr)) {
  1577. addr.setScopeId(QString());
  1578. }
  1579. candidate.setHost(addr);
  1580. candidate.setId(QXmppUtils::generateStanzaHash(10));
  1581. candidate.setPort(socket->localPort());
  1582. candidate.setProtocol("udp");
  1583. candidate.setType(QXmppJingleCandidate::HostType);
  1584. candidate.setPriority(candidatePriority(candidate));
  1585. m_sockets << socket;
  1586. m_localCandidates << candidate;
  1587. }
  1588. // start STUN checks
  1589. if (!m_stunHost.isNull() && m_stunPort) {
  1590. m_stunTries = 0;
  1591. checkStun();
  1592. m_stunTimer->start();
  1593. }
  1594. // connect to TURN server
  1595. if (m_turnConfigured)
  1596. m_turnAllocation->connectToHost();
  1597. }
  1598. /// Sets the STUN server to use to determine server-reflexive addresses
  1599. /// and ports.
  1600. ///
  1601. /// \param host The address of the STUN server.
  1602. /// \param port The port of the STUN server.
  1603. void QXmppIceComponent::setStunServer(const QHostAddress &host, quint16 port)
  1604. {
  1605. m_stunHost = host;
  1606. m_stunPort = port;
  1607. m_stunId = QXmppUtils::generateRandomBytes(ID_SIZE);
  1608. }
  1609. /// Sets the TURN server to use to relay packets in double-NAT configurations.
  1610. ///
  1611. /// \param host The address of the TURN server.
  1612. /// \param port The port of the TURN server.
  1613. void QXmppIceComponent::setTurnServer(const QHostAddress &host, quint16 port)
  1614. {
  1615. m_turnAllocation->setServer(host, port);
  1616. m_turnConfigured = !host.isNull() && port;
  1617. }
  1618. /// Sets the \a user used for authentication with the TURN server.
  1619. ///
  1620. /// \param user
  1621. void QXmppIceComponent::setTurnUser(const QString &user)
  1622. {
  1623. m_turnAllocation->setUser(user);
  1624. }
  1625. /// Sets the \a password used for authentication with the TURN server.
  1626. ///
  1627. /// \param password
  1628. void QXmppIceComponent::setTurnPassword(const QString &password)
  1629. {
  1630. m_turnAllocation->setPassword(password);
  1631. }
  1632. void QXmppIceComponent::readyRead()
  1633. {
  1634. QUdpSocket *socket = qobject_cast<QUdpSocket*>(sender());
  1635. if (!socket)
  1636. return;
  1637. QByteArray buffer;
  1638. QHostAddress remoteHost;
  1639. quint16 remotePort;
  1640. while (socket->hasPendingDatagrams()) {
  1641. const qint64 size = socket->pendingDatagramSize();
  1642. buffer.resize(size);
  1643. socket->readDatagram(buffer.data(), buffer.size(), &remoteHost, &remotePort);
  1644. handleDatagram(buffer, remoteHost, remotePort, socket);
  1645. }
  1646. }
  1647. void QXmppIceComponent::handleDatagram(const QByteArray &buffer, const QHostAddress &remoteHost, quint16 remotePort, QUdpSocket *socket)
  1648. {
  1649. // if this is not a STUN message, emit it
  1650. quint32 messageCookie;
  1651. QByteArray messageId;
  1652. quint16 messageType = QXmppStunMessage::peekType(buffer, messageCookie, messageId);
  1653. if (!messageType || messageCookie != STUN_MAGIC)
  1654. {
  1655. // use this as an opportunity to flag a potential pair
  1656. foreach (Pair *pair, m_pairs) {
  1657. if (pair->remote.host() == remoteHost &&
  1658. pair->remote.port() == remotePort) {
  1659. m_fallbackPair = pair;
  1660. break;
  1661. }
  1662. }
  1663. emit datagramReceived(buffer);
  1664. return;
  1665. }
  1666. // determine password to use
  1667. QString messagePassword;
  1668. if (messageId != m_stunId)
  1669. {
  1670. messagePassword = (messageType & 0xFF00) ? m_remotePassword : m_localPassword;
  1671. if (messagePassword.isEmpty())
  1672. return;
  1673. }
  1674. // parse STUN message
  1675. QXmppStunMessage message;
  1676. QStringList errors;
  1677. if (!message.decode(buffer, messagePassword.toUtf8(), &errors))
  1678. {
  1679. foreach (const QString &error, errors)
  1680. warning(error);
  1681. return;
  1682. }
  1683. #ifdef QXMPP_DEBUG_STUN
  1684. logReceived(QString("STUN packet from %1 port %2\n%3").arg(
  1685. remoteHost.toString(),
  1686. QString::number(remotePort),
  1687. message.toString()));
  1688. #endif
  1689. // check how to handle message
  1690. if (message.id() == m_stunId)
  1691. {
  1692. m_stunTimer->stop();
  1693. // determine server-reflexive address
  1694. QHostAddress reflexiveHost;
  1695. quint16 reflexivePort = 0;
  1696. if (!message.xorMappedHost.isNull() && message.xorMappedPort != 0)
  1697. {
  1698. reflexiveHost = message.xorMappedHost;
  1699. reflexivePort = message.xorMappedPort;
  1700. }
  1701. else if (!message.mappedHost.isNull() && message.mappedPort != 0)
  1702. {
  1703. reflexiveHost = message.mappedHost;
  1704. reflexivePort = message.mappedPort;
  1705. } else {
  1706. warning("STUN server did not provide a reflexive address");
  1707. return;
  1708. }
  1709. // check whether this candidates is already known
  1710. foreach (const QXmppJingleCandidate &candidate, m_localCandidates)
  1711. {
  1712. if (candidate.host() == reflexiveHost &&
  1713. candidate.port() == reflexivePort &&
  1714. candidate.type() == QXmppJingleCandidate::ServerReflexiveType)
  1715. return;
  1716. }
  1717. // add the new local candidate
  1718. debug(QString("Adding server-reflexive candidate %1 port %2").arg(reflexiveHost.toString(), QString::number(reflexivePort)));
  1719. QXmppJingleCandidate candidate;
  1720. candidate.setComponent(m_component);
  1721. candidate.setHost(reflexiveHost);
  1722. candidate.setId(QXmppUtils::generateStanzaHash(10));
  1723. candidate.setPort(reflexivePort);
  1724. candidate.setProtocol("udp");
  1725. candidate.setType(QXmppJingleCandidate::ServerReflexiveType);
  1726. candidate.setPriority(candidatePriority(candidate));
  1727. m_localCandidates << candidate;
  1728. emit localCandidatesChanged();
  1729. return;
  1730. }
  1731. // process message from peer
  1732. Pair *pair = 0;
  1733. if (message.type() == (QXmppStunMessage::Binding | QXmppStunMessage::Request))
  1734. {
  1735. // add remote candidate
  1736. pair = addRemoteCandidate(socket, remoteHost, remotePort, message.priority());
  1737. // send a binding response
  1738. QXmppStunMessage response;
  1739. response.setId(message.id());
  1740. response.setType(QXmppStunMessage::Binding | QXmppStunMessage::Response);
  1741. response.setUsername(message.username());
  1742. response.xorMappedHost = pair->remote.host();
  1743. response.xorMappedPort = pair->remote.port();
  1744. writeStun(response, pair);
  1745. // update state
  1746. if (m_iceControlling || message.useCandidate)
  1747. {
  1748. debug(QString("ICE reverse check complete %1").arg(pair->toString()));
  1749. pair->checked |= QIODevice::ReadOnly;
  1750. }
  1751. if (!m_iceControlling && !m_activePair && !m_remoteUser.isEmpty())
  1752. {
  1753. // send a triggered connectivity test
  1754. QXmppStunMessage message;
  1755. message.setId(pair->transaction);
  1756. message.setType(QXmppStunMessage::Binding | QXmppStunMessage::Request);
  1757. message.setPriority(m_peerReflexivePriority);
  1758. message.setUsername(QString("%1:%2").arg(m_remoteUser, m_localUser));
  1759. message.iceControlled = QByteArray(8, 0);
  1760. writeStun(message, pair);
  1761. }
  1762. } else if (message.type() == (QXmppStunMessage::Binding | QXmppStunMessage::Response)) {
  1763. // find the pair for this transaction
  1764. foreach (Pair *ptr, m_pairs)
  1765. {
  1766. if (ptr->transaction == message.id())
  1767. {
  1768. pair = ptr;
  1769. break;
  1770. }
  1771. }
  1772. if (!pair)
  1773. {
  1774. debug(QString("Unknown transaction %1").arg(QString::fromAscii(message.id().toHex())));
  1775. return;
  1776. }
  1777. // store peer-reflexive address
  1778. pair->reflexive.setHost(message.xorMappedHost);
  1779. pair->reflexive.setPort(message.xorMappedPort);
  1780. #if 0
  1781. // send a binding indication
  1782. QXmppStunMessage indication;
  1783. indication.setId(QXmppUtils::generateRandomBytes(ID_SIZE));
  1784. indication.setType(BindingIndication);
  1785. m_socket->writeStun(indication, pair);
  1786. #endif
  1787. // outgoing media can flow
  1788. debug(QString("ICE forward check complete %1").arg(pair->toString()));
  1789. pair->checked |= QIODevice::WriteOnly;
  1790. }
  1791. // signal completion
  1792. if (pair && pair->checked == QIODevice::ReadWrite)
  1793. {
  1794. m_timer->stop();
  1795. if (!m_activePair || pair->priority() > m_activePair->priority()) {
  1796. info(QString("ICE pair selected %1 (priority: %2)").arg(
  1797. pair->toString(), QString::number(pair->priority())));
  1798. const bool wasConnected = (m_activePair != 0);
  1799. m_activePair = pair;
  1800. if (!wasConnected)
  1801. emit connected();
  1802. }
  1803. }
  1804. }
  1805. void QXmppIceComponent::turnConnected()
  1806. {
  1807. // add the new local candidate
  1808. debug(QString("Adding relayed candidate %1 port %2").arg(
  1809. m_turnAllocation->relayedHost().toString(),
  1810. QString::number(m_turnAllocation->relayedPort())));
  1811. QXmppJingleCandidate candidate;
  1812. candidate.setComponent(m_component);
  1813. candidate.setHost(m_turnAllocation->relayedHost());
  1814. candidate.setId(QXmppUtils::generateStanzaHash(10));
  1815. candidate.setPort(m_turnAllocation->relayedPort());
  1816. candidate.setProtocol("udp");
  1817. candidate.setType(QXmppJingleCandidate::RelayedType);
  1818. candidate.setPriority(candidatePriority(candidate));
  1819. m_localCandidates << candidate;
  1820. emit localCandidatesChanged();
  1821. }
  1822. static QList<QUdpSocket*> reservePort(const QList<QHostAddress> &addresses, quint16 port, QObject *parent)
  1823. {
  1824. QList<QUdpSocket*> sockets;
  1825. foreach (const QHostAddress &address, addresses) {
  1826. QUdpSocket *socket = new QUdpSocket(parent);
  1827. sockets << socket;
  1828. if (!socket->bind(address, port)) {
  1829. for (int i = 0; i < sockets.size(); ++i)
  1830. delete sockets[i];
  1831. sockets.clear();
  1832. break;
  1833. }
  1834. }
  1835. return sockets;
  1836. }
  1837. /// Returns the list of local network addresses.
  1838. QList<QHostAddress> QXmppIceComponent::discoverAddresses()
  1839. {
  1840. QList<QHostAddress> addresses;
  1841. foreach (const QNetworkInterface &interface, QNetworkInterface::allInterfaces())
  1842. {
  1843. if (!(interface.flags() & QNetworkInterface::IsRunning) ||
  1844. interface.flags() & QNetworkInterface::IsLoopBack)
  1845. continue;
  1846. foreach (const QNetworkAddressEntry &entry, interface.addressEntries())
  1847. {
  1848. QHostAddress ip = entry.ip();
  1849. if ((ip.protocol() != QAbstractSocket::IPv4Protocol &&
  1850. ip.protocol() != QAbstractSocket::IPv6Protocol) ||
  1851. entry.netmask().isNull())
  1852. continue;
  1853. // FIXME: for now skip IPv6 link-local addresses, seems to upset
  1854. // clients such as empathy
  1855. if (isIPv6LinkLocalAddress(ip)) {
  1856. ip.setScopeId(interface.name());
  1857. continue;
  1858. }
  1859. addresses << ip;
  1860. }
  1861. }
  1862. return addresses;
  1863. }
  1864. /// Tries to bind \a count UDP sockets on each of the given \a addresses.
  1865. ///
  1866. /// The port numbers are chosen so that they are consecutive, starting at
  1867. /// an even port. This makes them suitable for RTP/RTCP sockets pairs.
  1868. ///
  1869. /// \param addresses The network address on which to bind the sockets.
  1870. /// \param count The number of ports to reserve.
  1871. /// \param parent The parent object for the sockets.
  1872. QList<QUdpSocket*> QXmppIceComponent::reservePorts(const QList<QHostAddress> &addresses, int count, QObject *parent)
  1873. {
  1874. QList<QUdpSocket*> sockets;
  1875. if (addresses.isEmpty() || !count)
  1876. return sockets;
  1877. const int expectedSize = addresses.size() * count;
  1878. quint16 port = 49152;
  1879. while (sockets.size() != expectedSize) {
  1880. // reserve first port (even number)
  1881. if (port % 2)
  1882. port++;
  1883. QList<QUdpSocket*> socketChunk;
  1884. while (socketChunk.isEmpty() && port <= 65536 - count) {
  1885. socketChunk = reservePort(addresses, port, parent);
  1886. if (socketChunk.isEmpty())
  1887. port += 2;
  1888. }
  1889. if (socketChunk.isEmpty())
  1890. return sockets;
  1891. // reserve other ports
  1892. sockets << socketChunk;
  1893. for (int i = 1; i < count; ++i) {
  1894. socketChunk = reservePort(addresses, ++port, parent);
  1895. if (socketChunk.isEmpty())
  1896. break;
  1897. sockets << socketChunk;
  1898. }
  1899. // cleanup if we failed
  1900. if (sockets.size() != expectedSize) {
  1901. for (int i = 0; i < sockets.size(); ++i)
  1902. delete sockets[i];
  1903. sockets.clear();
  1904. }
  1905. }
  1906. return sockets;
  1907. }
  1908. /// Sends a data packet to the remote party.
  1909. ///
  1910. /// \param datagram
  1911. qint64 QXmppIceComponent::sendDatagram(const QByteArray &datagram)
  1912. {
  1913. Pair *pair = m_activePair ? m_activePair : m_fallbackPair;
  1914. if (!pair)
  1915. return -1;
  1916. if (pair->socket)
  1917. return pair->socket->writeDatagram(datagram, pair->remote.host(), pair->remote.port());
  1918. else if (m_turnAllocation->state() == QXmppTurnAllocation::ConnectedState)
  1919. return m_turnAllocation->writeDatagram(datagram, pair->remote.host(), pair->remote.port());
  1920. else
  1921. return -1;
  1922. }
  1923. /// Sends a STUN packet to the remote party.
  1924. qint64 QXmppIceComponent::writeStun(const QXmppStunMessage &message, QXmppIceComponent::Pair *pair)
  1925. {
  1926. qint64 ret;
  1927. const QString messagePassword = (message.type() & 0xFF00) ? m_localPassword : m_remotePassword;
  1928. if (pair->socket)
  1929. ret = pair->socket->writeDatagram(
  1930. message.encode(messagePassword.toUtf8()),
  1931. pair->remote.host(),
  1932. pair->remote.port());
  1933. else if (m_turnAllocation->state() == QXmppTurnAllocation::ConnectedState)
  1934. ret = m_turnAllocation->writeDatagram(
  1935. message.encode(messagePassword.toUtf8()),
  1936. pair->remote.host(),
  1937. pair->remote.port());
  1938. else
  1939. return -1;
  1940. #ifdef QXMPP_DEBUG_STUN
  1941. logSent(QString("Sent to %1\n%2").arg(pair->toString(), message.toString()));
  1942. #endif
  1943. return ret;
  1944. }
  1945. /// Constructs a new ICE connection.
  1946. ///
  1947. /// \param parent
  1948. QXmppIceConnection::QXmppIceConnection(QObject *parent)
  1949. : QXmppLoggable(parent),
  1950. m_iceControlling(false),
  1951. m_stunPort(0)
  1952. {
  1953. bool check;
  1954. m_localUser = QXmppUtils::generateStanzaHash(4);
  1955. m_localPassword = QXmppUtils::generateStanzaHash(22);
  1956. // timer to limit connection time to 30 seconds
  1957. m_connectTimer = new QTimer(this);
  1958. m_connectTimer->setInterval(30000);
  1959. m_connectTimer->setSingleShot(true);
  1960. check = connect(m_connectTimer, SIGNAL(timeout()),
  1961. this, SLOT(slotTimeout()));
  1962. Q_ASSERT(check);
  1963. Q_UNUSED(check);
  1964. }
  1965. /// Returns the given component of this ICE connection.
  1966. ///
  1967. /// \param component
  1968. QXmppIceComponent *QXmppIceConnection::component(int component)
  1969. {
  1970. return m_components.value(component);
  1971. }
  1972. /// Adds a component to this ICE connection, for instance 1 for RTP
  1973. /// or 2 for RTCP.
  1974. ///
  1975. /// \param component
  1976. void QXmppIceConnection::addComponent(int component)
  1977. {
  1978. bool check;
  1979. Q_UNUSED(check);
  1980. if (m_components.contains(component))
  1981. {
  1982. warning(QString("Already have component %1").arg(QString::number(component)));
  1983. return;
  1984. }
  1985. QXmppIceComponent *socket = new QXmppIceComponent(this);
  1986. socket->setComponent(component);
  1987. socket->setIceControlling(m_iceControlling);
  1988. socket->setLocalUser(m_localUser);
  1989. socket->setLocalPassword(m_localPassword);
  1990. socket->setStunServer(m_stunHost, m_stunPort);
  1991. socket->setTurnServer(m_turnHost, m_turnPort);
  1992. socket->setTurnUser(m_turnUser);
  1993. socket->setTurnPassword(m_turnPassword);
  1994. check = connect(socket, SIGNAL(localCandidatesChanged()),
  1995. this, SIGNAL(localCandidatesChanged()));
  1996. Q_ASSERT(check);
  1997. check = connect(socket, SIGNAL(connected()),
  1998. this, SLOT(slotConnected()));
  1999. Q_ASSERT(check);
  2000. m_components[component] = socket;
  2001. }
  2002. /// Adds a candidate for one of the remote components.
  2003. ///
  2004. /// \param candidate
  2005. void QXmppIceConnection::addRemoteCandidate(const QXmppJingleCandidate &candidate)
  2006. {
  2007. QXmppIceComponent *socket = m_components.value(candidate.component());
  2008. if (!socket)
  2009. {
  2010. warning(QString("Not adding candidate for unknown component %1").arg(
  2011. QString::number(candidate.component())));
  2012. return;
  2013. }
  2014. socket->addRemoteCandidate(candidate);
  2015. }
  2016. /// Binds the local sockets to the specified addresses.
  2017. ///
  2018. /// \param addresses The addresses on which to listen.
  2019. bool QXmppIceConnection::bind(const QList<QHostAddress> &addresses)
  2020. {
  2021. // reserve ports
  2022. QList<QUdpSocket*> sockets = QXmppIceComponent::reservePorts(addresses, m_components.size());
  2023. if (sockets.isEmpty() && !addresses.isEmpty())
  2024. return false;
  2025. // assign sockets
  2026. QList<int> keys = m_components.keys();
  2027. qSort(keys);
  2028. int s = 0;
  2029. foreach (int k, keys) {
  2030. m_components[k]->setSockets(sockets.mid(s, addresses.size()));
  2031. s += addresses.size();
  2032. }
  2033. return true;
  2034. }
  2035. /// Closes the ICE connection.
  2036. void QXmppIceConnection::close()
  2037. {
  2038. m_connectTimer->stop();
  2039. foreach (QXmppIceComponent *socket, m_components.values())
  2040. socket->close();
  2041. }
  2042. /// Starts ICE connectivity checks.
  2043. void QXmppIceConnection::connectToHost()
  2044. {
  2045. if (isConnected() || m_connectTimer->isActive())
  2046. return;
  2047. foreach (QXmppIceComponent *socket, m_components.values())
  2048. socket->connectToHost();
  2049. m_connectTimer->start();
  2050. }
  2051. /// Returns true if ICE negotiation completed, false otherwise.
  2052. bool QXmppIceConnection::isConnected() const
  2053. {
  2054. foreach (QXmppIceComponent *socket, m_components.values())
  2055. if (!socket->isConnected())
  2056. return false;
  2057. return true;
  2058. }
  2059. /// Sets whether the local party has the ICE controlling role.
  2060. void QXmppIceConnection::setIceControlling(bool controlling)
  2061. {
  2062. m_iceControlling = controlling;
  2063. foreach (QXmppIceComponent *socket, m_components.values())
  2064. socket->setIceControlling(controlling);
  2065. }
  2066. /// Returns the list of local HOST CANDIDATES candidates by iterating
  2067. /// over the available network interfaces.
  2068. QList<QXmppJingleCandidate> QXmppIceConnection::localCandidates() const
  2069. {
  2070. QList<QXmppJingleCandidate> candidates;
  2071. foreach (QXmppIceComponent *socket, m_components.values())
  2072. candidates += socket->localCandidates();
  2073. return candidates;
  2074. }
  2075. /// Returns the local user fragment.
  2076. QString QXmppIceConnection::localUser() const
  2077. {
  2078. return m_localUser;
  2079. }
  2080. /// Sets the local user fragment.
  2081. ///
  2082. /// You do not usually need to call this as one is automatically generated.
  2083. ///
  2084. /// \param user
  2085. void QXmppIceConnection::setLocalUser(const QString &user)
  2086. {
  2087. m_localUser = user;
  2088. foreach (QXmppIceComponent *socket, m_components.values())
  2089. socket->setLocalUser(user);
  2090. }
  2091. /// Returns the local password.
  2092. QString QXmppIceConnection::localPassword() const
  2093. {
  2094. return m_localPassword;
  2095. }
  2096. /// Sets the local password.
  2097. ///
  2098. /// You do not usually need to call this as one is automatically generated.
  2099. ///
  2100. /// \param password
  2101. void QXmppIceConnection::setLocalPassword(const QString &password)
  2102. {
  2103. m_localPassword = password;
  2104. foreach (QXmppIceComponent *socket, m_components.values())
  2105. socket->setLocalPassword(password);
  2106. }
  2107. /// Sets the remote user fragment.
  2108. ///
  2109. /// \param user
  2110. void QXmppIceConnection::setRemoteUser(const QString &user)
  2111. {
  2112. foreach (QXmppIceComponent *socket, m_components.values())
  2113. socket->setRemoteUser(user);
  2114. }
  2115. /// Sets the remote password.
  2116. ///
  2117. /// \param password
  2118. void QXmppIceConnection::setRemotePassword(const QString &password)
  2119. {
  2120. foreach (QXmppIceComponent *socket, m_components.values())
  2121. socket->setRemotePassword(password);
  2122. }
  2123. /// Sets the STUN server to use to determine server-reflexive addresses
  2124. /// and ports.
  2125. ///
  2126. /// \param host The address of the STUN server.
  2127. /// \param port The port of the STUN server.
  2128. void QXmppIceConnection::setStunServer(const QHostAddress &host, quint16 port)
  2129. {
  2130. m_stunHost = host;
  2131. m_stunPort = port;
  2132. foreach (QXmppIceComponent *socket, m_components.values())
  2133. socket->setStunServer(host, port);
  2134. }
  2135. /// Sets the TURN server to use to relay packets in double-NAT configurations.
  2136. ///
  2137. /// \param host The address of the TURN server.
  2138. /// \param port The port of the TURN server.
  2139. void QXmppIceConnection::setTurnServer(const QHostAddress &host, quint16 port)
  2140. {
  2141. m_turnHost = host;
  2142. m_turnPort = port;
  2143. foreach (QXmppIceComponent *socket, m_components.values())
  2144. socket->setTurnServer(host, port);
  2145. }
  2146. /// Sets the \a user used for authentication with the TURN server.
  2147. ///
  2148. /// \param user
  2149. void QXmppIceConnection::setTurnUser(const QString &user)
  2150. {
  2151. m_turnUser = user;
  2152. foreach (QXmppIceComponent *socket, m_components.values())
  2153. socket->setTurnUser(user);
  2154. }
  2155. /// Sets the \a password used for authentication with the TURN server.
  2156. ///
  2157. /// \param password
  2158. void QXmppIceConnection::setTurnPassword(const QString &password)
  2159. {
  2160. m_turnPassword = password;
  2161. foreach (QXmppIceComponent *socket, m_components.values())
  2162. socket->setTurnPassword(password);
  2163. }
  2164. void QXmppIceConnection::slotConnected()
  2165. {
  2166. foreach (QXmppIceComponent *socket, m_components.values())
  2167. if (!socket->isConnected())
  2168. return;
  2169. info(QString("ICE negotiation completed"));
  2170. m_connectTimer->stop();
  2171. emit connected();
  2172. }
  2173. void QXmppIceConnection::slotTimeout()
  2174. {
  2175. warning(QString("ICE negotiation timed out"));
  2176. foreach (QXmppIceComponent *socket, m_components.values())
  2177. socket->close();
  2178. emit disconnected();
  2179. }