diff options
author | markus@openbsd.org <markus@openbsd.org> | 2015-01-19 19:52:16 +0000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2015-01-20 09:13:01 +1100 |
commit | 091c302829210c41e7f57c3f094c7b9c054306f0 (patch) | |
tree | 800de5dc85b877a85d1f269ae5bb09b0dc3fa7a7 /opacket.c | |
parent | 4e62cc68ce4ba20245d208b252e74e91d3785b74 (diff) |
upstream commit
update packet.c & isolate, introduce struct ssh a) switch
packet.c to buffer api and isolate per-connection info into struct ssh b)
(de)serialization of the state is moved from monitor to packet.c c) the old
packet.c API is implemented in opacket.[ch] d) compress.c/h is removed and
integrated into packet.c with and ok djm@
Diffstat (limited to 'opacket.c')
-rw-r--r-- | opacket.c | 279 |
1 files changed, 279 insertions, 0 deletions
diff --git a/opacket.c b/opacket.c new file mode 100644 index 000000000..63b419d5c --- /dev/null +++ b/opacket.c | |||
@@ -0,0 +1,279 @@ | |||
1 | /* 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 | |||
9 | struct ssh *active_state, *backup_state; | ||
10 | |||
11 | /* Map old to new API */ | ||
12 | |||
13 | void | ||
14 | ssh_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 | |||
22 | void | ||
23 | ssh_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 | |||
32 | void | ||
33 | ssh_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 | |||
41 | void | ||
42 | ssh_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 | |||
50 | void | ||
51 | ssh_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 | |||
59 | void | ||
60 | ssh_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 | |||
68 | void | ||
69 | ssh_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 | ||
78 | void | ||
79 | ssh_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 | |||
87 | void | ||
88 | ssh_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 | |||
96 | void | ||
97 | ssh_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 | |||
107 | void | ||
108 | ssh_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 | |||
116 | u_int | ||
117 | ssh_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 | |||
127 | u_int | ||
128 | ssh_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 | |||
138 | u_int64_t | ||
139 | ssh_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 | ||
150 | void | ||
151 | ssh_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 | |||
159 | void | ||
160 | ssh_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 | |||
168 | void | ||
169 | ssh_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 | |||
178 | void * | ||
179 | ssh_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 | |||
192 | const void * | ||
193 | ssh_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 | |||
206 | char * | ||
207 | ssh_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 | |||
222 | void | ||
223 | packet_set_connection(int fd_in, int fd_out) | ||
224 | { | ||
225 | active_state = ssh_packet_set_connection(active_state, fd_in, fd_out); | ||
226 | } | ||
227 | |||
228 | void | ||
229 | packet_backup_state(void) | ||
230 | { | ||
231 | ssh_packet_backup_state(active_state, backup_state); | ||
232 | } | ||
233 | |||
234 | void | ||
235 | packet_restore_state(void) | ||
236 | { | ||
237 | ssh_packet_restore_state(active_state, backup_state); | ||
238 | } | ||
239 | |||
240 | u_int | ||
241 | packet_get_char(void) | ||
242 | { | ||
243 | return (ssh_packet_get_char(active_state)); | ||
244 | } | ||
245 | |||
246 | u_int | ||
247 | packet_get_int(void) | ||
248 | { | ||
249 | return (ssh_packet_get_int(active_state)); | ||
250 | } | ||
251 | |||
252 | int | ||
253 | packet_read_seqnr(u_int32_t *seqnr) | ||
254 | { | ||
255 | u_char type; | ||
256 | int r; | ||
257 | |||
258 | if ((r = ssh_packet_read_seqnr(active_state, &type, seqnr))) | ||
259 | fatal("%s: %s", __func__, ssh_err(r)); | ||
260 | return type; | ||
261 | } | ||
262 | |||
263 | int | ||
264 | packet_read_poll_seqnr(u_int32_t *seqnr) | ||
265 | { | ||
266 | u_char type; | ||
267 | int r; | ||
268 | |||
269 | if ((r = ssh_packet_read_poll_seqnr(active_state, &type, seqnr))) | ||
270 | fatal("%s: %s", __func__, ssh_err(r)); | ||
271 | return type; | ||
272 | } | ||
273 | |||
274 | void | ||
275 | packet_close(void) | ||
276 | { | ||
277 | ssh_packet_close(active_state); | ||
278 | active_state = NULL; | ||
279 | } | ||