Mercurial > dropbear
comparison packet.c @ 165:0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 02 Jan 2005 20:25:56 +0000 |
parents | b0316ce64e4b |
children | e972be139cb5 |
comparison
equal
deleted
inserted
replaced
161:b9d3f725e00b | 165:0cfba3034be5 |
---|---|
50 void write_packet() { | 50 void write_packet() { |
51 | 51 |
52 int len, written; | 52 int len, written; |
53 buffer * writebuf = NULL; | 53 buffer * writebuf = NULL; |
54 | 54 |
55 TRACE(("enter write_packet")); | 55 TRACE(("enter write_packet")) |
56 assert(!isempty(&ses.writequeue)); | 56 assert(!isempty(&ses.writequeue)); |
57 | 57 |
58 /* Get the next buffer in the queue of encrypted packets to write*/ | 58 /* Get the next buffer in the queue of encrypted packets to write*/ |
59 writebuf = (buffer*)examine(&ses.writequeue); | 59 writebuf = (buffer*)examine(&ses.writequeue); |
60 | 60 |
63 /* Try to write as much as possible */ | 63 /* Try to write as much as possible */ |
64 written = write(ses.sock, buf_getptr(writebuf, len), len); | 64 written = write(ses.sock, buf_getptr(writebuf, len), len); |
65 | 65 |
66 if (written < 0) { | 66 if (written < 0) { |
67 if (errno == EINTR) { | 67 if (errno == EINTR) { |
68 TRACE(("leave writepacket: EINTR")); | 68 TRACE(("leave writepacket: EINTR")) |
69 return; | 69 return; |
70 } else { | 70 } else { |
71 dropbear_exit("error writing"); | 71 dropbear_exit("error writing"); |
72 } | 72 } |
73 } | 73 } |
84 } else { | 84 } else { |
85 /* More packet left to write, leave it in the queue for later */ | 85 /* More packet left to write, leave it in the queue for later */ |
86 buf_incrpos(writebuf, written); | 86 buf_incrpos(writebuf, written); |
87 } | 87 } |
88 | 88 |
89 TRACE(("leave write_packet")); | 89 TRACE(("leave write_packet")) |
90 } | 90 } |
91 | 91 |
92 /* Non-blocking function reading available portion of a packet into the | 92 /* Non-blocking function reading available portion of a packet into the |
93 * ses's buffer, decrypting the length if encrypted, decrypting the | 93 * ses's buffer, decrypting the length if encrypted, decrypting the |
94 * full portion if possible */ | 94 * full portion if possible */ |
96 | 96 |
97 int len; | 97 int len; |
98 unsigned int maxlen; | 98 unsigned int maxlen; |
99 unsigned char blocksize; | 99 unsigned char blocksize; |
100 | 100 |
101 TRACE(("enter read_packet")); | 101 TRACE(("enter read_packet")) |
102 blocksize = ses.keys->recv_algo_crypt->blocksize; | 102 blocksize = ses.keys->recv_algo_crypt->blocksize; |
103 | 103 |
104 if (ses.readbuf == NULL || ses.readbuf->len < blocksize) { | 104 if (ses.readbuf == NULL || ses.readbuf->len < blocksize) { |
105 /* In the first blocksize of a packet */ | 105 /* In the first blocksize of a packet */ |
106 | 106 |
109 read_packet_init(); | 109 read_packet_init(); |
110 | 110 |
111 /* If we don't have the length of decryptreadbuf, we didn't read | 111 /* If we don't have the length of decryptreadbuf, we didn't read |
112 * a whole blocksize and should exit */ | 112 * a whole blocksize and should exit */ |
113 if (ses.decryptreadbuf->len == 0) { | 113 if (ses.decryptreadbuf->len == 0) { |
114 TRACE(("leave read_packet: packetinit done")); | 114 TRACE(("leave read_packet: packetinit done")) |
115 return; | 115 return; |
116 } | 116 } |
117 } | 117 } |
118 | 118 |
119 /* Attempt to read the remainder of the packet, note that there | 119 /* Attempt to read the remainder of the packet, note that there |
126 ses.remoteclosed(); | 126 ses.remoteclosed(); |
127 } | 127 } |
128 | 128 |
129 if (len < 0) { | 129 if (len < 0) { |
130 if (errno == EINTR || errno == EAGAIN) { | 130 if (errno == EINTR || errno == EAGAIN) { |
131 TRACE(("leave read_packet: EINTR or EAGAIN")); | 131 TRACE(("leave read_packet: EINTR or EAGAIN")) |
132 return; | 132 return; |
133 } else { | 133 } else { |
134 dropbear_exit("error reading: %s", strerror(errno)); | 134 dropbear_exit("error reading: %s", strerror(errno)); |
135 } | 135 } |
136 } | 136 } |
141 /* The whole packet has been read */ | 141 /* The whole packet has been read */ |
142 decrypt_packet(); | 142 decrypt_packet(); |
143 /* The main select() loop process_packet() to | 143 /* The main select() loop process_packet() to |
144 * handle the packet contents... */ | 144 * handle the packet contents... */ |
145 } | 145 } |
146 TRACE(("leave read_packet")); | 146 TRACE(("leave read_packet")) |
147 } | 147 } |
148 | 148 |
149 /* Function used to read the initial portion of a packet, and determine the | 149 /* Function used to read the initial portion of a packet, and determine the |
150 * length. Only called during the first BLOCKSIZE of a packet. */ | 150 * length. Only called during the first BLOCKSIZE of a packet. */ |
151 static void read_packet_init() { | 151 static void read_packet_init() { |
174 if (len == 0) { | 174 if (len == 0) { |
175 ses.remoteclosed(); | 175 ses.remoteclosed(); |
176 } | 176 } |
177 if (len < 0) { | 177 if (len < 0) { |
178 if (errno == EINTR) { | 178 if (errno == EINTR) { |
179 TRACE(("leave read_packet_init: EINTR")); | 179 TRACE(("leave read_packet_init: EINTR")) |
180 return; | 180 return; |
181 } | 181 } |
182 dropbear_exit("error reading: %s", strerror(errno)); | 182 dropbear_exit("error reading: %s", strerror(errno)); |
183 } | 183 } |
184 | 184 |
228 unsigned char blocksize; | 228 unsigned char blocksize; |
229 unsigned char macsize; | 229 unsigned char macsize; |
230 unsigned int padlen; | 230 unsigned int padlen; |
231 unsigned int len; | 231 unsigned int len; |
232 | 232 |
233 TRACE(("enter decrypt_packet")); | 233 TRACE(("enter decrypt_packet")) |
234 blocksize = ses.keys->recv_algo_crypt->blocksize; | 234 blocksize = ses.keys->recv_algo_crypt->blocksize; |
235 macsize = ses.keys->recv_algo_mac->hashsize; | 235 macsize = ses.keys->recv_algo_mac->hashsize; |
236 | 236 |
237 ses.kexstate.datarecv += ses.readbuf->len; | 237 ses.kexstate.datarecv += ses.readbuf->len; |
238 | 238 |
303 ses.decryptreadbuf = NULL; | 303 ses.decryptreadbuf = NULL; |
304 buf_setpos(ses.payload, 0); | 304 buf_setpos(ses.payload, 0); |
305 | 305 |
306 ses.recvseq++; | 306 ses.recvseq++; |
307 | 307 |
308 TRACE(("leave decrypt_packet")); | 308 TRACE(("leave decrypt_packet")) |
309 } | 309 } |
310 | 310 |
311 /* Checks the mac in hashbuf, for the data in readbuf. | 311 /* Checks the mac in hashbuf, for the data in readbuf. |
312 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ | 312 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ |
313 static int checkmac(buffer* macbuf, buffer* sourcebuf) { | 313 static int checkmac(buffer* macbuf, buffer* sourcebuf) { |
411 unsigned char padlen; | 411 unsigned char padlen; |
412 unsigned char blocksize, macsize; | 412 unsigned char blocksize, macsize; |
413 buffer * writebuf; /* the packet which will go on the wire */ | 413 buffer * writebuf; /* the packet which will go on the wire */ |
414 buffer * clearwritebuf; /* unencrypted, possibly compressed */ | 414 buffer * clearwritebuf; /* unencrypted, possibly compressed */ |
415 | 415 |
416 TRACE(("enter encrypt_packet()")); | 416 TRACE(("enter encrypt_packet()")) |
417 TRACE(("encrypt_packet type is %d", ses.writepayload->data[0])); | 417 TRACE(("encrypt_packet type is %d", ses.writepayload->data[0])) |
418 blocksize = ses.keys->trans_algo_crypt->blocksize; | 418 blocksize = ses.keys->trans_algo_crypt->blocksize; |
419 macsize = ses.keys->trans_algo_mac->hashsize; | 419 macsize = ses.keys->trans_algo_mac->hashsize; |
420 | 420 |
421 /* Encrypted packet len is payload+5, then worst case is if we are 3 away | 421 /* Encrypted packet len is payload+5, then worst case is if we are 3 away |
422 * from a blocksize multiple. In which case we need to pad to the | 422 * from a blocksize multiple. In which case we need to pad to the |
512 | 512 |
513 /* Update counts */ | 513 /* Update counts */ |
514 ses.kexstate.datatrans += writebuf->len; | 514 ses.kexstate.datatrans += writebuf->len; |
515 ses.transseq++; | 515 ses.transseq++; |
516 | 516 |
517 TRACE(("leave encrypt_packet()")); | 517 TRACE(("leave encrypt_packet()")) |
518 } | 518 } |
519 | 519 |
520 | 520 |
521 /* Create the packet mac, and append H(seqno|clearbuf) to the output */ | 521 /* Create the packet mac, and append H(seqno|clearbuf) to the output */ |
522 static void writemac(buffer * outputbuffer, buffer * clearwritebuf) { | 522 static void writemac(buffer * outputbuffer, buffer * clearwritebuf) { |
524 int macsize; | 524 int macsize; |
525 unsigned char seqbuf[4]; | 525 unsigned char seqbuf[4]; |
526 unsigned long hashsize; | 526 unsigned long hashsize; |
527 hmac_state hmac; | 527 hmac_state hmac; |
528 | 528 |
529 TRACE(("enter writemac")); | 529 TRACE(("enter writemac")) |
530 | 530 |
531 macsize = ses.keys->trans_algo_mac->hashsize; | 531 macsize = ses.keys->trans_algo_mac->hashsize; |
532 | 532 |
533 if (macsize > 0) { | 533 if (macsize > 0) { |
534 /* calculate the mac */ | 534 /* calculate the mac */ |
559 != CRYPT_OK) { | 559 != CRYPT_OK) { |
560 dropbear_exit("HMAC error"); | 560 dropbear_exit("HMAC error"); |
561 } | 561 } |
562 buf_incrwritepos(outputbuffer, macsize); | 562 buf_incrwritepos(outputbuffer, macsize); |
563 } | 563 } |
564 TRACE(("leave writemac")); | 564 TRACE(("leave writemac")) |
565 } | 565 } |
566 | 566 |
567 #ifndef DISABLE_ZLIB | 567 #ifndef DISABLE_ZLIB |
568 /* compresses len bytes from src, outputting to dest (starting from the | 568 /* compresses len bytes from src, outputting to dest (starting from the |
569 * respective current positions. */ | 569 * respective current positions. */ |
570 static void buf_compress(buffer * dest, buffer * src, unsigned int len) { | 570 static void buf_compress(buffer * dest, buffer * src, unsigned int len) { |
571 | 571 |
572 unsigned int endpos = src->pos + len; | 572 unsigned int endpos = src->pos + len; |
573 int result; | 573 int result; |
574 | 574 |
575 TRACE(("enter buf_compress")); | 575 TRACE(("enter buf_compress")) |
576 | 576 |
577 while (1) { | 577 while (1) { |
578 | 578 |
579 ses.keys->trans_zstream->avail_in = endpos - src->pos; | 579 ses.keys->trans_zstream->avail_in = endpos - src->pos; |
580 ses.keys->trans_zstream->next_in = | 580 ses.keys->trans_zstream->next_in = |
604 * unusual circumstances where the data grows in size after deflate(), | 604 * unusual circumstances where the data grows in size after deflate(), |
605 * but it is possible */ | 605 * but it is possible */ |
606 buf_resize(dest, dest->size + ZLIB_COMPRESS_INCR); | 606 buf_resize(dest, dest->size + ZLIB_COMPRESS_INCR); |
607 | 607 |
608 } | 608 } |
609 TRACE(("leave buf_compress")); | 609 TRACE(("leave buf_compress")) |
610 } | 610 } |
611 #endif | 611 #endif |