candidate.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #include <netdb.h>
  23. #include <sys/socket.h>
  24. #include <sys/types.h>
  25. using std::array;
  26. using std::string;
  27. namespace {
  28. inline bool hasprefix(const string &str, const string &prefix) {
  29. return str.size() >= prefix.size() &&
  30. std::mismatch(prefix.begin(), prefix.end(), str.begin()).first == prefix.end();
  31. }
  32. } // namespace
  33. namespace rtc {
  34. Candidate::Candidate(string candidate, string mid) : mIsResolved(false) {
  35. const std::array prefixes{"a=", "candidate:"};
  36. for (string prefix : prefixes)
  37. if (hasprefix(candidate, prefix))
  38. candidate.erase(0, prefix.size());
  39. mCandidate = std::move(candidate);
  40. mMid = std::move(mid);
  41. }
  42. bool Candidate::resolve(ResolveMode mode) {
  43. if (mIsResolved)
  44. return true;
  45. // See RFC 8445 for format
  46. std::stringstream ss(mCandidate);
  47. int component{0}, priority{0};
  48. string foundation, transport, node, service, typ_, type;
  49. if (ss >> foundation >> component >> transport >> priority &&
  50. ss >> node >> service >> typ_ >> type && typ_ == "typ") {
  51. // Try to resolve the node
  52. struct addrinfo hints = {};
  53. hints.ai_family = AF_UNSPEC;
  54. hints.ai_flags = AI_ADDRCONFIG;
  55. if (transport == "UDP" || transport == "udp") {
  56. hints.ai_socktype = SOCK_DGRAM;
  57. hints.ai_protocol = IPPROTO_UDP;
  58. }
  59. if (transport == "TCP" || transport == "tcp") {
  60. hints.ai_socktype = SOCK_STREAM;
  61. hints.ai_protocol = IPPROTO_TCP;
  62. }
  63. if (mode == ResolveMode::Simple)
  64. hints.ai_flags |= AI_NUMERICHOST;
  65. struct addrinfo *result = nullptr;
  66. if (getaddrinfo(node.c_str(), service.c_str(), &hints, &result) == 0) {
  67. for (auto p = result; p; p = p->ai_next)
  68. if (p->ai_family == AF_INET || p->ai_family == AF_INET6) {
  69. // Rewrite the candidate
  70. char nodebuffer[MAX_NUMERICNODE_LEN];
  71. char servbuffer[MAX_NUMERICSERV_LEN];
  72. if (getnameinfo(p->ai_addr, p->ai_addrlen, nodebuffer, MAX_NUMERICNODE_LEN,
  73. servbuffer, MAX_NUMERICSERV_LEN,
  74. NI_NUMERICHOST | NI_NUMERICSERV) == 0) {
  75. string left;
  76. std::getline(ss, left);
  77. const char sp{' '};
  78. ss.clear();
  79. ss << foundation << sp << component << sp << transport << sp << priority;
  80. ss << sp << nodebuffer << sp << servbuffer << sp << "typ" << sp << type;
  81. if (!left.empty())
  82. ss << left;
  83. mCandidate = ss.str();
  84. return mIsResolved = true;
  85. }
  86. }
  87. }
  88. freeaddrinfo(result);
  89. }
  90. return false;
  91. }
  92. bool Candidate::isResolved() const { return mIsResolved; }
  93. string Candidate::candidate() const { return "candidate:" + mCandidate; }
  94. string Candidate::mid() const { return mMid; }
  95. Candidate::operator string() const {
  96. std::ostringstream line;
  97. line << "a=" << candidate();
  98. return line.str();
  99. }
  100. } // namespace rtc
  101. std::ostream &operator<<(std::ostream &out, const rtc::Candidate &candidate) {
  102. return out << std::string(candidate);
  103. }