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

Side by Side Diff: runtime/bin/socket_patch.dart

Issue 16514010: Add NetworkAdaptor to dart:io. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add support for win, mac and android. Created 7 years, 6 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
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 patch class RawServerSocket { 5 patch class RawServerSocket {
6 /* patch */ static Future<RawServerSocket> bind(address, 6 /* patch */ static Future<RawServerSocket> bind(address,
7 int port, 7 int port,
8 {int backlog: 0, 8 {int backlog: 0,
9 bool v6Only: false}) { 9 bool v6Only: false}) {
10 return _RawServerSocket.bind(address, port, backlog, v6Only); 10 return _RawServerSocket.bind(address, port, backlog, v6Only);
(...skipping 24 matching lines...) Expand all
35 /* patch */ static InternetAddress get ANY_IP_V6 { 35 /* patch */ static InternetAddress get ANY_IP_V6 {
36 return _InternetAddress.ANY_IP_V6; 36 return _InternetAddress.ANY_IP_V6;
37 } 37 }
38 38
39 /* patch */ static Future<List<InternetAddress>> lookup( 39 /* patch */ static Future<List<InternetAddress>> lookup(
40 String host, {InternetAddressType type: InternetAddressType.ANY}) { 40 String host, {InternetAddressType type: InternetAddressType.ANY}) {
41 return _NativeSocket.lookup(host, type: type); 41 return _NativeSocket.lookup(host, type: type);
42 } 42 }
43 } 43 }
44 44
45 patch class NetworkInterface {
46 /* patch */ static Future<List<NetworkInterface>> list({
47 bool includeLoopback: false,
48 bool includeLinkLocal: false,
49 InternetAddressType type: InternetAddressType.ANY}) {
50 return _NativeSocket.listInterfaces(includeLoopback: includeLoopback,
51 includeLinkLocal: includeLinkLocal,
52 type: type);
53 }
54 }
55
45 class _InternetAddress implements InternetAddress { 56 class _InternetAddress implements InternetAddress {
46 static const int _ADDRESS_LOOPBACK_IP_V4 = 0; 57 static const int _ADDRESS_LOOPBACK_IP_V4 = 0;
47 static const int _ADDRESS_LOOPBACK_IP_V6 = 1; 58 static const int _ADDRESS_LOOPBACK_IP_V6 = 1;
48 static const int _ADDRESS_ANY_IP_V4 = 2; 59 static const int _ADDRESS_ANY_IP_V4 = 2;
49 static const int _ADDRESS_ANY_IP_V6 = 3; 60 static const int _ADDRESS_ANY_IP_V6 = 3;
61 static const int _IPV4_ADDR_OFFSET = 4;
62 static const int _IPV6_ADDR_OFFSET = 8;
63 static const int _IPV6_ADDR_LENGTH = 16;
50 64
51 static _InternetAddress LOOPBACK_IP_V4 = 65 static _InternetAddress LOOPBACK_IP_V4 =
52 new _InternetAddress.fixed(_ADDRESS_LOOPBACK_IP_V4); 66 new _InternetAddress.fixed(_ADDRESS_LOOPBACK_IP_V4);
53 static _InternetAddress LOOPBACK_IP_V6 = 67 static _InternetAddress LOOPBACK_IP_V6 =
54 new _InternetAddress.fixed(_ADDRESS_LOOPBACK_IP_V6); 68 new _InternetAddress.fixed(_ADDRESS_LOOPBACK_IP_V6);
55 static _InternetAddress ANY_IP_V4 = 69 static _InternetAddress ANY_IP_V4 =
56 new _InternetAddress.fixed(_ADDRESS_ANY_IP_V4); 70 new _InternetAddress.fixed(_ADDRESS_ANY_IP_V4);
57 static _InternetAddress ANY_IP_V6 = 71 static _InternetAddress ANY_IP_V6 =
58 new _InternetAddress.fixed(_ADDRESS_ANY_IP_V6); 72 new _InternetAddress.fixed(_ADDRESS_ANY_IP_V6);
59 73
60 final InternetAddressType type; 74 final InternetAddressType type;
61 final String address; 75 final String address;
62 final String host; 76 final String host;
63 final Uint8List _sockaddr_storage; 77 final Uint8List _sockaddr_storage;
64 78
79 bool get isLoopback {
80 switch (type) {
81 case InternetAddressType.IP_V4:
82 return _sockaddr_storage[_IPV4_ADDR_OFFSET] == 127;
83
84 case InternetAddressType.IP_V6:
85 for (int i = 0; i < _IPV6_ADDR_LENGTH - 1; i++) {
86 if (_sockaddr_storage[_IPV6_ADDR_OFFSET + i] != 0) return false;
87 }
88 int lastByteIndex = _IPV6_ADDR_OFFSET + _IPV6_ADDR_LENGTH - 1;
89 return _sockaddr_storage[lastByteIndex] == 1;
90 }
91 }
92
93 bool get isLinkLocal {
94 switch (type) {
95 case InternetAddressType.IP_V4:
96 // Checking for 169.254.0.0/16.
97 return _sockaddr_storage[_IPV4_ADDR_OFFSET] == 169 &&
98 _sockaddr_storage[_IPV4_ADDR_OFFSET + 1] == 254;
99
100 case InternetAddressType.IP_V6:
101 // Checking for fe80::/10.
102 return _sockaddr_storage[_IPV6_ADDR_OFFSET] == 0xFE &&
103 (_sockaddr_storage[_IPV6_ADDR_OFFSET + 1] & 0xB0) == 0x80;
104 }
105 }
106
65 _InternetAddress(InternetAddressType this.type, 107 _InternetAddress(InternetAddressType this.type,
66 String this.address, 108 String this.address,
67 String this.host, 109 String this.host,
68 List<int> this._sockaddr_storage); 110 List<int> this._sockaddr_storage);
69 111
70 factory _InternetAddress.fixed(int id) { 112 factory _InternetAddress.fixed(int id) {
71 var sockaddr = _fixed(id); 113 var sockaddr = _fixed(id);
72 switch (id) { 114 switch (id) {
73 case _ADDRESS_LOOPBACK_IP_V4: 115 case _ADDRESS_LOOPBACK_IP_V4:
74 return new _InternetAddress( 116 return new _InternetAddress(
(...skipping 19 matching lines...) Expand all
94 type, address, host, new Uint8List.fromList(_sockaddr_storage)); 136 type, address, host, new Uint8List.fromList(_sockaddr_storage));
95 } 137 }
96 138
97 String toString() { 139 String toString() {
98 return "InternetAddress('$address', ${type.name})"; 140 return "InternetAddress('$address', ${type.name})";
99 } 141 }
100 142
101 static Uint8List _fixed(int id) native "InternetAddress_Fixed"; 143 static Uint8List _fixed(int id) native "InternetAddress_Fixed";
102 } 144 }
103 145
146 class _NetworkInterface implements NetworkInterface{
147 final String name;
148 final List<InternetAddress> addresses;
149
150 _NetworkInterface(String this.name, List<InternetAddress> this.addresses);
151
152 String toString() {
153 return "NetworkInterface('$name', $addresses)";
154 }
155 }
156
104 157
105 // The _NativeSocket class encapsulates an OS socket. 158 // The _NativeSocket class encapsulates an OS socket.
106 class _NativeSocket extends NativeFieldWrapperClass1 { 159 class _NativeSocket extends NativeFieldWrapperClass1 {
107 // Bit flags used when communicating between the eventhandler and 160 // Bit flags used when communicating between the eventhandler and
108 // dart code. The EVENT flags are used to indicate events of 161 // dart code. The EVENT flags are used to indicate events of
109 // interest when sending a message from dart code to the 162 // interest when sending a message from dart code to the
110 // eventhandler. When receiving a message from the eventhandler the 163 // eventhandler. When receiving a message from the eventhandler the
111 // EVENT flags indicate the events that actually happened. The 164 // EVENT flags indicate the events that actually happened. The
112 // COMMAND flags are used to send commands from dart to the 165 // COMMAND flags are used to send commands from dart to the
113 // eventhandler. COMMAND flags are never received from the 166 // eventhandler. COMMAND flags are never received from the
(...skipping 16 matching lines...) Expand all
130 // Type flag send to the eventhandler providing additional 183 // Type flag send to the eventhandler providing additional
131 // information on the type of the file descriptor. 184 // information on the type of the file descriptor.
132 static const int LISTENING_SOCKET = 16; 185 static const int LISTENING_SOCKET = 16;
133 static const int PIPE_SOCKET = 17; 186 static const int PIPE_SOCKET = 17;
134 static const int TYPE_NORMAL_SOCKET = 0; 187 static const int TYPE_NORMAL_SOCKET = 0;
135 static const int TYPE_LISTENING_SOCKET = 1 << LISTENING_SOCKET; 188 static const int TYPE_LISTENING_SOCKET = 1 << LISTENING_SOCKET;
136 static const int TYPE_PIPE = 1 << PIPE_SOCKET; 189 static const int TYPE_PIPE = 1 << PIPE_SOCKET;
137 190
138 // Native port messages. 191 // Native port messages.
139 static const HOST_NAME_LOOKUP = 0; 192 static const HOST_NAME_LOOKUP = 0;
193 static const LIST_INTERFACES = 1;
140 194
141 // Socket close state 195 // Socket close state
142 bool isClosed = false; 196 bool isClosed = false;
143 bool isClosedRead = false; 197 bool isClosedRead = false;
144 bool isClosedWrite = false; 198 bool isClosedWrite = false;
145 Completer closeCompleter = new Completer(); 199 Completer closeCompleter = new Completer();
146 200
147 // Handlers and receive port for socket events from the event handler. 201 // Handlers and receive port for socket events from the event handler.
148 int eventMask = 0; 202 int eventMask = 0;
149 List eventHandlers; 203 List eventHandlers;
(...skipping 23 matching lines...) Expand all
173 throw createError(response, "Failed host name lookup"); 227 throw createError(response, "Failed host name lookup");
174 } else { 228 } else {
175 return response.skip(1).map((result) { 229 return response.skip(1).map((result) {
176 var type = new InternetAddressType._from(result[0]); 230 var type = new InternetAddressType._from(result[0]);
177 return new _InternetAddress(type, result[1], host, result[2]); 231 return new _InternetAddress(type, result[1], host, result[2]);
178 }).toList(); 232 }).toList();
179 } 233 }
180 }); 234 });
181 } 235 }
182 236
237 static Future<List<NetworkInterface>> listInterfaces({
238 bool includeLoopback: false,
239 bool includeLinkLocal: false,
240 InternetAddressType type: InternetAddressType.ANY}) {
241 ensureSocketService();
242 return socketService.call([LIST_INTERFACES, type._value])
243 .then((response) {
244 if (isErrorResponse(response)) {
245 throw createError(response, "Failed listing interfaces");
246 } else {
247 var list = new List<NetworkInterface>();
248 var map = response.skip(1)
249 .fold(new Map<String, List<InternetAddress>>(), (map, result) {
250 var type = new InternetAddressType._from(result[0]);
251 var name = result[3];
252 var address = new _InternetAddress(
253 type, result[1], "", result[2]);
254 if (!includeLinkLocal && address.isLinkLocal) return map;
255 if (!includeLoopback && address.isLoopback) return map;
256 map.putIfAbsent(name, () => new List<InternetAddress>());
257 map[name].add(address);
258 return map;
259 })
260 .forEach((name, addresses) {
261 list.add(new _NetworkInterface(name, addresses));
262 });
263 return list;
264 }
265 });
266 }
267
183 static Future<_NativeSocket> connect(host, int port) { 268 static Future<_NativeSocket> connect(host, int port) {
184 return new Future.value(host) 269 return new Future.value(host)
185 .then((host) { 270 .then((host) {
186 if (host is _InternetAddress) return host; 271 if (host is _InternetAddress) return host;
187 return lookup(host) 272 return lookup(host)
188 .then((list) { 273 .then((list) {
189 if (list.length == 0) { 274 if (list.length == 0) {
190 throw createError(response, "Failed host name lookup"); 275 throw createError(response, "Failed host name lookup");
191 } 276 }
192 return list[0]; 277 return list[0];
(...skipping 878 matching lines...) Expand 10 before | Expand all | Expand 10 after
1071 if (_detachReady != null) { 1156 if (_detachReady != null) {
1072 _detachReady.complete(null); 1157 _detachReady.complete(null);
1073 } else { 1158 } else {
1074 if (_raw != null) { 1159 if (_raw != null) {
1075 _raw.shutdown(SocketDirection.SEND); 1160 _raw.shutdown(SocketDirection.SEND);
1076 _disableWriteEvent(); 1161 _disableWriteEvent();
1077 } 1162 }
1078 } 1163 }
1079 } 1164 }
1080 } 1165 }
OLDNEW
« no previous file with comments | « runtime/bin/socket_macos.cc ('k') | runtime/bin/socket_win.h » ('j') | runtime/bin/socket_win.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698