]> code.delx.au - gnu-emacs/blobdiff - src/gnutls.c
Merge from trunk.
[gnu-emacs] / src / gnutls.c
index 99fc5c10e2b6009f81f6d5a12aba6bd98a323382..8dbf01cedc9cf666604a206d165849921927ad05 100644 (file)
@@ -247,18 +247,27 @@ init_gnutls_functions (Lisp_Object libraries)
 #endif /* !WINDOWSNT */
 
 \f
+/* Function to log a simple message.  */
 static void
 gnutls_log_function (int level, const char* string)
 {
   message ("gnutls.c: [%d] %s", level, string);
 }
 
+/* Function to log a message and a string.  */
 static void
 gnutls_log_function2 (int level, const char* string, const char* extra)
 {
   message ("gnutls.c: [%d] %s %s", level, string, extra);
 }
 
+/* Function to log a message and an integer.  */
+static void
+gnutls_log_function2i (int level, const char* string, int extra)
+{
+  message ("gnutls.c: [%d] %s %d", level, string, extra);
+}
+
 static int
 emacs_gnutls_handshake (struct Lisp_Process *proc)
 {
@@ -399,10 +408,25 @@ emacs_gnutls_read (struct Lisp_Process *proc, char *buf, ptrdiff_t nbyte)
   ssize_t rtnval;
   gnutls_session_t state = proc->gnutls_state;
 
+  int log_level = proc->gnutls_log_level;
+
   if (proc->gnutls_initstage != GNUTLS_STAGE_READY)
     {
-      emacs_gnutls_handshake (proc);
-      return -1;
+      /* If the handshake count is under the limit, try the handshake
+         again and increment the handshake count.  This count is kept
+         per process (connection), not globally.  */
+      if (proc->gnutls_handshakes_tried < GNUTLS_EMACS_HANDSHAKES_LIMIT)
+        {
+          proc->gnutls_handshakes_tried++;
+          emacs_gnutls_handshake (proc);
+          GNUTLS_LOG2i (5, log_level, "Retried handshake", 
+                        proc->gnutls_handshakes_tried);
+          return -1;
+        }
+
+      GNUTLS_LOG (2, log_level, "Giving up on handshake; resetting retries");
+      proc->gnutls_handshakes_tried = 0;
+      return 0;
     }
   rtnval = fn_gnutls_record_recv (state, buf, nbyte);
   if (rtnval >= 0)