Annotation of src/usr.bin/ssh/packet.h, Revision 1.8
1.1 deraadt 1: /*
1.5 deraadt 2: *
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: */
1.1 deraadt 15:
1.8 ! markus 16: /* RCSID("$Id: packet.h,v 1.7 1999/12/27 10:46:11 markus Exp $"); */
1.1 deraadt 17:
18: #ifndef PACKET_H
19: #define PACKET_H
20:
1.2 provos 21: #include <ssl/bn.h>
1.1 deraadt 22:
1.6 markus 23: /*
24: * Sets the socket used for communication. Disables encryption until
25: * packet_set_encryption_key is called. It is permissible that fd_in and
26: * fd_out are the same descriptor; in that case it is assumed to be a socket.
27: */
1.5 deraadt 28: void packet_set_connection(int fd_in, int fd_out);
1.1 deraadt 29:
30: /* Puts the connection file descriptors into non-blocking mode. */
1.5 deraadt 31: void packet_set_nonblocking(void);
1.1 deraadt 32:
33: /* Returns the file descriptor used for input. */
1.5 deraadt 34: int packet_get_connection_in(void);
1.1 deraadt 35:
36: /* Returns the file descriptor used for output. */
1.5 deraadt 37: int packet_get_connection_out(void);
1.1 deraadt 38:
1.6 markus 39: /*
40: * Closes the connection (both descriptors) and clears and frees internal
41: * data structures.
42: */
1.5 deraadt 43: void packet_close(void);
1.1 deraadt 44:
1.6 markus 45: /*
46: * Causes any further packets to be encrypted using the given key. The same
47: * key is used for both sending and reception. However, both directions are
48: * encrypted independently of each other. Cipher types are defined in ssh.h.
49: */
1.5 deraadt 50: void
51: packet_set_encryption_key(const unsigned char *key, unsigned int keylen,
52: int cipher_type);
1.1 deraadt 53:
1.6 markus 54: /*
55: * Sets remote side protocol flags for the current connection. This can be
56: * called at any time.
57: */
1.5 deraadt 58: void packet_set_protocol_flags(unsigned int flags);
1.1 deraadt 59:
60: /* Returns the remote protocol flags set earlier by the above function. */
61: unsigned int packet_get_protocol_flags(void);
62:
63: /* Enables compression in both directions starting from the next packet. */
1.5 deraadt 64: void packet_start_compression(int level);
1.1 deraadt 65:
1.6 markus 66: /*
67: * Informs that the current session is interactive. Sets IP flags for
68: * optimal performance in interactive use.
69: */
1.5 deraadt 70: void packet_set_interactive(int interactive, int keepalives);
1.1 deraadt 71:
72: /* Returns true if the current connection is interactive. */
1.5 deraadt 73: int packet_is_interactive(void);
1.1 deraadt 74:
75: /* Starts constructing a packet to send. */
1.5 deraadt 76: void packet_start(int type);
1.1 deraadt 77:
78: /* Appends a character to the packet data. */
1.5 deraadt 79: void packet_put_char(int ch);
1.1 deraadt 80:
81: /* Appends an integer to the packet data. */
1.5 deraadt 82: void packet_put_int(unsigned int value);
1.1 deraadt 83:
84: /* Appends an arbitrary precision integer to packet data. */
1.5 deraadt 85: void packet_put_bignum(BIGNUM * value);
1.1 deraadt 86:
87: /* Appends a string to packet data. */
1.5 deraadt 88: void packet_put_string(const char *buf, unsigned int len);
1.1 deraadt 89:
1.6 markus 90: /*
91: * Finalizes and sends the packet. If the encryption key has been set,
92: * encrypts the packet before sending.
93: */
1.5 deraadt 94: void packet_send(void);
1.1 deraadt 95:
96: /* Waits until a packet has been received, and returns its type. */
1.5 deraadt 97: int packet_read(int *payload_len_ptr);
1.1 deraadt 98:
1.6 markus 99: /*
100: * Waits until a packet has been received, verifies that its type matches
101: * that given, and gives a fatal error and exits if there is a mismatch.
102: */
1.5 deraadt 103: void packet_read_expect(int *payload_len_ptr, int type);
1.1 deraadt 104:
1.6 markus 105: /*
106: * Checks if a full packet is available in the data received so far via
107: * packet_process_incoming. If so, reads the packet; otherwise returns
108: * SSH_MSG_NONE. This does not wait for data from the connection.
109: * SSH_MSG_DISCONNECT is handled specially here. Also, SSH_MSG_IGNORE
110: * messages are skipped by this function and are never returned to higher
111: * levels.
112: */
1.5 deraadt 113: int packet_read_poll(int *packet_len_ptr);
1.1 deraadt 114:
1.6 markus 115: /*
116: * Buffers the given amount of input characters. This is intended to be used
117: * together with packet_read_poll.
118: */
1.5 deraadt 119: void packet_process_incoming(const char *buf, unsigned int len);
1.1 deraadt 120:
121: /* Returns a character (0-255) from the packet data. */
122: unsigned int packet_get_char(void);
123:
124: /* Returns an integer from the packet data. */
125: unsigned int packet_get_int(void);
126:
1.6 markus 127: /*
128: * Returns an arbitrary precision integer from the packet data. The integer
129: * must have been initialized before this call.
130: */
1.5 deraadt 131: void packet_get_bignum(BIGNUM * value, int *length_ptr);
1.1 deraadt 132:
1.6 markus 133: /*
134: * Returns a string from the packet data. The string is allocated using
135: * xmalloc; it is the responsibility of the calling program to free it when
136: * no longer needed. The length_ptr argument may be NULL, or point to an
137: * integer into which the length of the string is stored.
138: */
1.5 deraadt 139: char *packet_get_string(unsigned int *length_ptr);
1.1 deraadt 140:
1.6 markus 141: /*
142: * Logs the error in syslog using LOG_INFO, constructs and sends a disconnect
143: * packet, closes the connection, and exits. This function never returns.
144: * The error message should not contain a newline. The total length of the
145: * message must not exceed 1024 bytes.
146: */
1.7 markus 147: void packet_disconnect(const char *fmt,...) __attribute__((format(printf, 1, 2)));;
1.1 deraadt 148:
1.6 markus 149: /*
150: * Sends a diagnostic message to the other side. This message can be sent at
151: * any time (but not while constructing another message). The message is
152: * printed immediately, but only if the client is being executed in verbose
153: * mode. These messages are primarily intended to ease debugging
154: * authentication problems. The total length of the message must not exceed
155: * 1024 bytes. This will automatically call packet_write_wait. If the
156: * remote side protocol flags do not indicate that it supports SSH_MSG_DEBUG,
157: * this will do nothing.
158: */
1.7 markus 159: void packet_send_debug(const char *fmt,...) __attribute__((format(printf, 1, 2)));;
1.1 deraadt 160:
1.6 markus 161: /* Checks if there is any buffered output, and tries to write some of the output. */
1.5 deraadt 162: void packet_write_poll(void);
1.1 deraadt 163:
164: /* Waits until all pending output data has been written. */
1.5 deraadt 165: void packet_write_wait(void);
1.1 deraadt 166:
167: /* Returns true if there is buffered data to write to the connection. */
1.5 deraadt 168: int packet_have_data_to_write(void);
1.1 deraadt 169:
170: /* Returns true if there is not too much data to write to the connection. */
1.5 deraadt 171: int packet_not_very_much_data_to_write(void);
1.4 markus 172:
173: /* maximum packet size, requested by client with SSH_CMSG_MAX_PACKET_SIZE */
174: extern int max_packet_size;
1.5 deraadt 175: int packet_set_maxsize(int s);
1.4 markus 176: #define packet_get_maxsize() max_packet_size
1.1 deraadt 177:
178: /* Stores tty modes from the fd into current packet. */
1.5 deraadt 179: void tty_make_modes(int fd);
1.1 deraadt 180:
181: /* Parses tty modes for the fd from the current packet. */
1.5 deraadt 182: void tty_parse_modes(int fd, int *n_bytes_ptr);
1.1 deraadt 183:
184: #define packet_integrity_check(payload_len, expected_len, type) \
185: do { \
186: int _p = (payload_len), _e = (expected_len); \
187: if (_p != _e) { \
188: log("Packet integrity error (%d != %d) at %s:%d", \
189: _p, _e, __FILE__, __LINE__); \
190: packet_disconnect("Packet integrity error. (%d)", (type)); \
191: } \
192: } while (0)
1.8 ! markus 193:
! 194: /* remote host is connected via a socket */
! 195: int packet_connection_is_on_socket(void);
1.1 deraadt 196:
1.5 deraadt 197: #endif /* PACKET_H */