common.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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 - 2016, 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. /*****************************************************************************
  40. written by
  41. Yunhong Gu, last updated 07/25/2010
  42. modified by
  43. Haivision Systems Inc.
  44. *****************************************************************************/
  45. #define SRT_IMPORT_TIME 1
  46. #include "platform_sys.h"
  47. #include <string>
  48. #include <sstream>
  49. #include <cmath>
  50. #include <iostream>
  51. #include <iomanip>
  52. #include <iterator>
  53. #include <vector>
  54. #include "udt.h"
  55. #include "md5.h"
  56. #include "common.h"
  57. #include "netinet_any.h"
  58. #include "logging.h"
  59. #include "packet.h"
  60. #include "threadname.h"
  61. #include <srt_compat.h> // SysStrError
  62. using namespace std;
  63. using namespace srt::sync;
  64. using namespace srt_logging;
  65. namespace srt_logging
  66. {
  67. extern Logger inlog;
  68. }
  69. namespace srt
  70. {
  71. const char* strerror_get_message(size_t major, size_t minor);
  72. } // namespace srt
  73. srt::CUDTException::CUDTException(CodeMajor major, CodeMinor minor, int err):
  74. m_iMajor(major),
  75. m_iMinor(minor)
  76. {
  77. if (err == -1)
  78. m_iErrno = NET_ERROR;
  79. else
  80. m_iErrno = err;
  81. }
  82. const char* srt::CUDTException::getErrorMessage() const ATR_NOTHROW
  83. {
  84. return strerror_get_message(m_iMajor, m_iMinor);
  85. }
  86. std::string srt::CUDTException::getErrorString() const
  87. {
  88. return getErrorMessage();
  89. }
  90. #define UDT_XCODE(mj, mn) (int(mj)*1000)+int(mn)
  91. int srt::CUDTException::getErrorCode() const
  92. {
  93. return UDT_XCODE(m_iMajor, m_iMinor);
  94. }
  95. int srt::CUDTException::getErrno() const
  96. {
  97. return m_iErrno;
  98. }
  99. void srt::CUDTException::clear()
  100. {
  101. m_iMajor = MJ_SUCCESS;
  102. m_iMinor = MN_NONE;
  103. m_iErrno = 0;
  104. }
  105. #undef UDT_XCODE
  106. //
  107. bool srt::CIPAddress::ipcmp(const sockaddr* addr1, const sockaddr* addr2, int ver)
  108. {
  109. if (AF_INET == ver)
  110. {
  111. sockaddr_in* a1 = (sockaddr_in*)addr1;
  112. sockaddr_in* a2 = (sockaddr_in*)addr2;
  113. if ((a1->sin_port == a2->sin_port) && (a1->sin_addr.s_addr == a2->sin_addr.s_addr))
  114. return true;
  115. }
  116. else
  117. {
  118. sockaddr_in6* a1 = (sockaddr_in6*)addr1;
  119. sockaddr_in6* a2 = (sockaddr_in6*)addr2;
  120. if (a1->sin6_port == a2->sin6_port)
  121. {
  122. for (int i = 0; i < 16; ++ i)
  123. if (*((char*)&(a1->sin6_addr) + i) != *((char*)&(a2->sin6_addr) + i))
  124. return false;
  125. return true;
  126. }
  127. }
  128. return false;
  129. }
  130. void srt::CIPAddress::ntop(const sockaddr_any& addr, uint32_t ip[4])
  131. {
  132. if (addr.family() == AF_INET)
  133. {
  134. // SRT internal format of IPv4 address.
  135. // The IPv4 address is in the first field. The rest is 0.
  136. ip[0] = addr.sin.sin_addr.s_addr;
  137. ip[1] = ip[2] = ip[3] = 0;
  138. }
  139. else
  140. {
  141. std::memcpy(ip, addr.sin6.sin6_addr.s6_addr, 16);
  142. }
  143. }
  144. namespace srt {
  145. bool checkMappedIPv4(const uint16_t* addr)
  146. {
  147. static const uint16_t ipv4on6_model [8] =
  148. {
  149. 0, 0, 0, 0, 0, 0xFFFF, 0, 0
  150. };
  151. // Compare only first 6 words. Remaining 2 words
  152. // comprise the IPv4 address, if these first 6 match.
  153. const uint16_t* mbegin = ipv4on6_model;
  154. const uint16_t* mend = ipv4on6_model + 6;
  155. return std::equal(mbegin, mend, addr);
  156. }
  157. }
  158. // XXX This has void return and the first argument is passed by reference.
  159. // Consider simply returning sockaddr_any by value.
  160. void srt::CIPAddress::pton(sockaddr_any& w_addr, const uint32_t ip[4], const sockaddr_any& peer)
  161. {
  162. //using ::srt_logging::inlog;
  163. uint32_t* target_ipv4_addr = NULL;
  164. if (peer.family() == AF_INET)
  165. {
  166. sockaddr_in* a = (&w_addr.sin);
  167. target_ipv4_addr = (uint32_t*) &a->sin_addr.s_addr;
  168. }
  169. else // AF_INET6
  170. {
  171. // Check if the peer address is a model of IPv4-mapped-on-IPv6.
  172. // If so, it means that the `ip` array should be interpreted as IPv4.
  173. const bool is_mapped_ipv4 = checkMappedIPv4((uint16_t*)peer.sin6.sin6_addr.s6_addr);
  174. sockaddr_in6* a = (&w_addr.sin6);
  175. // This whole above procedure was only in order to EXCLUDE the
  176. // possibility of IPv4-mapped-on-IPv6. This below may only happen
  177. // if BOTH peers are IPv6. Otherwise we have a situation of cross-IP
  178. // version connection in which case the address in question is always
  179. // IPv4 in various mapping formats.
  180. if (!is_mapped_ipv4)
  181. {
  182. // Here both agent and peer use IPv6, in which case
  183. // `ip` contains the full IPv6 address, so just copy
  184. // it as is.
  185. std::memcpy(a->sin6_addr.s6_addr, ip, 16);
  186. return; // The address is written, nothing left to do.
  187. }
  188. //
  189. // IPv4 mapped on IPv6
  190. // Here agent uses IPv6 with IPPROTO_IPV6/IPV6_V6ONLY == 0
  191. // In this case, the address in `ip` is always an IPv4,
  192. // although we are not certain as to whether it's using the
  193. // IPv6 encoding (0::FFFF:IPv4) or SRT encoding (IPv4::0);
  194. // this must be extra determined.
  195. //
  196. // Unfortunately, sockaddr_in6 doesn't give any straightforward
  197. // method for it, although the official size of a single element
  198. // of the IPv6 address is 16-bit.
  199. memset((a->sin6_addr.s6_addr), 0, sizeof a->sin6_addr.s6_addr);
  200. // The sin6_addr.s6_addr32 is non that portable to use here.
  201. uint32_t* paddr32 = (uint32_t*)a->sin6_addr.s6_addr;
  202. uint16_t* paddr16 = (uint16_t*)a->sin6_addr.s6_addr;
  203. // layout: of IPv4 address 192.168.128.2
  204. // 16-bit:
  205. // [0000: 0000: 0000: 0000: 0000: FFFF: 192.168:128.2]
  206. // 8-bit
  207. // [00/00/00/00/00/00/00/00/00/00/FF/FF/192/168/128/2]
  208. // 32-bit
  209. // [00000000 && 00000000 && 0000FFFF && 192.168.128.2]
  210. // Spreading every 16-bit word separately to avoid endian dilemmas
  211. paddr16[2 * 2 + 1] = 0xFFFF;
  212. target_ipv4_addr = &paddr32[3];
  213. }
  214. // Now we have two possible formats of encoding the IPv4 address:
  215. // 1. If peer is IPv4, it's IPv4::0
  216. // 2. If peer is IPv6, it's 0::FFFF:IPv4.
  217. //
  218. // Has any other possibility happen here, copy an empty address,
  219. // which will be the only sign of an error.
  220. const uint16_t* peeraddr16 = (uint16_t*)ip;
  221. const bool is_mapped_ipv4 = checkMappedIPv4(peeraddr16);
  222. if (is_mapped_ipv4)
  223. {
  224. *target_ipv4_addr = ip[3];
  225. HLOGC(inlog.Debug, log << "pton: Handshake address: " << w_addr.str() << " provided in IPv6 mapping format");
  226. }
  227. // Check SRT IPv4 format.
  228. else if ((ip[1] | ip[2] | ip[3]) == 0)
  229. {
  230. *target_ipv4_addr = ip[0];
  231. HLOGC(inlog.Debug, log << "pton: Handshake address: " << w_addr.str() << " provided in SRT IPv4 format");
  232. }
  233. else
  234. {
  235. LOGC(inlog.Error, log << "pton: IPE or net error: can't determine IPv4 carryover format: " << std::hex
  236. << peeraddr16[0] << ":"
  237. << peeraddr16[1] << ":"
  238. << peeraddr16[2] << ":"
  239. << peeraddr16[3] << ":"
  240. << peeraddr16[4] << ":"
  241. << peeraddr16[5] << ":"
  242. << peeraddr16[6] << ":"
  243. << peeraddr16[7] << std::dec);
  244. *target_ipv4_addr = 0;
  245. if (peer.family() != AF_INET)
  246. {
  247. // Additionally overwrite the 0xFFFF that has been
  248. // just written 50 lines above.
  249. w_addr.sin6.sin6_addr.s6_addr[10] = 0;
  250. w_addr.sin6.sin6_addr.s6_addr[11] = 0;
  251. }
  252. }
  253. }
  254. namespace srt {
  255. static string ShowIP4(const sockaddr_in* sin)
  256. {
  257. ostringstream os;
  258. union
  259. {
  260. in_addr sinaddr;
  261. unsigned char ip[4];
  262. };
  263. sinaddr = sin->sin_addr;
  264. os << int(ip[0]);
  265. os << ".";
  266. os << int(ip[1]);
  267. os << ".";
  268. os << int(ip[2]);
  269. os << ".";
  270. os << int(ip[3]);
  271. return os.str();
  272. }
  273. static string ShowIP6(const sockaddr_in6* sin)
  274. {
  275. ostringstream os;
  276. os.setf(ios::uppercase);
  277. bool sep = false;
  278. for (size_t i = 0; i < 16; ++i)
  279. {
  280. int v = sin->sin6_addr.s6_addr[i];
  281. if ( v )
  282. {
  283. if ( sep )
  284. os << ":";
  285. os << hex << v;
  286. sep = true;
  287. }
  288. }
  289. return os.str();
  290. }
  291. string CIPAddress::show(const sockaddr* adr)
  292. {
  293. if ( adr->sa_family == AF_INET )
  294. return ShowIP4((const sockaddr_in*)adr);
  295. else if ( adr->sa_family == AF_INET6 )
  296. return ShowIP6((const sockaddr_in6*)adr);
  297. else
  298. return "(unsupported sockaddr type)";
  299. }
  300. } // namespace srt
  301. //
  302. void srt::CMD5::compute(const char* input, unsigned char result[16])
  303. {
  304. md5_state_t state;
  305. md5_init(&state);
  306. md5_append(&state, (const md5_byte_t *)input, (int) strlen(input));
  307. md5_finish(&state, result);
  308. }
  309. namespace srt {
  310. std::string MessageTypeStr(UDTMessageType mt, uint32_t extt)
  311. {
  312. using std::string;
  313. static const char* const udt_types [] = {
  314. "handshake",
  315. "keepalive",
  316. "ack",
  317. "lossreport",
  318. "cgwarning", //4
  319. "shutdown",
  320. "ackack",
  321. "dropreq",
  322. "peererror", //8
  323. };
  324. static const char* const srt_types [] = {
  325. "EXT:none",
  326. "EXT:hsreq",
  327. "EXT:hsrsp",
  328. "EXT:kmreq",
  329. "EXT:kmrsp",
  330. "EXT:sid",
  331. "EXT:congctl",
  332. "EXT:filter",
  333. "EXT:group"
  334. };
  335. if ( mt == UMSG_EXT )
  336. {
  337. if ( extt >= Size(srt_types) )
  338. return "EXT:unknown";
  339. return srt_types[extt];
  340. }
  341. if ( size_t(mt) > Size(udt_types) )
  342. return "unknown";
  343. return udt_types[mt];
  344. }
  345. std::string ConnectStatusStr(EConnectStatus cst)
  346. {
  347. return
  348. cst == CONN_CONTINUE ? "INDUCED/CONCLUDING"
  349. : cst == CONN_RUNNING ? "RUNNING"
  350. : cst == CONN_ACCEPT ? "ACCEPTED"
  351. : cst == CONN_RENDEZVOUS ? "RENDEZVOUS (HSv5)"
  352. : cst == CONN_AGAIN ? "AGAIN"
  353. : cst == CONN_CONFUSED ? "MISSING HANDSHAKE"
  354. : "REJECTED";
  355. }
  356. std::string TransmissionEventStr(ETransmissionEvent ev)
  357. {
  358. static const char* const vals [] =
  359. {
  360. "init",
  361. "ack",
  362. "ackack",
  363. "lossreport",
  364. "checktimer",
  365. "send",
  366. "receive",
  367. "custom",
  368. "sync"
  369. };
  370. size_t vals_size = Size(vals);
  371. if (size_t(ev) >= vals_size)
  372. return "UNKNOWN";
  373. return vals[ev];
  374. }
  375. bool SrtParseConfig(const string& s, SrtConfig& w_config)
  376. {
  377. using namespace std;
  378. vector<string> parts;
  379. Split(s, ',', back_inserter(parts));
  380. w_config.type = parts[0];
  381. for (vector<string>::iterator i = parts.begin()+1; i != parts.end(); ++i)
  382. {
  383. vector<string> keyval;
  384. Split(*i, ':', back_inserter(keyval));
  385. if (keyval.size() != 2)
  386. return false;
  387. if (keyval[1] != "")
  388. w_config.parameters[keyval[0]] = keyval[1];
  389. }
  390. return true;
  391. }
  392. } // namespace srt
  393. namespace srt_logging
  394. {
  395. // Value display utilities
  396. // (also useful for applications)
  397. std::string SockStatusStr(SRT_SOCKSTATUS s)
  398. {
  399. if (int(s) < int(SRTS_INIT) || int(s) > int(SRTS_NONEXIST))
  400. return "???";
  401. static struct AutoMap
  402. {
  403. // Values start from 1, so do -1 to avoid empty cell
  404. std::string names[int(SRTS_NONEXIST)-1+1];
  405. AutoMap()
  406. {
  407. #define SINI(statename) names[SRTS_##statename-1] = #statename
  408. SINI(INIT);
  409. SINI(OPENED);
  410. SINI(LISTENING);
  411. SINI(CONNECTING);
  412. SINI(CONNECTED);
  413. SINI(BROKEN);
  414. SINI(CLOSING);
  415. SINI(CLOSED);
  416. SINI(NONEXIST);
  417. #undef SINI
  418. }
  419. } names;
  420. return names.names[int(s)-1];
  421. }
  422. #if ENABLE_BONDING
  423. std::string MemberStatusStr(SRT_MEMBERSTATUS s)
  424. {
  425. if (int(s) < int(SRT_GST_PENDING) || int(s) > int(SRT_GST_BROKEN))
  426. return "???";
  427. static struct AutoMap
  428. {
  429. std::string names[int(SRT_GST_BROKEN)+1];
  430. AutoMap()
  431. {
  432. #define SINI(statename) names[SRT_GST_##statename] = #statename
  433. SINI(PENDING);
  434. SINI(IDLE);
  435. SINI(RUNNING);
  436. SINI(BROKEN);
  437. #undef SINI
  438. }
  439. } names;
  440. return names.names[int(s)];
  441. }
  442. #endif
  443. // Logging system implementation
  444. #if ENABLE_LOGGING
  445. srt::logging::LogDispatcher::Proxy::Proxy(LogDispatcher& guy) : that(guy), that_enabled(that.CheckEnabled())
  446. {
  447. if (that_enabled)
  448. {
  449. i_file = "";
  450. i_line = 0;
  451. flags = that.src_config->flags;
  452. // Create logger prefix
  453. that.CreateLogLinePrefix(os);
  454. }
  455. }
  456. LogDispatcher::Proxy LogDispatcher::operator()()
  457. {
  458. return Proxy(*this);
  459. }
  460. void LogDispatcher::CreateLogLinePrefix(std::ostringstream& serr)
  461. {
  462. using namespace std;
  463. using namespace srt;
  464. SRT_STATIC_ASSERT(ThreadName::BUFSIZE >= sizeof("hh:mm:ss.") * 2, // multiply 2 for some margin
  465. "ThreadName::BUFSIZE is too small to be used for strftime");
  466. char tmp_buf[ThreadName::BUFSIZE];
  467. if ( !isset(SRT_LOGF_DISABLE_TIME) )
  468. {
  469. // Not necessary if sending through the queue.
  470. timeval tv;
  471. gettimeofday(&tv, NULL);
  472. struct tm tm = SysLocalTime((time_t) tv.tv_sec);
  473. if (strftime(tmp_buf, sizeof(tmp_buf), "%X.", &tm))
  474. {
  475. serr << tmp_buf << setw(6) << setfill('0') << tv.tv_usec;
  476. }
  477. }
  478. string out_prefix;
  479. if ( !isset(SRT_LOGF_DISABLE_SEVERITY) )
  480. {
  481. out_prefix = prefix;
  482. }
  483. // Note: ThreadName::get needs a buffer of size min. ThreadName::BUFSIZE
  484. if ( !isset(SRT_LOGF_DISABLE_THREADNAME) && ThreadName::get(tmp_buf) )
  485. {
  486. serr << "/" << tmp_buf << out_prefix << ": ";
  487. }
  488. else
  489. {
  490. serr << out_prefix << ": ";
  491. }
  492. }
  493. std::string LogDispatcher::Proxy::ExtractName(std::string pretty_function)
  494. {
  495. if ( pretty_function == "" )
  496. return "";
  497. size_t pos = pretty_function.find('(');
  498. if ( pos == std::string::npos )
  499. return pretty_function; // return unchanged.
  500. pretty_function = pretty_function.substr(0, pos);
  501. // There are also template instantiations where the instantiating
  502. // parameters are encrypted inside. Therefore, search for the first
  503. // open < and if found, search for symmetric >.
  504. int depth = 1;
  505. pos = pretty_function.find('<');
  506. if ( pos != std::string::npos )
  507. {
  508. size_t end = pos+1;
  509. for(;;)
  510. {
  511. ++pos;
  512. if ( pos == pretty_function.size() )
  513. {
  514. --pos;
  515. break;
  516. }
  517. if ( pretty_function[pos] == '<' )
  518. {
  519. ++depth;
  520. continue;
  521. }
  522. if ( pretty_function[pos] == '>' )
  523. {
  524. --depth;
  525. if ( depth <= 0 )
  526. break;
  527. continue;
  528. }
  529. }
  530. std::string afterpart = pretty_function.substr(pos+1);
  531. pretty_function = pretty_function.substr(0, end) + ">" + afterpart;
  532. }
  533. // Now see how many :: can be found in the name.
  534. // If this occurs more than once, take the last two.
  535. pos = pretty_function.rfind("::");
  536. if ( pos == std::string::npos || pos < 2 )
  537. return pretty_function; // return whatever this is. No scope name.
  538. // Find the next occurrence of :: - if found, copy up to it. If not,
  539. // return whatever is found.
  540. pos -= 2;
  541. pos = pretty_function.rfind("::", pos);
  542. if ( pos == std::string::npos )
  543. return pretty_function; // nothing to cut
  544. return pretty_function.substr(pos+2);
  545. }
  546. #endif
  547. } // (end namespace srt_logging)