version 1.1, 1999/09/26 20:53:36 |
version 1.2, 1999/09/28 04:45:36 |
|
|
RCSID("$Id$"); |
RCSID("$Id$"); |
|
|
#include "xmalloc.h" |
#include "xmalloc.h" |
#include "randoms.h" |
|
#include "buffer.h" |
#include "buffer.h" |
#include "packet.h" |
#include "packet.h" |
#include "bufaux.h" |
#include "bufaux.h" |
|
|
static int packet_compression = 0; |
static int packet_compression = 0; |
#endif /* WITH_ZLIB */ |
#endif /* WITH_ZLIB */ |
|
|
/* Pointer to the random number generator state. */ |
|
static RandomState *random_state; |
|
|
|
/* Flag indicating whether this module has been initialized. */ |
/* Flag indicating whether this module has been initialized. */ |
static int initialized = 0; |
static int initialized = 0; |
|
|
|
|
/* Sets the descriptors used for communication. Disables encryption until |
/* Sets the descriptors used for communication. Disables encryption until |
packet_set_encryption_key is called. */ |
packet_set_encryption_key is called. */ |
|
|
void packet_set_connection(int fd_in, int fd_out, RandomState *state) |
void |
|
packet_set_connection(int fd_in, int fd_out) |
{ |
{ |
connection_in = fd_in; |
connection_in = fd_in; |
connection_out = fd_out; |
connection_out = fd_out; |
random_state = state; |
|
cipher_type = SSH_CIPHER_NONE; |
cipher_type = SSH_CIPHER_NONE; |
cipher_set_key(&send_context, SSH_CIPHER_NONE, (unsigned char *)"", 0, 1); |
cipher_set_key(&send_context, SSH_CIPHER_NONE, (unsigned char *)"", 0, 1); |
cipher_set_key(&receive_context, SSH_CIPHER_NONE, (unsigned char *)"", 0, 0); |
cipher_set_key(&receive_context, SSH_CIPHER_NONE, (unsigned char *)"", 0, 0); |
|
|
|
|
/* Sets the connection into non-blocking mode. */ |
/* Sets the connection into non-blocking mode. */ |
|
|
void packet_set_nonblocking() |
void |
|
packet_set_nonblocking() |
{ |
{ |
/* Set the socket into non-blocking mode. */ |
/* Set the socket into non-blocking mode. */ |
#if defined(O_NONBLOCK) && !defined(O_NONBLOCK_BROKEN) |
#if defined(O_NONBLOCK) && !defined(O_NONBLOCK_BROKEN) |
|
|
|
|
/* Returns the socket used for reading. */ |
/* Returns the socket used for reading. */ |
|
|
int packet_get_connection_in() |
int |
|
packet_get_connection_in() |
{ |
{ |
return connection_in; |
return connection_in; |
} |
} |
|
|
/* Returns the descriptor used for writing. */ |
/* Returns the descriptor used for writing. */ |
|
|
int packet_get_connection_out() |
int |
|
packet_get_connection_out() |
{ |
{ |
return connection_out; |
return connection_out; |
} |
} |
|
|
/* Closes the connection and clears and frees internal data structures. */ |
/* Closes the connection and clears and frees internal data structures. */ |
|
|
void packet_close() |
void |
|
packet_close() |
{ |
{ |
if (!initialized) |
if (!initialized) |
return; |
return; |
|
|
|
|
/* Sets remote side protocol flags. */ |
/* Sets remote side protocol flags. */ |
|
|
void packet_set_protocol_flags(unsigned int protocol_flags) |
void |
|
packet_set_protocol_flags(unsigned int protocol_flags) |
{ |
{ |
remote_protocol_flags = protocol_flags; |
remote_protocol_flags = protocol_flags; |
channel_set_options((protocol_flags & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) != 0); |
channel_set_options((protocol_flags & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) != 0); |
|
|
|
|
/* Returns the remote protocol flags set earlier by the above function. */ |
/* Returns the remote protocol flags set earlier by the above function. */ |
|
|
unsigned int packet_get_protocol_flags() |
unsigned int |
|
packet_get_protocol_flags() |
{ |
{ |
return remote_protocol_flags; |
return remote_protocol_flags; |
} |
} |
|
|
/* Starts packet compression from the next packet on in both directions. |
/* Starts packet compression from the next packet on in both directions. |
Level is compression level 1 (fastest) - 9 (slow, best) as in gzip. */ |
Level is compression level 1 (fastest) - 9 (slow, best) as in gzip. */ |
|
|
void packet_start_compression(int level) |
void |
|
packet_start_compression(int level) |
{ |
{ |
if (packet_compression) |
if (packet_compression) |
fatal("Compression already enabled."); |
fatal("Compression already enabled."); |
|
|
/* Encrypts the given number of bytes, copying from src to dest. |
/* Encrypts the given number of bytes, copying from src to dest. |
bytes is known to be a multiple of 8. */ |
bytes is known to be a multiple of 8. */ |
|
|
void packet_encrypt(CipherContext *cc, void *dest, void *src, |
void |
unsigned int bytes) |
packet_encrypt(CipherContext *cc, void *dest, void *src, |
|
unsigned int bytes) |
{ |
{ |
assert((bytes % 8) == 0); |
assert((bytes % 8) == 0); |
cipher_encrypt(cc, dest, src, bytes); |
cipher_encrypt(cc, dest, src, bytes); |
|
|
/* Decrypts the given number of bytes, copying from src to dest. |
/* Decrypts the given number of bytes, copying from src to dest. |
bytes is known to be a multiple of 8. */ |
bytes is known to be a multiple of 8. */ |
|
|
void packet_decrypt(CipherContext *cc, void *dest, void *src, |
void |
unsigned int bytes) |
packet_decrypt(CipherContext *cc, void *dest, void *src, |
|
unsigned int bytes) |
{ |
{ |
assert((bytes % 8) == 0); |
assert((bytes % 8) == 0); |
cipher_decrypt(cc, dest, src, bytes); |
cipher_decrypt(cc, dest, src, bytes); |
|
|
key is used for both sending and reception. However, both directions |
key is used for both sending and reception. However, both directions |
are encrypted independently of each other. */ |
are encrypted independently of each other. */ |
|
|
void packet_set_encryption_key(const unsigned char *key, unsigned int keylen, |
void |
int cipher, int is_client) |
packet_set_encryption_key(const unsigned char *key, unsigned int keylen, |
|
int cipher, int is_client) |
{ |
{ |
cipher_type = cipher; |
cipher_type = cipher; |
if (cipher == SSH_CIPHER_RC4) |
if (cipher == SSH_CIPHER_RC4) |
|
|
|
|
/* Starts constructing a packet to send. */ |
/* Starts constructing a packet to send. */ |
|
|
void packet_start(int type) |
void |
|
packet_start(int type) |
{ |
{ |
char buf[9]; |
char buf[9]; |
|
|
|
|
|
|
/* Appends a character to the packet data. */ |
/* Appends a character to the packet data. */ |
|
|
void packet_put_char(int value) |
void |
|
packet_put_char(int value) |
{ |
{ |
char ch = value; |
char ch = value; |
buffer_append(&outgoing_packet, &ch, 1); |
buffer_append(&outgoing_packet, &ch, 1); |
|
|
|
|
/* Appends an integer to the packet data. */ |
/* Appends an integer to the packet data. */ |
|
|
void packet_put_int(unsigned int value) |
void |
|
packet_put_int(unsigned int value) |
{ |
{ |
buffer_put_int(&outgoing_packet, value); |
buffer_put_int(&outgoing_packet, value); |
} |
} |
|
|
/* Appends a string to packet data. */ |
/* Appends a string to packet data. */ |
|
|
void packet_put_string(const char *buf, unsigned int len) |
void |
|
packet_put_string(const char *buf, unsigned int len) |
{ |
{ |
buffer_put_string(&outgoing_packet, buf, len); |
buffer_put_string(&outgoing_packet, buf, len); |
} |
} |
|
|
/* Appends an arbitrary precision integer to packet data. */ |
/* Appends an arbitrary precision integer to packet data. */ |
|
|
void packet_put_mp_int(MP_INT *value) |
void |
|
packet_put_bignum(BIGNUM *value) |
{ |
{ |
buffer_put_mp_int(&outgoing_packet, value); |
buffer_put_bignum(&outgoing_packet, value); |
} |
} |
|
|
/* Finalizes and sends the packet. If the encryption key has been set, |
/* Finalizes and sends the packet. If the encryption key has been set, |
encrypts the packet before sending. */ |
encrypts the packet before sending. */ |
|
|
void packet_send() |
void |
|
packet_send() |
{ |
{ |
char buf[8], *cp; |
char buf[8], *cp; |
int i, padding, len; |
int i, padding, len; |
unsigned long checksum; |
unsigned long checksum; |
|
u_int32_t rand; |
|
|
#ifdef WITH_ZLIB |
#ifdef WITH_ZLIB |
/* If using packet compression, compress the payload of the outgoing |
/* If using packet compression, compress the payload of the outgoing |
|
|
if (cipher_type != SSH_CIPHER_NONE) |
if (cipher_type != SSH_CIPHER_NONE) |
{ |
{ |
cp = buffer_ptr(&outgoing_packet); |
cp = buffer_ptr(&outgoing_packet); |
for (i = 0; i < padding; i++) |
for (i = 0; i < padding; i++) { |
cp[7 - i] = random_get_byte(random_state); |
if (i % 4 == 0) |
|
rand = arc4random(); |
|
cp[7 - i] = rand & 0xff; |
|
rand >>= 8; |
|
} |
} |
} |
buffer_consume(&outgoing_packet, 8 - padding); |
buffer_consume(&outgoing_packet, 8 - padding); |
|
|
|
|
no other data is processed until this returns, so this function should |
no other data is processed until this returns, so this function should |
not be used during the interactive session. */ |
not be used during the interactive session. */ |
|
|
int packet_read(int *payload_len_ptr) |
int |
|
packet_read(int *payload_len_ptr) |
{ |
{ |
int type, len; |
int type, len; |
fd_set set; |
fd_set set; |
|
|
/* Waits until a packet has been received, verifies that its type matches |
/* Waits until a packet has been received, verifies that its type matches |
that given, and gives a fatal error and exits if there is a mismatch. */ |
that given, and gives a fatal error and exits if there is a mismatch. */ |
|
|
void packet_read_expect(int *payload_len_ptr, int expected_type) |
void |
|
packet_read_expect(int *payload_len_ptr, int expected_type) |
{ |
{ |
int type; |
int type; |
|
|
|
|
|
|
*/ |
*/ |
|
|
int packet_read_poll(int *payload_len_ptr) |
int |
|
packet_read_poll(int *payload_len_ptr) |
{ |
{ |
unsigned int len, padded_len; |
unsigned int len, padded_len; |
unsigned char *ucp; |
unsigned char *ucp; |
|
|
/* Buffers the given amount of input characters. This is intended to be |
/* Buffers the given amount of input characters. This is intended to be |
used together with packet_read_poll. */ |
used together with packet_read_poll. */ |
|
|
void packet_process_incoming(const char *buf, unsigned int len) |
void |
|
packet_process_incoming(const char *buf, unsigned int len) |
{ |
{ |
buffer_append(&input, buf, len); |
buffer_append(&input, buf, len); |
} |
} |
|
|
/* Returns a character from the packet. */ |
/* Returns a character from the packet. */ |
|
|
unsigned int packet_get_char() |
unsigned int |
|
packet_get_char() |
{ |
{ |
char ch; |
char ch; |
buffer_get(&incoming_packet, &ch, 1); |
buffer_get(&incoming_packet, &ch, 1); |
|
|
|
|
/* Returns an integer from the packet data. */ |
/* Returns an integer from the packet data. */ |
|
|
unsigned int packet_get_int() |
unsigned int |
|
packet_get_int() |
{ |
{ |
return buffer_get_int(&incoming_packet); |
return buffer_get_int(&incoming_packet); |
} |
} |
|
|
/* Returns an arbitrary precision integer from the packet data. The integer |
/* Returns an arbitrary precision integer from the packet data. The integer |
must have been initialized before this call. */ |
must have been initialized before this call. */ |
|
|
void packet_get_mp_int(MP_INT *value, int *length_ptr) |
void |
|
packet_get_bignum(BIGNUM *value, int *length_ptr) |
{ |
{ |
*length_ptr = buffer_get_mp_int(&incoming_packet, value); |
*length_ptr = buffer_get_bignum(&incoming_packet, value); |
} |
} |
|
|
/* Returns a string from the packet data. The string is allocated using |
/* Returns a string from the packet data. The string is allocated using |
|
|
no longer needed. The length_ptr argument may be NULL, or point to an |
no longer needed. The length_ptr argument may be NULL, or point to an |
integer into which the length of the string is stored. */ |
integer into which the length of the string is stored. */ |
|
|
char *packet_get_string(unsigned int *length_ptr) |
char |
|
*packet_get_string(unsigned int *length_ptr) |
{ |
{ |
return buffer_get_string(&incoming_packet, length_ptr); |
return buffer_get_string(&incoming_packet, length_ptr); |
} |
} |
|
|
message must not exceed 1024 bytes. This will automatically call |
message must not exceed 1024 bytes. This will automatically call |
packet_write_wait. */ |
packet_write_wait. */ |
|
|
void packet_send_debug(const char *fmt, ...) |
void |
|
packet_send_debug(const char *fmt, ...) |
{ |
{ |
char buf[1024]; |
char buf[1024]; |
va_list args; |
va_list args; |
|
|
The error message should not contain a newline. The length of the |
The error message should not contain a newline. The length of the |
formatted message must not exceed 1024 bytes. */ |
formatted message must not exceed 1024 bytes. */ |
|
|
void packet_disconnect(const char *fmt, ...) |
void |
|
packet_disconnect(const char *fmt, ...) |
{ |
{ |
char buf[1024]; |
char buf[1024]; |
va_list args; |
va_list args; |
|
|
/* Checks if there is any buffered output, and tries to write some of the |
/* Checks if there is any buffered output, and tries to write some of the |
output. */ |
output. */ |
|
|
void packet_write_poll() |
void |
|
packet_write_poll() |
{ |
{ |
int len = buffer_len(&output); |
int len = buffer_len(&output); |
if (len > 0) |
if (len > 0) |
{ |
{ |
len = write(connection_out, buffer_ptr(&output), len); |
len = write(connection_out, buffer_ptr(&output), len); |
if (len <= 0) |
if (len <= 0) { |
if (errno == EAGAIN) |
if (errno == EAGAIN) |
return; |
return; |
else |
else |
fatal("Write failed: %.100s", strerror(errno)); |
fatal("Write failed: %.100s", strerror(errno)); |
|
} |
buffer_consume(&output, len); |
buffer_consume(&output, len); |
} |
} |
} |
} |
|
|
/* Calls packet_write_poll repeatedly until all pending output data has |
/* Calls packet_write_poll repeatedly until all pending output data has |
been written. */ |
been written. */ |
|
|
void packet_write_wait() |
void |
|
packet_write_wait() |
{ |
{ |
packet_write_poll(); |
packet_write_poll(); |
while (packet_have_data_to_write()) |
while (packet_have_data_to_write()) |
|
|
|
|
/* Returns true if there is buffered data to write to the connection. */ |
/* Returns true if there is buffered data to write to the connection. */ |
|
|
int packet_have_data_to_write() |
int |
|
packet_have_data_to_write() |
{ |
{ |
return buffer_len(&output) != 0; |
return buffer_len(&output) != 0; |
} |
} |
|
|
/* Returns true if there is not too much data to write to the connection. */ |
/* Returns true if there is not too much data to write to the connection. */ |
|
|
int packet_not_very_much_data_to_write() |
int |
|
packet_not_very_much_data_to_write() |
{ |
{ |
if (interactive_mode) |
if (interactive_mode) |
return buffer_len(&output) < 16384; |
return buffer_len(&output) < 16384; |
|
|
|
|
/* Informs that the current session is interactive. Sets IP flags for that. */ |
/* Informs that the current session is interactive. Sets IP flags for that. */ |
|
|
void packet_set_interactive(int interactive, int keepalives) |
void |
|
packet_set_interactive(int interactive, int keepalives) |
{ |
{ |
int on = 1; |
int on = 1; |
|
|
|
|
|
|
/* Returns true if the current connection is interactive. */ |
/* Returns true if the current connection is interactive. */ |
|
|
int packet_is_interactive() |
int |
|
packet_is_interactive() |
{ |
{ |
return interactive_mode; |
return interactive_mode; |
} |
} |