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

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

Issue 2361373003: Throw an ArgumentError if the port is out of range (Closed)
Patch Set: Address comments Created 4 years, 2 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
« no previous file with comments | « no previous file | tests/standalone/io/socket_invalid_arguments_test.dart » ('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 @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 bool shared: false}) { 10 bool shared: false}) {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 bool includeLinkLocal: false, 63 bool includeLinkLocal: false,
64 InternetAddressType type: InternetAddressType.ANY}) { 64 InternetAddressType type: InternetAddressType.ANY}) {
65 return _NativeSocket.listInterfaces(includeLoopback: includeLoopback, 65 return _NativeSocket.listInterfaces(includeLoopback: includeLoopback,
66 includeLinkLocal: includeLinkLocal, 66 includeLinkLocal: includeLinkLocal,
67 type: type); 67 type: type);
68 } 68 }
69 69
70 static bool _listSupported() native "NetworkInterface_ListSupported"; 70 static bool _listSupported() native "NetworkInterface_ListSupported";
71 } 71 }
72 72
73 void _throwOnBadPort(int port) {
74 if ((port == null) || (port < 0) || (port > 0xFFFF)) {
75 throw new ArgumentError("Invalid port $port");
76 }
77 }
78
73 class _InternetAddress implements InternetAddress { 79 class _InternetAddress implements InternetAddress {
74 static const int _ADDRESS_LOOPBACK_IP_V4 = 0; 80 static const int _ADDRESS_LOOPBACK_IP_V4 = 0;
75 static const int _ADDRESS_LOOPBACK_IP_V6 = 1; 81 static const int _ADDRESS_LOOPBACK_IP_V6 = 1;
76 static const int _ADDRESS_ANY_IP_V4 = 2; 82 static const int _ADDRESS_ANY_IP_V4 = 2;
77 static const int _ADDRESS_ANY_IP_V6 = 3; 83 static const int _ADDRESS_ANY_IP_V6 = 3;
78 static const int _IPV4_ADDR_LENGTH = 4; 84 static const int _IPV4_ADDR_LENGTH = 4;
79 static const int _IPV6_ADDR_LENGTH = 16; 85 static const int _IPV6_ADDR_LENGTH = 16;
80 86
81 static _InternetAddress LOOPBACK_IP_V4 = 87 static _InternetAddress LOOPBACK_IP_V4 =
82 new _InternetAddress.fixed(_ADDRESS_LOOPBACK_IP_V4); 88 new _InternetAddress.fixed(_ADDRESS_LOOPBACK_IP_V4);
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 name, () => new _NetworkInterface(name, index)); 386 name, () => new _NetworkInterface(name, index));
381 map[name].addresses.add(address); 387 map[name].addresses.add(address);
382 return map; 388 return map;
383 }); 389 });
384 return map.values.toList(); 390 return map.values.toList();
385 } 391 }
386 }); 392 });
387 } 393 }
388 394
389 static Future<_NativeSocket> connect(host, int port, sourceAddress) { 395 static Future<_NativeSocket> connect(host, int port, sourceAddress) {
396 _throwOnBadPort(port);
390 if (sourceAddress != null && sourceAddress is! _InternetAddress) { 397 if (sourceAddress != null && sourceAddress is! _InternetAddress) {
391 if (sourceAddress is String) { 398 if (sourceAddress is String) {
392 sourceAddress = new InternetAddress(sourceAddress); 399 sourceAddress = new InternetAddress(sourceAddress);
393 } 400 }
394 } 401 }
395 return new Future.value(host) 402 return new Future.value(host)
396 .then((host) { 403 .then((host) {
397 if (host is _InternetAddress) return [host]; 404 if (host is _InternetAddress) return [host];
398 return lookup(host) 405 return lookup(host)
399 .then((addresses) { 406 .then((addresses) {
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
481 connectNext(); 488 connectNext();
482 return completer.future; 489 return completer.future;
483 }); 490 });
484 } 491 }
485 492
486 static Future<_NativeSocket> bind(host, 493 static Future<_NativeSocket> bind(host,
487 int port, 494 int port,
488 int backlog, 495 int backlog,
489 bool v6Only, 496 bool v6Only,
490 bool shared) { 497 bool shared) {
498 _throwOnBadPort(port);
491 return new Future.value(host) 499 return new Future.value(host)
492 .then((host) { 500 .then((host) {
493 if (host is _InternetAddress) return host; 501 if (host is _InternetAddress) return host;
494 return lookup(host) 502 return lookup(host)
495 .then((list) { 503 .then((list) {
496 if (list.length == 0) { 504 if (list.length == 0) {
497 throw createError(response, "Failed host lookup: '$host'"); 505 throw createError(response, "Failed host lookup: '$host'");
498 } 506 }
499 return list[0]; 507 return list[0];
500 }); 508 });
(...skipping 18 matching lines...) Expand all
519 return socket; 527 return socket;
520 }); 528 });
521 } 529 }
522 530
523 static void setupResourceInfo(_NativeSocket socket) { 531 static void setupResourceInfo(_NativeSocket socket) {
524 socket.resourceInfo = new _SocketResourceInfo(socket); 532 socket.resourceInfo = new _SocketResourceInfo(socket);
525 } 533 }
526 534
527 static Future<_NativeSocket> bindDatagram( 535 static Future<_NativeSocket> bindDatagram(
528 host, int port, bool reuseAddress) { 536 host, int port, bool reuseAddress) {
537 _throwOnBadPort(port);
529 return new Future.value(host) 538 return new Future.value(host)
530 .then((host) { 539 .then((host) {
531 if (host is _InternetAddress) return host; 540 if (host is _InternetAddress) return host;
532 return lookup(host) 541 return lookup(host)
533 .then((list) { 542 .then((list) {
534 if (list.length == 0) { 543 if (list.length == 0) {
535 throw createError(response, "Failed host lookup: '$host'"); 544 throw createError(response, "Failed host lookup: '$host'");
536 } 545 }
537 return list[0]; 546 return list[0];
538 }); 547 });
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
669 // TODO(ricow): Remove when we track internal and pipe uses. 678 // TODO(ricow): Remove when we track internal and pipe uses.
670 assert(resourceInfo != null || isPipe || isInternal); 679 assert(resourceInfo != null || isPipe || isInternal);
671 if (resourceInfo != null) { 680 if (resourceInfo != null) {
672 resourceInfo.addWrite(result); 681 resourceInfo.addWrite(result);
673 } 682 }
674 return result; 683 return result;
675 } 684 }
676 685
677 int send(List<int> buffer, int offset, int bytes, 686 int send(List<int> buffer, int offset, int bytes,
678 InternetAddress address, int port) { 687 InternetAddress address, int port) {
688 _throwOnBadPort(port);
679 if (isClosing || isClosed) return 0; 689 if (isClosing || isClosed) return 0;
680 _BufferAndStart bufferAndStart = 690 _BufferAndStart bufferAndStart =
681 _ensureFastAndSerializableByteData( 691 _ensureFastAndSerializableByteData(
682 buffer, offset, bytes); 692 buffer, offset, bytes);
683 var result = nativeSendTo( 693 var result = nativeSendTo(
684 bufferAndStart.buffer, bufferAndStart.start, bytes, 694 bufferAndStart.buffer, bufferAndStart.start, bytes,
685 address._in_addr, port); 695 address._in_addr, port);
686 if (result is OSError) { 696 if (result is OSError) {
687 scheduleMicrotask(() => reportError(result, "Send failed")); 697 scheduleMicrotask(() => reportError(result, "Send failed"));
688 result = 0; 698 result = 0;
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after
1109 final _NativeSocket _socket; 1119 final _NativeSocket _socket;
1110 StreamController<RawSocket> _controller; 1120 StreamController<RawSocket> _controller;
1111 ReceivePort _referencePort; 1121 ReceivePort _referencePort;
1112 bool _v6Only; 1122 bool _v6Only;
1113 1123
1114 static Future<_RawServerSocket> bind(address, 1124 static Future<_RawServerSocket> bind(address,
1115 int port, 1125 int port,
1116 int backlog, 1126 int backlog,
1117 bool v6Only, 1127 bool v6Only,
1118 bool shared) { 1128 bool shared) {
1119 if (port < 0 || port > 0xFFFF) 1129 _throwOnBadPort(port);
1120 throw new ArgumentError("Invalid port $port");
1121 if (backlog < 0) throw new ArgumentError("Invalid backlog $backlog"); 1130 if (backlog < 0) throw new ArgumentError("Invalid backlog $backlog");
1122 return _NativeSocket.bind(address, port, backlog, v6Only, shared) 1131 return _NativeSocket.bind(address, port, backlog, v6Only, shared)
1123 .then((socket) => new _RawServerSocket(socket, v6Only)); 1132 .then((socket) => new _RawServerSocket(socket, v6Only));
1124 } 1133 }
1125 1134
1126 _RawServerSocket(this._socket, this._v6Only); 1135 _RawServerSocket(this._socket, this._v6Only);
1127 1136
1128 StreamSubscription<RawSocket> listen(void onData(RawSocket event), 1137 StreamSubscription<RawSocket> listen(void onData(RawSocket event),
1129 {Function onError, 1138 {Function onError,
1130 void onDone(), 1139 void onDone(),
(...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after
1755 }, 1764 },
1756 error: zone.bindUnaryCallback((e) { 1765 error: zone.bindUnaryCallback((e) {
1757 _controller.addError(e); 1766 _controller.addError(e);
1758 _socket.close(); 1767 _socket.close();
1759 }) 1768 })
1760 ); 1769 );
1761 } 1770 }
1762 1771
1763 static Future<RawDatagramSocket> bind( 1772 static Future<RawDatagramSocket> bind(
1764 host, int port, bool reuseAddress) { 1773 host, int port, bool reuseAddress) {
1765 if (port < 0 || port > 0xffff) 1774 _throwOnBadPort(port);
1766 throw new ArgumentError("Invalid port $port");
1767 return _NativeSocket.bindDatagram(host, port, reuseAddress) 1775 return _NativeSocket.bindDatagram(host, port, reuseAddress)
1768 .then((socket) => new _RawDatagramSocket(socket)); 1776 .then((socket) => new _RawDatagramSocket(socket));
1769 } 1777 }
1770 1778
1771 StreamSubscription<RawSocketEvent> listen(void onData(RawSocketEvent event), 1779 StreamSubscription<RawSocketEvent> listen(void onData(RawSocketEvent event),
1772 {Function onError, 1780 {Function onError,
1773 void onDone(), 1781 void onDone(),
1774 bool cancelOnError}) { 1782 bool cancelOnError}) {
1775 return _controller.stream.listen( 1783 return _controller.stream.listen(
1776 onData, 1784 onData,
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
1864 Datagram _makeDatagram(List<int> data, 1872 Datagram _makeDatagram(List<int> data,
1865 String address, 1873 String address,
1866 List<int> in_addr, 1874 List<int> in_addr,
1867 int port) { 1875 int port) {
1868 return new Datagram( 1876 return new Datagram(
1869 data, 1877 data,
1870 new _InternetAddress(address, null, in_addr), 1878 new _InternetAddress(address, null, in_addr),
1871 port); 1879 port);
1872 } 1880 }
1873 1881
OLDNEW
« no previous file with comments | « no previous file | tests/standalone/io/socket_invalid_arguments_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698