OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef BIN_SOCKET_H_ | 5 #ifndef BIN_SOCKET_H_ |
6 #define BIN_SOCKET_H_ | 6 #define BIN_SOCKET_H_ |
7 | 7 |
8 #include "bin/builtin.h" | 8 #include "bin/builtin.h" |
9 #include "bin/utils.h" | 9 #include "bin/utils.h" |
10 #include "bin/dartutils.h" | 10 #include "bin/dartutils.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 | 63 |
64 const char* as_string() const { return as_string_; } | 64 const char* as_string() const { return as_string_; } |
65 const RawAddr& addr() const { return addr_; } | 65 const RawAddr& addr() const { return addr_; } |
66 | 66 |
67 static intptr_t GetAddrLength(const RawAddr* addr) { | 67 static intptr_t GetAddrLength(const RawAddr* addr) { |
68 ASSERT(addr->ss.ss_family == AF_INET || addr->ss.ss_family == AF_INET6); | 68 ASSERT(addr->ss.ss_family == AF_INET || addr->ss.ss_family == AF_INET6); |
69 return addr->ss.ss_family == AF_INET6 ? | 69 return addr->ss.ss_family == AF_INET6 ? |
70 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); | 70 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); |
71 } | 71 } |
72 | 72 |
| 73 static intptr_t GetInAddrLength(const RawAddr* addr) { |
| 74 ASSERT(addr->ss.ss_family == AF_INET || addr->ss.ss_family == AF_INET6); |
| 75 return addr->ss.ss_family == AF_INET6 ? |
| 76 sizeof(struct in6_addr) : sizeof(struct in_addr); |
| 77 } |
| 78 |
| 79 static void GetSockAddr(Dart_Handle obj, RawAddr* addr) { |
| 80 Dart_TypedData_Type data_type; |
| 81 uint8_t* data = NULL; |
| 82 intptr_t len; |
| 83 Dart_Handle result = Dart_TypedDataAcquireData( |
| 84 obj, &data_type, reinterpret_cast<void**>(&data), &len); |
| 85 if (Dart_IsError(result)) Dart_PropagateError(result); |
| 86 if (data_type != Dart_TypedData_kUint8 || |
| 87 (len != sizeof(in_addr) && len != sizeof(in6_addr))) { |
| 88 Dart_PropagateError( |
| 89 Dart_NewApiError("Unexpected type for socket address")); |
| 90 } |
| 91 memset(reinterpret_cast<void*>(addr), 0, sizeof(RawAddr)); |
| 92 if (len == sizeof(in_addr)) { |
| 93 addr->in.sin_family = AF_INET; |
| 94 memmove(reinterpret_cast<void *>(&addr->in.sin_addr), data, len); |
| 95 } else { |
| 96 ASSERT(len == sizeof(in6_addr)); |
| 97 addr->in6.sin6_family = AF_INET6; |
| 98 memmove(reinterpret_cast<void*>(&addr->in6.sin6_addr), data, len); |
| 99 } |
| 100 Dart_TypedDataReleaseData(obj); |
| 101 } |
| 102 |
73 static int16_t FromType(int type) { | 103 static int16_t FromType(int type) { |
74 if (type == TYPE_ANY) return AF_UNSPEC; | 104 if (type == TYPE_ANY) return AF_UNSPEC; |
75 if (type == TYPE_IPV4) return AF_INET; | 105 if (type == TYPE_IPV4) return AF_INET; |
76 ASSERT(type == TYPE_IPV6 && "Invalid type"); | 106 ASSERT(type == TYPE_IPV6 && "Invalid type"); |
77 return AF_INET6; | 107 return AF_INET6; |
78 } | 108 } |
79 | 109 |
80 static void SetAddrPort(RawAddr* addr, intptr_t port) { | 110 static void SetAddrPort(RawAddr* addr, intptr_t port) { |
81 if (addr->ss.ss_family == AF_INET) { | 111 if (addr->ss.ss_family == AF_INET) { |
82 addr->in.sin_port = htons(port); | 112 addr->in.sin_port = htons(port); |
83 } else { | 113 } else { |
84 addr->in6.sin6_port = htons(port); | 114 addr->in6.sin6_port = htons(port); |
85 } | 115 } |
86 } | 116 } |
87 | 117 |
88 static intptr_t GetAddrPort(RawAddr* addr) { | 118 static intptr_t GetAddrPort(const RawAddr* addr) { |
89 if (addr->ss.ss_family == AF_INET) { | 119 if (addr->ss.ss_family == AF_INET) { |
90 return ntohs(addr->in.sin_port); | 120 return ntohs(addr->in.sin_port); |
91 } else { | 121 } else { |
92 return ntohs(addr->in6.sin6_port); | 122 return ntohs(addr->in6.sin6_port); |
93 } | 123 } |
94 } | 124 } |
95 | 125 |
96 static Dart_Handle ToTypedData(RawAddr* addr) { | 126 static Dart_Handle ToTypedData(RawAddr* raw) { |
97 int len = GetAddrLength(addr); | 127 int len = GetInAddrLength(raw); |
98 Dart_Handle result = Dart_NewTypedData(Dart_TypedData_kUint8, len); | 128 Dart_Handle result = Dart_NewTypedData(Dart_TypedData_kUint8, len); |
99 if (Dart_IsError(result)) Dart_PropagateError(result); | 129 if (Dart_IsError(result)) Dart_PropagateError(result); |
100 Dart_ListSetAsBytes(result, 0, reinterpret_cast<uint8_t *>(addr), len); | 130 Dart_Handle err; |
| 131 if (raw->addr.sa_family == AF_INET6) { |
| 132 err = Dart_ListSetAsBytes( |
| 133 result, 0, reinterpret_cast<uint8_t*>(&raw->in6.sin6_addr), len); |
| 134 } else { |
| 135 err = Dart_ListSetAsBytes( |
| 136 result, 0, reinterpret_cast<uint8_t*>(&raw->in.sin_addr), len); |
| 137 } |
| 138 if (Dart_IsError(err)) Dart_PropagateError(err); |
101 return result; | 139 return result; |
102 } | 140 } |
103 | 141 |
| 142 static CObjectUint8Array* ToCObject(RawAddr* raw) { |
| 143 int in_addr_len = SocketAddress::GetInAddrLength(raw); |
| 144 void* in_addr; |
| 145 CObjectUint8Array* data = |
| 146 new CObjectUint8Array(CObject::NewUint8Array(in_addr_len)); |
| 147 if (raw->addr.sa_family == AF_INET6) { |
| 148 in_addr = reinterpret_cast<void*>(&raw->in6.sin6_addr); |
| 149 } else { |
| 150 in_addr = reinterpret_cast<void*>(&raw->in.sin_addr); |
| 151 } |
| 152 memmove(data->Buffer(), in_addr, in_addr_len); |
| 153 return data; |
| 154 } |
| 155 |
104 private: | 156 private: |
105 char as_string_[INET6_ADDRSTRLEN]; | 157 char as_string_[INET6_ADDRSTRLEN]; |
106 RawAddr addr_; | 158 RawAddr addr_; |
107 | 159 |
108 DISALLOW_COPY_AND_ASSIGN(SocketAddress); | 160 DISALLOW_COPY_AND_ASSIGN(SocketAddress); |
109 }; | 161 }; |
110 | 162 |
111 class InterfaceSocketAddress { | 163 class InterfaceSocketAddress { |
112 public: | 164 public: |
113 explicit InterfaceSocketAddress(struct sockaddr* sa, | 165 explicit InterfaceSocketAddress(struct sockaddr* sa, |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 | 310 |
259 private: | 311 private: |
260 DISALLOW_ALLOCATION(); | 312 DISALLOW_ALLOCATION(); |
261 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); | 313 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); |
262 }; | 314 }; |
263 | 315 |
264 } // namespace bin | 316 } // namespace bin |
265 } // namespace dart | 317 } // namespace dart |
266 | 318 |
267 #endif // BIN_SOCKET_H_ | 319 #endif // BIN_SOCKET_H_ |
OLD | NEW |