candidate.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "candidate.hpp"
  19. #include <algorithm>
  20. #include <array>
  21. #include <sstream>
  22. #ifdef _WIN32
  23. #include <winsock2.h>
  24. #include <ws2tcpip.h>
  25. #else
  26. #include <netdb.h>
  27. #include <sys/socket.h>
  28. #endif
  29. #include <sys/types.h>
  30. using std::array;
  31. using std::string;
  32. namespace {
  33. inline bool hasprefix(const string &str, const string &prefix) {
  34. return str.size() >= prefix.size() &&
  35. std::mismatch(prefix.begin(), prefix.end(), str.begin()).first == prefix.end();
  36. }
  37. } // namespace
  38. namespace rtc {
  39. Candidate::Candidate(string candidate, string mid) : mIsResolved(false) {
  40. const std::array prefixes{"a=", "candidate:"};
  41. for (string prefix : prefixes)
  42. if (hasprefix(candidate, prefix))
  43. candidate.erase(0, prefix.size());
  44. mCandidate = std::move(candidate);
  45. mMid = std::move(mid);
  46. }
  47. bool Candidate::resolve(ResolveMode mode) {
  48. if (mIsResolved)
  49. return true;
  50. PLOG_VERBOSE << "Resolving candidate (mode="
  51. << (mode == ResolveMode::Simple ? "simple" : "lookup")
  52. << "): " << mCandidate;
  53. // See RFC 8445 for format
  54. std::istringstream iss(mCandidate);
  55. int component{0}, priority{0};
  56. string foundation, transport, node, service, typ_, type;
  57. if (iss >> foundation >> component >> transport >> priority &&
  58. iss >> node >> service >> typ_ >> type && typ_ == "typ") {
  59. string left;
  60. std::getline(iss, left);
  61. // Try to resolve the node
  62. struct addrinfo hints = {};
  63. hints.ai_family = AF_UNSPEC;
  64. hints.ai_flags = AI_ADDRCONFIG;
  65. if (transport == "UDP" || transport == "udp") {
  66. hints.ai_socktype = SOCK_DGRAM;
  67. hints.ai_protocol = IPPROTO_UDP;
  68. }
  69. if (transport == "TCP" || transport == "tcp") {
  70. hints.ai_socktype = SOCK_STREAM;
  71. hints.ai_protocol = IPPROTO_TCP;
  72. }
  73. if (mode == ResolveMode::Simple)
  74. hints.ai_flags |= AI_NUMERICHOST;
  75. struct addrinfo *result = nullptr;
  76. if (getaddrinfo(node.c_str(), service.c_str(), &hints, &result) == 0) {
  77. for (auto p = result; p; p = p->ai_next)
  78. if (p->ai_family == AF_INET || p->ai_family == AF_INET6) {
  79. // Rewrite the candidate
  80. char nodebuffer[MAX_NUMERICNODE_LEN];
  81. char servbuffer[MAX_NUMERICSERV_LEN];
  82. if (getnameinfo(p->ai_addr, socklen_t(p->ai_addrlen), nodebuffer,
  83. MAX_NUMERICNODE_LEN, servbuffer, MAX_NUMERICSERV_LEN,
  84. NI_NUMERICHOST | NI_NUMERICSERV) == 0) {
  85. const char sp{' '};
  86. std::ostringstream oss;
  87. oss << foundation << sp << component << sp << transport << sp << priority;
  88. oss << sp << nodebuffer << sp << servbuffer << sp << "typ" << sp << type;
  89. oss << left;
  90. mCandidate = oss.str();
  91. PLOG_VERBOSE << "Resolved candidate: " << mCandidate;
  92. return mIsResolved = true;
  93. }
  94. }
  95. }
  96. freeaddrinfo(result);
  97. }
  98. return false;
  99. }
  100. bool Candidate::isResolved() const { return mIsResolved; }
  101. string Candidate::candidate() const { return "candidate:" + mCandidate; }
  102. string Candidate::mid() const { return mMid; }
  103. Candidate::operator string() const {
  104. std::ostringstream line;
  105. line << "a=" << candidate();
  106. return line.str();
  107. }
  108. } // namespace rtc
  109. std::ostream &operator<<(std::ostream &out, const rtc::Candidate &candidate) {
  110. return out << std::string(candidate);
  111. }
  112. std::ostream &operator<<(std::ostream &out, const rtc::CandidateType &type) {
  113. switch (type) {
  114. case rtc::CandidateType::Host:
  115. return out << "Host";
  116. case rtc::CandidateType::PeerReflexive:
  117. return out << "PeerReflexive";
  118. case rtc::CandidateType::Relayed:
  119. return out << "Relayed";
  120. case rtc::CandidateType::ServerReflexive:
  121. return out << "ServerReflexive";
  122. default:
  123. return out << "Unknown";
  124. }
  125. }
  126. std::ostream &operator<<(std::ostream &out, const rtc::CandidateTransportType &transportType) {
  127. switch (transportType) {
  128. case rtc::CandidateTransportType::TcpActive:
  129. return out << "TcpActive";
  130. case rtc::CandidateTransportType::TcpPassive:
  131. return out << "TcpPassive";
  132. case rtc::CandidateTransportType::TcpSo:
  133. return out << "TcpSo";
  134. case rtc::CandidateTransportType::Udp:
  135. return out << "Udp";
  136. default:
  137. return out << "Unknown";
  138. }
  139. }