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