handshake.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * SRT - Secure, Reliable, Transport
  3. * Copyright (c) 2018 Haivision Systems Inc.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. */
  10. /*****************************************************************************
  11. Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois.
  12. All rights reserved.
  13. Redistribution and use in source and binary forms, with or without
  14. modification, are permitted provided that the following conditions are
  15. met:
  16. * Redistributions of source code must retain the above
  17. copyright notice, this list of conditions and the
  18. following disclaimer.
  19. * Redistributions in binary form must reproduce the
  20. above copyright notice, this list of conditions
  21. and the following disclaimer in the documentation
  22. and/or other materials provided with the distribution.
  23. * Neither the name of the University of Illinois
  24. nor the names of its contributors may be used to
  25. endorse or promote products derived from this
  26. software without specific prior written permission.
  27. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *****************************************************************************/
  39. #include "platform_sys.h"
  40. #include <cstring>
  41. #include <string>
  42. #include <sstream>
  43. #include <iterator>
  44. #include <algorithm>
  45. #include "udt.h"
  46. #include "api.h"
  47. #include "core.h"
  48. #include "handshake.h"
  49. #include "utilities.h"
  50. using namespace std;
  51. using namespace srt;
  52. srt::CHandShake::CHandShake()
  53. : m_iVersion(0)
  54. , m_iType(0) // Universal: UDT_UNDEFINED or no flags
  55. , m_iISN(0)
  56. , m_iMSS(0)
  57. , m_iFlightFlagSize(0)
  58. , m_iReqType(URQ_WAVEAHAND)
  59. , m_iID(0)
  60. , m_iCookie(0)
  61. , m_extension(false)
  62. {
  63. for (int i = 0; i < 4; ++ i)
  64. m_piPeerIP[i] = 0;
  65. }
  66. int srt::CHandShake::store_to(char* buf, size_t& w_size)
  67. {
  68. if (w_size < m_iContentSize)
  69. return -1;
  70. int32_t* p = reinterpret_cast<int32_t*>(buf);
  71. *p++ = m_iVersion;
  72. *p++ = m_iType;
  73. *p++ = m_iISN;
  74. *p++ = m_iMSS;
  75. *p++ = m_iFlightFlagSize;
  76. *p++ = int32_t(m_iReqType);
  77. *p++ = m_iID;
  78. *p++ = m_iCookie;
  79. for (int i = 0; i < 4; ++ i)
  80. *p++ = m_piPeerIP[i];
  81. w_size = m_iContentSize;
  82. return 0;
  83. }
  84. int srt::CHandShake::load_from(const char* buf, size_t size)
  85. {
  86. if (size < m_iContentSize)
  87. return -1;
  88. const int32_t* p = reinterpret_cast<const int32_t*>(buf);
  89. m_iVersion = *p++;
  90. m_iType = *p++;
  91. m_iISN = *p++;
  92. m_iMSS = *p++;
  93. m_iFlightFlagSize = *p++;
  94. m_iReqType = UDTRequestType(*p++);
  95. m_iID = *p++;
  96. m_iCookie = *p++;
  97. for (int i = 0; i < 4; ++ i)
  98. m_piPeerIP[i] = *p++;
  99. return 0;
  100. }
  101. #ifdef ENABLE_LOGGING
  102. namespace srt
  103. {
  104. const char* srt_rejectreason_name [] = {
  105. "UNKNOWN",
  106. "SYSTEM",
  107. "PEER",
  108. "RESOURCE",
  109. "ROGUE",
  110. "BACKLOG",
  111. "IPE",
  112. "CLOSE",
  113. "VERSION",
  114. "RDVCOOKIE",
  115. "BADSECRET",
  116. "UNSECURE",
  117. "MESSAGEAPI",
  118. "CONGESTION",
  119. "FILTER",
  120. "GROUP",
  121. "TIMEOUT",
  122. "CRYPTO"
  123. };
  124. }
  125. std::string srt::RequestTypeStr(UDTRequestType rq)
  126. {
  127. if (rq >= URQ_FAILURE_TYPES)
  128. {
  129. std::ostringstream rt;
  130. rt << "ERROR:";
  131. int id = RejectReasonForURQ(rq);
  132. if (id < (int) Size(srt_rejectreason_name))
  133. rt << srt_rejectreason_name[id];
  134. else if (id < SRT_REJC_USERDEFINED)
  135. {
  136. if (id < SRT_REJC_PREDEFINED)
  137. rt << "UNKNOWN:" << id;
  138. else
  139. rt << "PREDEFINED:" << (id - SRT_REJC_PREDEFINED);
  140. }
  141. else
  142. rt << "USERDEFINED:" << (id - SRT_REJC_USERDEFINED);
  143. return rt.str();
  144. }
  145. switch ( rq )
  146. {
  147. case URQ_INDUCTION: return "induction";
  148. case URQ_WAVEAHAND: return "waveahand";
  149. case URQ_CONCLUSION: return "conclusion";
  150. case URQ_AGREEMENT: return "agreement";
  151. default: return "INVALID";
  152. }
  153. }
  154. string srt::CHandShake::RdvStateStr(CHandShake::RendezvousState s)
  155. {
  156. switch (s)
  157. {
  158. case RDV_WAVING: return "waving";
  159. case RDV_ATTENTION: return "attention";
  160. case RDV_FINE: return "fine";
  161. case RDV_INITIATED: return "initiated";
  162. case RDV_CONNECTED: return "connected";
  163. default: ;
  164. }
  165. return "invalid";
  166. }
  167. #endif
  168. bool srt::CHandShake::valid()
  169. {
  170. if (m_iVersion < CUDT::HS_VERSION_UDT4
  171. || m_iISN < 0 || m_iISN >= CSeqNo::m_iMaxSeqNo
  172. || m_iMSS < 32
  173. || m_iFlightFlagSize < 2)
  174. return false;
  175. return true;
  176. }
  177. string srt::CHandShake::show()
  178. {
  179. ostringstream so;
  180. so << "version=" << m_iVersion << " type=0x" << hex << m_iType << dec
  181. << " ISN=" << m_iISN << " MSS=" << m_iMSS << " FLW=" << m_iFlightFlagSize
  182. << " reqtype=" << RequestTypeStr(m_iReqType) << " srcID=" << m_iID
  183. << " cookie=" << hex << m_iCookie << dec
  184. << " srcIP=";
  185. const unsigned char* p = (const unsigned char*)m_piPeerIP;
  186. const unsigned char* pe = p + 4 * (sizeof(uint32_t));
  187. copy(p, pe, ostream_iterator<unsigned>(so, "."));
  188. // XXX HS version symbols should be probably declared inside
  189. // CHandShake, not CUDT.
  190. if ( m_iVersion > CUDT::HS_VERSION_UDT4 )
  191. {
  192. const int flags = SrtHSRequest::SRT_HSTYPE_HSFLAGS::unwrap(m_iType);
  193. so << "FLAGS: ";
  194. if (flags == SrtHSRequest::SRT_MAGIC_CODE)
  195. so << "MAGIC";
  196. else if (m_iType == 0)
  197. so << "NONE"; // no flags and no advertised pbkeylen
  198. else
  199. so << ExtensionFlagStr(m_iType);
  200. }
  201. return so.str();
  202. }
  203. string srt::CHandShake::ExtensionFlagStr(int32_t fl)
  204. {
  205. std::ostringstream out;
  206. if ( fl & HS_EXT_HSREQ )
  207. out << " hsx";
  208. if ( fl & HS_EXT_KMREQ )
  209. out << " kmx";
  210. if ( fl & HS_EXT_CONFIG )
  211. out << " config";
  212. const int kl = SrtHSRequest::SRT_HSTYPE_ENCFLAGS::unwrap(fl) << 6;
  213. if (kl != 0)
  214. {
  215. out << " AES-" << kl;
  216. }
  217. else
  218. {
  219. out << " no-pbklen";
  220. }
  221. return out.str();
  222. }
  223. // XXX This code isn't currently used. Left here because it can
  224. // be used in future, should any refactoring for the "manual word placement"
  225. // code be done.
  226. bool srt::SrtHSRequest::serialize(char* buf, size_t size) const
  227. {
  228. if (size < SRT_HS_SIZE)
  229. return false;
  230. int32_t* p = reinterpret_cast<int32_t*>(buf);
  231. *p++ = m_iSrtVersion;
  232. *p++ = m_iSrtFlags;
  233. *p++ = m_iSrtTsbpd;
  234. *p++ = 0; // SURPRISE! Seriously, use (something) if this "reserved" is going to be used for something.
  235. return true;
  236. }
  237. bool srt::SrtHSRequest::deserialize(const char* buf, size_t size)
  238. {
  239. m_iSrtVersion = 0; // just to let users recognize if it succeeded or not.
  240. if (size < SRT_HS_SIZE)
  241. return false;
  242. const int32_t* p = reinterpret_cast<const int32_t*>(buf);
  243. m_iSrtVersion = (*p++);
  244. m_iSrtFlags = (*p++);
  245. m_iSrtTsbpd = (*p++);
  246. m_iSrtReserved = (*p++);
  247. return true;
  248. }
  249. std::string srt::SrtFlagString(int32_t flags)
  250. {
  251. #define LEN(arr) (sizeof (arr)/(sizeof ((arr)[0])))
  252. std::string output;
  253. static std::string namera[] = { "TSBPD-snd", "TSBPD-rcv", "haicrypt", "TLPktDrop", "NAKReport", "ReXmitFlag", "StreamAPI" };
  254. size_t i = 0;
  255. for (; i < LEN(namera); ++i)
  256. {
  257. if ((flags & 1) == 1)
  258. {
  259. output += "+" + namera[i] + " ";
  260. }
  261. else
  262. {
  263. output += "-" + namera[i] + " ";
  264. }
  265. flags >>= 1;
  266. }
  267. #undef LEN
  268. if (flags != 0)
  269. {
  270. output += "+unknown";
  271. }
  272. return output;
  273. }