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