1 /* 2 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * All rights reserved 5 * Interface for the packet protocol functions. 6 * 7 * As far as I am concerned, the code I have written for this software 8 * can be used freely for any purpose. Any derived versions of this 9 * software must be clearly marked as such, and if the derived work is 10 * incompatible with the protocol description in the RFC file, it must be 11 * called by a name other than "ssh" or "Secure Shell". 12 */ 13 14 /* RCSID("$OpenBSD: packet.h,v 1.22 2001/04/14 16:33:20 stevesk Exp $"); */ 15 /* RCSID("$FreeBSD$"); */ 16 17 #ifndef PACKET_H 18 #define PACKET_H 19 20 #include <openssl/bn.h> 21 22 /* 23 * Sets the socket used for communication. Disables encryption until 24 * packet_set_encryption_key is called. It is permissible that fd_in and 25 * fd_out are the same descriptor; in that case it is assumed to be a socket. 26 */ 27 void packet_set_connection(int fd_in, int fd_out); 28 29 /* Puts the connection file descriptors into non-blocking mode. */ 30 void packet_set_nonblocking(void); 31 32 /* Returns the file descriptor used for input. */ 33 int packet_get_connection_in(void); 34 35 /* Returns the file descriptor used for output. */ 36 int packet_get_connection_out(void); 37 38 /* 39 * Closes the connection (both descriptors) and clears and frees internal 40 * data structures. 41 */ 42 void packet_close(void); 43 44 /* 45 * Causes any further packets to be encrypted using the given key. The same 46 * key is used for both sending and reception. However, both directions are 47 * encrypted independently of each other. Cipher types are defined in ssh.h. 48 */ 49 void 50 packet_set_encryption_key(const u_char *key, u_int keylen, 51 int cipher_type); 52 53 /* 54 * Sets remote side protocol flags for the current connection. This can be 55 * called at any time. 56 */ 57 void packet_set_protocol_flags(u_int flags); 58 59 /* Returns the remote protocol flags set earlier by the above function. */ 60 u_int packet_get_protocol_flags(void); 61 62 /* Enables compression in both directions starting from the next packet. */ 63 void packet_start_compression(int level); 64 65 /* 66 * Informs that the current session is interactive. Sets IP flags for 67 * optimal performance in interactive use. 68 */ 69 void packet_set_interactive(int interactive); 70 71 /* Returns true if the current connection is interactive. */ 72 int packet_is_interactive(void); 73 74 /* Starts constructing a packet to send. */ 75 void packet_start(int type); 76 77 /* Appends a character to the packet data. */ 78 void packet_put_char(int ch); 79 80 /* Appends an integer to the packet data. */ 81 void packet_put_int(u_int value); 82 83 /* Appends an arbitrary precision integer to packet data. */ 84 void packet_put_bignum(BIGNUM * value); 85 void packet_put_bignum2(BIGNUM * value); 86 87 /* Appends a string to packet data. */ 88 void packet_put_string(const char *buf, u_int len); 89 void packet_put_cstring(const char *str); 90 void packet_put_raw(const char *buf, u_int len); 91 92 /* 93 * Finalizes and sends the packet. If the encryption key has been set, 94 * encrypts the packet before sending. 95 */ 96 void packet_send(void); 97 98 /* Waits until a packet has been received, and returns its type. */ 99 int packet_read(int *payload_len_ptr); 100 101 /* 102 * Waits until a packet has been received, verifies that its type matches 103 * that given, and gives a fatal error and exits if there is a mismatch. 104 */ 105 void packet_read_expect(int *payload_len_ptr, int type); 106 107 /* 108 * Checks if a full packet is available in the data received so far via 109 * packet_process_incoming. If so, reads the packet; otherwise returns 110 * SSH_MSG_NONE. This does not wait for data from the connection. 111 * SSH_MSG_DISCONNECT is handled specially here. Also, SSH_MSG_IGNORE 112 * messages are skipped by this function and are never returned to higher 113 * levels. 114 */ 115 int packet_read_poll(int *packet_len_ptr); 116 117 /* 118 * Buffers the given amount of input characters. This is intended to be used 119 * together with packet_read_poll. 120 */ 121 void packet_process_incoming(const char *buf, u_int len); 122 123 /* Returns a character (0-255) from the packet data. */ 124 u_int packet_get_char(void); 125 126 /* Returns an integer from the packet data. */ 127 u_int packet_get_int(void); 128 129 /* 130 * Returns an arbitrary precision integer from the packet data. The integer 131 * must have been initialized before this call. 132 */ 133 void packet_get_bignum(BIGNUM * value, int *length_ptr); 134 void packet_get_bignum2(BIGNUM * value, int *length_ptr); 135 char *packet_get_raw(int *length_ptr); 136 137 /* 138 * Returns a string from the packet data. The string is allocated using 139 * xmalloc; it is the responsibility of the calling program to free it when 140 * no longer needed. The length_ptr argument may be NULL, or point to an 141 * integer into which the length of the string is stored. 142 */ 143 char *packet_get_string(u_int *length_ptr); 144 145 /* 146 * Logs the error in syslog using LOG_INFO, constructs and sends a disconnect 147 * packet, closes the connection, and exits. This function never returns. 148 * The error message should not contain a newline. The total length of the 149 * message must not exceed 1024 bytes. 150 */ 151 void packet_disconnect(const char *fmt,...) __attribute__((format(printf, 1, 2))); 152 153 /* 154 * Sends a diagnostic message to the other side. This message can be sent at 155 * any time (but not while constructing another message). The message is 156 * printed immediately, but only if the client is being executed in verbose 157 * mode. These messages are primarily intended to ease debugging 158 * authentication problems. The total length of the message must not exceed 159 * 1024 bytes. This will automatically call packet_write_wait. If the 160 * remote side protocol flags do not indicate that it supports SSH_MSG_DEBUG, 161 * this will do nothing. 162 */ 163 void packet_send_debug(const char *fmt,...) __attribute__((format(printf, 1, 2))); 164 165 /* Checks if there is any buffered output, and tries to write some of the output. */ 166 void packet_write_poll(void); 167 168 /* Waits until all pending output data has been written. */ 169 void packet_write_wait(void); 170 171 /* Returns true if there is buffered data to write to the connection. */ 172 int packet_have_data_to_write(void); 173 174 /* Returns true if there is not too much data to write to the connection. */ 175 int packet_not_very_much_data_to_write(void); 176 177 /* maximum packet size, requested by client with SSH_CMSG_MAX_PACKET_SIZE */ 178 extern int max_packet_size; 179 int packet_set_maxsize(int s); 180 #define packet_get_maxsize() max_packet_size 181 182 /* Stores tty modes from the fd or tiop into current packet. */ 183 void tty_make_modes(int fd, struct termios *tiop); 184 185 /* Parses tty modes for the fd from the current packet. */ 186 void tty_parse_modes(int fd, int *n_bytes_ptr); 187 188 #define packet_integrity_check(payload_len, expected_len, type) \ 189 do { \ 190 int _p = (payload_len), _e = (expected_len); \ 191 if (_p != _e) { \ 192 log("Packet integrity error (%d != %d) at %s:%d", \ 193 _p, _e, __FILE__, __LINE__); \ 194 packet_disconnect("Packet integrity error. (%d)", (type)); \ 195 } \ 196 } while (0) 197 198 #define packet_done() \ 199 do { \ 200 int _len = packet_remaining(); \ 201 if (_len > 0) { \ 202 log("Packet integrity error (%d bytes remaining) at %s:%d", \ 203 _len ,__FILE__, __LINE__); \ 204 packet_disconnect("Packet integrity error."); \ 205 } \ 206 } while (0) 207 208 /* remote host is connected via a socket/ipv4 */ 209 int packet_connection_is_on_socket(void); 210 int packet_connection_is_ipv4(void); 211 212 /* enable SSH2 packet format */ 213 void packet_set_ssh2_format(void); 214 215 /* returns remaining payload bytes */ 216 int packet_remaining(void); 217 218 /* append an ignore message */ 219 void packet_send_ignore(int nbytes); 220 221 /* add an ignore message and make sure size (current+ignore) = n*sumlen */ 222 void packet_inject_ignore(int sumlen); 223 224 #endif /* PACKET_H */ 225