comparison packet.c @ 1074:10f198d4a308

Make main socket nonblocking. Limit writequeue size.
author Matt Johnston <matt@ucc.asn.au>
date Fri, 20 Mar 2015 23:36:42 +0800
parents 686cd3e8e13e
children acf444bcb115
comparison
equal deleted inserted replaced
1073:88043f9d40bd 1074:10f198d4a308
57 void write_packet() { 57 void write_packet() {
58 58
59 ssize_t written; 59 ssize_t written;
60 #ifdef HAVE_WRITEV 60 #ifdef HAVE_WRITEV
61 /* 50 is somewhat arbitrary */ 61 /* 50 is somewhat arbitrary */
62 int iov_count = 50; 62 unsigned int iov_count = 50;
63 struct iovec iov[50]; 63 struct iovec iov[50];
64 #endif 64 #endif
65 65
66 TRACE2(("enter write_packet")) 66 TRACE2(("enter write_packet"))
67 dropbear_assert(!isempty(&ses.writequeue)); 67 dropbear_assert(!isempty(&ses.writequeue));
81 dropbear_exit("Error writing: %s", strerror(errno)); 81 dropbear_exit("Error writing: %s", strerror(errno));
82 } 82 }
83 } 83 }
84 84
85 packet_queue_consume(&ses.writequeue, written); 85 packet_queue_consume(&ses.writequeue, written);
86 ses.writequeue_len -= written;
86 87
87 if (written == 0) { 88 if (written == 0) {
88 ses.remoteclosed(); 89 ses.remoteclosed();
89 } 90 }
90 91
110 } 111 }
111 112
112 if (written == 0) { 113 if (written == 0) {
113 ses.remoteclosed(); 114 ses.remoteclosed();
114 } 115 }
116
117 ses.writequeue_len -= written;
115 118
116 if (written == len) { 119 if (written == len) {
117 /* We've finished with the packet, free it */ 120 /* We've finished with the packet, free it */
118 dequeue(&ses.writequeue); 121 dequeue(&ses.writequeue);
119 buf_free(writebuf); 122 buf_free(writebuf);
568 buf_incrpos(writebuf, len); 571 buf_incrpos(writebuf, len);
569 572
570 /* stick the MAC on it */ 573 /* stick the MAC on it */
571 buf_putbytes(writebuf, mac_bytes, mac_size); 574 buf_putbytes(writebuf, mac_bytes, mac_size);
572 575
573 /* The last byte of the buffer stores the cleartext packet_type. It is not
574 * transmitted but is used for transmit timeout purposes */
575 buf_putbyte(writebuf, packet_type);
576 /* enqueue the packet for sending. It will get freed after transmission. */
577 buf_setpos(writebuf, 0);
578 enqueue(&ses.writequeue, (void*)writebuf);
579
580 /* Update counts */ 576 /* Update counts */
581 ses.kexstate.datatrans += writebuf->len; 577 ses.kexstate.datatrans += writebuf->len;
578
579 writebuf_enqueue(writebuf, packet_type);
580
581 /* Update counts */
582 ses.transseq++; 582 ses.transseq++;
583 583
584 now = monotonic_now(); 584 now = monotonic_now();
585 ses.last_packet_time_any_sent = now; 585 ses.last_packet_time_any_sent = now;
586 /* idle timeout shouldn't be affected by responses to keepalives. 586 /* idle timeout shouldn't be affected by responses to keepalives.
594 } 594 }
595 595
596 TRACE2(("leave encrypt_packet()")) 596 TRACE2(("leave encrypt_packet()"))
597 } 597 }
598 598
599 void writebuf_enqueue(buffer * writebuf, unsigned char packet_type) {
600 /* The last byte of the buffer stores the cleartext packet_type. It is not
601 * transmitted but is used for transmit timeout purposes */
602 buf_putbyte(writebuf, packet_type);
603 /* enqueue the packet for sending. It will get freed after transmission. */
604 buf_setpos(writebuf, 0);
605 enqueue(&ses.writequeue, (void*)writebuf);
606 ses.writequeue_len += writebuf->len-1;
607 }
608
599 609
600 /* Create the packet mac, and append H(seqno|clearbuf) to the output */ 610 /* Create the packet mac, and append H(seqno|clearbuf) to the output */
601 /* output_mac must have ses.keys->trans.algo_mac->hashsize bytes. */ 611 /* output_mac must have ses.keys->trans.algo_mac->hashsize bytes. */
602 static void make_mac(unsigned int seqno, const struct key_context_directional * key_state, 612 static void make_mac(unsigned int seqno, const struct key_context_directional * key_state,
603 buffer * clear_buf, unsigned int clear_len, 613 buffer * clear_buf, unsigned int clear_len,