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.
5 #include "remoting/client/plugin/pepper_util.h"
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "base/sys_byteorder.h"
10 #include "ppapi/cpp/module.h"
11 #include "ppapi/cpp/net_address.h"
12 #include "third_party/webrtc/base/socketaddress.h"
16 bool SocketAddressToPpNetAddressWithPort(
17 const pp::InstanceHandle
& instance
,
18 const rtc::SocketAddress
& address
,
19 pp::NetAddress
* pp_address
,
21 switch (address
.ipaddr().family()) {
23 in_addr ipv4_addr
= address
.ipaddr().ipv4_address();
24 PP_NetAddress_IPv4 ip_addr
;
25 ip_addr
.port
= base::HostToNet16(port
);
26 memcpy(&ip_addr
.addr
, &ipv4_addr
, sizeof(ip_addr
.addr
));
27 *pp_address
= pp::NetAddress(instance
, ip_addr
);
31 in6_addr ipv6_addr
= address
.ipaddr().ipv6_address();
32 PP_NetAddress_IPv6 ip_addr
;
33 ip_addr
.port
= base::HostToNet16(port
);
34 memcpy(&ip_addr
.addr
, &ipv6_addr
, sizeof(ip_addr
.addr
));
35 *pp_address
= pp::NetAddress(instance
, ip_addr
);
39 LOG(WARNING
) << "Unknown address family: " << address
.ipaddr().family();
45 bool SocketAddressToPpNetAddress(const pp::InstanceHandle
& instance
,
46 const rtc::SocketAddress
& address
,
47 pp::NetAddress
* pp_net_address
) {
48 return SocketAddressToPpNetAddressWithPort(instance
,
54 void PpNetAddressToSocketAddress(const pp::NetAddress
& pp_net_address
,
55 rtc::SocketAddress
* address
) {
56 switch (pp_net_address
.GetFamily()) {
57 case PP_NETADDRESS_FAMILY_IPV4
: {
58 PP_NetAddress_IPv4 ipv4_addr
;
59 CHECK(pp_net_address
.DescribeAsIPv4Address(&ipv4_addr
));
60 address
->SetIP(rtc::IPAddress(
61 bit_cast
<in_addr
>(ipv4_addr
.addr
)));
62 address
->SetPort(base::NetToHost16(ipv4_addr
.port
));
65 case PP_NETADDRESS_FAMILY_IPV6
: {
66 PP_NetAddress_IPv6 ipv6_addr
;
67 CHECK(pp_net_address
.DescribeAsIPv6Address(&ipv6_addr
));
68 address
->SetIP(rtc::IPAddress(
69 bit_cast
<in6_addr
>(ipv6_addr
.addr
)));
70 address
->SetPort(base::NetToHost16(ipv6_addr
.port
));
73 case PP_NETADDRESS_FAMILY_UNSPECIFIED
: {
82 } // namespace remoting