From 4a773328a009f9111f0e7cd5c487ed337b589411 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Anthony=20L=C3=A9onard?=
 <anthony.leonard@savoirfairelinux.com>
Date: Fri, 7 Jul 2017 16:17:00 -0400
Subject: [PATCH] tls: set errno with gnutls function on Windows

On some platforms, such as Windows, the errno variable is not a
reliable way to send an error code to GnuTLS. A function called
gnutls_transport_set_errno is provided as a better way for push/pull
callbacks to return those error codes to GnuTLS.

We now use it in the push callback which prevent cases where a TLS
session could be terminated due to a misreading of errno by GnuTLS
(especially if an EAGAIN error code is to be returned).

Moreover, as the SIP session MTU is queried during media session setup,
we ensure that the session is still alive at this moment. If not, we
throw a runtime error as it is a nonsense to establish a media
communication if SIP is dead.

Change-Id: Id9220f1b3c7feea72e6ad18481fc039b4b5a2f4e
Reviewed-by: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
---
 src/security/tls_session.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/security/tls_session.cpp b/src/security/tls_session.cpp
index 5753dacb07..2207e0d1a6 100644
--- a/src/security/tls_session.cpp
+++ b/src/security/tls_session.cpp
@@ -524,6 +524,9 @@ TlsSession::sendRaw(const void* buf, size_t size)
         stTxRawBytesCnt_ += size;
         return ret;
     }
+
+    // Must be called to pass errno value to GnuTLS on Windows (cf. GnuTLS doc)
+    gnutls_transport_set_errno(session_, errno);
     return -1;
 }
 
@@ -1113,6 +1116,8 @@ DhParams::generate()
 uint16_t
 TlsSession::getMtu()
 {
+    if (state_ == TlsSessionState::SHUTDOWN)
+        throw std::runtime_error("Getting MTU from dead TLS session.");
     return gnutls_dtls_get_mtu(session_);
 }
 
-- 
GitLab