Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(248)

Side by Side Diff: runtime/bin/socket.h

Issue 910183003: Reafctor to use 'const RawAddr&' or 'RawAddr*' in the eventhandler code (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/secure_socket.cc ('k') | runtime/bin/socket.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <map> 8 #include <map>
9 9
10 #include "platform/globals.h" 10 #include "platform/globals.h"
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 ~SocketAddress() {} 59 ~SocketAddress() {}
60 60
61 int GetType() { 61 int GetType() {
62 if (addr_.ss.ss_family == AF_INET6) return TYPE_IPV6; 62 if (addr_.ss.ss_family == AF_INET6) return TYPE_IPV6;
63 return TYPE_IPV4; 63 return TYPE_IPV4;
64 } 64 }
65 65
66 const char* as_string() const { return as_string_; } 66 const char* as_string() const { return as_string_; }
67 const RawAddr& addr() const { return addr_; } 67 const RawAddr& addr() const { return addr_; }
68 68
69 static intptr_t GetAddrLength(const RawAddr* addr) { 69 static intptr_t GetAddrLength(const RawAddr& addr) {
70 ASSERT(addr->ss.ss_family == AF_INET || addr->ss.ss_family == AF_INET6); 70 ASSERT(addr.ss.ss_family == AF_INET || addr.ss.ss_family == AF_INET6);
71 return addr->ss.ss_family == AF_INET6 ? 71 return addr.ss.ss_family == AF_INET6 ?
72 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); 72 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
73 } 73 }
74 74
75 static intptr_t GetInAddrLength(const RawAddr* addr) { 75 static intptr_t GetInAddrLength(const RawAddr& addr) {
76 ASSERT(addr->ss.ss_family == AF_INET || addr->ss.ss_family == AF_INET6); 76 ASSERT(addr.ss.ss_family == AF_INET || addr.ss.ss_family == AF_INET6);
77 return addr->ss.ss_family == AF_INET6 ? 77 return addr.ss.ss_family == AF_INET6 ?
78 sizeof(struct in6_addr) : sizeof(struct in_addr); 78 sizeof(struct in6_addr) : sizeof(struct in_addr);
79 } 79 }
80 80
81 static bool AreAddressesEqual(const RawAddr& a, const RawAddr& b) { 81 static bool AreAddressesEqual(const RawAddr& a, const RawAddr& b) {
82 if (a.ss.ss_family == AF_INET) { 82 if (a.ss.ss_family == AF_INET) {
83 if (b.ss.ss_family != AF_INET) return false; 83 if (b.ss.ss_family != AF_INET) return false;
84 return memcmp(&a.in.sin_addr, &b.in.sin_addr, sizeof(a.in.sin_addr)) == 0; 84 return memcmp(&a.in.sin_addr, &b.in.sin_addr, sizeof(a.in.sin_addr)) == 0;
85 } else if (a.ss.ss_family == AF_INET6) { 85 } else if (a.ss.ss_family == AF_INET6) {
86 if (b.ss.ss_family != AF_INET6) return false; 86 if (b.ss.ss_family != AF_INET6) return false;
87 return memcmp(&a.in6.sin6_addr, 87 return memcmp(&a.in6.sin6_addr,
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 } 125 }
126 126
127 static void SetAddrPort(RawAddr* addr, intptr_t port) { 127 static void SetAddrPort(RawAddr* addr, intptr_t port) {
128 if (addr->ss.ss_family == AF_INET) { 128 if (addr->ss.ss_family == AF_INET) {
129 addr->in.sin_port = htons(port); 129 addr->in.sin_port = htons(port);
130 } else { 130 } else {
131 addr->in6.sin6_port = htons(port); 131 addr->in6.sin6_port = htons(port);
132 } 132 }
133 } 133 }
134 134
135 static intptr_t GetAddrPort(const RawAddr* addr) { 135 static intptr_t GetAddrPort(const RawAddr& addr) {
136 if (addr->ss.ss_family == AF_INET) { 136 if (addr.ss.ss_family == AF_INET) {
137 return ntohs(addr->in.sin_port); 137 return ntohs(addr.in.sin_port);
138 } else { 138 } else {
139 return ntohs(addr->in6.sin6_port); 139 return ntohs(addr.in6.sin6_port);
140 } 140 }
141 } 141 }
142 142
143 static Dart_Handle ToTypedData(RawAddr* raw) { 143 static Dart_Handle ToTypedData(const RawAddr& raw) {
144 int len = GetInAddrLength(raw); 144 int len = GetInAddrLength(raw);
145 Dart_Handle result = Dart_NewTypedData(Dart_TypedData_kUint8, len); 145 Dart_Handle result = Dart_NewTypedData(Dart_TypedData_kUint8, len);
146 if (Dart_IsError(result)) Dart_PropagateError(result); 146 if (Dart_IsError(result)) Dart_PropagateError(result);
147 Dart_Handle err; 147 Dart_Handle err;
148 if (raw->addr.sa_family == AF_INET6) { 148 RawAddr* ptr = &const_cast<RawAddr&>(raw);
kustermann 2015/02/11 14:54:31 Could you explain why it is necessary to remove th
Søren Gjesse 2015/02/11 15:19:39 Taking the address below is not allowed whithout c
149 if (raw.addr.sa_family == AF_INET6) {
149 err = Dart_ListSetAsBytes( 150 err = Dart_ListSetAsBytes(
150 result, 0, reinterpret_cast<uint8_t*>(&raw->in6.sin6_addr), len); 151 result, 0, reinterpret_cast<uint8_t*>(&ptr->in6.sin6_addr), len);
151 } else { 152 } else {
152 err = Dart_ListSetAsBytes( 153 err = Dart_ListSetAsBytes(
153 result, 0, reinterpret_cast<uint8_t*>(&raw->in.sin_addr), len); 154 result, 0, reinterpret_cast<uint8_t*>(&ptr->in.sin_addr), len);
154 } 155 }
155 if (Dart_IsError(err)) Dart_PropagateError(err); 156 if (Dart_IsError(err)) Dart_PropagateError(err);
156 return result; 157 return result;
157 } 158 }
158 159
159 static CObjectUint8Array* ToCObject(RawAddr* raw) { 160 static CObjectUint8Array* ToCObject(const RawAddr& raw) {
160 int in_addr_len = SocketAddress::GetInAddrLength(raw); 161 int in_addr_len = SocketAddress::GetInAddrLength(raw);
161 void* in_addr; 162 void* in_addr;
163 RawAddr* ptr = &const_cast<RawAddr&>(raw);
162 CObjectUint8Array* data = 164 CObjectUint8Array* data =
163 new CObjectUint8Array(CObject::NewUint8Array(in_addr_len)); 165 new CObjectUint8Array(CObject::NewUint8Array(in_addr_len));
164 if (raw->addr.sa_family == AF_INET6) { 166 if (raw.addr.sa_family == AF_INET6) {
165 in_addr = reinterpret_cast<void*>(&raw->in6.sin6_addr); 167 in_addr = reinterpret_cast<void*>(&ptr->in6.sin6_addr);
166 } else { 168 } else {
167 in_addr = reinterpret_cast<void*>(&raw->in.sin_addr); 169 in_addr = reinterpret_cast<void*>(&ptr->in.sin_addr);
168 } 170 }
169 memmove(data->Buffer(), in_addr, in_addr_len); 171 memmove(data->Buffer(), in_addr, in_addr_len);
170 return data; 172 return data;
171 } 173 }
172 174
173 private: 175 private:
174 char as_string_[INET6_ADDRSTRLEN]; 176 char as_string_[INET6_ADDRSTRLEN];
175 RawAddr addr_; 177 RawAddr addr_;
176 178
177 DISALLOW_COPY_AND_ASSIGN(SocketAddress); 179 DISALLOW_COPY_AND_ASSIGN(SocketAddress);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 kLookupRequest = 0, 236 kLookupRequest = 0,
235 kListInterfacesRequest = 1, 237 kListInterfacesRequest = 1,
236 kReverseLookupRequest = 2, 238 kReverseLookupRequest = 2,
237 }; 239 };
238 240
239 static bool Initialize(); 241 static bool Initialize();
240 static intptr_t Available(intptr_t fd); 242 static intptr_t Available(intptr_t fd);
241 static intptr_t Read(intptr_t fd, void* buffer, intptr_t num_bytes); 243 static intptr_t Read(intptr_t fd, void* buffer, intptr_t num_bytes);
242 static intptr_t Write(intptr_t fd, const void* buffer, intptr_t num_bytes); 244 static intptr_t Write(intptr_t fd, const void* buffer, intptr_t num_bytes);
243 static intptr_t SendTo( 245 static intptr_t SendTo(
244 intptr_t fd, const void* buffer, intptr_t num_bytes, RawAddr addr); 246 intptr_t fd, const void* buffer, intptr_t num_bytes, const RawAddr& addr);
245 static intptr_t RecvFrom( 247 static intptr_t RecvFrom(
246 intptr_t fd, void* buffer, intptr_t num_bytes, RawAddr* addr); 248 intptr_t fd, void* buffer, intptr_t num_bytes, RawAddr* addr);
247 static intptr_t CreateConnect(const RawAddr& addr, 249 static intptr_t CreateConnect(const RawAddr& addr);
248 const intptr_t port);
249 static intptr_t CreateBindConnect(const RawAddr& addr, 250 static intptr_t CreateBindConnect(const RawAddr& addr,
250 const intptr_t port,
251 const RawAddr& source_addr); 251 const RawAddr& source_addr);
252 static intptr_t CreateBindDatagram( 252 static intptr_t CreateBindDatagram(const RawAddr& addr, bool reuseAddress);
253 RawAddr* addr, intptr_t port, bool reuseAddress);
254 static intptr_t GetPort(intptr_t fd); 253 static intptr_t GetPort(intptr_t fd);
255 static SocketAddress* GetRemotePeer(intptr_t fd, intptr_t* port); 254 static SocketAddress* GetRemotePeer(intptr_t fd, intptr_t* port);
256 static void GetError(intptr_t fd, OSError* os_error); 255 static void GetError(intptr_t fd, OSError* os_error);
257 static int GetType(intptr_t fd); 256 static int GetType(intptr_t fd);
258 static intptr_t GetStdioHandle(intptr_t num); 257 static intptr_t GetStdioHandle(intptr_t num);
259 static void Close(intptr_t fd); 258 static void Close(intptr_t fd);
260 static bool GetNoDelay(intptr_t fd, bool* enabled); 259 static bool GetNoDelay(intptr_t fd, bool* enabled);
261 static bool SetNoDelay(intptr_t fd, bool enabled); 260 static bool SetNoDelay(intptr_t fd, bool enabled);
262 static bool GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled); 261 static bool GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled);
263 static bool SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled); 262 static bool SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled);
264 static bool GetMulticastHops(intptr_t fd, intptr_t protocol, int* value); 263 static bool GetMulticastHops(intptr_t fd, intptr_t protocol, int* value);
265 static bool SetMulticastHops(intptr_t fd, intptr_t protocol, int value); 264 static bool SetMulticastHops(intptr_t fd, intptr_t protocol, int value);
266 static bool GetBroadcast(intptr_t fd, bool* value); 265 static bool GetBroadcast(intptr_t fd, bool* value);
267 static bool SetBroadcast(intptr_t fd, bool value); 266 static bool SetBroadcast(intptr_t fd, bool value);
268 static bool JoinMulticast( 267 static bool JoinMulticast(intptr_t fd,
269 intptr_t fd, RawAddr* addr, RawAddr* interface, int interfaceIndex); 268 const RawAddr& addr,
270 static bool LeaveMulticast( 269 const RawAddr& interface,
271 intptr_t fd, RawAddr* addr, RawAddr* interface, int interfaceIndex); 270 int interfaceIndex);
271 static bool LeaveMulticast(intptr_t fd,
272 const RawAddr& addr,
273 const RawAddr& interface,
274 int interfaceIndex);
272 275
273 // Perform a hostname lookup. Returns a AddressList of SocketAddress's. 276 // Perform a hostname lookup. Returns a AddressList of SocketAddress's.
274 static AddressList<SocketAddress>* LookupAddress(const char* host, 277 static AddressList<SocketAddress>* LookupAddress(const char* host,
275 int type, 278 int type,
276 OSError** os_error); 279 OSError** os_error);
277 280
278 static bool ReverseLookup(RawAddr addr, 281 static bool ReverseLookup(const RawAddr& addr,
279 char* host, 282 char* host,
280 intptr_t host_len, 283 intptr_t host_len,
281 OSError** os_error); 284 OSError** os_error);
282 285
283 static bool ParseAddress(int type, const char* address, RawAddr* addr); 286 static bool ParseAddress(int type, const char* address, RawAddr* addr);
284 static bool FormatNumericAddress(RawAddr* addr, char* address, int len); 287 static bool FormatNumericAddress(const RawAddr& addr, char* address, int len);
285 288
286 // List interfaces. Returns a AddressList of InterfaceSocketAddress's. 289 // List interfaces. Returns a AddressList of InterfaceSocketAddress's.
287 static AddressList<InterfaceSocketAddress>* ListInterfaces( 290 static AddressList<InterfaceSocketAddress>* ListInterfaces(
288 int type, 291 int type,
289 OSError** os_error); 292 OSError** os_error);
290 293
291 static CObject* LookupRequest(const CObjectArray& request); 294 static CObject* LookupRequest(const CObjectArray& request);
292 static CObject* ListInterfacesRequest(const CObjectArray& request); 295 static CObject* ListInterfacesRequest(const CObjectArray& request);
293 static CObject* ReverseLookupRequest(const CObjectArray& request); 296 static CObject* ReverseLookupRequest(const CObjectArray& request);
294 297
(...skipping 12 matching lines...) Expand all
307 public: 310 public:
308 static const intptr_t kTemporaryFailure = -2; 311 static const intptr_t kTemporaryFailure = -2;
309 312
310 static intptr_t Accept(intptr_t fd); 313 static intptr_t Accept(intptr_t fd);
311 314
312 // Returns a positive integer if the call is successful. In case of failure 315 // Returns a positive integer if the call is successful. In case of failure
313 // it returns: 316 // it returns:
314 // 317 //
315 // -1: system error (errno set) 318 // -1: system error (errno set)
316 // -5: invalid bindAddress 319 // -5: invalid bindAddress
317 static intptr_t CreateBindListen(RawAddr addr, 320 static intptr_t CreateBindListen(const RawAddr& addr,
kustermann 2015/02/11 14:54:31 Please add a comment here (and other places) where
Søren Gjesse 2015/02/11 15:19:39 Done.
318 intptr_t port,
319 intptr_t backlog, 321 intptr_t backlog,
320 bool v6_only = false); 322 bool v6_only = false);
321 323
322 // Start accepting on a newly created listening socket. If it was unable to 324 // Start accepting on a newly created listening socket. If it was unable to
323 // start accepting incoming sockets, the fd is invalidated. 325 // start accepting incoming sockets, the fd is invalidated.
324 static bool StartAccept(intptr_t fd); 326 static bool StartAccept(intptr_t fd);
325 327
326 private: 328 private:
327 DISALLOW_ALLOCATION(); 329 DISALLOW_ALLOCATION();
328 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); 330 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 362
361 ~ListeningSocketRegistry() { 363 ~ListeningSocketRegistry() {
362 delete mutex_; 364 delete mutex_;
363 mutex_ = NULL; 365 mutex_ = NULL;
364 } 366 }
365 367
366 // This function should be called from a dart runtime call in order to create 368 // This function should be called from a dart runtime call in order to create
367 // a new (potentially shared) socket. 369 // a new (potentially shared) socket.
368 Dart_Handle CreateBindListen(Dart_Handle socket_object, 370 Dart_Handle CreateBindListen(Dart_Handle socket_object,
369 RawAddr addr, 371 RawAddr addr,
370 intptr_t port,
371 intptr_t backlog, 372 intptr_t backlog,
372 bool v6_only, 373 bool v6_only,
373 bool shared); 374 bool shared);
374 375
375 // This should be called from the event handler for every kCloseEvent it gets 376 // This should be called from the event handler for every kCloseEvent it gets
376 // on listening sockets. 377 // on listening sockets.
377 // 378 //
378 // Returns `true` if the last reference has been dropped and the underlying 379 // Returns `true` if the last reference has been dropped and the underlying
379 // socket can be closed. 380 // socket can be closed.
380 // 381 //
(...skipping 29 matching lines...) Expand all
410 411
411 private: 412 private:
412 DISALLOW_COPY_AND_ASSIGN(ListeningSocketRegistry); 413 DISALLOW_COPY_AND_ASSIGN(ListeningSocketRegistry);
413 }; 414 };
414 415
415 416
416 } // namespace bin 417 } // namespace bin
417 } // namespace dart 418 } // namespace dart
418 419
419 #endif // BIN_SOCKET_H_ 420 #endif // BIN_SOCKET_H_
OLDNEW
« no previous file with comments | « runtime/bin/secure_socket.cc ('k') | runtime/bin/socket.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698