[BACK]Return to packet.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/packet.c, Revision 1.6

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