[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.4

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