Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * |
| 3 | * packet.c |
| 4 | * |
| 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 6 | * |
| 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 8 | * All rights reserved |
| 9 | * |
| 10 | * Created: Sat Mar 18 02:40:40 1995 ylo |
| 11 | * |
| 12 | * This file contains code implementing the packet protocol and communication |
| 13 | * with the other side. This same code is used both on client and server side. |
| 14 | * |
| 15 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 16 | |
| 17 | #include "includes.h" |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 18 | RCSID("$Id: packet.c,v 1.7 1999/12/06 00:47:29 damien Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 19 | |
| 20 | #include "xmalloc.h" |
| 21 | #include "buffer.h" |
| 22 | #include "packet.h" |
| 23 | #include "bufaux.h" |
| 24 | #include "ssh.h" |
| 25 | #include "crc32.h" |
| 26 | #include "cipher.h" |
| 27 | #include "getput.h" |
| 28 | |
| 29 | #include "compress.h" |
| 30 | #include "deattack.h" |
| 31 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 32 | /* |
| 33 | * This variable contains the file descriptors used for communicating with |
| 34 | * the other side. connection_in is used for reading; connection_out for |
| 35 | * writing. These can be the same descriptor, in which case it is assumed to |
| 36 | * be a socket. |
| 37 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 38 | static int connection_in = -1; |
| 39 | static int connection_out = -1; |
| 40 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 41 | /* |
| 42 | * Cipher type. This value is only used to determine whether to pad the |
| 43 | * packets with zeroes or random data. |
| 44 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 45 | static int cipher_type = SSH_CIPHER_NONE; |
| 46 | |
| 47 | /* Protocol flags for the remote side. */ |
| 48 | static unsigned int remote_protocol_flags = 0; |
| 49 | |
| 50 | /* Encryption context for receiving data. This is only used for decryption. */ |
| 51 | static CipherContext receive_context; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 52 | |
| 53 | /* Encryption context for sending data. This is only used for encryption. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 54 | static CipherContext send_context; |
| 55 | |
| 56 | /* Buffer for raw input data from the socket. */ |
| 57 | static Buffer input; |
| 58 | |
| 59 | /* Buffer for raw output data going to the socket. */ |
| 60 | static Buffer output; |
| 61 | |
| 62 | /* Buffer for the partial outgoing packet being constructed. */ |
| 63 | static Buffer outgoing_packet; |
| 64 | |
| 65 | /* Buffer for the incoming packet currently being processed. */ |
| 66 | static Buffer incoming_packet; |
| 67 | |
| 68 | /* Scratch buffer for packet compression/decompression. */ |
| 69 | static Buffer compression_buffer; |
| 70 | |
| 71 | /* Flag indicating whether packet compression/decompression is enabled. */ |
| 72 | static int packet_compression = 0; |
| 73 | |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 74 | /* default maximum packet size */ |
| 75 | int max_packet_size = 32768; |
| 76 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 77 | /* Flag indicating whether this module has been initialized. */ |
| 78 | static int initialized = 0; |
| 79 | |
| 80 | /* Set to true if the connection is interactive. */ |
| 81 | static int interactive_mode = 0; |
| 82 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 83 | /* |
| 84 | * Sets the descriptors used for communication. Disables encryption until |
| 85 | * packet_set_encryption_key is called. |
| 86 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 87 | |
| 88 | void |
| 89 | packet_set_connection(int fd_in, int fd_out) |
| 90 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 91 | connection_in = fd_in; |
| 92 | connection_out = fd_out; |
| 93 | cipher_type = SSH_CIPHER_NONE; |
| 94 | cipher_set_key(&send_context, SSH_CIPHER_NONE, (unsigned char *) "", 0, 1); |
| 95 | cipher_set_key(&receive_context, SSH_CIPHER_NONE, (unsigned char *) "", 0, 0); |
| 96 | if (!initialized) { |
| 97 | initialized = 1; |
| 98 | buffer_init(&input); |
| 99 | buffer_init(&output); |
| 100 | buffer_init(&outgoing_packet); |
| 101 | buffer_init(&incoming_packet); |
| 102 | } |
| 103 | /* Kludge: arrange the close function to be called from fatal(). */ |
| 104 | fatal_add_cleanup((void (*) (void *)) packet_close, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* Sets the connection into non-blocking mode. */ |
| 108 | |
| 109 | void |
| 110 | packet_set_nonblocking() |
| 111 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 112 | /* Set the socket into non-blocking mode. */ |
| 113 | if (fcntl(connection_in, F_SETFL, O_NONBLOCK) < 0) |
| 114 | error("fcntl O_NONBLOCK: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 115 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 116 | if (connection_out != connection_in) { |
| 117 | if (fcntl(connection_out, F_SETFL, O_NONBLOCK) < 0) |
| 118 | error("fcntl O_NONBLOCK: %.100s", strerror(errno)); |
| 119 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* Returns the socket used for reading. */ |
| 123 | |
| 124 | int |
| 125 | packet_get_connection_in() |
| 126 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 127 | return connection_in; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* Returns the descriptor used for writing. */ |
| 131 | |
| 132 | int |
| 133 | packet_get_connection_out() |
| 134 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 135 | return connection_out; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* Closes the connection and clears and frees internal data structures. */ |
| 139 | |
| 140 | void |
| 141 | packet_close() |
| 142 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 143 | if (!initialized) |
| 144 | return; |
| 145 | initialized = 0; |
| 146 | if (connection_in == connection_out) { |
| 147 | shutdown(connection_out, SHUT_RDWR); |
| 148 | close(connection_out); |
| 149 | } else { |
| 150 | close(connection_in); |
| 151 | close(connection_out); |
| 152 | } |
| 153 | buffer_free(&input); |
| 154 | buffer_free(&output); |
| 155 | buffer_free(&outgoing_packet); |
| 156 | buffer_free(&incoming_packet); |
| 157 | if (packet_compression) { |
| 158 | buffer_free(&compression_buffer); |
| 159 | buffer_compress_uninit(); |
| 160 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /* Sets remote side protocol flags. */ |
| 164 | |
| 165 | void |
| 166 | packet_set_protocol_flags(unsigned int protocol_flags) |
| 167 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 168 | remote_protocol_flags = protocol_flags; |
| 169 | channel_set_options((protocol_flags & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) != 0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* Returns the remote protocol flags set earlier by the above function. */ |
| 173 | |
| 174 | unsigned int |
| 175 | packet_get_protocol_flags() |
| 176 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 177 | return remote_protocol_flags; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 178 | } |
| 179 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 180 | /* |
| 181 | * Starts packet compression from the next packet on in both directions. |
| 182 | * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip. |
| 183 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 184 | |
| 185 | void |
| 186 | packet_start_compression(int level) |
| 187 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 188 | if (packet_compression) |
| 189 | fatal("Compression already enabled."); |
| 190 | packet_compression = 1; |
| 191 | buffer_init(&compression_buffer); |
| 192 | buffer_compress_init(level); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 193 | } |
| 194 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 195 | /* |
| 196 | * Encrypts the given number of bytes, copying from src to dest. bytes is |
| 197 | * known to be a multiple of 8. |
| 198 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 199 | |
| 200 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 201 | packet_encrypt(CipherContext * cc, void *dest, void *src, |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 202 | unsigned int bytes) |
| 203 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 204 | cipher_encrypt(cc, dest, src, bytes); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 205 | } |
| 206 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 207 | /* |
| 208 | * Decrypts the given number of bytes, copying from src to dest. bytes is |
| 209 | * known to be a multiple of 8. |
| 210 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 211 | |
| 212 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 213 | packet_decrypt(CipherContext * cc, void *dest, void *src, |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 214 | unsigned int bytes) |
| 215 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 216 | int i; |
| 217 | |
| 218 | if ((bytes % 8) != 0) |
| 219 | fatal("packet_decrypt: bad ciphertext length %d", bytes); |
| 220 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 221 | /* |
| 222 | * Cryptographic attack detector for ssh - Modifications for packet.c |
| 223 | * (C)1998 CORE-SDI, Buenos Aires Argentina Ariel Futoransky(futo@core-sdi.com) |
| 224 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 225 | |
| 226 | switch (cc->type) { |
| 227 | case SSH_CIPHER_NONE: |
| 228 | i = DEATTACK_OK; |
| 229 | break; |
| 230 | default: |
| 231 | i = detect_attack(src, bytes, NULL); |
| 232 | break; |
| 233 | } |
| 234 | |
| 235 | if (i == DEATTACK_DETECTED) |
| 236 | packet_disconnect("crc32 compensation attack: network attack detected"); |
| 237 | |
| 238 | cipher_decrypt(cc, dest, src, bytes); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 239 | } |
| 240 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 241 | /* |
| 242 | * Causes any further packets to be encrypted using the given key. The same |
| 243 | * key is used for both sending and reception. However, both directions are |
| 244 | * encrypted independently of each other. |
| 245 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 246 | |
| 247 | void |
| 248 | packet_set_encryption_key(const unsigned char *key, unsigned int keylen, |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 249 | int cipher) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 250 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 251 | /* All other ciphers use the same key in both directions for now. */ |
| 252 | cipher_set_key(&receive_context, cipher, key, keylen, 0); |
| 253 | cipher_set_key(&send_context, cipher, key, keylen, 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* Starts constructing a packet to send. */ |
| 257 | |
| 258 | void |
| 259 | packet_start(int type) |
| 260 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 261 | char buf[9]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 262 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 263 | buffer_clear(&outgoing_packet); |
| 264 | memset(buf, 0, 8); |
| 265 | buf[8] = type; |
| 266 | buffer_append(&outgoing_packet, buf, 9); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* Appends a character to the packet data. */ |
| 270 | |
| 271 | void |
| 272 | packet_put_char(int value) |
| 273 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 274 | char ch = value; |
| 275 | buffer_append(&outgoing_packet, &ch, 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /* Appends an integer to the packet data. */ |
| 279 | |
| 280 | void |
| 281 | packet_put_int(unsigned int value) |
| 282 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 283 | buffer_put_int(&outgoing_packet, value); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /* Appends a string to packet data. */ |
| 287 | |
| 288 | void |
| 289 | packet_put_string(const char *buf, unsigned int len) |
| 290 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 291 | buffer_put_string(&outgoing_packet, buf, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /* Appends an arbitrary precision integer to packet data. */ |
| 295 | |
| 296 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 297 | packet_put_bignum(BIGNUM * value) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 298 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 299 | buffer_put_bignum(&outgoing_packet, value); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 300 | } |
| 301 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 302 | /* |
| 303 | * Finalizes and sends the packet. If the encryption key has been set, |
| 304 | * encrypts the packet before sending. |
| 305 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 306 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 307 | void |
| 308 | packet_send() |
| 309 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 310 | char buf[8], *cp; |
| 311 | int i, padding, len; |
| 312 | unsigned int checksum; |
| 313 | u_int32_t rand = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 314 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 315 | /* |
| 316 | * If using packet compression, compress the payload of the outgoing |
| 317 | * packet. |
| 318 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 319 | if (packet_compression) { |
| 320 | buffer_clear(&compression_buffer); |
| 321 | /* Skip padding. */ |
| 322 | buffer_consume(&outgoing_packet, 8); |
| 323 | /* padding */ |
| 324 | buffer_append(&compression_buffer, "\0\0\0\0\0\0\0\0", 8); |
| 325 | buffer_compress(&outgoing_packet, &compression_buffer); |
| 326 | buffer_clear(&outgoing_packet); |
| 327 | buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer), |
| 328 | buffer_len(&compression_buffer)); |
| 329 | } |
| 330 | /* Compute packet length without padding (add checksum, remove padding). */ |
| 331 | len = buffer_len(&outgoing_packet) + 4 - 8; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 332 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 333 | /* Insert padding. */ |
| 334 | padding = 8 - len % 8; |
| 335 | if (cipher_type != SSH_CIPHER_NONE) { |
| 336 | cp = buffer_ptr(&outgoing_packet); |
| 337 | for (i = 0; i < padding; i++) { |
| 338 | if (i % 4 == 0) |
| 339 | rand = arc4random(); |
| 340 | cp[7 - i] = rand & 0xff; |
| 341 | rand >>= 8; |
| 342 | } |
| 343 | } |
| 344 | buffer_consume(&outgoing_packet, 8 - padding); |
| 345 | |
| 346 | /* Add check bytes. */ |
| 347 | checksum = crc32((unsigned char *) buffer_ptr(&outgoing_packet), |
| 348 | buffer_len(&outgoing_packet)); |
| 349 | PUT_32BIT(buf, checksum); |
| 350 | buffer_append(&outgoing_packet, buf, 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 351 | |
| 352 | #ifdef PACKET_DEBUG |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 353 | fprintf(stderr, "packet_send plain: "); |
| 354 | buffer_dump(&outgoing_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 355 | #endif |
| 356 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 357 | /* Append to output. */ |
| 358 | PUT_32BIT(buf, len); |
| 359 | buffer_append(&output, buf, 4); |
| 360 | buffer_append_space(&output, &cp, buffer_len(&outgoing_packet)); |
| 361 | packet_encrypt(&send_context, cp, buffer_ptr(&outgoing_packet), |
| 362 | buffer_len(&outgoing_packet)); |
| 363 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 364 | #ifdef PACKET_DEBUG |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 365 | fprintf(stderr, "encrypted: "); |
| 366 | buffer_dump(&output); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 367 | #endif |
| 368 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 369 | buffer_clear(&outgoing_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 370 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 371 | /* |
| 372 | * Note that the packet is now only buffered in output. It won\'t be |
| 373 | * actually sent until packet_write_wait or packet_write_poll is |
| 374 | * called. |
| 375 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 376 | } |
| 377 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 378 | /* |
| 379 | * Waits until a packet has been received, and returns its type. Note that |
| 380 | * no other data is processed until this returns, so this function should not |
| 381 | * be used during the interactive session. |
| 382 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 383 | |
| 384 | int |
| 385 | packet_read(int *payload_len_ptr) |
| 386 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 387 | int type, len; |
| 388 | fd_set set; |
| 389 | char buf[8192]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 390 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 391 | /* Since we are blocking, ensure that all written packets have been sent. */ |
| 392 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 393 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 394 | /* Stay in the loop until we have received a complete packet. */ |
| 395 | for (;;) { |
| 396 | /* Try to read a packet from the buffer. */ |
| 397 | type = packet_read_poll(payload_len_ptr); |
| 398 | if (type == SSH_SMSG_SUCCESS |
| 399 | || type == SSH_SMSG_FAILURE |
| 400 | || type == SSH_CMSG_EOF |
| 401 | || type == SSH_CMSG_EXIT_CONFIRMATION) |
| 402 | packet_integrity_check(*payload_len_ptr, 0, type); |
| 403 | /* If we got a packet, return it. */ |
| 404 | if (type != SSH_MSG_NONE) |
| 405 | return type; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 406 | /* |
| 407 | * Otherwise, wait for some data to arrive, add it to the |
| 408 | * buffer, and try again. |
| 409 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 410 | FD_ZERO(&set); |
| 411 | FD_SET(connection_in, &set); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 412 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 413 | /* Wait for some data to arrive. */ |
| 414 | select(connection_in + 1, &set, NULL, NULL, NULL); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 415 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 416 | /* Read data from the socket. */ |
| 417 | len = read(connection_in, buf, sizeof(buf)); |
| 418 | if (len == 0) |
| 419 | fatal("Connection closed by %.200s", get_remote_ipaddr()); |
| 420 | if (len < 0) |
| 421 | fatal("Read from socket failed: %.100s", strerror(errno)); |
| 422 | /* Append it to the buffer. */ |
| 423 | packet_process_incoming(buf, len); |
| 424 | } |
| 425 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 426 | } |
| 427 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 428 | /* |
| 429 | * Waits until a packet has been received, verifies that its type matches |
| 430 | * that given, and gives a fatal error and exits if there is a mismatch. |
| 431 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 432 | |
| 433 | void |
| 434 | packet_read_expect(int *payload_len_ptr, int expected_type) |
| 435 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 436 | int type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 437 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 438 | type = packet_read(payload_len_ptr); |
| 439 | if (type != expected_type) |
| 440 | packet_disconnect("Protocol error: expected packet type %d, got %d", |
| 441 | expected_type, type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /* Checks if a full packet is available in the data received so far via |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 445 | * packet_process_incoming. If so, reads the packet; otherwise returns |
| 446 | * SSH_MSG_NONE. This does not wait for data from the connection. |
| 447 | * |
| 448 | * SSH_MSG_DISCONNECT is handled specially here. Also, |
| 449 | * SSH_MSG_IGNORE messages are skipped by this function and are never returned |
| 450 | * to higher levels. |
| 451 | * |
| 452 | * The returned payload_len does include space consumed by: |
| 453 | * Packet length |
| 454 | * Padding |
| 455 | * Packet type |
| 456 | * Check bytes |
| 457 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 458 | |
| 459 | int |
| 460 | packet_read_poll(int *payload_len_ptr) |
| 461 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 462 | unsigned int len, padded_len; |
| 463 | unsigned char *ucp; |
| 464 | char buf[8], *cp; |
| 465 | unsigned int checksum, stored_checksum; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 466 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 467 | restart: |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 468 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 469 | /* Check if input size is less than minimum packet size. */ |
| 470 | if (buffer_len(&input) < 4 + 8) |
| 471 | return SSH_MSG_NONE; |
| 472 | /* Get length of incoming packet. */ |
| 473 | ucp = (unsigned char *) buffer_ptr(&input); |
| 474 | len = GET_32BIT(ucp); |
| 475 | if (len < 1 + 2 + 2 || len > 256 * 1024) |
| 476 | packet_disconnect("Bad packet length %d.", len); |
| 477 | padded_len = (len + 8) & ~7; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 478 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 479 | /* Check if the packet has been entirely received. */ |
| 480 | if (buffer_len(&input) < 4 + padded_len) |
| 481 | return SSH_MSG_NONE; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 482 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 483 | /* The entire packet is in buffer. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 484 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 485 | /* Consume packet length. */ |
| 486 | buffer_consume(&input, 4); |
| 487 | |
| 488 | /* Copy data to incoming_packet. */ |
| 489 | buffer_clear(&incoming_packet); |
| 490 | buffer_append_space(&incoming_packet, &cp, padded_len); |
| 491 | packet_decrypt(&receive_context, cp, buffer_ptr(&input), padded_len); |
| 492 | buffer_consume(&input, padded_len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 493 | |
| 494 | #ifdef PACKET_DEBUG |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 495 | fprintf(stderr, "read_poll plain: "); |
| 496 | buffer_dump(&incoming_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 497 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 498 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 499 | /* Compute packet checksum. */ |
| 500 | checksum = crc32((unsigned char *) buffer_ptr(&incoming_packet), |
| 501 | buffer_len(&incoming_packet) - 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 502 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 503 | /* Skip padding. */ |
| 504 | buffer_consume(&incoming_packet, 8 - len % 8); |
Damien Miller | fd7c911 | 1999-11-08 16:15:55 +1100 | [diff] [blame] | 505 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 506 | /* Test check bytes. */ |
Damien Miller | fd7c911 | 1999-11-08 16:15:55 +1100 | [diff] [blame] | 507 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 508 | if (len != buffer_len(&incoming_packet)) |
| 509 | packet_disconnect("packet_read_poll: len %d != buffer_len %d.", |
| 510 | len, buffer_len(&incoming_packet)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 511 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 512 | ucp = (unsigned char *) buffer_ptr(&incoming_packet) + len - 4; |
| 513 | stored_checksum = GET_32BIT(ucp); |
| 514 | if (checksum != stored_checksum) |
| 515 | packet_disconnect("Corrupted check bytes on input."); |
| 516 | buffer_consume_end(&incoming_packet, 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 517 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 518 | /* If using packet compression, decompress the packet. */ |
| 519 | if (packet_compression) { |
| 520 | buffer_clear(&compression_buffer); |
| 521 | buffer_uncompress(&incoming_packet, &compression_buffer); |
| 522 | buffer_clear(&incoming_packet); |
| 523 | buffer_append(&incoming_packet, buffer_ptr(&compression_buffer), |
| 524 | buffer_len(&compression_buffer)); |
| 525 | } |
| 526 | /* Get packet type. */ |
| 527 | buffer_get(&incoming_packet, &buf[0], 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 528 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 529 | /* Return length of payload (without type field). */ |
| 530 | *payload_len_ptr = buffer_len(&incoming_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 531 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 532 | /* Handle disconnect message. */ |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 533 | if ((unsigned char) buf[0] == SSH_MSG_DISCONNECT) { |
| 534 | log("Received disconnect: %.900s", packet_get_string(NULL)); |
| 535 | fatal_cleanup(); |
| 536 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 537 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 538 | /* Ignore ignore messages. */ |
| 539 | if ((unsigned char) buf[0] == SSH_MSG_IGNORE) |
| 540 | goto restart; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 541 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 542 | /* Send debug messages as debugging output. */ |
| 543 | if ((unsigned char) buf[0] == SSH_MSG_DEBUG) { |
| 544 | debug("Remote: %.900s", packet_get_string(NULL)); |
| 545 | goto restart; |
| 546 | } |
| 547 | /* Return type. */ |
| 548 | return (unsigned char) buf[0]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 549 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 550 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 551 | /* |
| 552 | * Buffers the given amount of input characters. This is intended to be used |
| 553 | * together with packet_read_poll. |
| 554 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 555 | |
| 556 | void |
| 557 | packet_process_incoming(const char *buf, unsigned int len) |
| 558 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 559 | buffer_append(&input, buf, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | /* Returns a character from the packet. */ |
| 563 | |
| 564 | unsigned int |
| 565 | packet_get_char() |
| 566 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 567 | char ch; |
| 568 | buffer_get(&incoming_packet, &ch, 1); |
| 569 | return (unsigned char) ch; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /* Returns an integer from the packet data. */ |
| 573 | |
| 574 | unsigned int |
| 575 | packet_get_int() |
| 576 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 577 | return buffer_get_int(&incoming_packet); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 578 | } |
| 579 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 580 | /* |
| 581 | * Returns an arbitrary precision integer from the packet data. The integer |
| 582 | * must have been initialized before this call. |
| 583 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 584 | |
| 585 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 586 | packet_get_bignum(BIGNUM * value, int *length_ptr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 587 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 588 | *length_ptr = buffer_get_bignum(&incoming_packet, value); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 589 | } |
| 590 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 591 | /* |
| 592 | * Returns a string from the packet data. The string is allocated using |
| 593 | * xmalloc; it is the responsibility of the calling program to free it when |
| 594 | * no longer needed. The length_ptr argument may be NULL, or point to an |
| 595 | * integer into which the length of the string is stored. |
| 596 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 597 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 598 | char * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 599 | packet_get_string(unsigned int *length_ptr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 600 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 601 | return buffer_get_string(&incoming_packet, length_ptr); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 602 | } |
| 603 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 604 | /* |
| 605 | * Sends a diagnostic message from the server to the client. This message |
| 606 | * can be sent at any time (but not while constructing another message). The |
| 607 | * message is printed immediately, but only if the client is being executed |
| 608 | * in verbose mode. These messages are primarily intended to ease debugging |
| 609 | * authentication problems. The length of the formatted message must not |
| 610 | * exceed 1024 bytes. This will automatically call packet_write_wait. |
| 611 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 612 | |
| 613 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 614 | packet_send_debug(const char *fmt,...) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 615 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 616 | char buf[1024]; |
| 617 | va_list args; |
| 618 | |
| 619 | va_start(args, fmt); |
| 620 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 621 | va_end(args); |
| 622 | |
| 623 | packet_start(SSH_MSG_DEBUG); |
| 624 | packet_put_string(buf, strlen(buf)); |
| 625 | packet_send(); |
| 626 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 627 | } |
| 628 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 629 | /* |
| 630 | * Logs the error plus constructs and sends a disconnect packet, closes the |
| 631 | * connection, and exits. This function never returns. The error message |
| 632 | * should not contain a newline. The length of the formatted message must |
| 633 | * not exceed 1024 bytes. |
| 634 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 635 | |
| 636 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 637 | packet_disconnect(const char *fmt,...) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 638 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 639 | char buf[1024]; |
| 640 | va_list args; |
| 641 | static int disconnecting = 0; |
| 642 | if (disconnecting) /* Guard against recursive invocations. */ |
| 643 | fatal("packet_disconnect called recursively."); |
| 644 | disconnecting = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 645 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 646 | /* |
| 647 | * Format the message. Note that the caller must make sure the |
| 648 | * message is of limited size. |
| 649 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 650 | va_start(args, fmt); |
| 651 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 652 | va_end(args); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 653 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 654 | /* Send the disconnect message to the other side, and wait for it to get sent. */ |
| 655 | packet_start(SSH_MSG_DISCONNECT); |
| 656 | packet_put_string(buf, strlen(buf)); |
| 657 | packet_send(); |
| 658 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 659 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 660 | /* Stop listening for connections. */ |
| 661 | channel_stop_listening(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 662 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 663 | /* Close the connection. */ |
| 664 | packet_close(); |
| 665 | |
| 666 | /* Display the error locally and exit. */ |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 667 | log("Disconnecting: %.100s", buf); |
| 668 | fatal_cleanup(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 669 | } |
| 670 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 671 | /* Checks if there is any buffered output, and tries to write some of the output. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 672 | |
| 673 | void |
| 674 | packet_write_poll() |
| 675 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 676 | int len = buffer_len(&output); |
| 677 | if (len > 0) { |
| 678 | len = write(connection_out, buffer_ptr(&output), len); |
| 679 | if (len <= 0) { |
| 680 | if (errno == EAGAIN) |
| 681 | return; |
| 682 | else |
| 683 | fatal("Write failed: %.100s", strerror(errno)); |
| 684 | } |
| 685 | buffer_consume(&output, len); |
| 686 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 687 | } |
| 688 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 689 | /* |
| 690 | * Calls packet_write_poll repeatedly until all pending output data has been |
| 691 | * written. |
| 692 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 693 | |
| 694 | void |
| 695 | packet_write_wait() |
| 696 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 697 | packet_write_poll(); |
| 698 | while (packet_have_data_to_write()) { |
| 699 | fd_set set; |
| 700 | FD_ZERO(&set); |
| 701 | FD_SET(connection_out, &set); |
| 702 | select(connection_out + 1, NULL, &set, NULL, NULL); |
| 703 | packet_write_poll(); |
| 704 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | /* Returns true if there is buffered data to write to the connection. */ |
| 708 | |
| 709 | int |
| 710 | packet_have_data_to_write() |
| 711 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 712 | return buffer_len(&output) != 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* Returns true if there is not too much data to write to the connection. */ |
| 716 | |
| 717 | int |
| 718 | packet_not_very_much_data_to_write() |
| 719 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 720 | if (interactive_mode) |
| 721 | return buffer_len(&output) < 16384; |
| 722 | else |
| 723 | return buffer_len(&output) < 128 * 1024; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | /* Informs that the current session is interactive. Sets IP flags for that. */ |
| 727 | |
| 728 | void |
| 729 | packet_set_interactive(int interactive, int keepalives) |
| 730 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 731 | int on = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 732 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 733 | /* Record that we are in interactive mode. */ |
| 734 | interactive_mode = interactive; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 735 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 736 | /* |
| 737 | * Only set socket options if using a socket (as indicated by the |
| 738 | * descriptors being the same). |
| 739 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 740 | if (connection_in != connection_out) |
| 741 | return; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 742 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 743 | if (keepalives) { |
| 744 | /* Set keepalives if requested. */ |
| 745 | if (setsockopt(connection_in, SOL_SOCKET, SO_KEEPALIVE, (void *) &on, |
| 746 | sizeof(on)) < 0) |
| 747 | error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno)); |
| 748 | } |
| 749 | if (interactive) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 750 | /* |
| 751 | * Set IP options for an interactive connection. Use |
| 752 | * IPTOS_LOWDELAY and TCP_NODELAY. |
| 753 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 754 | int lowdelay = IPTOS_LOWDELAY; |
| 755 | if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &lowdelay, |
| 756 | sizeof(lowdelay)) < 0) |
| 757 | error("setsockopt IPTOS_LOWDELAY: %.100s", strerror(errno)); |
| 758 | if (setsockopt(connection_in, IPPROTO_TCP, TCP_NODELAY, (void *) &on, |
| 759 | sizeof(on)) < 0) |
| 760 | error("setsockopt TCP_NODELAY: %.100s", strerror(errno)); |
| 761 | } else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 762 | /* |
| 763 | * Set IP options for a non-interactive connection. Use |
| 764 | * IPTOS_THROUGHPUT. |
| 765 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 766 | int throughput = IPTOS_THROUGHPUT; |
| 767 | if (setsockopt(connection_in, IPPROTO_IP, IP_TOS, (void *) &throughput, |
| 768 | sizeof(throughput)) < 0) |
| 769 | error("setsockopt IPTOS_THROUGHPUT: %.100s", strerror(errno)); |
| 770 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /* Returns true if the current connection is interactive. */ |
| 774 | |
| 775 | int |
| 776 | packet_is_interactive() |
| 777 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 778 | return interactive_mode; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 779 | } |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 780 | |
| 781 | int |
| 782 | packet_set_maxsize(int s) |
| 783 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 784 | static int called = 0; |
| 785 | if (called) { |
| 786 | log("packet_set_maxsize: called twice: old %d new %d", max_packet_size, s); |
| 787 | return -1; |
| 788 | } |
| 789 | if (s < 4 * 1024 || s > 1024 * 1024) { |
| 790 | log("packet_set_maxsize: bad size %d", s); |
| 791 | return -1; |
| 792 | } |
| 793 | log("packet_set_maxsize: setting to %d", s); |
| 794 | max_packet_size = s; |
| 795 | return s; |
Damien Miller | 6162d12 | 1999-11-21 13:23:52 +1100 | [diff] [blame] | 796 | } |