candidate.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #elif __linux__
  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. // See RFC 8445 for format
  51. std::stringstream ss(mCandidate);
  52. int component{0}, priority{0};
  53. string foundation, transport, node, service, typ_, type;
  54. if (ss >> foundation >> component >> transport >> priority &&
  55. ss >> node >> service >> typ_ >> type && typ_ == "typ") {
  56. // Try to resolve the node
  57. struct addrinfo hints = {};
  58. hints.ai_family = AF_UNSPEC;
  59. hints.ai_flags = AI_ADDRCONFIG;
  60. if (transport == "UDP" || transport == "udp") {
  61. hints.ai_socktype = SOCK_DGRAM;
  62. hints.ai_protocol = IPPROTO_UDP;
  63. }
  64. if (transport == "TCP" || transport == "tcp") {
  65. hints.ai_socktype = SOCK_STREAM;
  66. hints.ai_protocol = IPPROTO_TCP;
  67. }
  68. if (mode == ResolveMode::Simple)
  69. hints.ai_flags |= AI_NUMERICHOST;
  70. struct addrinfo *result = nullptr;
  71. if (getaddrinfo(node.c_str(), service.c_str(), &hints, &result) == 0) {
  72. for (auto p = result; p; p = p->ai_next)
  73. if (p->ai_family == AF_INET || p->ai_family == AF_INET6) {
  74. // Rewrite the candidate
  75. char nodebuffer[MAX_NUMERICNODE_LEN];
  76. char servbuffer[MAX_NUMERICSERV_LEN];
  77. if (getnameinfo(p->ai_addr, p->ai_addrlen, nodebuffer, MAX_NUMERICNODE_LEN,
  78. servbuffer, MAX_NUMERICSERV_LEN,
  79. NI_NUMERICHOST | NI_NUMERICSERV) == 0) {
  80. string left;
  81. std::getline(ss, left);
  82. const char sp{' '};
  83. ss.clear();
  84. ss << foundation << sp << component << sp << transport << sp << priority;
  85. ss << sp << nodebuffer << sp << servbuffer << sp << "typ" << sp << type;
  86. if (!left.empty())
  87. ss << left;
  88. mCandidate = ss.str();
  89. return mIsResolved = true;
  90. }
  91. }
  92. }
  93. freeaddrinfo(result);
  94. }
  95. return false;
  96. }
  97. bool Candidate::isResolved() const { return mIsResolved; }
  98. string Candidate::candidate() const { return "candidate:" + mCandidate; }
  99. string Candidate::mid() const { return mMid; }
  100. Candidate::operator string() const {
  101. std::ostringstream line;
  102. line << "a=" << candidate();
  103. return line.str();
  104. }
  105. } // namespace rtc
  106. std::ostream &operator<<(std::ostream &out, const rtc::Candidate &candidate) {
  107. return out << std::string(candidate);
  108. }