packet.c (181110) | packet.c (181111) |
---|---|
1/* $OpenBSD: packet.c,v 1.145 2006/09/19 21:14:08 markus Exp $ */ | 1/* $OpenBSD: packet.c,v 1.157 2008/07/10 18:08:11 markus Exp $ */ |
2/* 3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 5 * All rights reserved 6 * This file contains code implementing the packet protocol and communication 7 * with the other side. This same code is used both on client and server side. 8 * 9 * As far as I am concerned, the code I have written for this software --- 32 unchanged lines hidden (view full) --- 42#include <sys/types.h> 43#include "openbsd-compat/sys-queue.h" 44#include <sys/param.h> 45#include <sys/socket.h> 46#ifdef HAVE_SYS_TIME_H 47# include <sys/time.h> 48#endif 49 | 2/* 3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 5 * All rights reserved 6 * This file contains code implementing the packet protocol and communication 7 * with the other side. This same code is used both on client and server side. 8 * 9 * As far as I am concerned, the code I have written for this software --- 32 unchanged lines hidden (view full) --- 42#include <sys/types.h> 43#include "openbsd-compat/sys-queue.h" 44#include <sys/param.h> 45#include <sys/socket.h> 46#ifdef HAVE_SYS_TIME_H 47# include <sys/time.h> 48#endif 49 |
50#include <netinet/in_systm.h> | |
51#include <netinet/in.h> 52#include <netinet/ip.h> 53#include <arpa/inet.h> 54 55#include <errno.h> 56#include <stdarg.h> 57#include <stdio.h> 58#include <stdlib.h> --- 73 unchanged lines hidden (view full) --- 132static int interactive_mode = 0; 133 134/* Set to true if we are the server side. */ 135static int server_side = 0; 136 137/* Set to true if we are authenticated. */ 138static int after_authentication = 0; 139 | 50#include <netinet/in.h> 51#include <netinet/ip.h> 52#include <arpa/inet.h> 53 54#include <errno.h> 55#include <stdarg.h> 56#include <stdio.h> 57#include <stdlib.h> --- 73 unchanged lines hidden (view full) --- 131static int interactive_mode = 0; 132 133/* Set to true if we are the server side. */ 134static int server_side = 0; 135 136/* Set to true if we are authenticated. */ 137static int after_authentication = 0; 138 |
139int keep_alive_timeouts = 0; 140 141/* Set to the maximum time that we will wait to send or receive a packet */ 142static int packet_timeout_ms = -1; 143 |
|
140/* Session key information for Encryption and MAC */ 141Newkeys *newkeys[MODE_MAX]; 142static struct packet_state { 143 u_int32_t seqnr; 144 u_int32_t packets; 145 u_int64_t blocks; | 144/* Session key information for Encryption and MAC */ 145Newkeys *newkeys[MODE_MAX]; 146static struct packet_state { 147 u_int32_t seqnr; 148 u_int32_t packets; 149 u_int64_t blocks; |
150 u_int64_t bytes; |
|
146} p_read, p_send; 147 148static u_int64_t max_blocks_in, max_blocks_out; 149static u_int32_t rekey_limit; 150 151/* Session key for protocol v1 */ 152static u_char ssh1_key[SSH_SESSION_KEY_LENGTH]; 153static u_int ssh1_keylen; --- 28 unchanged lines hidden (view full) --- 182 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL; 183 if (!initialized) { 184 initialized = 1; 185 buffer_init(&input); 186 buffer_init(&output); 187 buffer_init(&outgoing_packet); 188 buffer_init(&incoming_packet); 189 TAILQ_INIT(&outgoing); | 151} p_read, p_send; 152 153static u_int64_t max_blocks_in, max_blocks_out; 154static u_int32_t rekey_limit; 155 156/* Session key for protocol v1 */ 157static u_char ssh1_key[SSH_SESSION_KEY_LENGTH]; 158static u_int ssh1_keylen; --- 28 unchanged lines hidden (view full) --- 187 newkeys[MODE_IN] = newkeys[MODE_OUT] = NULL; 188 if (!initialized) { 189 initialized = 1; 190 buffer_init(&input); 191 buffer_init(&output); 192 buffer_init(&outgoing_packet); 193 buffer_init(&incoming_packet); 194 TAILQ_INIT(&outgoing); |
195 p_send.packets = p_read.packets = 0; |
|
190 } 191} 192 | 196 } 197} 198 |
199void 200packet_set_timeout(int timeout, int count) 201{ 202 if (timeout == 0 || count == 0) { 203 packet_timeout_ms = -1; 204 return; 205 } 206 if ((INT_MAX / 1000) / count < timeout) 207 packet_timeout_ms = INT_MAX; 208 else 209 packet_timeout_ms = timeout * count * 1000; 210} 211 |
|
193/* Returns 1 if remote host is connected via socket, 0 if not. */ 194 195int 196packet_connection_is_on_socket(void) 197{ 198 struct sockaddr_storage from, to; 199 socklen_t fromlen, tolen; 200 --- 88 unchanged lines hidden (view full) --- 289 290int 291packet_get_ssh1_cipher(void) 292{ 293 return (cipher_get_number(receive_context.cipher)); 294} 295 296void | 212/* Returns 1 if remote host is connected via socket, 0 if not. */ 213 214int 215packet_connection_is_on_socket(void) 216{ 217 struct sockaddr_storage from, to; 218 socklen_t fromlen, tolen; 219 --- 88 unchanged lines hidden (view full) --- 308 309int 310packet_get_ssh1_cipher(void) 311{ 312 return (cipher_get_number(receive_context.cipher)); 313} 314 315void |
297packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets) | 316packet_get_state(int mode, u_int32_t *seqnr, u_int64_t *blocks, u_int32_t *packets, 317 u_int64_t *bytes) |
298{ 299 struct packet_state *state; 300 301 state = (mode == MODE_IN) ? &p_read : &p_send; | 318{ 319 struct packet_state *state; 320 321 state = (mode == MODE_IN) ? &p_read : &p_send; |
302 *seqnr = state->seqnr; 303 *blocks = state->blocks; 304 *packets = state->packets; | 322 if (seqnr) 323 *seqnr = state->seqnr; 324 if (blocks) 325 *blocks = state->blocks; 326 if (packets) 327 *packets = state->packets; 328 if (bytes) 329 *bytes = state->bytes; |
305} 306 307void | 330} 331 332void |
308packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets) | 333packet_set_state(int mode, u_int32_t seqnr, u_int64_t blocks, u_int32_t packets, 334 u_int64_t bytes) |
309{ 310 struct packet_state *state; 311 312 state = (mode == MODE_IN) ? &p_read : &p_send; 313 state->seqnr = seqnr; 314 state->blocks = blocks; 315 state->packets = packets; | 335{ 336 struct packet_state *state; 337 338 state = (mode == MODE_IN) ? &p_read : &p_send; 339 state->seqnr = seqnr; 340 state->blocks = blocks; 341 state->packets = packets; |
342 state->bytes = bytes; |
|
316} 317 318/* returns 1 if connection is via ipv4 */ 319 320int 321packet_connection_is_ipv4(void) 322{ 323 struct sockaddr_storage to; --- 262 unchanged lines hidden (view full) --- 586 cp = buffer_append_space(&output, buffer_len(&outgoing_packet)); 587 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet), 588 buffer_len(&outgoing_packet)); 589 590#ifdef PACKET_DEBUG 591 fprintf(stderr, "encrypted: "); 592 buffer_dump(&output); 593#endif | 343} 344 345/* returns 1 if connection is via ipv4 */ 346 347int 348packet_connection_is_ipv4(void) 349{ 350 struct sockaddr_storage to; --- 262 unchanged lines hidden (view full) --- 613 cp = buffer_append_space(&output, buffer_len(&outgoing_packet)); 614 cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet), 615 buffer_len(&outgoing_packet)); 616 617#ifdef PACKET_DEBUG 618 fprintf(stderr, "encrypted: "); 619 buffer_dump(&output); 620#endif |
594 | 621 p_send.packets++; 622 p_send.bytes += len + buffer_len(&outgoing_packet); |
595 buffer_clear(&outgoing_packet); 596 597 /* 598 * Note that the packet is now only buffered in output. It won't be 599 * actually sent until packet_write_wait or packet_write_poll is 600 * called. 601 */ 602} --- 22 unchanged lines hidden (view full) --- 625 max_blocks = &max_blocks_in; 626 } 627 if (newkeys[mode] != NULL) { 628 debug("set_newkeys: rekeying"); 629 cipher_cleanup(cc); 630 enc = &newkeys[mode]->enc; 631 mac = &newkeys[mode]->mac; 632 comp = &newkeys[mode]->comp; | 623 buffer_clear(&outgoing_packet); 624 625 /* 626 * Note that the packet is now only buffered in output. It won't be 627 * actually sent until packet_write_wait or packet_write_poll is 628 * called. 629 */ 630} --- 22 unchanged lines hidden (view full) --- 653 max_blocks = &max_blocks_in; 654 } 655 if (newkeys[mode] != NULL) { 656 debug("set_newkeys: rekeying"); 657 cipher_cleanup(cc); 658 enc = &newkeys[mode]->enc; 659 mac = &newkeys[mode]->mac; 660 comp = &newkeys[mode]->comp; |
633 memset(mac->key, 0, mac->key_len); | 661 mac_clear(mac); |
634 xfree(enc->name); 635 xfree(enc->iv); 636 xfree(enc->key); 637 xfree(mac->name); 638 xfree(mac->key); 639 xfree(comp->name); 640 xfree(newkeys[mode]); 641 } 642 newkeys[mode] = kex_get_newkeys(mode); 643 if (newkeys[mode] == NULL) 644 fatal("newkeys: no keys for mode %d", mode); 645 enc = &newkeys[mode]->enc; 646 mac = &newkeys[mode]->mac; 647 comp = &newkeys[mode]->comp; | 662 xfree(enc->name); 663 xfree(enc->iv); 664 xfree(enc->key); 665 xfree(mac->name); 666 xfree(mac->key); 667 xfree(comp->name); 668 xfree(newkeys[mode]); 669 } 670 newkeys[mode] = kex_get_newkeys(mode); 671 if (newkeys[mode] == NULL) 672 fatal("newkeys: no keys for mode %d", mode); 673 enc = &newkeys[mode]->enc; 674 mac = &newkeys[mode]->mac; 675 comp = &newkeys[mode]->comp; |
648 if (mac->md != NULL) | 676 if (mac_init(mac) == 0) |
649 mac->enabled = 1; 650 DBG(debug("cipher_init_context: %d", mode)); 651 cipher_init(cc, enc->cipher, enc->key, enc->key_len, 652 enc->iv, enc->block_size, crypt_type); 653 /* Deleting the keys does not gain extra security */ 654 /* memset(enc->iv, 0, enc->block_size); | 677 mac->enabled = 1; 678 DBG(debug("cipher_init_context: %d", mode)); 679 cipher_init(cc, enc->cipher, enc->key, enc->key_len, 680 enc->iv, enc->block_size, crypt_type); 681 /* Deleting the keys does not gain extra security */ 682 /* memset(enc->iv, 0, enc->block_size); |
655 memset(enc->key, 0, enc->key_len); */ | 683 memset(enc->key, 0, enc->key_len); 684 memset(mac->key, 0, mac->key_len); */ |
656 if ((comp->type == COMP_ZLIB || 657 (comp->type == COMP_DELAYED && after_authentication)) && 658 comp->enabled == 0) { 659 packet_init_compression(); 660 if (mode == MODE_OUT) 661 buffer_compress_init_send(6); 662 else 663 buffer_compress_init_recv(); --- 147 unchanged lines hidden (view full) --- 811#endif 812 /* increment sequence number for outgoing packets */ 813 if (++p_send.seqnr == 0) 814 logit("outgoing seqnr wraps around"); 815 if (++p_send.packets == 0) 816 if (!(datafellows & SSH_BUG_NOREKEY)) 817 fatal("XXX too many packets with same key"); 818 p_send.blocks += (packet_length + 4) / block_size; | 685 if ((comp->type == COMP_ZLIB || 686 (comp->type == COMP_DELAYED && after_authentication)) && 687 comp->enabled == 0) { 688 packet_init_compression(); 689 if (mode == MODE_OUT) 690 buffer_compress_init_send(6); 691 else 692 buffer_compress_init_recv(); --- 147 unchanged lines hidden (view full) --- 840#endif 841 /* increment sequence number for outgoing packets */ 842 if (++p_send.seqnr == 0) 843 logit("outgoing seqnr wraps around"); 844 if (++p_send.packets == 0) 845 if (!(datafellows & SSH_BUG_NOREKEY)) 846 fatal("XXX too many packets with same key"); 847 p_send.blocks += (packet_length + 4) / block_size; |
848 p_send.bytes += packet_length + 4; |
|
819 buffer_clear(&outgoing_packet); 820 821 if (type == SSH2_MSG_NEWKEYS) 822 set_newkeys(MODE_OUT); 823 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side) 824 packet_enable_delayed_compress(); 825} 826 --- 57 unchanged lines hidden (view full) --- 884 * Waits until a packet has been received, and returns its type. Note that 885 * no other data is processed until this returns, so this function should not 886 * be used during the interactive session. 887 */ 888 889int 890packet_read_seqnr(u_int32_t *seqnr_p) 891{ | 849 buffer_clear(&outgoing_packet); 850 851 if (type == SSH2_MSG_NEWKEYS) 852 set_newkeys(MODE_OUT); 853 else if (type == SSH2_MSG_USERAUTH_SUCCESS && server_side) 854 packet_enable_delayed_compress(); 855} 856 --- 57 unchanged lines hidden (view full) --- 914 * Waits until a packet has been received, and returns its type. Note that 915 * no other data is processed until this returns, so this function should not 916 * be used during the interactive session. 917 */ 918 919int 920packet_read_seqnr(u_int32_t *seqnr_p) 921{ |
892 int type, len; | 922 int type, len, ret, ms_remain; |
893 fd_set *setp; 894 char buf[8192]; | 923 fd_set *setp; 924 char buf[8192]; |
925 struct timeval timeout, start, *timeoutp = NULL; 926 |
|
895 DBG(debug("packet_read()")); 896 897 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS), 898 sizeof(fd_mask)); 899 900 /* Since we are blocking, ensure that all written packets have been sent. */ 901 packet_write_wait(); 902 --- 15 unchanged lines hidden (view full) --- 918 /* 919 * Otherwise, wait for some data to arrive, add it to the 920 * buffer, and try again. 921 */ 922 memset(setp, 0, howmany(connection_in + 1, NFDBITS) * 923 sizeof(fd_mask)); 924 FD_SET(connection_in, setp); 925 | 927 DBG(debug("packet_read()")); 928 929 setp = (fd_set *)xcalloc(howmany(connection_in+1, NFDBITS), 930 sizeof(fd_mask)); 931 932 /* Since we are blocking, ensure that all written packets have been sent. */ 933 packet_write_wait(); 934 --- 15 unchanged lines hidden (view full) --- 950 /* 951 * Otherwise, wait for some data to arrive, add it to the 952 * buffer, and try again. 953 */ 954 memset(setp, 0, howmany(connection_in + 1, NFDBITS) * 955 sizeof(fd_mask)); 956 FD_SET(connection_in, setp); 957 |
958 if (packet_timeout_ms > 0) { 959 ms_remain = packet_timeout_ms; 960 timeoutp = &timeout; 961 } |
|
926 /* Wait for some data to arrive. */ | 962 /* Wait for some data to arrive. */ |
927 while (select(connection_in + 1, setp, NULL, NULL, NULL) == -1 && 928 (errno == EAGAIN || errno == EINTR)) 929 ; 930 | 963 for (;;) { 964 if (packet_timeout_ms != -1) { 965 ms_to_timeval(&timeout, ms_remain); 966 gettimeofday(&start, NULL); 967 } 968 if ((ret = select(connection_in + 1, setp, NULL, 969 NULL, timeoutp)) >= 0) 970 break; 971 if (errno != EAGAIN && errno != EINTR && 972 errno != EWOULDBLOCK) 973 break; 974 if (packet_timeout_ms == -1) 975 continue; 976 ms_subtract_diff(&start, &ms_remain); 977 if (ms_remain <= 0) { 978 ret = 0; 979 break; 980 } 981 } 982 if (ret == 0) { 983 logit("Connection to %.200s timed out while " 984 "waiting to read", get_remote_ipaddr()); 985 cleanup_exit(255); 986 } |
931 /* Read data from the socket. */ 932 len = read(connection_in, buf, sizeof(buf)); 933 if (len == 0) { 934 logit("Connection closed by %.200s", get_remote_ipaddr()); 935 cleanup_exit(255); 936 } 937 if (len < 0) 938 fatal("Read from socket failed: %.100s", strerror(errno)); --- 108 unchanged lines hidden (view full) --- 1047 1048 if (packet_compression) { 1049 buffer_clear(&compression_buffer); 1050 buffer_uncompress(&incoming_packet, &compression_buffer); 1051 buffer_clear(&incoming_packet); 1052 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer), 1053 buffer_len(&compression_buffer)); 1054 } | 987 /* Read data from the socket. */ 988 len = read(connection_in, buf, sizeof(buf)); 989 if (len == 0) { 990 logit("Connection closed by %.200s", get_remote_ipaddr()); 991 cleanup_exit(255); 992 } 993 if (len < 0) 994 fatal("Read from socket failed: %.100s", strerror(errno)); --- 108 unchanged lines hidden (view full) --- 1103 1104 if (packet_compression) { 1105 buffer_clear(&compression_buffer); 1106 buffer_uncompress(&incoming_packet, &compression_buffer); 1107 buffer_clear(&incoming_packet); 1108 buffer_append(&incoming_packet, buffer_ptr(&compression_buffer), 1109 buffer_len(&compression_buffer)); 1110 } |
1111 p_read.packets++; 1112 p_read.bytes += padded_len + 4; |
|
1055 type = buffer_get_char(&incoming_packet); 1056 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX) 1057 packet_disconnect("Invalid ssh1 packet type: %d", type); 1058 return type; 1059} 1060 1061static int 1062packet_read_poll2(u_int32_t *seqnr_p) --- 72 unchanged lines hidden (view full) --- 1135 if (seqnr_p != NULL) 1136 *seqnr_p = p_read.seqnr; 1137 if (++p_read.seqnr == 0) 1138 logit("incoming seqnr wraps around"); 1139 if (++p_read.packets == 0) 1140 if (!(datafellows & SSH_BUG_NOREKEY)) 1141 fatal("XXX too many packets with same key"); 1142 p_read.blocks += (packet_length + 4) / block_size; | 1113 type = buffer_get_char(&incoming_packet); 1114 if (type < SSH_MSG_MIN || type > SSH_MSG_MAX) 1115 packet_disconnect("Invalid ssh1 packet type: %d", type); 1116 return type; 1117} 1118 1119static int 1120packet_read_poll2(u_int32_t *seqnr_p) --- 72 unchanged lines hidden (view full) --- 1193 if (seqnr_p != NULL) 1194 *seqnr_p = p_read.seqnr; 1195 if (++p_read.seqnr == 0) 1196 logit("incoming seqnr wraps around"); 1197 if (++p_read.packets == 0) 1198 if (!(datafellows & SSH_BUG_NOREKEY)) 1199 fatal("XXX too many packets with same key"); 1200 p_read.blocks += (packet_length + 4) / block_size; |
1201 p_read.bytes += packet_length + 4; |
|
1143 1144 /* get padlen */ 1145 cp = buffer_ptr(&incoming_packet); 1146 padlen = cp[4]; 1147 DBG(debug("input: padlen %d", padlen)); 1148 if (padlen < 4) 1149 packet_disconnect("Corrupted padlen %d on input.", padlen); 1150 --- 36 unchanged lines hidden (view full) --- 1187{ 1188 u_int reason, seqnr; 1189 u_char type; 1190 char *msg; 1191 1192 for (;;) { 1193 if (compat20) { 1194 type = packet_read_poll2(seqnr_p); | 1202 1203 /* get padlen */ 1204 cp = buffer_ptr(&incoming_packet); 1205 padlen = cp[4]; 1206 DBG(debug("input: padlen %d", padlen)); 1207 if (padlen < 4) 1208 packet_disconnect("Corrupted padlen %d on input.", padlen); 1209 --- 36 unchanged lines hidden (view full) --- 1246{ 1247 u_int reason, seqnr; 1248 u_char type; 1249 char *msg; 1250 1251 for (;;) { 1252 if (compat20) { 1253 type = packet_read_poll2(seqnr_p); |
1195 if (type) | 1254 if (type) { 1255 keep_alive_timeouts = 0; |
1196 DBG(debug("received packet type %d", type)); | 1256 DBG(debug("received packet type %d", type)); |
1257 } |
|
1197 switch (type) { 1198 case SSH2_MSG_IGNORE: | 1258 switch (type) { 1259 case SSH2_MSG_IGNORE: |
1260 debug3("Received SSH2_MSG_IGNORE"); |
|
1199 break; 1200 case SSH2_MSG_DEBUG: 1201 packet_get_char(); 1202 msg = packet_get_string(NULL); 1203 debug("Remote: %.900s", msg); 1204 xfree(msg); 1205 msg = packet_get_string(NULL); 1206 xfree(msg); --- 24 unchanged lines hidden (view full) --- 1231 debug("Remote: %.900s", msg); 1232 xfree(msg); 1233 break; 1234 case SSH_MSG_DISCONNECT: 1235 msg = packet_get_string(NULL); 1236 logit("Received disconnect from %s: %.400s", 1237 get_remote_ipaddr(), msg); 1238 cleanup_exit(255); | 1261 break; 1262 case SSH2_MSG_DEBUG: 1263 packet_get_char(); 1264 msg = packet_get_string(NULL); 1265 debug("Remote: %.900s", msg); 1266 xfree(msg); 1267 msg = packet_get_string(NULL); 1268 xfree(msg); --- 24 unchanged lines hidden (view full) --- 1293 debug("Remote: %.900s", msg); 1294 xfree(msg); 1295 break; 1296 case SSH_MSG_DISCONNECT: 1297 msg = packet_get_string(NULL); 1298 logit("Received disconnect from %s: %.400s", 1299 get_remote_ipaddr(), msg); 1300 cleanup_exit(255); |
1239 xfree(msg); | |
1240 break; 1241 default: 1242 if (type) 1243 DBG(debug("received packet type %d", type)); 1244 return type; 1245 } 1246 } 1247 } --- 76 unchanged lines hidden (view full) --- 1324 */ 1325 1326void * 1327packet_get_string(u_int *length_ptr) 1328{ 1329 return buffer_get_string(&incoming_packet, length_ptr); 1330} 1331 | 1301 break; 1302 default: 1303 if (type) 1304 DBG(debug("received packet type %d", type)); 1305 return type; 1306 } 1307 } 1308 } --- 76 unchanged lines hidden (view full) --- 1385 */ 1386 1387void * 1388packet_get_string(u_int *length_ptr) 1389{ 1390 return buffer_get_string(&incoming_packet, length_ptr); 1391} 1392 |
1393void * 1394packet_get_string_ptr(u_int *length_ptr) 1395{ 1396 return buffer_get_string_ptr(&incoming_packet, length_ptr); 1397} 1398 |
|
1332/* 1333 * Sends a diagnostic message from the server to the client. This message 1334 * can be sent at any time (but not while constructing another message). The 1335 * message is printed immediately, but only if the client is being executed 1336 * in verbose mode. These messages are primarily intended to ease debugging 1337 * authentication problems. The length of the formatted message must not 1338 * exceed 1024 bytes. This will automatically call packet_write_wait. 1339 */ --- 78 unchanged lines hidden (view full) --- 1418 1419void 1420packet_write_poll(void) 1421{ 1422 int len = buffer_len(&output); 1423 1424 if (len > 0) { 1425 len = write(connection_out, buffer_ptr(&output), len); | 1399/* 1400 * Sends a diagnostic message from the server to the client. This message 1401 * can be sent at any time (but not while constructing another message). The 1402 * message is printed immediately, but only if the client is being executed 1403 * in verbose mode. These messages are primarily intended to ease debugging 1404 * authentication problems. The length of the formatted message must not 1405 * exceed 1024 bytes. This will automatically call packet_write_wait. 1406 */ --- 78 unchanged lines hidden (view full) --- 1485 1486void 1487packet_write_poll(void) 1488{ 1489 int len = buffer_len(&output); 1490 1491 if (len > 0) { 1492 len = write(connection_out, buffer_ptr(&output), len); |
1426 if (len <= 0) { 1427 if (errno == EAGAIN) | 1493 if (len == -1) { 1494 if (errno == EINTR || errno == EAGAIN || 1495 errno == EWOULDBLOCK) |
1428 return; | 1496 return; |
1429 else 1430 fatal("Write failed: %.100s", strerror(errno)); | 1497 fatal("Write failed: %.100s", strerror(errno)); |
1431 } | 1498 } |
1499 if (len == 0) 1500 fatal("Write connection closed"); |
|
1432 buffer_consume(&output, len); 1433 } 1434} 1435 | 1501 buffer_consume(&output, len); 1502 } 1503} 1504 |
1505 |
|
1436/* 1437 * Calls packet_write_poll repeatedly until all pending output data has been 1438 * written. 1439 */ 1440 1441void 1442packet_write_wait(void) 1443{ 1444 fd_set *setp; | 1506/* 1507 * Calls packet_write_poll repeatedly until all pending output data has been 1508 * written. 1509 */ 1510 1511void 1512packet_write_wait(void) 1513{ 1514 fd_set *setp; |
1515 int ret, ms_remain; 1516 struct timeval start, timeout, *timeoutp = NULL; |
|
1445 1446 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS), 1447 sizeof(fd_mask)); 1448 packet_write_poll(); 1449 while (packet_have_data_to_write()) { 1450 memset(setp, 0, howmany(connection_out + 1, NFDBITS) * 1451 sizeof(fd_mask)); 1452 FD_SET(connection_out, setp); | 1517 1518 setp = (fd_set *)xcalloc(howmany(connection_out + 1, NFDBITS), 1519 sizeof(fd_mask)); 1520 packet_write_poll(); 1521 while (packet_have_data_to_write()) { 1522 memset(setp, 0, howmany(connection_out + 1, NFDBITS) * 1523 sizeof(fd_mask)); 1524 FD_SET(connection_out, setp); |
1453 while (select(connection_out + 1, NULL, setp, NULL, NULL) == -1 && 1454 (errno == EAGAIN || errno == EINTR)) 1455 ; | 1525 1526 if (packet_timeout_ms > 0) { 1527 ms_remain = packet_timeout_ms; 1528 timeoutp = &timeout; 1529 } 1530 for (;;) { 1531 if (packet_timeout_ms != -1) { 1532 ms_to_timeval(&timeout, ms_remain); 1533 gettimeofday(&start, NULL); 1534 } 1535 if ((ret = select(connection_out + 1, NULL, setp, 1536 NULL, timeoutp)) >= 0) 1537 break; 1538 if (errno != EAGAIN && errno != EINTR && 1539 errno != EWOULDBLOCK) 1540 break; 1541 if (packet_timeout_ms == -1) 1542 continue; 1543 ms_subtract_diff(&start, &ms_remain); 1544 if (ms_remain <= 0) { 1545 ret = 0; 1546 break; 1547 } 1548 } 1549 if (ret == 0) { 1550 logit("Connection to %.200s timed out while " 1551 "waiting to write", get_remote_ipaddr()); 1552 cleanup_exit(255); 1553 } |
1456 packet_write_poll(); 1457 } 1458 xfree(setp); 1459} 1460 1461/* Returns true if there is buffered data to write to the connection. */ 1462 1463int --- 146 unchanged lines hidden --- | 1554 packet_write_poll(); 1555 } 1556 xfree(setp); 1557} 1558 1559/* Returns true if there is buffered data to write to the connection. */ 1560 1561int --- 146 unchanged lines hidden --- |