| 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/ossocket.h" | 5 #include "nacl_io/ossocket.h" |
| 6 #if defined(PROVIDES_SOCKET_API) && !defined(__GLIBC__) && !defined(__BIONIC__) | 6 #if defined(PROVIDES_SOCKET_API) && !defined(__GLIBC__) && !defined(__BIONIC__) |
| 7 | 7 |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 | 10 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 } | 36 } |
| 37 | 37 |
| 38 // Convert to an array of 8 host order shorts | 38 // Convert to an array of 8 host order shorts |
| 39 const uint16_t* tuples = static_cast<const uint16_t*>(src); | 39 const uint16_t* tuples = static_cast<const uint16_t*>(src); |
| 40 uint16_t host_tuples[8]; | 40 uint16_t host_tuples[8]; |
| 41 int zero_run_start = -1; | 41 int zero_run_start = -1; |
| 42 int zero_run_end = -1; | 42 int zero_run_end = -1; |
| 43 for (int i = 0; i < 8; i++) { | 43 for (int i = 0; i < 8; i++) { |
| 44 host_tuples[i] = ntohs(tuples[i]); | 44 host_tuples[i] = ntohs(tuples[i]); |
| 45 if (host_tuples[i] == 0) { | 45 if (host_tuples[i] == 0) { |
| 46 if (zero_run_start == -1) | 46 if (zero_run_start == -1) |
| 47 zero_run_start = i; | 47 zero_run_start = i; |
| 48 } else if (zero_run_start != -1 && zero_run_end == -1) { | 48 } else if (zero_run_start != -1 && zero_run_end == -1) { |
| 49 zero_run_end = i; | 49 zero_run_end = i; |
| 50 } | 50 } |
| 51 } | 51 } |
| 52 | 52 |
| 53 if (zero_run_start != -1) { | 53 if (zero_run_start != -1) { |
| 54 if (zero_run_end == -1) | 54 if (zero_run_end == -1) |
| 55 zero_run_end = 8; | 55 zero_run_end = 8; |
| 56 if (zero_run_end - zero_run_start < 2) { | 56 if (zero_run_end - zero_run_start < 2) { |
| 57 zero_run_start = -1; | 57 zero_run_start = -1; |
| 58 zero_run_end = -1; | 58 zero_run_end = -1; |
| 59 } | 59 } |
| 60 } | 60 } |
| 61 | 61 |
| 62 // Mimick glibc's behaviour here and allow ipv4 address to be specified | 62 // Mimick glibc's behaviour here and allow ipv4 address to be specified |
| 63 // as either ::A.B.C.D or ::ffff:A.B.C.D. | 63 // as either ::A.B.C.D or ::ffff:A.B.C.D. |
| 64 if (zero_run_start == 0 && | 64 if (zero_run_start == 0 && |
| 65 (zero_run_end == 6 || | 65 (zero_run_end == 6 || |
| 66 (zero_run_end == 5 && host_tuples[zero_run_end] == 0xffff))) { | 66 (zero_run_end == 5 && host_tuples[zero_run_end] == 0xffff))) { |
| 67 | |
| 68 if (zero_run_end == 5) { | 67 if (zero_run_end == 5) { |
| 69 strcpy(dst, "::ffff:"); | 68 strcpy(dst, "::ffff:"); |
| 70 } else { | 69 } else { |
| 71 strcpy(dst, "::"); | 70 strcpy(dst, "::"); |
| 72 } | 71 } |
| 73 inet_ntop(AF_INET, host_tuples+6, dst+strlen(dst), INET_ADDRSTRLEN); | 72 inet_ntop(AF_INET, host_tuples + 6, dst + strlen(dst), INET_ADDRSTRLEN); |
| 74 } else { | 73 } else { |
| 75 std::stringstream output; | 74 std::stringstream output; |
| 76 for (int i = 0; i < 8; i++) { | 75 for (int i = 0; i < 8; i++) { |
| 77 if (i == zero_run_start) { | 76 if (i == zero_run_start) { |
| 78 output << "::"; | 77 output << "::"; |
| 79 continue; | 78 continue; |
| 80 } | 79 } |
| 81 if (i > zero_run_start && i < zero_run_end) | 80 if (i > zero_run_start && i < zero_run_end) |
| 82 continue; | 81 continue; |
| 83 output << std::hex << host_tuples[i]; | 82 output << std::hex << host_tuples[i]; |
| 84 if (i < 7 && i + 1 != zero_run_start) | 83 if (i < 7 && i + 1 != zero_run_start) |
| 85 output << ":"; | 84 output << ":"; |
| 86 } | 85 } |
| 87 memcpy(dst, output.str().c_str(), output.str().size() + 1); | 86 memcpy(dst, output.str().c_str(), output.str().size() + 1); |
| 88 } | 87 } |
| 89 return dst; | 88 return dst; |
| 90 } | 89 } |
| 91 | 90 |
| 92 errno = EAFNOSUPPORT; | 91 errno = EAFNOSUPPORT; |
| 93 return NULL; | 92 return NULL; |
| 94 } | 93 } |
| 95 | 94 |
| 96 EXTERN_C_END | 95 EXTERN_C_END |
| 97 | 96 |
| 98 #endif // defined(PROVIDES_SOCKET_API) && !defined(__GLIBC__) ... | 97 #endif // defined(PROVIDES_SOCKET_API) && !defined(__GLIBC__) ... |
| OLD | NEW |