Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "nacl_io/host_resolver.h" | 5 #include "nacl_io/host_resolver.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "nacl_io/kernel_proxy.h" | 10 #include "nacl_io/kernel_proxy.h" |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 } | 27 } |
| 28 | 28 |
| 29 struct hostent* HostResolver::gethostbyname(const char* name) { | 29 struct hostent* HostResolver::gethostbyname(const char* name) { |
| 30 h_errno = NETDB_INTERNAL; | 30 h_errno = NETDB_INTERNAL; |
| 31 | 31 |
| 32 if (NULL == ppapi_) | 32 if (NULL == ppapi_) |
| 33 return NULL; | 33 return NULL; |
| 34 | 34 |
| 35 HostResolverInterface* resolver_interface = | 35 HostResolverInterface* resolver_interface = |
| 36 ppapi_->GetHostResolverInterface(); | 36 ppapi_->GetHostResolverInterface(); |
| 37 | |
| 38 if (NULL == resolver_interface) | |
| 39 return NULL; | |
| 40 | |
| 37 ScopedResource resolver(ppapi_, | 41 ScopedResource resolver(ppapi_, |
| 38 resolver_interface->Create(ppapi_->GetInstance())); | 42 resolver_interface->Create(ppapi_->GetInstance())); |
| 39 | 43 |
| 40 struct PP_CompletionCallback callback; | 44 struct PP_CompletionCallback callback; |
| 41 callback.func = NULL; | 45 callback.func = NULL; |
| 42 struct PP_HostResolver_Hint hint; | 46 struct PP_HostResolver_Hint hint; |
| 43 hint.family = PP_NETADDRESS_FAMILY_IPV4; | 47 hint.family = PP_NETADDRESS_FAMILY_IPV4; |
| 44 hint.flags = PP_HOSTRESOLVER_FLAG_CANONNAME; | 48 hint.flags = PP_HOSTRESOLVER_FLAG_CANONNAME; |
| 45 | 49 |
| 46 int err = resolver_interface->Resolve(resolver.pp_resource(), | 50 int err = resolver_interface->Resolve(resolver.pp_resource(), |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 // get an empty string we copy over the input string instead. | 85 // get an empty string we copy over the input string instead. |
| 82 len = strlen(name); | 86 len = strlen(name); |
| 83 name_ptr = name; | 87 name_ptr = name; |
| 84 } | 88 } |
| 85 hostent_.h_name = static_cast<char*>(malloc(len + 1)); | 89 hostent_.h_name = static_cast<char*>(malloc(len + 1)); |
| 86 if (NULL == hostent_.h_name) | 90 if (NULL == hostent_.h_name) |
| 87 return NULL; | 91 return NULL; |
| 88 memcpy(hostent_.h_name, name_ptr, len); | 92 memcpy(hostent_.h_name, name_ptr, len); |
| 89 hostent_.h_name[len] = '\0'; | 93 hostent_.h_name[len] = '\0'; |
| 90 | 94 |
| 95 ppapi_->GetVarInterface()->Release(name_var); | |
| 96 | |
| 91 // Aliases aren't supported at the moment, so we just make an empty list. | 97 // Aliases aren't supported at the moment, so we just make an empty list. |
| 92 hostent_.h_aliases = static_cast<char**>(malloc(sizeof(char*))); | 98 hostent_.h_aliases = static_cast<char**>(malloc(sizeof(char*))); |
| 93 if (NULL == hostent_.h_aliases) | 99 if (NULL == hostent_.h_aliases) |
| 94 return NULL; | 100 return NULL; |
| 95 hostent_.h_aliases[0] = NULL; | 101 hostent_.h_aliases[0] = NULL; |
| 96 | 102 |
| 97 NetAddressInterface* netaddr_interface = ppapi_->GetNetAddressInterface(); | 103 NetAddressInterface* netaddr_interface = ppapi_->GetNetAddressInterface(); |
| 98 PP_Resource addr = | 104 PP_Resource addr = |
| 99 resolver_interface->GetNetAddress(resolver.pp_resource(), 0); | 105 resolver_interface->GetNetAddress(resolver.pp_resource(), 0); |
| 100 if (!PP_ToBool(netaddr_interface->IsNetAddress(addr))) | 106 if (!PP_ToBool(netaddr_interface->IsNetAddress(addr))) { |
| 107 ppapi_->ReleaseResource(addr); | |
| 101 return NULL; | 108 return NULL; |
| 109 } | |
| 102 | 110 |
| 103 switch (netaddr_interface->GetFamily(addr)) { | 111 switch (netaddr_interface->GetFamily(addr)) { |
| 104 case PP_NETADDRESS_FAMILY_IPV4: | 112 case PP_NETADDRESS_FAMILY_IPV4: |
| 105 hostent_.h_addrtype = AF_INET; | 113 hostent_.h_addrtype = AF_INET; |
| 106 hostent_.h_length = 4; | 114 hostent_.h_length = 4; |
| 107 break; | 115 break; |
| 108 case PP_NETADDRESS_FAMILY_IPV6: | 116 case PP_NETADDRESS_FAMILY_IPV6: |
| 109 hostent_.h_addrtype = AF_INET6; | 117 hostent_.h_addrtype = AF_INET6; |
| 110 hostent_.h_length = 16; | 118 hostent_.h_length = 16; |
| 111 break; | 119 break; |
| 112 default: | 120 default: |
| 121 ppapi_->ReleaseResource(addr); | |
| 113 return NULL; | 122 return NULL; |
| 114 } | 123 } |
| 115 | 124 |
| 125 ppapi_->ReleaseResource(addr); | |
| 126 | |
| 116 const uint32_t num_addresses = | 127 const uint32_t num_addresses = |
| 117 resolver_interface->GetNetAddressCount(resolver.pp_resource()); | 128 resolver_interface->GetNetAddressCount(resolver.pp_resource()); |
| 118 if (0 == num_addresses) | 129 if (0 == num_addresses) |
| 119 return NULL; | 130 return NULL; |
| 120 hostent_.h_addr_list = | 131 hostent_.h_addr_list = |
| 121 static_cast<char**>(calloc(num_addresses + 1, sizeof(char*))); | 132 static_cast<char**>(calloc(num_addresses + 1, sizeof(char*))); |
| 122 if (NULL == hostent_.h_addr_list) | 133 if (NULL == hostent_.h_addr_list) |
| 123 return NULL; | 134 return NULL; |
| 124 | 135 |
| 125 for (uint32_t i = 0; i < num_addresses; i++) { | 136 for (uint32_t i = 0; i < num_addresses; i++) { |
| 126 PP_Resource addr = | 137 PP_Resource addr = |
| 127 resolver_interface->GetNetAddress(resolver.pp_resource(), i); | 138 resolver_interface->GetNetAddress(resolver.pp_resource(), i); |
| 128 if (!PP_ToBool(netaddr_interface->IsNetAddress(addr))) | 139 if (!PP_ToBool(netaddr_interface->IsNetAddress(addr))) { |
| 140 ppapi_->ReleaseResource(addr); | |
|
binji
2014/01/28 18:13:22
You can use a ScopedResource (see pepper_interface
Sam Clegg
2014/01/28 19:55:22
Done.
| |
| 129 return NULL; | 141 return NULL; |
| 142 } | |
| 130 if (AF_INET == hostent_.h_addrtype) { | 143 if (AF_INET == hostent_.h_addrtype) { |
| 131 struct PP_NetAddress_IPv4 addr_struct; | 144 struct PP_NetAddress_IPv4 addr_struct; |
| 132 if (!netaddr_interface->DescribeAsIPv4Address(addr, &addr_struct)) { | 145 if (!netaddr_interface->DescribeAsIPv4Address(addr, &addr_struct)) { |
| 146 ppapi_->ReleaseResource(addr); | |
| 133 return NULL; | 147 return NULL; |
| 134 } | 148 } |
| 135 hostent_.h_addr_list[i] = static_cast<char*>(malloc(hostent_.h_length)); | 149 hostent_.h_addr_list[i] = static_cast<char*>(malloc(hostent_.h_length)); |
| 136 if (NULL == hostent_.h_addr_list[i]) | 150 if (NULL == hostent_.h_addr_list[i]) { |
| 151 ppapi_->ReleaseResource(addr); | |
| 137 return NULL; | 152 return NULL; |
| 153 } | |
| 138 memcpy(hostent_.h_addr_list[i], addr_struct.addr, hostent_.h_length); | 154 memcpy(hostent_.h_addr_list[i], addr_struct.addr, hostent_.h_length); |
| 139 } else { // IPv6 | 155 } else { // IPv6 |
| 140 struct PP_NetAddress_IPv6 addr_struct; | 156 struct PP_NetAddress_IPv6 addr_struct; |
| 141 if (!netaddr_interface->DescribeAsIPv6Address(addr, &addr_struct)) | 157 if (!netaddr_interface->DescribeAsIPv6Address(addr, &addr_struct)) { |
| 158 ppapi_->ReleaseResource(addr); | |
| 142 return NULL; | 159 return NULL; |
| 160 } | |
| 143 hostent_.h_addr_list[i] = static_cast<char*>(malloc(hostent_.h_length)); | 161 hostent_.h_addr_list[i] = static_cast<char*>(malloc(hostent_.h_length)); |
| 144 if (NULL == hostent_.h_addr_list[i]) | 162 if (NULL == hostent_.h_addr_list[i]) { |
| 163 ppapi_->ReleaseResource(addr); | |
| 145 return NULL; | 164 return NULL; |
| 165 } | |
| 146 memcpy(hostent_.h_addr_list[i], addr_struct.addr, hostent_.h_length); | 166 memcpy(hostent_.h_addr_list[i], addr_struct.addr, hostent_.h_length); |
| 147 } | 167 } |
| 168 ppapi_->ReleaseResource(addr); | |
| 148 } | 169 } |
| 149 return &hostent_; | 170 return &hostent_; |
| 150 } | 171 } |
| 151 | 172 |
| 152 // Frees all of the deep pointers in a hostent struct. Called between uses of | 173 // Frees all of the deep pointers in a hostent struct. Called between uses of |
| 153 // gethostbyname, and when the kernel_proxy object is destroyed. | 174 // gethostbyname, and when the kernel_proxy object is destroyed. |
| 154 void HostResolver::hostent_cleanup() { | 175 void HostResolver::hostent_cleanup() { |
| 155 if (NULL != hostent_.h_name) { | 176 if (NULL != hostent_.h_name) { |
| 156 free(hostent_.h_name); | 177 free(hostent_.h_name); |
| 157 } | 178 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 168 free(hostent_.h_addr_list); | 189 free(hostent_.h_addr_list); |
| 169 } | 190 } |
| 170 hostent_.h_name = NULL; | 191 hostent_.h_name = NULL; |
| 171 hostent_.h_aliases = NULL; | 192 hostent_.h_aliases = NULL; |
| 172 hostent_.h_addr_list = NULL; | 193 hostent_.h_addr_list = NULL; |
| 173 } | 194 } |
| 174 | 195 |
| 175 } // namespace nacl_io | 196 } // namespace nacl_io |
| 176 | 197 |
| 177 #endif // PROVIDES_SOCKET_API | 198 #endif // PROVIDES_SOCKET_API |
| OLD | NEW |