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

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

Issue 14469002: Add new InternetAddress class with a static lookup function (including IPv6 (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 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 "bin/builtin.h" 8 #include "bin/builtin.h"
9 #include "bin/utils.h" 9 #include "bin/utils.h"
10 10
11 #include "platform/globals.h" 11 #include "platform/globals.h"
12 #include "platform/thread.h" 12 #include "platform/thread.h"
13 // Declare the OS-specific types ahead of defining the generic class. 13 // Declare the OS-specific types ahead of defining the generic class.
14 #if defined(TARGET_OS_ANDROID) 14 #if defined(TARGET_OS_ANDROID)
15 #include "bin/socket_android.h" 15 #include "bin/socket_android.h"
16 #elif defined(TARGET_OS_LINUX) 16 #elif defined(TARGET_OS_LINUX)
17 #include "bin/socket_linux.h" 17 #include "bin/socket_linux.h"
18 #elif defined(TARGET_OS_MACOS) 18 #elif defined(TARGET_OS_MACOS)
19 #include "bin/socket_macos.h" 19 #include "bin/socket_macos.h"
20 #elif defined(TARGET_OS_WINDOWS) 20 #elif defined(TARGET_OS_WINDOWS)
21 #include "bin/socket_win.h" 21 #include "bin/socket_win.h"
22 #else 22 #else
23 #error Unknown target os. 23 #error Unknown target os.
24 #endif 24 #endif
25 25
26 26
27 union RawAddr {
28 // ss_family is always at ofset 0, thus we can add it here.
29 uint16_t ss_family;
30 struct sockaddr_in in;
31 struct sockaddr_in6 in6;
32 struct sockaddr addr;
33 };
34
35 class SocketAddress {
36 public:
37 enum {
38 TYPE_ANY = -1,
39 TYPE_IPV4,
40 TYPE_IPV6,
41 };
42
43 explicit SocketAddress(struct addrinfo* addrinfo);
44
45 int GetType() {
46 if (addr_.ss_family == AF_INET6) return TYPE_IPV6;
47 return TYPE_IPV4;
48 }
49
50 const char* as_string() const { return as_string_; }
51 const RawAddr& addr() const { return addr_; }
52
53 static intptr_t GetAddrLength(const RawAddr& addr) {
54 return addr.ss_family == AF_INET6 ?
55 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
56 }
57
58 static int16_t FromType(int type) {
59 if (type == TYPE_ANY) return AF_UNSPEC;
60 if (type == TYPE_IPV4) return AF_INET;
61 ASSERT(type == TYPE_IPV6 && "Invalid type");
62 return AF_INET6;
63 }
64
65 static void SetAddrPort(RawAddr* addr, intptr_t port) {
66 if (addr->ss_family == AF_INET) {
67 addr->in.sin_port = htons(port);
68 } else {
69 addr->in6.sin6_port = htons(port);
70 }
71 }
72
73 static intptr_t GetAddrPort(RawAddr* addr) {
74 if (addr->ss_family == AF_INET) {
75 return ntohs(addr->in.sin_port);
76 } else {
77 return ntohs(addr->in6.sin6_port);
78 }
79 }
80
81 private:
82 char as_string_[INET6_ADDRSTRLEN];
83 RawAddr addr_;
84
85 DISALLOW_COPY_AND_ASSIGN(SocketAddress);
86 };
87
88 class SocketAddresses {
89 public:
90 explicit SocketAddresses(intptr_t count)
91 : count_(count),
92 addresses_(new SocketAddress*[count_]) {}
93
94 ~SocketAddresses() {
95 for (intptr_t i = 0; i < count_; i++) {
96 delete addresses_[i];
97 }
98 delete[] addresses_;
99 }
100
101 intptr_t count() const { return count_; }
102 SocketAddress* GetAt(intptr_t i) const { return addresses_[i]; }
103 void SetAt(intptr_t i, SocketAddress* addr) { addresses_[i] = addr; }
104
105 private:
106 const intptr_t count_;
107 SocketAddress** addresses_;
108
109 DISALLOW_COPY_AND_ASSIGN(SocketAddresses);
110 };
111
27 class Socket { 112 class Socket {
28 public: 113 public:
29 enum SocketRequest { 114 enum SocketRequest {
30 kLookupRequest = 0, 115 kLookupRequest = 0,
31 }; 116 };
32 117
33 static bool Initialize(); 118 static bool Initialize();
34 static intptr_t Available(intptr_t fd); 119 static intptr_t Available(intptr_t fd);
35 static int Read(intptr_t fd, void* buffer, intptr_t num_bytes); 120 static int Read(intptr_t fd, void* buffer, intptr_t num_bytes);
36 static int Write(intptr_t fd, const void* buffer, intptr_t num_bytes); 121 static int Write(intptr_t fd, const void* buffer, intptr_t num_bytes);
37 static intptr_t CreateConnect(const char* host, const intptr_t port); 122 static intptr_t CreateConnect(RawAddr addr,
123 const intptr_t port);
38 static intptr_t GetPort(intptr_t fd); 124 static intptr_t GetPort(intptr_t fd);
39 static bool GetRemotePeer(intptr_t fd, char* host, intptr_t* port); 125 static bool GetRemotePeer(intptr_t fd, char* host, intptr_t* port);
40 static void GetError(intptr_t fd, OSError* os_error); 126 static void GetError(intptr_t fd, OSError* os_error);
41 static int GetType(intptr_t fd); 127 static int GetType(intptr_t fd);
42 static intptr_t GetStdioHandle(int num); 128 static intptr_t GetStdioHandle(int num);
43 static void Close(intptr_t fd); 129 static void Close(intptr_t fd);
44 static bool SetNonBlocking(intptr_t fd); 130 static bool SetNonBlocking(intptr_t fd);
45 static bool SetBlocking(intptr_t fd); 131 static bool SetBlocking(intptr_t fd);
46 static bool SetNoDelay(intptr_t fd, bool enabled); 132 static bool SetNoDelay(intptr_t fd, bool enabled);
47 133
48 // Perform a IPv4 hostname lookup. Returns the hostname string in 134 // Perform a hostname lookup. Returns the SocketAddresses.
49 // IPv4 dotted-decimal format. 135 static SocketAddresses* LookupAddress(const char* host,
50 static const char* LookupIPv4Address(char* host, OSError** os_error); 136 int type,
137 OSError** os_error);
51 138
52 static Dart_Port GetServicePort(); 139 static Dart_Port GetServicePort();
53 140
54 static Dart_Handle SetSocketIdNativeField(Dart_Handle socket, intptr_t id); 141 static Dart_Handle SetSocketIdNativeField(Dart_Handle socket, intptr_t id);
55 static Dart_Handle GetSocketIdNativeField(Dart_Handle socket, intptr_t* id); 142 static Dart_Handle GetSocketIdNativeField(Dart_Handle socket, intptr_t* id);
56 143
57 private: 144 private:
58 static dart::Mutex mutex_; 145 static dart::Mutex mutex_;
59 static int service_ports_size_; 146 static int service_ports_size_;
60 static Dart_Port* service_ports_; 147 static Dart_Port* service_ports_;
61 static int service_ports_index_; 148 static int service_ports_index_;
62 149
63 DISALLOW_ALLOCATION(); 150 DISALLOW_ALLOCATION();
64 DISALLOW_IMPLICIT_CONSTRUCTORS(Socket); 151 DISALLOW_IMPLICIT_CONSTRUCTORS(Socket);
65 }; 152 };
66 153
67 154
68 class ServerSocket { 155 class ServerSocket {
69 public: 156 public:
70 static const intptr_t kTemporaryFailure = -2; 157 static const intptr_t kTemporaryFailure = -2;
71 158
72 static intptr_t Accept(intptr_t fd); 159 static intptr_t Accept(intptr_t fd);
73 160
74 // Returns a positive integer if the call is successful. In case of failure 161 // Returns a positive integer if the call is successful. In case of failure
75 // it returns: 162 // it returns:
76 // 163 //
77 // -1: system error (errno set) 164 // -1: system error (errno set)
78 // -5: invalid bindAddress 165 // -5: invalid bindAddress
79 static intptr_t CreateBindListen(const char* bindAddress, 166 static intptr_t CreateBindListen(RawAddr addr,
80 intptr_t port, 167 intptr_t port,
81 intptr_t backlog); 168 intptr_t backlog);
82 169
83 DISALLOW_ALLOCATION(); 170 DISALLOW_ALLOCATION();
84 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); 171 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket);
85 }; 172 };
86 173
87 #endif // BIN_SOCKET_H_ 174 #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