utils.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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
5#include "jingle/glue/utils.h"
6
7#include "base/json/json_reader.h"
8#include "base/json/json_writer.h"
9#include "base/logging.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/values.h"
12#include "net/base/ip_endpoint.h"
13#include "net/base/net_util.h"
14#include "third_party/libjingle/source/talk/base/byteorder.h"
15#include "third_party/libjingle/source/talk/base/socketaddress.h"
16#include "third_party/libjingle/source/talk/p2p/base/candidate.h"
17
18namespace jingle_glue {
19
20bool IPEndPointToSocketAddress(const net::IPEndPoint& address_chrome,
21                               talk_base::SocketAddress* address_lj) {
22  if (address_chrome.GetFamily() != AF_INET) {
23    LOG(ERROR) << "Only IPv4 addresses are supported.";
24    return false;
25  }
26  uint32 ip_as_int = talk_base::NetworkToHost32(
27      *reinterpret_cast<const uint32*>(&address_chrome.address()[0]));
28  *address_lj =  talk_base::SocketAddress(ip_as_int, address_chrome.port());
29  return true;
30}
31
32bool SocketAddressToIPEndPoint(const talk_base::SocketAddress& address_lj,
33                               net::IPEndPoint* address_chrome) {
34  uint32 ip = talk_base::HostToNetwork32(address_lj.ip());
35  net::IPAddressNumber address;
36  address.resize(net::kIPv4AddressSize);
37  memcpy(&address[0], &ip, net::kIPv4AddressSize);
38  *address_chrome = net::IPEndPoint(address, address_lj.port());
39  return true;
40}
41
42std::string SerializeP2PCandidate(const cricket::Candidate& candidate) {
43  // TODO(sergeyu): Use SDP to format candidates?
44  DictionaryValue value;
45  value.SetString("ip", candidate.address().ipaddr().ToString());
46  value.SetInteger("port", candidate.address().port());
47  value.SetString("type", candidate.type());
48  value.SetString("protocol", candidate.protocol());
49  value.SetString("username", candidate.username());
50  value.SetString("password", candidate.password());
51  value.SetDouble("preference", candidate.preference());
52  value.SetInteger("generation", candidate.generation());
53
54  std::string result;
55  base::JSONWriter::Write(&value, &result);
56  return result;
57}
58
59bool DeserializeP2PCandidate(const std::string& candidate_str,
60                             cricket::Candidate* candidate) {
61  scoped_ptr<Value> value(
62      base::JSONReader::Read(candidate_str, base::JSON_ALLOW_TRAILING_COMMAS));
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 ip;
70  int port;
71  std::string type;
72  std::string protocol;
73  std::string username;
74  std::string password;
75  double preference;
76  int generation;
77
78  if (!dic_value->GetString("ip", &ip) ||
79      !dic_value->GetInteger("port", &port) ||
80      !dic_value->GetString("type", &type) ||
81      !dic_value->GetString("protocol", &protocol) ||
82      !dic_value->GetString("username", &username) ||
83      !dic_value->GetString("password", &password) ||
84      !dic_value->GetDouble("preference", &preference) ||
85      !dic_value->GetInteger("generation", &generation)) {
86    return false;
87  }
88
89  candidate->set_address(talk_base::SocketAddress(ip, port));
90  candidate->set_type(type);
91  candidate->set_protocol(protocol);
92  candidate->set_username(username);
93  candidate->set_password(password);
94  candidate->set_preference(static_cast<float>(preference));
95  candidate->set_generation(generation);
96
97  return true;
98}
99
100}  // namespace jingle_glue
101