OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "jingle/glue/utils.h" | 5 #include "jingle/glue/utils.h" |
6 | 6 |
| 7 #include "base/json/json_reader.h" |
| 8 #include "base/json/json_writer.h" |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/values.h" |
8 #include "net/base/ip_endpoint.h" | 12 #include "net/base/ip_endpoint.h" |
9 #include "net/base/net_util.h" | 13 #include "net/base/net_util.h" |
10 #include "third_party/libjingle/source/talk/base/byteorder.h" | 14 #include "third_party/libjingle/source/talk/base/byteorder.h" |
11 #include "third_party/libjingle/source/talk/base/socketaddress.h" | 15 #include "third_party/libjingle/source/talk/base/socketaddress.h" |
| 16 #include "third_party/libjingle/source/talk/p2p/base/candidate.h" |
12 | 17 |
13 namespace jingle_glue { | 18 namespace jingle_glue { |
14 | 19 |
15 bool IPEndPointToSocketAddress(const net::IPEndPoint& address_chrome, | 20 bool IPEndPointToSocketAddress(const net::IPEndPoint& address_chrome, |
16 talk_base::SocketAddress* address_lj) { | 21 talk_base::SocketAddress* address_lj) { |
17 if (address_chrome.GetFamily() != AF_INET) { | 22 if (address_chrome.GetFamily() != AF_INET) { |
18 LOG(ERROR) << "Only IPv4 addresses are supported."; | 23 LOG(ERROR) << "Only IPv4 addresses are supported."; |
19 return false; | 24 return false; |
20 } | 25 } |
21 uint32 ip_as_int = talk_base::NetworkToHost32( | 26 uint32 ip_as_int = talk_base::NetworkToHost32( |
22 *reinterpret_cast<const uint32*>(&address_chrome.address()[0])); | 27 *reinterpret_cast<const uint32*>(&address_chrome.address()[0])); |
23 *address_lj = talk_base::SocketAddress(ip_as_int, address_chrome.port()); | 28 *address_lj = talk_base::SocketAddress(ip_as_int, address_chrome.port()); |
24 return true; | 29 return true; |
25 } | 30 } |
26 | 31 |
27 bool SocketAddressToIPEndPoint(const talk_base::SocketAddress& address_lj, | 32 bool SocketAddressToIPEndPoint(const talk_base::SocketAddress& address_lj, |
28 net::IPEndPoint* address_chrome) { | 33 net::IPEndPoint* address_chrome) { |
29 uint32 ip = talk_base::HostToNetwork32(address_lj.ip()); | 34 uint32 ip = talk_base::HostToNetwork32(address_lj.ip()); |
30 net::IPAddressNumber address; | 35 net::IPAddressNumber address; |
31 address.resize(net::kIPv4AddressSize); | 36 address.resize(net::kIPv4AddressSize); |
32 memcpy(&address[0], &ip, net::kIPv4AddressSize); | 37 memcpy(&address[0], &ip, net::kIPv4AddressSize); |
33 *address_chrome = net::IPEndPoint(address, address_lj.port()); | 38 *address_chrome = net::IPEndPoint(address, address_lj.port()); |
34 return true; | 39 return true; |
35 } | 40 } |
36 | 41 |
| 42 std::string SerializeP2PCandidate(const cricket::Candidate& candidate) { |
| 43 // TODO(sergeyu): Use SDP to format candidates? |
| 44 DictionaryValue value; |
| 45 value.SetString("name", candidate.name()); |
| 46 value.SetString("ip", candidate.address().IPAsString()); |
| 47 value.SetInteger("port", candidate.address().port()); |
| 48 value.SetString("type", candidate.type()); |
| 49 value.SetString("protocol", candidate.protocol()); |
| 50 value.SetString("username", candidate.username()); |
| 51 value.SetString("password", candidate.password()); |
| 52 value.SetDouble("preference", candidate.preference()); |
| 53 value.SetInteger("generation", candidate.generation()); |
| 54 |
| 55 std::string result; |
| 56 base::JSONWriter::Write(&value, false, &result); |
| 57 return result; |
| 58 } |
| 59 |
| 60 bool DeserializeP2PCandidate(const std::string& candidate_str, |
| 61 cricket::Candidate* candidate) { |
| 62 scoped_ptr<Value> value(base::JSONReader::Read(candidate_str, true)); |
| 63 if (!value.get() || !value->IsType(Value::TYPE_DICTIONARY)) { |
| 64 return false; |
| 65 } |
| 66 |
| 67 DictionaryValue* dic_value = static_cast<DictionaryValue*>(value.get()); |
| 68 |
| 69 std::string name; |
| 70 std::string ip; |
| 71 int port; |
| 72 std::string type; |
| 73 std::string protocol; |
| 74 std::string username; |
| 75 std::string password; |
| 76 double preference; |
| 77 int generation; |
| 78 |
| 79 if (!dic_value->GetString("name", &name) || |
| 80 !dic_value->GetString("ip", &ip) || |
| 81 !dic_value->GetInteger("port", &port) || |
| 82 !dic_value->GetString("type", &type) || |
| 83 !dic_value->GetString("protocol", &protocol) || |
| 84 !dic_value->GetString("username", &username) || |
| 85 !dic_value->GetString("password", &password) || |
| 86 !dic_value->GetDouble("preference", &preference) || |
| 87 !dic_value->GetInteger("generation", &generation)) { |
| 88 return false; |
| 89 } |
| 90 |
| 91 candidate->set_name(name); |
| 92 candidate->set_address(talk_base::SocketAddress(ip, port)); |
| 93 candidate->set_type(type); |
| 94 candidate->set_protocol(protocol); |
| 95 candidate->set_username(username); |
| 96 candidate->set_password(password); |
| 97 candidate->set_preference(static_cast<float>(preference)); |
| 98 candidate->set_generation(generation); |
| 99 |
| 100 return true; |
| 101 } |
| 102 |
37 } // namespace jingle_glue | 103 } // namespace jingle_glue |
OLD | NEW |