Chromium Code Reviews| 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 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 Loading... | |
| 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; | |
| 50 | 63 |
| 51 static _InternetAddress LOOPBACK_IP_V4 = | 64 static _InternetAddress LOOPBACK_IP_V4 = |
| 52 new _InternetAddress.fixed(_ADDRESS_LOOPBACK_IP_V4); | 65 new _InternetAddress.fixed(_ADDRESS_LOOPBACK_IP_V4); |
| 53 static _InternetAddress LOOPBACK_IP_V6 = | 66 static _InternetAddress LOOPBACK_IP_V6 = |
| 54 new _InternetAddress.fixed(_ADDRESS_LOOPBACK_IP_V6); | 67 new _InternetAddress.fixed(_ADDRESS_LOOPBACK_IP_V6); |
| 55 static _InternetAddress ANY_IP_V4 = | 68 static _InternetAddress ANY_IP_V4 = |
| 56 new _InternetAddress.fixed(_ADDRESS_ANY_IP_V4); | 69 new _InternetAddress.fixed(_ADDRESS_ANY_IP_V4); |
| 57 static _InternetAddress ANY_IP_V6 = | 70 static _InternetAddress ANY_IP_V6 = |
| 58 new _InternetAddress.fixed(_ADDRESS_ANY_IP_V6); | 71 new _InternetAddress.fixed(_ADDRESS_ANY_IP_V6); |
| 59 | 72 |
| 60 final InternetAddressType type; | 73 final InternetAddressType type; |
| 61 final String address; | 74 final String address; |
| 62 final String host; | 75 final String host; |
| 63 final Uint8List _sockaddr_storage; | 76 final Uint8List _sockaddr_storage; |
| 64 | 77 |
| 78 bool get isLoopback { | |
| 79 switch (type) { | |
| 80 case InternetAddressType.IP_V4: | |
| 81 return _sockaddr_storage[_IPV4_ADDR_OFFSET] == 127; | |
| 82 | |
| 83 case InternetAddressType.IP_V6: | |
| 84 for (int i = 0; i < 15; i++) { | |
|
Søren Gjesse
2013/06/20 14:38:17
15 -> _IPV6_ADDR_LENGTH
Anders Johnsen
2013/06/20 15:00:25
Done.
| |
| 85 if (_sockaddr_storage[_IPV6_ADDR_OFFSET + i] != 0) return false; | |
| 86 } | |
| 87 return _sockaddr_storage[_IPV6_ADDR_OFFSET + 15] == 1; | |
|
Søren Gjesse
2013/06/20 14:38:17
Ditto.
Anders Johnsen
2013/06/20 15:00:25
Done.
| |
| 88 } | |
| 89 } | |
| 90 | |
| 91 bool get isLinkLocal { | |
| 92 switch (type) { | |
|
Søren Gjesse
2013/06/20 14:38:17
Please add comment here "Checking for 169.256.0.0/
Anders Johnsen
2013/06/20 15:00:25
Done.
| |
| 93 case InternetAddressType.IP_V4: | |
| 94 return _sockaddr_storage[_IPV4_ADDR_OFFSET] == 169 && | |
| 95 _sockaddr_storage[_IPV4_ADDR_OFFSET + 1] == 254; | |
| 96 | |
| 97 case InternetAddressType.IP_V6: | |
|
Søren Gjesse
2013/06/20 14:38:17
Ditto.
Anders Johnsen
2013/06/20 15:00:25
Done.
| |
| 98 return _sockaddr_storage[_IPV6_ADDR_OFFSET] == 0xFE && | |
| 99 (_sockaddr_storage[_IPV6_ADDR_OFFSET + 1] & 0xB0) == 0x80; | |
| 100 } | |
| 101 } | |
| 102 | |
| 65 _InternetAddress(InternetAddressType this.type, | 103 _InternetAddress(InternetAddressType this.type, |
| 66 String this.address, | 104 String this.address, |
| 67 String this.host, | 105 String this.host, |
| 68 List<int> this._sockaddr_storage); | 106 List<int> this._sockaddr_storage); |
| 69 | 107 |
| 70 factory _InternetAddress.fixed(int id) { | 108 factory _InternetAddress.fixed(int id) { |
| 71 var sockaddr = _fixed(id); | 109 var sockaddr = _fixed(id); |
| 72 switch (id) { | 110 switch (id) { |
| 73 case _ADDRESS_LOOPBACK_IP_V4: | 111 case _ADDRESS_LOOPBACK_IP_V4: |
| 74 return new _InternetAddress( | 112 return new _InternetAddress( |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 88 } | 126 } |
| 89 } | 127 } |
| 90 | 128 |
| 91 String toString() { | 129 String toString() { |
| 92 return "InternetAddress('$address', ${type.name})"; | 130 return "InternetAddress('$address', ${type.name})"; |
| 93 } | 131 } |
| 94 | 132 |
| 95 static Uint8List _fixed(int id) native "InternetAddress_Fixed"; | 133 static Uint8List _fixed(int id) native "InternetAddress_Fixed"; |
| 96 } | 134 } |
| 97 | 135 |
| 136 class _NetworkInterface implements NetworkInterface{ | |
| 137 final String name; | |
| 138 final List<InternetAddress> addresses; | |
| 139 | |
| 140 _NetworkInterface(String this.name, List<InternetAddress> this.addresses); | |
| 141 | |
| 142 String toString() { | |
| 143 return "NetworkInterface('$name', $addresses)"; | |
| 144 } | |
| 145 } | |
| 146 | |
| 98 | 147 |
| 99 // The _NativeSocket class encapsulates an OS socket. | 148 // The _NativeSocket class encapsulates an OS socket. |
| 100 class _NativeSocket extends NativeFieldWrapperClass1 { | 149 class _NativeSocket extends NativeFieldWrapperClass1 { |
| 101 // Bit flags used when communicating between the eventhandler and | 150 // Bit flags used when communicating between the eventhandler and |
| 102 // dart code. The EVENT flags are used to indicate events of | 151 // dart code. The EVENT flags are used to indicate events of |
| 103 // interest when sending a message from dart code to the | 152 // interest when sending a message from dart code to the |
| 104 // eventhandler. When receiving a message from the eventhandler the | 153 // eventhandler. When receiving a message from the eventhandler the |
| 105 // EVENT flags indicate the events that actually happened. The | 154 // EVENT flags indicate the events that actually happened. The |
| 106 // COMMAND flags are used to send commands from dart to the | 155 // COMMAND flags are used to send commands from dart to the |
| 107 // eventhandler. COMMAND flags are never received from the | 156 // eventhandler. COMMAND flags are never received from the |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 124 // Type flag send to the eventhandler providing additional | 173 // Type flag send to the eventhandler providing additional |
| 125 // information on the type of the file descriptor. | 174 // information on the type of the file descriptor. |
| 126 static const int LISTENING_SOCKET = 16; | 175 static const int LISTENING_SOCKET = 16; |
| 127 static const int PIPE_SOCKET = 17; | 176 static const int PIPE_SOCKET = 17; |
| 128 static const int TYPE_NORMAL_SOCKET = 0; | 177 static const int TYPE_NORMAL_SOCKET = 0; |
| 129 static const int TYPE_LISTENING_SOCKET = 1 << LISTENING_SOCKET; | 178 static const int TYPE_LISTENING_SOCKET = 1 << LISTENING_SOCKET; |
| 130 static const int TYPE_PIPE = 1 << PIPE_SOCKET; | 179 static const int TYPE_PIPE = 1 << PIPE_SOCKET; |
| 131 | 180 |
| 132 // Native port messages. | 181 // Native port messages. |
| 133 static const HOST_NAME_LOOKUP = 0; | 182 static const HOST_NAME_LOOKUP = 0; |
| 183 static const LIST_INTERFACES = 1; | |
| 134 | 184 |
| 135 // Socket close state | 185 // Socket close state |
| 136 bool isClosed = false; | 186 bool isClosed = false; |
| 137 bool isClosedRead = false; | 187 bool isClosedRead = false; |
| 138 bool isClosedWrite = false; | 188 bool isClosedWrite = false; |
| 139 Completer closeCompleter = new Completer(); | 189 Completer closeCompleter = new Completer(); |
| 140 | 190 |
| 141 // Handlers and receive port for socket events from the event handler. | 191 // Handlers and receive port for socket events from the event handler. |
| 142 int eventMask = 0; | 192 int eventMask = 0; |
| 143 List eventHandlers; | 193 List eventHandlers; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 167 throw createError(response, "Failed host name lookup"); | 217 throw createError(response, "Failed host name lookup"); |
| 168 } else { | 218 } else { |
| 169 return response.skip(1).map((result) { | 219 return response.skip(1).map((result) { |
| 170 var type = new InternetAddressType._from(result[0]); | 220 var type = new InternetAddressType._from(result[0]); |
| 171 return new _InternetAddress(type, result[1], host, result[2]); | 221 return new _InternetAddress(type, result[1], host, result[2]); |
| 172 }).toList(); | 222 }).toList(); |
| 173 } | 223 } |
| 174 }); | 224 }); |
| 175 } | 225 } |
| 176 | 226 |
| 227 static Future<List<NetworkInterface>> listInterfaces({ | |
| 228 bool includeLoopback: false, | |
| 229 bool includeLinkLocal: false, | |
| 230 InternetAddressType type: InternetAddressType.ANY}) { | |
| 231 ensureSocketService(); | |
| 232 return socketService.call([LIST_INTERFACES, type._value]) | |
| 233 .then((response) { | |
| 234 if (isErrorResponse(response)) { | |
| 235 throw createError(response, "Failed listing interfaces"); | |
| 236 } else { | |
| 237 var list = new List<NetworkInterface>(); | |
| 238 var map = response.skip(1) | |
| 239 .fold(new Map<String, List<InternetAddress>>(), (map, result) { | |
| 240 var type = new InternetAddressType._from(result[0]); | |
| 241 var name = result[3]; | |
| 242 var address = new _InternetAddress( | |
| 243 type, result[1], "", result[2]); | |
| 244 if (!includeLinkLocal && address.isLinkLocal) return map; | |
| 245 if (!includeLoopback && address.isLoopback) return map; | |
| 246 map.putIfAbsent(name, () => new List<InternetAddress>()); | |
| 247 map[name].add(address); | |
| 248 return map; | |
| 249 }) | |
| 250 .forEach((name, addresses) { | |
| 251 list.add(new _NetworkInterface(name, addresses)); | |
| 252 }); | |
| 253 return list; | |
| 254 } | |
| 255 }); | |
| 256 } | |
| 257 | |
| 177 static Future<_NativeSocket> connect(host, int port) { | 258 static Future<_NativeSocket> connect(host, int port) { |
| 178 return new Future.value(host) | 259 return new Future.value(host) |
| 179 .then((host) { | 260 .then((host) { |
| 180 if (host is _InternetAddress) return host; | 261 if (host is _InternetAddress) return host; |
| 181 return lookup(host) | 262 return lookup(host) |
| 182 .then((list) { | 263 .then((list) { |
| 183 if (list.length == 0) { | 264 if (list.length == 0) { |
| 184 throw createError(response, "Failed host name lookup"); | 265 throw createError(response, "Failed host name lookup"); |
| 185 } | 266 } |
| 186 return list[0]; | 267 return list[0]; |
| (...skipping 878 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1065 if (_detachReady != null) { | 1146 if (_detachReady != null) { |
| 1066 _detachReady.complete(null); | 1147 _detachReady.complete(null); |
| 1067 } else { | 1148 } else { |
| 1068 if (_raw != null) { | 1149 if (_raw != null) { |
| 1069 _raw.shutdown(SocketDirection.SEND); | 1150 _raw.shutdown(SocketDirection.SEND); |
| 1070 _disableWriteEvent(); | 1151 _disableWriteEvent(); |
| 1071 } | 1152 } |
| 1072 } | 1153 } |
| 1073 } | 1154 } |
| 1074 } | 1155 } |
| OLD | NEW |