blob: a137b5a8a62bc8b6174645009af45d5380ffc4dd [file] [log] [blame]
markus@openbsd.org091c3022015-01-19 19:52:16 +00001/* Written by Markus Friedl. Placed in the public domain. */
2
3#include "includes.h"
4
5#include "ssherr.h"
6#include "packet.h"
7#include "log.h"
8
9struct ssh *active_state, *backup_state;
10
11/* Map old to new API */
12
13void
14ssh_packet_start(struct ssh *ssh, u_char type)
15{
16 int r;
17
18 if ((r = sshpkt_start(ssh, type)) != 0)
19 fatal("%s: %s", __func__, ssh_err(r));
20}
21
22void
23ssh_packet_put_char(struct ssh *ssh, int value)
24{
25 u_char ch = value;
26 int r;
27
28 if ((r = sshpkt_put_u8(ssh, ch)) != 0)
29 fatal("%s: %s", __func__, ssh_err(r));
30}
31
32void
33ssh_packet_put_int(struct ssh *ssh, u_int value)
34{
35 int r;
36
37 if ((r = sshpkt_put_u32(ssh, value)) != 0)
38 fatal("%s: %s", __func__, ssh_err(r));
39}
40
41void
42ssh_packet_put_int64(struct ssh *ssh, u_int64_t value)
43{
44 int r;
45
46 if ((r = sshpkt_put_u64(ssh, value)) != 0)
47 fatal("%s: %s", __func__, ssh_err(r));
48}
49
50void
51ssh_packet_put_string(struct ssh *ssh, const void *buf, u_int len)
52{
53 int r;
54
55 if ((r = sshpkt_put_string(ssh, buf, len)) != 0)
56 fatal("%s: %s", __func__, ssh_err(r));
57}
58
59void
60ssh_packet_put_cstring(struct ssh *ssh, const char *str)
61{
62 int r;
63
64 if ((r = sshpkt_put_cstring(ssh, str)) != 0)
65 fatal("%s: %s", __func__, ssh_err(r));
66}
67
68void
69ssh_packet_put_raw(struct ssh *ssh, const void *buf, u_int len)
70{
71 int r;
72
73 if ((r = sshpkt_put(ssh, buf, len)) != 0)
74 fatal("%s: %s", __func__, ssh_err(r));
75}
76
77#ifdef WITH_OPENSSL
78void
79ssh_packet_put_bignum(struct ssh *ssh, BIGNUM * value)
80{
81 int r;
82
83 if ((r = sshpkt_put_bignum1(ssh, value)) != 0)
84 fatal("%s: %s", __func__, ssh_err(r));
85}
86
87void
88ssh_packet_put_bignum2(struct ssh *ssh, BIGNUM * value)
89{
90 int r;
91
92 if ((r = sshpkt_put_bignum2(ssh, value)) != 0)
93 fatal("%s: %s", __func__, ssh_err(r));
94}
95
96void
97ssh_packet_put_ecpoint(struct ssh *ssh, const EC_GROUP *curve,
98 const EC_POINT *point)
99{
100 int r;
101
102 if ((r = sshpkt_put_ec(ssh, point, curve)) != 0)
103 fatal("%s: %s", __func__, ssh_err(r));
104}
105#endif /* WITH_OPENSSL */
106
107void
108ssh_packet_send(struct ssh *ssh)
109{
110 int r;
111
112 if ((r = sshpkt_send(ssh)) != 0)
113 fatal("%s: %s", __func__, ssh_err(r));
114}
115
116u_int
117ssh_packet_get_char(struct ssh *ssh)
118{
119 u_char ch;
120 int r;
121
122 if ((r = sshpkt_get_u8(ssh, &ch)) != 0)
123 fatal("%s: %s", __func__, ssh_err(r));
124 return ch;
125}
126
127u_int
128ssh_packet_get_int(struct ssh *ssh)
129{
130 u_int val;
131 int r;
132
133 if ((r = sshpkt_get_u32(ssh, &val)) != 0)
134 fatal("%s: %s", __func__, ssh_err(r));
135 return val;
136}
137
138u_int64_t
139ssh_packet_get_int64(struct ssh *ssh)
140{
141 u_int64_t val;
142 int r;
143
144 if ((r = sshpkt_get_u64(ssh, &val)) != 0)
145 fatal("%s: %s", __func__, ssh_err(r));
146 return val;
147}
148
149#ifdef WITH_OPENSSL
150void
151ssh_packet_get_bignum(struct ssh *ssh, BIGNUM * value)
152{
153 int r;
154
155 if ((r = sshpkt_get_bignum1(ssh, value)) != 0)
156 fatal("%s: %s", __func__, ssh_err(r));
157}
158
159void
160ssh_packet_get_bignum2(struct ssh *ssh, BIGNUM * value)
161{
162 int r;
163
164 if ((r = sshpkt_get_bignum2(ssh, value)) != 0)
165 fatal("%s: %s", __func__, ssh_err(r));
166}
167
168void
169ssh_packet_get_ecpoint(struct ssh *ssh, const EC_GROUP *curve, EC_POINT *point)
170{
171 int r;
172
173 if ((r = sshpkt_get_ec(ssh, point, curve)) != 0)
174 fatal("%s: %s", __func__, ssh_err(r));
175}
176#endif /* WITH_OPENSSL */
177
178void *
179ssh_packet_get_string(struct ssh *ssh, u_int *length_ptr)
180{
181 int r;
182 size_t len;
183 u_char *val;
184
185 if ((r = sshpkt_get_string(ssh, &val, &len)) != 0)
186 fatal("%s: %s", __func__, ssh_err(r));
187 if (length_ptr != NULL)
188 *length_ptr = (u_int)len;
189 return val;
190}
191
192const void *
193ssh_packet_get_string_ptr(struct ssh *ssh, u_int *length_ptr)
194{
195 int r;
196 size_t len;
197 const u_char *val;
198
199 if ((r = sshpkt_get_string_direct(ssh, &val, &len)) != 0)
200 fatal("%s: %s", __func__, ssh_err(r));
201 if (length_ptr != NULL)
202 *length_ptr = (u_int)len;
203 return val;
204}
205
206char *
207ssh_packet_get_cstring(struct ssh *ssh, u_int *length_ptr)
208{
209 int r;
210 size_t len;
211 char *val;
212
213 if ((r = sshpkt_get_cstring(ssh, &val, &len)) != 0)
214 fatal("%s: %s", __func__, ssh_err(r));
215 if (length_ptr != NULL)
216 *length_ptr = (u_int)len;
217 return val;
218}
219
220/* Old API, that had to be reimplemented */
221
222void
223packet_set_connection(int fd_in, int fd_out)
224{
225 active_state = ssh_packet_set_connection(active_state, fd_in, fd_out);
226}
227
228void
229packet_backup_state(void)
230{
231 ssh_packet_backup_state(active_state, backup_state);
232}
233
234void
235packet_restore_state(void)
236{
237 ssh_packet_restore_state(active_state, backup_state);
238}
239
240u_int
241packet_get_char(void)
242{
243 return (ssh_packet_get_char(active_state));
244}
245
246u_int
247packet_get_int(void)
248{
249 return (ssh_packet_get_int(active_state));
250}
251
252int
253packet_read_seqnr(u_int32_t *seqnr)
254{
255 u_char type;
256 int r;
257
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +0000258 if ((r = ssh_packet_read_seqnr(active_state, &type, seqnr)) != 0) {
259 switch (r) {
260 case SSH_ERR_CONN_CLOSED:
261 logit("Connection closed by %.200s",
262 ssh_remote_ipaddr(active_state));
263 cleanup_exit(255);
264 case SSH_ERR_CONN_TIMEOUT:
265 logit("Connection to %.200s timed out while "
266 "waiting to read", ssh_remote_ipaddr(active_state));
267 cleanup_exit(255);
268 default:
269 fatal("%s: %s", __func__, ssh_err(r));
270 }
271 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000272 return type;
273}
274
275int
276packet_read_poll_seqnr(u_int32_t *seqnr)
277{
278 u_char type;
279 int r;
280
281 if ((r = ssh_packet_read_poll_seqnr(active_state, &type, seqnr)))
282 fatal("%s: %s", __func__, ssh_err(r));
283 return type;
284}
285
286void
287packet_close(void)
288{
289 ssh_packet_close(active_state);
290 active_state = NULL;
291}
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +0000292
293void
294packet_process_incoming(const char *buf, u_int len)
295{
296 int r;
297
298 if ((r = ssh_packet_process_incoming(active_state, buf, len)) != 0)
299 fatal("%s: %s", __func__, ssh_err(r));
300}