Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: ppapi/shared_impl/private/flash_net_address_impl.cc

Issue 8511032: Make the Pepper Flash net address just private, not Flash-specific. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ppapi/shared_impl/private/flash_net_address_impl.h"
6
7 #include <string.h>
8
9 #include <string>
10
11 #include "base/basictypes.h"
12 #include "build/build_config.h"
13 #include "net/base/net_util.h"
14 #include "net/base/sys_addrinfo.h"
15 #include "net/base/sys_byteorder.h"
16 #include "ppapi/c/pp_var.h"
17 #include "ppapi/c/private/ppb_flash_net_address.h"
18 #include "ppapi/shared_impl/var.h"
19 #include "ppapi/thunk/thunk.h"
20
21 // The Flash net address interface doesn't have a normal C -> C++ thunk since it
22 // doesn't actually have any proxy wrapping or associated objects; it's just a
23 // call into base. So we implement the entire interface here, using the thunk
24 // namespace so it magically gets hooked up in the proper places.
25
26 namespace ppapi {
27
28 namespace {
29
30 #if defined(OS_WIN)
31 // The type of |sockaddr::sa_family|.
32 typedef ADDRESS_FAMILY sa_family_t;
33 #endif
34
35 inline sa_family_t GetFamily(const PP_Flash_NetAddress& addr) {
36 return reinterpret_cast<const sockaddr*>(addr.data)->sa_family;
37 }
38
39 PP_Bool AreHostsEqual(const PP_Flash_NetAddress* addr1,
40 const PP_Flash_NetAddress* addr2) {
41 if (!FlashNetAddressImpl::ValidateNetAddress(*addr1) ||
42 !FlashNetAddressImpl::ValidateNetAddress(*addr2))
43 return PP_FALSE;
44
45 if (GetFamily(*addr1) != GetFamily(*addr2))
46 return PP_FALSE;
47
48 if (GetFamily(*addr1) == AF_INET) {
49 const sockaddr_in* a1 = reinterpret_cast<const sockaddr_in*>(addr1->data);
50 const sockaddr_in* a2 = reinterpret_cast<const sockaddr_in*>(addr2->data);
51 return PP_FromBool(a1->sin_addr.s_addr == a2->sin_addr.s_addr);
52 }
53
54 if (GetFamily(*addr1) == AF_INET6) {
55 const sockaddr_in6* a1 = reinterpret_cast<const sockaddr_in6*>(addr1->data);
56 const sockaddr_in6* a2 = reinterpret_cast<const sockaddr_in6*>(addr2->data);
57 return PP_FromBool(a1->sin6_flowinfo == a2->sin6_flowinfo &&
58 memcmp(&a1->sin6_addr, &a2->sin6_addr,
59 sizeof(a1->sin6_addr)) == 0 &&
60 a1->sin6_scope_id == a2->sin6_scope_id);
61 }
62
63 return PP_FALSE;
64 }
65
66 PP_Bool AreEqual(const PP_Flash_NetAddress* addr1,
67 const PP_Flash_NetAddress* addr2) {
68 // |AreHostsEqual()| will also validate the addresses and return false if
69 // either is invalid.
70 if (!AreHostsEqual(addr1, addr2))
71 return PP_FALSE;
72
73 // Note: Here, we know that |addr1| and |addr2| have the same family.
74 if (GetFamily(*addr1) == AF_INET) {
75 const sockaddr_in* a1 = reinterpret_cast<const sockaddr_in*>(addr1->data);
76 const sockaddr_in* a2 = reinterpret_cast<const sockaddr_in*>(addr2->data);
77 return PP_FromBool(a1->sin_port == a2->sin_port);
78 }
79
80 if (GetFamily(*addr1) == AF_INET6) {
81 const sockaddr_in6* a1 = reinterpret_cast<const sockaddr_in6*>(addr1->data);
82 const sockaddr_in6* a2 = reinterpret_cast<const sockaddr_in6*>(addr2->data);
83 return PP_FromBool(a1->sin6_port == a2->sin6_port);
84 }
85
86 return PP_FALSE;
87 }
88
89 PP_Var Describe(PP_Module module,
90 const struct PP_Flash_NetAddress* addr,
91 PP_Bool include_port) {
92 if (!FlashNetAddressImpl::ValidateNetAddress(*addr))
93 return PP_MakeUndefined();
94
95 const sockaddr* a = reinterpret_cast<const sockaddr*>(addr->data);
96 socklen_t l = addr->size;
97 std::string description =
98 include_port ? net::NetAddressToStringWithPort(a, l) :
99 net::NetAddressToString(a, l);
100 return StringVar::StringToPPVar(module, description);
101 }
102
103 PP_Bool ReplacePort(const struct PP_Flash_NetAddress* src_addr,
104 uint16_t port,
105 struct PP_Flash_NetAddress* dest_addr) {
106 if (!FlashNetAddressImpl::ValidateNetAddress(*src_addr))
107 return PP_FALSE;
108
109 if (GetFamily(*src_addr) == AF_INET) {
110 memmove(dest_addr, src_addr, sizeof(*src_addr));
111 reinterpret_cast<sockaddr_in*>(dest_addr->data)->sin_port = htons(port);
112 return PP_TRUE;
113 }
114
115 if (GetFamily(*src_addr) == AF_INET6) {
116 memmove(dest_addr, src_addr, sizeof(*src_addr));
117 reinterpret_cast<sockaddr_in6*>(dest_addr->data)->sin6_port = htons(port);
118 return PP_TRUE;
119 }
120
121 return PP_FALSE;
122 }
123
124 void GetAnyAddress(PP_Bool is_ipv6, struct PP_Flash_NetAddress* addr) {
125 memset(addr->data, 0, arraysize(addr->data) * sizeof(addr->data[0]));
126 if (is_ipv6) {
127 sockaddr_in6* a = reinterpret_cast<sockaddr_in6*>(addr->data);
128 addr->size = sizeof(*a);
129 a->sin6_family = AF_INET6;
130 a->sin6_addr = in6addr_any;
131 } else {
132 sockaddr_in* a = reinterpret_cast<sockaddr_in*>(addr->data);
133 addr->size = sizeof(*a);
134 a->sin_family = AF_INET;
135 a->sin_addr.s_addr = INADDR_ANY;
136 }
137 }
138
139 const PPB_Flash_NetAddress flash_net_address_interface = {
140 &AreEqual,
141 &AreHostsEqual,
142 &Describe,
143 &ReplacePort,
144 &GetAnyAddress
145 };
146
147 } // namespace
148
149 namespace thunk {
150
151 PPAPI_THUNK_EXPORT const PPB_Flash_NetAddress* GetPPB_Flash_NetAddress_Thunk() {
152 return &flash_net_address_interface;
153 }
154
155 } // namespace thunk
156
157 // static
158 bool FlashNetAddressImpl::ValidateNetAddress(const PP_Flash_NetAddress& addr) {
159 if (addr.size < sizeof(reinterpret_cast<sockaddr*>(0)->sa_family))
160 return false;
161
162 // TODO(viettrungluu): more careful validation?
163 // Just do a size check for AF_INET.
164 if (GetFamily(addr) == AF_INET && addr.size >= sizeof(sockaddr_in))
165 return true;
166
167 // Ditto for AF_INET6.
168 if (GetFamily(addr) == AF_INET6 && addr.size >= sizeof(sockaddr_in6))
169 return true;
170
171 // Reject everything else.
172 return false;
173 }
174
175 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698