blob: ad1a5b0895a673945a4043475abc83f3b6c52803 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 *
3 * packet.h
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:02:14 1995 ylo
11 *
12 * Interface for the packet protocol functions.
13 *
14 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
Damien Miller95def091999-11-25 00:26:21 +110016/* RCSID("$Id: packet.h,v 1.6 1999/11/24 13:26:22 damien Exp $"); */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100017
18#ifndef PACKET_H
19#define PACKET_H
20
Damien Miller3bd49ec1999-11-15 15:40:55 +110021#include "config.h"
22
Damien Miller7f6ea021999-10-28 13:25:17 +100023#ifdef HAVE_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +100024#include <openssl/bn.h>
Damien Miller7f6ea021999-10-28 13:25:17 +100025#endif
26#ifdef HAVE_SSL
27#include <ssl/bn.h>
28#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100029
30/* Sets the socket used for communication. Disables encryption until
31 packet_set_encryption_key is called. It is permissible that fd_in
32 and fd_out are the same descriptor; in that case it is assumed to
33 be a socket. */
Damien Miller95def091999-11-25 00:26:21 +110034void packet_set_connection(int fd_in, int fd_out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035
36/* Puts the connection file descriptors into non-blocking mode. */
Damien Miller95def091999-11-25 00:26:21 +110037void packet_set_nonblocking(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
39/* Returns the file descriptor used for input. */
Damien Miller95def091999-11-25 00:26:21 +110040int packet_get_connection_in(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42/* Returns the file descriptor used for output. */
Damien Miller95def091999-11-25 00:26:21 +110043int packet_get_connection_out(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
45/* Closes the connection (both descriptors) and clears and frees
Damien Miller95def091999-11-25 00:26:21 +110046 internal data structures. */
47void packet_close(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048
49/* Causes any further packets to be encrypted using the given key. The same
50 key is used for both sending and reception. However, both directions
51 are encrypted independently of each other. Cipher types are
52 defined in ssh.h. */
Damien Miller95def091999-11-25 00:26:21 +110053void
54packet_set_encryption_key(const unsigned char *key, unsigned int keylen,
55 int cipher_type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056
57/* Sets remote side protocol flags for the current connection. This can
58 be called at any time. */
Damien Miller95def091999-11-25 00:26:21 +110059void packet_set_protocol_flags(unsigned int flags);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060
61/* Returns the remote protocol flags set earlier by the above function. */
62unsigned int packet_get_protocol_flags(void);
63
64/* Enables compression in both directions starting from the next packet. */
Damien Miller95def091999-11-25 00:26:21 +110065void packet_start_compression(int level);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066
67/* Informs that the current session is interactive. Sets IP flags for optimal
68 performance in interactive use. */
Damien Miller95def091999-11-25 00:26:21 +110069void packet_set_interactive(int interactive, int keepalives);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
71/* Returns true if the current connection is interactive. */
Damien Miller95def091999-11-25 00:26:21 +110072int packet_is_interactive(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073
74/* Starts constructing a packet to send. */
Damien Miller95def091999-11-25 00:26:21 +110075void packet_start(int type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076
77/* Appends a character to the packet data. */
Damien Miller95def091999-11-25 00:26:21 +110078void packet_put_char(int ch);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
80/* Appends an integer to the packet data. */
Damien Miller95def091999-11-25 00:26:21 +110081void packet_put_int(unsigned int value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
83/* Appends an arbitrary precision integer to packet data. */
Damien Miller95def091999-11-25 00:26:21 +110084void packet_put_bignum(BIGNUM * value);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
86/* Appends a string to packet data. */
Damien Miller95def091999-11-25 00:26:21 +110087void packet_put_string(const char *buf, unsigned int len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
89/* Finalizes and sends the packet. If the encryption key has been set,
90 encrypts the packet before sending. */
Damien Miller95def091999-11-25 00:26:21 +110091void packet_send(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
93/* Waits until a packet has been received, and returns its type. */
Damien Miller95def091999-11-25 00:26:21 +110094int packet_read(int *payload_len_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
96/* Waits until a packet has been received, verifies that its type matches
97 that given, and gives a fatal error and exits if there is a mismatch. */
Damien Miller95def091999-11-25 00:26:21 +110098void packet_read_expect(int *payload_len_ptr, int type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
100/* Checks if a full packet is available in the data received so far via
101 packet_process_incoming. If so, reads the packet; otherwise returns
Damien Miller95def091999-11-25 00:26:21 +1100102 SSH_MSG_NONE. This does not wait for data from the connection.
103
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104 SSH_MSG_DISCONNECT is handled specially here. Also,
105 SSH_MSG_IGNORE messages are skipped by this function and are never returned
106 to higher levels. */
Damien Miller95def091999-11-25 00:26:21 +1100107int packet_read_poll(int *packet_len_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108
109/* Buffers the given amount of input characters. This is intended to be
110 used together with packet_read_poll. */
Damien Miller95def091999-11-25 00:26:21 +1100111void packet_process_incoming(const char *buf, unsigned int len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112
113/* Returns a character (0-255) from the packet data. */
114unsigned int packet_get_char(void);
115
116/* Returns an integer from the packet data. */
117unsigned int packet_get_int(void);
118
119/* Returns an arbitrary precision integer from the packet data. The integer
120 must have been initialized before this call. */
Damien Miller95def091999-11-25 00:26:21 +1100121void packet_get_bignum(BIGNUM * value, int *length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
123/* Returns a string from the packet data. The string is allocated using
124 xmalloc; it is the responsibility of the calling program to free it when
125 no longer needed. The length_ptr argument may be NULL, or point to an
126 integer into which the length of the string is stored. */
Damien Miller95def091999-11-25 00:26:21 +1100127char *packet_get_string(unsigned int *length_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128
129/* Logs the error in syslog using LOG_INFO, constructs and sends a disconnect
130 packet, closes the connection, and exits. This function never returns.
131 The error message should not contain a newline. The total length of the
132 message must not exceed 1024 bytes. */
Damien Miller95def091999-11-25 00:26:21 +1100133void packet_disconnect(const char *fmt,...);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134
135/* Sends a diagnostic message to the other side. This message
136 can be sent at any time (but not while constructing another message).
137 The message is printed immediately, but only if the client is being
138 executed in verbose mode. These messages are primarily intended to
139 ease debugging authentication problems. The total length of the message
140 must not exceed 1024 bytes. This will automatically call
141 packet_write_wait. If the remote side protocol flags do not indicate
142 that it supports SSH_MSG_DEBUG, this will do nothing. */
Damien Miller95def091999-11-25 00:26:21 +1100143void packet_send_debug(const char *fmt,...);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
145/* Checks if there is any buffered output, and tries to write some of the
146 output. */
Damien Miller95def091999-11-25 00:26:21 +1100147void packet_write_poll(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148
149/* Waits until all pending output data has been written. */
Damien Miller95def091999-11-25 00:26:21 +1100150void packet_write_wait(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151
152/* Returns true if there is buffered data to write to the connection. */
Damien Miller95def091999-11-25 00:26:21 +1100153int packet_have_data_to_write(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154
155/* Returns true if there is not too much data to write to the connection. */
Damien Miller95def091999-11-25 00:26:21 +1100156int packet_not_very_much_data_to_write(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157
Damien Miller6162d121999-11-21 13:23:52 +1100158/* maximum packet size, requested by client with SSH_CMSG_MAX_PACKET_SIZE */
159extern int max_packet_size;
Damien Miller95def091999-11-25 00:26:21 +1100160int packet_set_maxsize(int s);
Damien Miller6162d121999-11-21 13:23:52 +1100161#define packet_get_maxsize() max_packet_size
162
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163/* Stores tty modes from the fd into current packet. */
Damien Miller95def091999-11-25 00:26:21 +1100164void tty_make_modes(int fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165
166/* Parses tty modes for the fd from the current packet. */
Damien Miller95def091999-11-25 00:26:21 +1100167void tty_parse_modes(int fd, int *n_bytes_ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168
169#define packet_integrity_check(payload_len, expected_len, type) \
170do { \
171 int _p = (payload_len), _e = (expected_len); \
172 if (_p != _e) { \
173 log("Packet integrity error (%d != %d) at %s:%d", \
174 _p, _e, __FILE__, __LINE__); \
175 packet_disconnect("Packet integrity error. (%d)", (type)); \
176 } \
177} while (0)
178
Damien Miller95def091999-11-25 00:26:21 +1100179#endif /* PACKET_H */