blob: ad244b452634badd69ad7ffd4e8f7f6400dde123 [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
markus@openbsd.org091c3022015-01-19 19:52:16 +000077
Damien Millerbbffb232015-03-03 13:50:27 -080078#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +000079void
80ssh_packet_put_bignum2(struct ssh *ssh, BIGNUM * value)
81{
82 int r;
83
84 if ((r = sshpkt_put_bignum2(ssh, value)) != 0)
85 fatal("%s: %s", __func__, ssh_err(r));
86}
87
Darren Tuckerf2004cd2015-02-23 05:04:21 +110088# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +000089void
90ssh_packet_put_ecpoint(struct ssh *ssh, const EC_GROUP *curve,
91 const EC_POINT *point)
92{
93 int r;
94
95 if ((r = sshpkt_put_ec(ssh, point, curve)) != 0)
96 fatal("%s: %s", __func__, ssh_err(r));
97}
Darren Tuckerf2004cd2015-02-23 05:04:21 +110098# endif
markus@openbsd.org091c3022015-01-19 19:52:16 +000099#endif /* WITH_OPENSSL */
100
101void
102ssh_packet_send(struct ssh *ssh)
103{
104 int r;
105
106 if ((r = sshpkt_send(ssh)) != 0)
107 fatal("%s: %s", __func__, ssh_err(r));
108}
109
110u_int
111ssh_packet_get_char(struct ssh *ssh)
112{
113 u_char ch;
114 int r;
115
116 if ((r = sshpkt_get_u8(ssh, &ch)) != 0)
117 fatal("%s: %s", __func__, ssh_err(r));
118 return ch;
119}
120
121u_int
122ssh_packet_get_int(struct ssh *ssh)
123{
124 u_int val;
125 int r;
126
127 if ((r = sshpkt_get_u32(ssh, &val)) != 0)
128 fatal("%s: %s", __func__, ssh_err(r));
129 return val;
130}
131
132u_int64_t
133ssh_packet_get_int64(struct ssh *ssh)
134{
135 u_int64_t val;
136 int r;
137
138 if ((r = sshpkt_get_u64(ssh, &val)) != 0)
139 fatal("%s: %s", __func__, ssh_err(r));
140 return val;
141}
142
markus@openbsd.org091c3022015-01-19 19:52:16 +0000143
Damien Millerbbffb232015-03-03 13:50:27 -0800144#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +0000145void
146ssh_packet_get_bignum2(struct ssh *ssh, BIGNUM * value)
147{
148 int r;
149
150 if ((r = sshpkt_get_bignum2(ssh, value)) != 0)
151 fatal("%s: %s", __func__, ssh_err(r));
152}
153
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100154# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +0000155void
156ssh_packet_get_ecpoint(struct ssh *ssh, const EC_GROUP *curve, EC_POINT *point)
157{
158 int r;
159
160 if ((r = sshpkt_get_ec(ssh, point, curve)) != 0)
161 fatal("%s: %s", __func__, ssh_err(r));
162}
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100163# endif
markus@openbsd.org091c3022015-01-19 19:52:16 +0000164#endif /* WITH_OPENSSL */
165
166void *
167ssh_packet_get_string(struct ssh *ssh, u_int *length_ptr)
168{
169 int r;
170 size_t len;
171 u_char *val;
172
173 if ((r = sshpkt_get_string(ssh, &val, &len)) != 0)
174 fatal("%s: %s", __func__, ssh_err(r));
175 if (length_ptr != NULL)
176 *length_ptr = (u_int)len;
177 return val;
178}
179
180const void *
181ssh_packet_get_string_ptr(struct ssh *ssh, u_int *length_ptr)
182{
183 int r;
184 size_t len;
185 const u_char *val;
186
187 if ((r = sshpkt_get_string_direct(ssh, &val, &len)) != 0)
188 fatal("%s: %s", __func__, ssh_err(r));
189 if (length_ptr != NULL)
190 *length_ptr = (u_int)len;
191 return val;
192}
193
194char *
195ssh_packet_get_cstring(struct ssh *ssh, u_int *length_ptr)
196{
197 int r;
198 size_t len;
199 char *val;
200
201 if ((r = sshpkt_get_cstring(ssh, &val, &len)) != 0)
202 fatal("%s: %s", __func__, ssh_err(r));
203 if (length_ptr != NULL)
204 *length_ptr = (u_int)len;
205 return val;
206}
207
208/* Old API, that had to be reimplemented */
209
210void
211packet_set_connection(int fd_in, int fd_out)
212{
213 active_state = ssh_packet_set_connection(active_state, fd_in, fd_out);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000214 if (active_state == NULL)
215 fatal("%s: ssh_packet_set_connection failed", __func__);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000216}
217
markus@openbsd.org091c3022015-01-19 19:52:16 +0000218u_int
219packet_get_char(void)
220{
221 return (ssh_packet_get_char(active_state));
222}
223
224u_int
225packet_get_int(void)
226{
227 return (ssh_packet_get_int(active_state));
228}
229
230int
231packet_read_seqnr(u_int32_t *seqnr)
232{
233 u_char type;
234 int r;
235
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000236 if ((r = ssh_packet_read_seqnr(active_state, &type, seqnr)) != 0)
237 sshpkt_fatal(active_state, __func__, r);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000238 return type;
239}
240
241int
242packet_read_poll_seqnr(u_int32_t *seqnr)
243{
244 u_char type;
245 int r;
246
247 if ((r = ssh_packet_read_poll_seqnr(active_state, &type, seqnr)))
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000248 sshpkt_fatal(active_state, __func__, r);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000249 return type;
250}
251
252void
253packet_close(void)
254{
255 ssh_packet_close(active_state);
256 active_state = NULL;
257}
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +0000258
259void
260packet_process_incoming(const char *buf, u_int len)
261{
262 int r;
263
264 if ((r = ssh_packet_process_incoming(active_state, buf, len)) != 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000265 sshpkt_fatal(active_state, __func__, r);
266}
267
268void
269packet_write_wait(void)
270{
271 int r;
272
273 if ((r = ssh_packet_write_wait(active_state)) != 0)
274 sshpkt_fatal(active_state, __func__, r);
275}
276
277void
278packet_write_poll(void)
279{
280 int r;
281
282 if ((r = ssh_packet_write_poll(active_state)) != 0)
283 sshpkt_fatal(active_state, __func__, r);
284}
285
286void
287packet_read_expect(int expected_type)
288{
289 int r;
290
291 if ((r = ssh_packet_read_expect(active_state, expected_type)) != 0)
292 sshpkt_fatal(active_state, __func__, r);
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +0000293}
Darren Tuckerdcc89972015-02-24 12:30:59 +1100294
295void
296packet_disconnect(const char *fmt, ...)
297{
298 char buf[1024];
299 va_list args;
300
301 va_start(args, fmt);
302 vsnprintf(buf, sizeof(buf), fmt, args);
303 va_end(args);
304 ssh_packet_disconnect(active_state, "%s", buf);
305}
306
307void
308packet_send_debug(const char *fmt, ...)
309{
310 char buf[1024];
311 va_list args;
312
313 va_start(args, fmt);
314 vsnprintf(buf, sizeof(buf), fmt, args);
315 va_end(args);
316 ssh_packet_send_debug(active_state, "%s", buf);
317}