| 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 <assert.h> | 7 #include <assert.h> |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 | 10 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 case AF_INET6: { | 51 case AF_INET6: { |
| 52 sockaddr_in6* in = | 52 sockaddr_in6* in = |
| 53 static_cast<sockaddr_in6*>(malloc(sizeof(sockaddr_in6))); | 53 static_cast<sockaddr_in6*>(malloc(sizeof(sockaddr_in6))); |
| 54 *in = *(sockaddr_in6*)addr; | 54 *in = *(sockaddr_in6*)addr; |
| 55 ai->ai_family = AF_INET6; | 55 ai->ai_family = AF_INET6; |
| 56 ai->ai_addr = reinterpret_cast<sockaddr*>(in); | 56 ai->ai_addr = reinterpret_cast<sockaddr*>(in); |
| 57 ai->ai_addrlen = sizeof(*in); | 57 ai->ai_addrlen = sizeof(*in); |
| 58 break; | 58 break; |
| 59 } | 59 } |
| 60 case AF_INET: { | 60 case AF_INET: { |
| 61 sockaddr_in* in = | 61 sockaddr_in* in = static_cast<sockaddr_in*>(malloc(sizeof(sockaddr_in))); |
| 62 static_cast<sockaddr_in*>(malloc(sizeof(sockaddr_in))); | |
| 63 *in = *(sockaddr_in*)addr; | 62 *in = *(sockaddr_in*)addr; |
| 64 ai->ai_family = AF_INET; | 63 ai->ai_family = AF_INET; |
| 65 ai->ai_addr = reinterpret_cast<sockaddr*>(in); | 64 ai->ai_addr = reinterpret_cast<sockaddr*>(in); |
| 66 ai->ai_addrlen = sizeof(*in); | 65 ai->ai_addrlen = sizeof(*in); |
| 67 break; | 66 break; |
| 68 } | 67 } |
| 69 default: | 68 default: |
| 70 assert(0); | 69 assert(0); |
| 71 return; | 70 return; |
| 72 } | 71 } |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 | 198 |
| 200 #if !defined(h_addr) | 199 #if !defined(h_addr) |
| 201 // Copy element zero of h_addr_list to h_addr when h_addr is not defined | 200 // Copy element zero of h_addr_list to h_addr when h_addr is not defined |
| 202 // as in some libc's h_addr may be a separate member instead of a macro. | 201 // as in some libc's h_addr may be a separate member instead of a macro. |
| 203 hostent_.h_addr = hostent_.h_addr_list[0]; | 202 hostent_.h_addr = hostent_.h_addr_list[0]; |
| 204 #endif | 203 #endif |
| 205 | 204 |
| 206 return &hostent_; | 205 return &hostent_; |
| 207 } | 206 } |
| 208 | 207 |
| 209 void HostResolver::freeaddrinfo(struct addrinfo *res) { | 208 void HostResolver::freeaddrinfo(struct addrinfo* res) { |
| 210 while (res) { | 209 while (res) { |
| 211 struct addrinfo* cur = res; | 210 struct addrinfo* cur = res; |
| 212 res = res->ai_next; | 211 res = res->ai_next; |
| 213 free(cur->ai_addr); | 212 free(cur->ai_addr); |
| 214 free(cur->ai_canonname); | 213 free(cur->ai_canonname); |
| 215 free(cur); | 214 free(cur); |
| 216 } | 215 } |
| 217 } | 216 } |
| 218 | 217 |
| 219 int HostResolver::getaddrinfo(const char* node, const char* service, | 218 int HostResolver::getaddrinfo(const char* node, |
| 219 const char* service, |
| 220 const struct addrinfo* hints_in, | 220 const struct addrinfo* hints_in, |
| 221 struct addrinfo** result) { | 221 struct addrinfo** result) { |
| 222 *result = NULL; | 222 *result = NULL; |
| 223 struct addrinfo* end = NULL; | 223 struct addrinfo* end = NULL; |
| 224 | 224 |
| 225 if (node == NULL && service == NULL) | 225 if (node == NULL && service == NULL) |
| 226 return EAI_NONAME; | 226 return EAI_NONAME; |
| 227 | 227 |
| 228 // Check the service name (port). Currently we only handle numeric | 228 // Check the service name (port). Currently we only handle numeric |
| 229 // services. | 229 // services. |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 if (hints->ai_flags & AI_CANONNAME) { | 336 if (hints->ai_flags & AI_CANONNAME) { |
| 337 PP_Var name_var = resolver_iface->GetCanonicalName(resolver); | 337 PP_Var name_var = resolver_iface->GetCanonicalName(resolver); |
| 338 if (PP_VARTYPE_STRING == name_var.type) { | 338 if (PP_VARTYPE_STRING == name_var.type) { |
| 339 uint32_t len = 0; | 339 uint32_t len = 0; |
| 340 const char* tmp = var_interface->VarToUtf8(name_var, &len); | 340 const char* tmp = var_interface->VarToUtf8(name_var, &len); |
| 341 // For some reason GetCanonicalName alway returns an empty | 341 // For some reason GetCanonicalName alway returns an empty |
| 342 // string so this condition is never true. | 342 // string so this condition is never true. |
| 343 // TODO(sbc): investigate this issue with PPAPI team. | 343 // TODO(sbc): investigate this issue with PPAPI team. |
| 344 if (len > 0) { | 344 if (len > 0) { |
| 345 // Copy and NULL-terminate the UTF8 string var. | 345 // Copy and NULL-terminate the UTF8 string var. |
| 346 canon_name = static_cast<char*>(malloc(len+1)); | 346 canon_name = static_cast<char*>(malloc(len + 1)); |
| 347 strncpy(canon_name, tmp, len); | 347 strncpy(canon_name, tmp, len); |
| 348 canon_name[len] = '\0'; | 348 canon_name[len] = '\0'; |
| 349 } | 349 } |
| 350 } | 350 } |
| 351 if (!canon_name) | 351 if (!canon_name) |
| 352 canon_name = strdup(node); | 352 canon_name = strdup(node); |
| 353 var_interface->Release(name_var); | 353 var_interface->Release(name_var); |
| 354 } | 354 } |
| 355 | 355 |
| 356 int num_addresses = resolver_iface->GetNetAddressCount(resolver); | 356 int num_addresses = resolver_iface->GetNetAddressCount(resolver); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 374 memcpy(&addr_in.sin_addr.s_addr, pp_addr.addr, sizeof(in_addr_t)); | 374 memcpy(&addr_in.sin_addr.s_addr, pp_addr.addr, sizeof(in_addr_t)); |
| 375 sockaddr = (struct sockaddr*)&addr_in; | 375 sockaddr = (struct sockaddr*)&addr_in; |
| 376 break; | 376 break; |
| 377 } | 377 } |
| 378 case PP_NETADDRESS_FAMILY_IPV6: { | 378 case PP_NETADDRESS_FAMILY_IPV6: { |
| 379 struct PP_NetAddress_IPv6 pp_addr; | 379 struct PP_NetAddress_IPv6 pp_addr; |
| 380 if (!netaddr_iface->DescribeAsIPv6Address(resource, &pp_addr)) { | 380 if (!netaddr_iface->DescribeAsIPv6Address(resource, &pp_addr)) { |
| 381 assert(false); | 381 assert(false); |
| 382 break; | 382 break; |
| 383 } | 383 } |
| 384 memcpy(&addr_in6.sin6_addr.s6_addr, pp_addr.addr, | 384 memcpy(&addr_in6.sin6_addr.s6_addr, pp_addr.addr, sizeof(in6_addr)); |
| 385 sizeof(in6_addr)); | |
| 386 sockaddr = (struct sockaddr*)&addr_in6; | 385 sockaddr = (struct sockaddr*)&addr_in6; |
| 387 break; | 386 break; |
| 388 } | 387 } |
| 389 default: | 388 default: |
| 390 return EAI_SYSTEM; | 389 return EAI_SYSTEM; |
| 391 } | 390 } |
| 392 | 391 |
| 393 if (sockaddr != NULL) | 392 if (sockaddr != NULL) |
| 394 CreateAddrInfo(hints, sockaddr, canon_name, result, &end); | 393 CreateAddrInfo(hints, sockaddr, canon_name, result, &end); |
| 395 | 394 |
| 396 if (canon_name) { | 395 if (canon_name) { |
| 397 free(canon_name); | 396 free(canon_name); |
| 398 canon_name = NULL; | 397 canon_name = NULL; |
| 399 } | 398 } |
| 400 } | 399 } |
| 401 | 400 |
| 402 return 0; | 401 return 0; |
| 403 } | 402 } |
| 404 | 403 |
| 405 // Frees all of the deep pointers in a hostent struct. Called between uses of | 404 // Frees all of the deep pointers in a hostent struct. Called between uses of |
| 406 // gethostbyname, and when the kernel_proxy object is destroyed. | 405 // gethostbyname, and when the kernel_proxy object is destroyed. |
| 407 void HostResolver::hostent_cleanup() { | 406 void HostResolver::hostent_cleanup() { |
| 408 if (NULL != hostent_.h_name) { | 407 if (NULL != hostent_.h_name) { |
| 409 free(hostent_.h_name); | 408 free(hostent_.h_name); |
| 410 } | 409 } |
| 411 if (NULL != hostent_.h_aliases) { | 410 if (NULL != hostent_.h_aliases) { |
| 412 for (int i = 0; NULL != hostent_.h_aliases[i]; i++) { | 411 for (int i = 0; NULL != hostent_.h_aliases[i]; i++) { |
| 413 free(hostent_.h_aliases[i]); | 412 free(hostent_.h_aliases[i]); |
| 414 } | 413 } |
| 415 free(hostent_.h_aliases); | 414 free(hostent_.h_aliases); |
| 416 } | 415 } |
| 417 if (NULL != hostent_.h_addr_list) { | 416 if (NULL != hostent_.h_addr_list) { |
| 418 for (int i = 0; NULL != hostent_.h_addr_list[i]; i++) { | 417 for (int i = 0; NULL != hostent_.h_addr_list[i]; i++) { |
| 419 free(hostent_.h_addr_list[i]); | 418 free(hostent_.h_addr_list[i]); |
| 420 } | 419 } |
| 421 free(hostent_.h_addr_list); | 420 free(hostent_.h_addr_list); |
| 422 } | 421 } |
| 423 hostent_.h_name = NULL; | 422 hostent_.h_name = NULL; |
| 424 hostent_.h_aliases = NULL; | 423 hostent_.h_aliases = NULL; |
| 425 hostent_.h_addr_list = NULL; | 424 hostent_.h_addr_list = NULL; |
| 426 #if !defined(h_addr) | 425 #if !defined(h_addr) |
| 427 // Initialize h_addr separately in the case where it is not a macro. | 426 // Initialize h_addr separately in the case where it is not a macro. |
| 428 hostent_.h_addr = NULL; | 427 hostent_.h_addr = NULL; |
| 429 #endif | 428 #endif |
| 430 } | 429 } |
| 431 | 430 |
| 432 } // namespace nacl_io | 431 } // namespace nacl_io |
| 433 | 432 |
| 434 #endif // PROVIDES_SOCKET_API | 433 #endif // PROVIDES_SOCKET_API |
| OLD | NEW |