diff common-session.c @ 970:0bb16232e7c4

Make keepalive handling more robust, this should now match what OpenSSH does
author Matt Johnston <matt@ucc.asn.au>
date Tue, 19 Aug 2014 23:08:56 +0800
parents f7f6c15b0ec3
children 73ea0dce9a57 363c0feca5d4
line wrap: on
line diff
--- a/common-session.c	Wed Aug 13 22:07:43 2014 +0800
+++ b/common-session.c	Tue Aug 19 23:08:56 2014 +0800
@@ -394,19 +394,30 @@
 	return pos+1;
 }
 
-void ignore_recv_msg_request_failure() {
+void ignore_recv_response() {
 	// Do nothing
-	TRACE(("Ignored msg_request_failure"))
+	TRACE(("Ignored msg_request_response"))
 }
 
 static void send_msg_keepalive() {
 	CHECKCLEARTOWRITE();
 	time_t old_time_idle = ses.last_packet_time_idle;
-	/* Try to force a response from the other end. Some peers will
-	reply with SSH_MSG_REQUEST_FAILURE, some will reply with SSH_MSG_UNIMPLEMENTED */
-	buf_putbyte(ses.writepayload, SSH_MSG_GLOBAL_REQUEST);
-	/* A short string */
-	buf_putstring(ses.writepayload, "[email protected]", 0);
+
+	struct Channel *chan = get_any_ready_channel();
+
+	if (chan) {
+		/* Channel requests are preferable, more implementations
+		handle them than SSH_MSG_GLOBAL_REQUEST */
+		TRACE(("keepalive channel request %d", chan->index))
+		start_send_channel_request(chan, DROPBEAR_KEEPALIVE_STRING);
+	} else {
+		TRACE(("keepalive global request"))
+		/* Some peers will reply with SSH_MSG_REQUEST_FAILURE, 
+		some will reply with SSH_MSG_UNIMPLEMENTED, some will exit. */
+		buf_putbyte(ses.writepayload, SSH_MSG_GLOBAL_REQUEST); 
+		buf_putstring(ses.writepayload, DROPBEAR_KEEPALIVE_STRING,
+			strlen(DROPBEAR_KEEPALIVE_STRING));
+	}
 	buf_putbyte(ses.writepayload, 1); /* want_reply */
 	encrypt_packet();
 
@@ -435,7 +446,10 @@
 		send_msg_kexinit();
 	}
 	
-	if (opts.keepalive_secs > 0) {
+	if (opts.keepalive_secs > 0 && ses.authstate.authdone) {
+		/* Avoid sending keepalives prior to auth - those are
+		not valid pre-auth packet types */
+
 		/* Send keepalives if we've been idle */
 		if (now - ses.last_packet_time_any_sent >= opts.keepalive_secs) {
 			send_msg_keepalive();