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

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

Issue 8357030: Add private Pepper API for dealing with PP_Flash_NetAddress. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: added C++ wrapper for GetAnyAddress 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/c/private/ppb_flash_net_address.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/shared_impl/var.h"
18 #include "ppapi/thunk/thunk.h"
19
20 // The Flash net address interface doesn't have a normal C -> C++ thunk since it
21 // doesn't actually have any proxy wrapping or associated objects; it's just a
22 // call into base. So we implement the entire interface here, using the thunk
23 // namespace so it magically gets hooked up in the proper places.
24
25 namespace ppapi {
26
27 namespace {
28
29 #if defined(OS_WIN)
30 // The type of |sockaddr::sa_family|.
31 typedef ADDRESS_FAMILY sa_family_t;
32 #endif
33
34 inline sa_family_t GetFamily(const PP_Flash_NetAddress& addr) {
yzshen1 2011/11/07 21:57:44 It might be better to: (1) put all code manipulati
viettrungluu 2011/11/07 23:33:37 I've added a ppapi/shared_impl/private/flash_net_a
35 return reinterpret_cast<const sockaddr*>(addr.data)->sa_family;
36 }
37
38 // TODO(viettrungluu): adapted from elsewhere; remove duplication.
39 bool ValidateNetAddress(const PP_Flash_NetAddress& addr) {
40 if (addr.size < sizeof(reinterpret_cast<sockaddr*>(0)->sa_family))
41 return false;
42
43 // TODO(viettrungluu): more careful validation?
44 // Just do a size check for AF_INET.
45 if (GetFamily(addr) == AF_INET && addr.size >= sizeof(sockaddr_in))
46 return true;
47
48 // Ditto for AF_INET6.
49 if (GetFamily(addr) == AF_INET6 && addr.size >= sizeof(sockaddr_in6))
50 return true;
51
52 // Reject everything else.
53 return false;
54 }
55
56 PP_Bool AreHostsEqual(const PP_Flash_NetAddress* addr1,
57 const PP_Flash_NetAddress* addr2) {
58 if (!ValidateNetAddress(*addr1) || !ValidateNetAddress(*addr2))
59 return PP_FALSE;
60
61 if (GetFamily(*addr1) != GetFamily(*addr2))
62 return PP_FALSE;
63
64 if (GetFamily(*addr1) == AF_INET) {
65 const sockaddr_in* a1 = reinterpret_cast<const sockaddr_in*>(addr1->data);
66 const sockaddr_in* a2 = reinterpret_cast<const sockaddr_in*>(addr2->data);
67 return PP_FromBool(a1->sin_addr.s_addr == a2->sin_addr.s_addr);
68 }
69
70 if (GetFamily(*addr1) == AF_INET6) {
71 const sockaddr_in6* a1 = reinterpret_cast<const sockaddr_in6*>(addr1->data);
72 const sockaddr_in6* a2 = reinterpret_cast<const sockaddr_in6*>(addr2->data);
73 return PP_FromBool(a1->sin6_flowinfo == a2->sin6_flowinfo &&
74 memcmp(a1->sin6_addr.s6_addr, a2->sin6_addr.s6_addr,
yzshen1 2011/11/07 21:57:44 I think on Windows the sin6_addr is of the follow
viettrungluu 2011/11/07 23:33:37 Fixed. (I agree that that's what the documentatio
75 sizeof(a1->sin6_addr.s6_addr)) == 0 &&
76 a1->sin6_scope_id == a2->sin6_scope_id);
77 }
78
79 return PP_FALSE;
80 }
81
82 PP_Bool AreEqual(const PP_Flash_NetAddress* addr1,
83 const PP_Flash_NetAddress* addr2) {
84 // |AreHostsEqual()| will also validate the addresses and return false if
85 // either is invalid.
86 if (!AreHostsEqual(addr1, addr2))
87 return PP_FALSE;
88
89 // Note: Here, we know that |addr1| and |addr2| have the same family.
90 if (GetFamily(*addr1) == AF_INET) {
91 const sockaddr_in* a1 = reinterpret_cast<const sockaddr_in*>(addr1->data);
92 const sockaddr_in* a2 = reinterpret_cast<const sockaddr_in*>(addr2->data);
93 return PP_FromBool(a1->sin_port == a2->sin_port);
94 }
95
96 if (GetFamily(*addr1) == AF_INET6) {
97 const sockaddr_in6* a1 = reinterpret_cast<const sockaddr_in6*>(addr1->data);
98 const sockaddr_in6* a2 = reinterpret_cast<const sockaddr_in6*>(addr2->data);
99 return PP_FromBool(a1->sin6_port == a2->sin6_port);
100 }
101
102 return PP_FALSE;
103 }
104
105 PP_Var Describe(PP_Module module,
106 const struct PP_Flash_NetAddress* addr,
107 PP_Bool include_port) {
108 if (!ValidateNetAddress(*addr))
109 return PP_MakeUndefined();
110
111 const sockaddr* a = reinterpret_cast<const sockaddr*>(addr->data);
112 socklen_t l = addr->size;
113 std::string description =
114 include_port ? net::NetAddressToStringWithPort(a, l) :
115 net::NetAddressToString(a, l);
116 return StringVar::StringToPPVar(module, description);
117 }
118
119 PP_Bool ReplacePort(const struct PP_Flash_NetAddress* src_addr,
120 uint16_t port,
121 struct PP_Flash_NetAddress* dest_addr) {
122 if (!ValidateNetAddress(*src_addr))
123 return PP_FALSE;
124
125 if (GetFamily(*src_addr) == AF_INET) {
126 memmove(dest_addr, src_addr, sizeof(*src_addr));
127 reinterpret_cast<sockaddr_in*>(dest_addr->data)->sin_port = htons(port);
128 return PP_TRUE;
129 }
130
131 if (GetFamily(*src_addr) == AF_INET6) {
132 memmove(dest_addr, src_addr, sizeof(*src_addr));
133 reinterpret_cast<sockaddr_in6*>(dest_addr->data)->sin6_port = htons(port);
134 return PP_TRUE;
135 }
136
137 return PP_FALSE;
138 }
139
140 void GetAnyAddress(PP_Bool is_ipv6, struct PP_Flash_NetAddress* addr) {
141 memset(addr->data, 0, arraysize(addr->data) * sizeof(addr->data[0]));
142 if (is_ipv6) {
143 sockaddr_in6* a = reinterpret_cast<sockaddr_in6*>(addr->data);
144 addr->size = sizeof(*a);
145 a->sin6_family = AF_INET6;
146 a->sin6_addr = in6addr_any;
147 } else {
148 sockaddr_in* a = reinterpret_cast<sockaddr_in*>(addr->data);
149 addr->size = sizeof(*a);
150 a->sin_family = AF_INET;
151 a->sin_addr.s_addr = INADDR_ANY;
152 }
153 }
154
155 const PPB_Flash_NetAddress flash_net_address_interface = {
156 &AreEqual,
157 &AreHostsEqual,
158 &Describe,
159 &ReplacePort,
160 &GetAnyAddress
161 };
162
163 } // namespace
164
165 namespace thunk {
166
167 PPAPI_THUNK_EXPORT const PPB_Flash_NetAddress* GetPPB_Flash_NetAddress_Thunk() {
168 return &flash_net_address_interface;
169 }
170
171 } // namespace thunk
172
173 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698