JKRhb / dtls2

A DTLS library for Dart based on OpenSSL.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Catch the unhandled Exception Network is unreachable

Ifilehk opened this issue · comments

This exception occurs when during an open connection the adapter is turned off. Not really annoying on Desktops but on mobiles where we often turn WIFI on and off a must. I realized that this exception cannot be catch for what ever isolates reasons but a check of number of returned written bytes from _socket.send can help.

Unhandled exception:
SocketException: Send failed (OS Error: Network is unreachable, errno = 101), address = ::, port = 8888
#0 _NativeSocket.send (dart:io-patch/socket_patch.dart:1224:34)
#1 _RawDatagramSocket.send (dart:io-patch/socket_patch.dart:2491:15)
#2 _DtlsServerConnection._maintainOutgoing (package:dtls2/src/dtls_server.dart:315:27)
#3 _DtlsServerConnection._maintainState (package:dtls2/src/dtls_server.dart:282:9)
#4 _DtlsServerConnection.close (package:dtls2/src/dtls_server.dart:350:7)
#5 Server.connect.. (package:ptt_server/server_socket.dart:333:24)
#6 ConnectionTimer.start. (package:ptt_server/server_socket.dart:268:16)
#7 Timer._createTimer. (dart:async-patch/timer_patch.dart:18:15)
#8 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398:19)
#9 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429:5)
#10 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:192:26)

Thank you for pointing this out, @Ifilehk! Do you already have a potential patch ready that could be applied to address this issue?

For the client side

--- a/original
+++ b/patch
@@ -1,23 +1,30 @@
   void _connectToPeer() {
     final ret = _libSsl.SSL_connect(_ssl);
-    _maintainOutgoing();
+    int res = _maintainOutgoing();
     if (ret == 1) {
       _connected = true;
       _connectCompleter.complete(this);
     } else if (ret == 0) {
-      _connectCompleter.completeError(DtlsException('handshake shut down'));
+        _connectCompleter.completeError(DtlsException('handshake shut down'));
     } else {
-      _handleError(ret, _connectCompleter.completeError);
+      if (res == 0) {
+        _connectCompleter.completeError(SocketException('Network is unreachable'));
+      } else {
+        _handleError(ret, _connectCompleter.completeError);
+      }
     }
   }
 
-  void _maintainOutgoing() {
+
+  int _maintainOutgoing() {
     final ret = _libCrypto.BIO_read(_wbio, buffer.cast(), bufferSize);
+    int r = -1;
     if (ret > 0) {
-      _dtlsClient._socket.send(buffer.asTypedList(ret), _address, _port);
+      r = _dtlsClient._socket.send(buffer.asTypedList(ret), _address, _port);
     }
     _timer?.cancel();
     if (_libSsl.SSL_ctrl(_ssl, DTLS_CTRL_GET_TIMEOUT, 0, buffer.cast()) > 0) {
       _timer = Timer(buffer.cast<timeval>().ref.duration, _maintainState);
     }
+    return r;
   }
``

For the server side adapter off => unhandled exception and death of the process. Hope dart:io will catch it one day, or if you have a clue let me know ...

Can you have a look at #55? After merging I will release a new patch version.