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