comparison packet.c @ 939:a0819ecfee0b

Make -K keepalive behave like OpenSSH's ServerAliveInterval
author Matt Johnston <matt@ucc.asn.au>
date Wed, 09 Jul 2014 00:15:20 +0800
parents 68723d66dec6
children c4f138dae2fd
comparison
equal deleted inserted replaced
938:c88dce72f6d2 939:a0819ecfee0b
55 /* non-blocking function writing out a current encrypted packet */ 55 /* non-blocking function writing out a current encrypted packet */
56 void write_packet() { 56 void write_packet() {
57 57
58 int len, written; 58 int len, written;
59 buffer * writebuf = NULL; 59 buffer * writebuf = NULL;
60 time_t now;
61 unsigned packet_type; 60 unsigned packet_type;
62 int all_ignore = 1;
63 #ifdef HAVE_WRITEV 61 #ifdef HAVE_WRITEV
64 struct iovec *iov = NULL; 62 struct iovec *iov = NULL;
65 int i; 63 int i;
66 struct Link *l; 64 struct Link *l;
67 int iov_max_count; 65 int iov_max_count;
88 { 86 {
89 writebuf = (buffer*)l->item; 87 writebuf = (buffer*)l->item;
90 packet_type = writebuf->data[writebuf->len-1]; 88 packet_type = writebuf->data[writebuf->len-1];
91 len = writebuf->len - 1 - writebuf->pos; 89 len = writebuf->len - 1 - writebuf->pos;
92 dropbear_assert(len > 0); 90 dropbear_assert(len > 0);
93 all_ignore &= (packet_type == SSH_MSG_IGNORE);
94 TRACE2(("write_packet writev #%d type %d len %d/%d", i, packet_type, 91 TRACE2(("write_packet writev #%d type %d len %d/%d", i, packet_type,
95 len, writebuf->len-1)) 92 len, writebuf->len-1))
96 iov[i].iov_base = buf_getptr(writebuf, len); 93 iov[i].iov_base = buf_getptr(writebuf, len);
97 iov[i].iov_len = len; 94 iov[i].iov_len = len;
98 } 95 }
144 return; 141 return;
145 } else { 142 } else {
146 dropbear_exit("Error writing: %s", strerror(errno)); 143 dropbear_exit("Error writing: %s", strerror(errno));
147 } 144 }
148 } 145 }
149 all_ignore = (packet_type == SSH_MSG_IGNORE);
150 146
151 if (written == 0) { 147 if (written == 0) {
152 ses.remoteclosed(); 148 ses.remoteclosed();
153 } 149 }
154 150
160 } else { 156 } else {
161 /* More packet left to write, leave it in the queue for later */ 157 /* More packet left to write, leave it in the queue for later */
162 buf_incrpos(writebuf, written); 158 buf_incrpos(writebuf, written);
163 } 159 }
164 #endif /* writev */ 160 #endif /* writev */
165
166 now = monotonic_now();
167 ses.last_trx_packet_time = now;
168
169 if (!all_ignore) {
170 ses.last_packet_time = now;
171 }
172 161
173 TRACE2(("leave write_packet")) 162 TRACE2(("leave write_packet"))
174 } 163 }
175 164
176 /* Non-blocking function reading available portion of a packet into the 165 /* Non-blocking function reading available portion of a packet into the
513 buffer * writebuf; /* the packet which will go on the wire. This is 502 buffer * writebuf; /* the packet which will go on the wire. This is
514 encrypted in-place. */ 503 encrypted in-place. */
515 unsigned char packet_type; 504 unsigned char packet_type;
516 unsigned int len, encrypt_buf_size; 505 unsigned int len, encrypt_buf_size;
517 unsigned char mac_bytes[MAX_MAC_LEN]; 506 unsigned char mac_bytes[MAX_MAC_LEN];
507
508 time_t now;
518 509
519 TRACE2(("enter encrypt_packet()")) 510 TRACE2(("enter encrypt_packet()"))
520 511
521 buf_setpos(ses.writepayload, 0); 512 buf_setpos(ses.writepayload, 0);
522 packet_type = buf_getbyte(ses.writepayload); 513 packet_type = buf_getbyte(ses.writepayload);
620 611
621 /* Update counts */ 612 /* Update counts */
622 ses.kexstate.datatrans += writebuf->len; 613 ses.kexstate.datatrans += writebuf->len;
623 ses.transseq++; 614 ses.transseq++;
624 615
616 now = monotonic_now();
617 ses.last_packet_time_any_sent = now;
618 /* idle timeout shouldn't be affected by responses to keepalives.
619 send_msg_keepalive() itself also does tricks with
620 ses.last_packet_idle_time - read that if modifying this code */
621 if (packet_type != SSH_MSG_REQUEST_FAILURE
622 && packet_type != SSH_MSG_UNIMPLEMENTED
623 && packet_type != SSH_MSG_IGNORE) {
624 ses.last_packet_time_idle = now;
625
626 }
627
625 TRACE2(("leave encrypt_packet()")) 628 TRACE2(("leave encrypt_packet()"))
626 } 629 }
627 630
628 631
629 /* Create the packet mac, and append H(seqno|clearbuf) to the output */ 632 /* Create the packet mac, and append H(seqno|clearbuf) to the output */