net_address_utils.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/webrtc/net_address_utils.h"
  5. #include <stdint.h>
  6. #include <memory>
  7. #include "base/json/json_reader.h"
  8. #include "base/json/json_writer.h"
  9. #include "base/values.h"
  10. #include "net/base/ip_address.h"
  11. #include "net/base/ip_endpoint.h"
  12. #include "third_party/webrtc/api/candidate.h"
  13. #include "third_party/webrtc/rtc_base/byte_order.h"
  14. #include "third_party/webrtc/rtc_base/socket_address.h"
  15. namespace webrtc {
  16. bool IPEndPointToSocketAddress(const net::IPEndPoint& ip_endpoint,
  17. rtc::SocketAddress* address) {
  18. sockaddr_storage addr;
  19. socklen_t len = sizeof(addr);
  20. return ip_endpoint.ToSockAddr(reinterpret_cast<sockaddr*>(&addr), &len) &&
  21. rtc::SocketAddressFromSockAddrStorage(addr, address);
  22. }
  23. bool SocketAddressToIPEndPoint(const rtc::SocketAddress& address,
  24. net::IPEndPoint* ip_endpoint) {
  25. sockaddr_storage addr;
  26. int size = address.ToSockAddrStorage(&addr);
  27. return (size > 0) &&
  28. ip_endpoint->FromSockAddr(reinterpret_cast<sockaddr*>(&addr), size);
  29. }
  30. rtc::IPAddress NetIPAddressToRtcIPAddress(const net::IPAddress& ip_address) {
  31. if (ip_address.IsIPv4()) {
  32. uint32_t address;
  33. memcpy(&address, ip_address.bytes().data(), sizeof(uint32_t));
  34. address = rtc::NetworkToHost32(address);
  35. return rtc::IPAddress(address);
  36. }
  37. if (ip_address.IsIPv6()) {
  38. in6_addr address;
  39. memcpy(&address, ip_address.bytes().data(), sizeof(in6_addr));
  40. return rtc::IPAddress(address);
  41. }
  42. return rtc::IPAddress();
  43. }
  44. net::IPAddress RtcIPAddressToNetIPAddress(const rtc::IPAddress& ip_address) {
  45. rtc::SocketAddress socket_address(ip_address, 0);
  46. net::IPEndPoint ip_endpoint;
  47. webrtc::SocketAddressToIPEndPoint(socket_address, &ip_endpoint);
  48. return ip_endpoint.address();
  49. }
  50. std::string SerializeP2PCandidate(const cricket::Candidate& candidate) {
  51. // TODO(sergeyu): Use SDP to format candidates?
  52. base::Value::Dict value;
  53. value.Set("ip", candidate.address().ipaddr().ToString());
  54. value.Set("port", candidate.address().port());
  55. value.Set("type", candidate.type());
  56. value.Set("protocol", candidate.protocol());
  57. value.Set("username", candidate.username());
  58. value.Set("password", candidate.password());
  59. value.Set("preference", candidate.preference());
  60. value.Set("generation", static_cast<int>(candidate.generation()));
  61. std::string result;
  62. base::JSONWriter::Write(value, &result);
  63. return result;
  64. }
  65. bool DeserializeP2PCandidate(const std::string& candidate_str,
  66. cricket::Candidate* candidate) {
  67. absl::optional<base::Value> value(
  68. base::JSONReader::Read(candidate_str, base::JSON_ALLOW_TRAILING_COMMAS));
  69. if (!value || !value->is_dict()) {
  70. return false;
  71. }
  72. base::Value::Dict& dic_value = value->GetDict();
  73. std::string* ip = dic_value.FindString("ip");
  74. absl::optional<int> port = dic_value.FindInt("port");
  75. std::string* type = dic_value.FindString("type");
  76. std::string* protocol = dic_value.FindString("protocol");
  77. std::string* username = dic_value.FindString("username");
  78. std::string* password = dic_value.FindString("password");
  79. absl::optional<double> preference = dic_value.FindDouble("preference");
  80. absl::optional<int> generation = dic_value.FindInt("generation");
  81. if (!ip || !port || !type || !protocol || !username || !password ||
  82. !preference || !generation) {
  83. return false;
  84. }
  85. candidate->set_address(rtc::SocketAddress(std::move(*ip), *port));
  86. candidate->set_type(std::move(*type));
  87. candidate->set_protocol(std::move(*protocol));
  88. candidate->set_username(std::move(*username));
  89. candidate->set_password(std::move(*password));
  90. candidate->set_preference(static_cast<float>(*preference));
  91. candidate->set_generation(*generation);
  92. return true;
  93. }
  94. } // namespace webrtc