buffer.c revision 1.2.2.2 1 1.2.2.2 pgoyette /* $NetBSD: buffer.c,v 1.2.2.2 2018/04/16 01:59:48 pgoyette Exp $ */
2 1.2.2.2 pgoyette
3 1.2.2.2 pgoyette /* buffer.c
4 1.2.2.2 pgoyette
5 1.2.2.2 pgoyette Buffer access functions for the object management protocol... */
6 1.2.2.2 pgoyette
7 1.2.2.2 pgoyette /*
8 1.2.2.2 pgoyette * Copyright (c) 2004-2017 by Internet Systems Consortium, Inc. ("ISC")
9 1.2.2.2 pgoyette * Copyright (c) 1999-2003 by Internet Software Consortium
10 1.2.2.2 pgoyette *
11 1.2.2.2 pgoyette * This Source Code Form is subject to the terms of the Mozilla Public
12 1.2.2.2 pgoyette * License, v. 2.0. If a copy of the MPL was not distributed with this
13 1.2.2.2 pgoyette * file, You can obtain one at http://mozilla.org/MPL/2.0/.
14 1.2.2.2 pgoyette *
15 1.2.2.2 pgoyette * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16 1.2.2.2 pgoyette * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 1.2.2.2 pgoyette * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
18 1.2.2.2 pgoyette * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 1.2.2.2 pgoyette * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 1.2.2.2 pgoyette * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 1.2.2.2 pgoyette * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 1.2.2.2 pgoyette *
23 1.2.2.2 pgoyette * Internet Systems Consortium, Inc.
24 1.2.2.2 pgoyette * 950 Charter Street
25 1.2.2.2 pgoyette * Redwood City, CA 94063
26 1.2.2.2 pgoyette * <info (at) isc.org>
27 1.2.2.2 pgoyette * https://www.isc.org/
28 1.2.2.2 pgoyette *
29 1.2.2.2 pgoyette */
30 1.2.2.2 pgoyette
31 1.2.2.2 pgoyette #include <sys/cdefs.h>
32 1.2.2.2 pgoyette __RCSID("$NetBSD: buffer.c,v 1.2.2.2 2018/04/16 01:59:48 pgoyette Exp $");
33 1.2.2.2 pgoyette
34 1.2.2.2 pgoyette #include "dhcpd.h"
35 1.2.2.2 pgoyette
36 1.2.2.2 pgoyette #include <omapip/omapip_p.h>
37 1.2.2.2 pgoyette #include <errno.h>
38 1.2.2.2 pgoyette
39 1.2.2.2 pgoyette #if defined (TRACING)
40 1.2.2.2 pgoyette static void trace_connection_input_input (trace_type_t *, unsigned, char *);
41 1.2.2.2 pgoyette static void trace_connection_input_stop (trace_type_t *);
42 1.2.2.2 pgoyette static void trace_connection_output_input (trace_type_t *, unsigned, char *);
43 1.2.2.2 pgoyette static void trace_connection_output_stop (trace_type_t *);
44 1.2.2.2 pgoyette static trace_type_t *trace_connection_input;
45 1.2.2.2 pgoyette static trace_type_t *trace_connection_output;
46 1.2.2.2 pgoyette static isc_result_t omapi_connection_reader_trace (omapi_object_t *,
47 1.2.2.2 pgoyette unsigned, char *,
48 1.2.2.2 pgoyette unsigned *);
49 1.2.2.2 pgoyette extern omapi_array_t *omapi_connections;
50 1.2.2.2 pgoyette
51 1.2.2.2 pgoyette void omapi_buffer_trace_setup ()
52 1.2.2.2 pgoyette {
53 1.2.2.2 pgoyette trace_connection_input =
54 1.2.2.2 pgoyette trace_type_register ("connection-input",
55 1.2.2.2 pgoyette (void *)0,
56 1.2.2.2 pgoyette trace_connection_input_input,
57 1.2.2.2 pgoyette trace_connection_input_stop, MDL);
58 1.2.2.2 pgoyette trace_connection_output =
59 1.2.2.2 pgoyette trace_type_register ("connection-output",
60 1.2.2.2 pgoyette (void *)0,
61 1.2.2.2 pgoyette trace_connection_output_input,
62 1.2.2.2 pgoyette trace_connection_output_stop, MDL);
63 1.2.2.2 pgoyette }
64 1.2.2.2 pgoyette
65 1.2.2.2 pgoyette static void trace_connection_input_input (trace_type_t *ttype,
66 1.2.2.2 pgoyette unsigned length, char *buf)
67 1.2.2.2 pgoyette {
68 1.2.2.2 pgoyette unsigned left, taken, cc = 0;
69 1.2.2.2 pgoyette char *s;
70 1.2.2.2 pgoyette int32_t connect_index;
71 1.2.2.2 pgoyette isc_result_t status;
72 1.2.2.2 pgoyette omapi_connection_object_t *c = (omapi_connection_object_t *)0;
73 1.2.2.2 pgoyette
74 1.2.2.2 pgoyette memcpy (&connect_index, buf, sizeof connect_index);
75 1.2.2.2 pgoyette connect_index = ntohl (connect_index);
76 1.2.2.2 pgoyette
77 1.2.2.2 pgoyette omapi_array_foreach_begin (omapi_connections,
78 1.2.2.2 pgoyette omapi_connection_object_t, lp) {
79 1.2.2.2 pgoyette if (lp -> index == ntohl (connect_index)) {
80 1.2.2.2 pgoyette omapi_connection_reference (&c, lp, MDL);
81 1.2.2.2 pgoyette omapi_connection_dereference (&lp, MDL);
82 1.2.2.2 pgoyette break;
83 1.2.2.2 pgoyette }
84 1.2.2.2 pgoyette } omapi_array_foreach_end (omapi_connections,
85 1.2.2.2 pgoyette omapi_connection_object_t, lp);
86 1.2.2.2 pgoyette
87 1.2.2.2 pgoyette if (!c) {
88 1.2.2.2 pgoyette log_error ("trace connection input: no connection index %ld",
89 1.2.2.2 pgoyette (long int)connect_index);
90 1.2.2.2 pgoyette return;
91 1.2.2.2 pgoyette }
92 1.2.2.2 pgoyette
93 1.2.2.2 pgoyette s = buf + sizeof connect_index;
94 1.2.2.2 pgoyette left = length - sizeof connect_index;
95 1.2.2.2 pgoyette
96 1.2.2.2 pgoyette while (left) {
97 1.2.2.2 pgoyette taken = 0;
98 1.2.2.2 pgoyette status = omapi_connection_reader_trace ((omapi_object_t *)c,
99 1.2.2.2 pgoyette left, s, &taken);
100 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS) {
101 1.2.2.2 pgoyette log_error ("trace connection input: %s",
102 1.2.2.2 pgoyette isc_result_totext (status));
103 1.2.2.2 pgoyette break;
104 1.2.2.2 pgoyette }
105 1.2.2.2 pgoyette if (!taken) {
106 1.2.2.2 pgoyette if (cc > 0) {
107 1.2.2.2 pgoyette log_error ("trace connection_input: %s",
108 1.2.2.2 pgoyette "input is not being consumed.");
109 1.2.2.2 pgoyette break;
110 1.2.2.2 pgoyette }
111 1.2.2.2 pgoyette cc++;
112 1.2.2.2 pgoyette } else {
113 1.2.2.2 pgoyette cc = 0;
114 1.2.2.2 pgoyette left -= taken;
115 1.2.2.2 pgoyette }
116 1.2.2.2 pgoyette }
117 1.2.2.2 pgoyette omapi_connection_dereference (&c, MDL);
118 1.2.2.2 pgoyette }
119 1.2.2.2 pgoyette
120 1.2.2.2 pgoyette static void trace_connection_input_stop (trace_type_t *ttype) { }
121 1.2.2.2 pgoyette
122 1.2.2.2 pgoyette static void trace_connection_output_input (trace_type_t *ttype,
123 1.2.2.2 pgoyette unsigned length, char *buf)
124 1.2.2.2 pgoyette {
125 1.2.2.2 pgoyette /* We *could* check to see if the output is correct, but for now
126 1.2.2.2 pgoyette we aren't going to do that. */
127 1.2.2.2 pgoyette }
128 1.2.2.2 pgoyette
129 1.2.2.2 pgoyette static void trace_connection_output_stop (trace_type_t *ttype) { }
130 1.2.2.2 pgoyette
131 1.2.2.2 pgoyette #endif
132 1.2.2.2 pgoyette
133 1.2.2.2 pgoyette /* Make sure that at least len bytes are in the input buffer, and if not,
134 1.2.2.2 pgoyette read enough bytes to make up the difference. */
135 1.2.2.2 pgoyette
136 1.2.2.2 pgoyette isc_result_t omapi_connection_reader (omapi_object_t *h)
137 1.2.2.2 pgoyette {
138 1.2.2.2 pgoyette #if defined (TRACING)
139 1.2.2.2 pgoyette return omapi_connection_reader_trace (h, 0, (char *)0, (unsigned *)0);
140 1.2.2.2 pgoyette }
141 1.2.2.2 pgoyette
142 1.2.2.2 pgoyette static isc_result_t omapi_connection_reader_trace (omapi_object_t *h,
143 1.2.2.2 pgoyette unsigned stuff_len,
144 1.2.2.2 pgoyette char *stuff_buf,
145 1.2.2.2 pgoyette unsigned *stuff_taken)
146 1.2.2.2 pgoyette {
147 1.2.2.2 pgoyette #endif
148 1.2.2.2 pgoyette omapi_buffer_t *buffer;
149 1.2.2.2 pgoyette isc_result_t status;
150 1.2.2.2 pgoyette unsigned read_len;
151 1.2.2.2 pgoyette int read_status;
152 1.2.2.2 pgoyette omapi_connection_object_t *c;
153 1.2.2.2 pgoyette unsigned bytes_to_read;
154 1.2.2.2 pgoyette
155 1.2.2.2 pgoyette if (!h || h -> type != omapi_type_connection)
156 1.2.2.2 pgoyette return DHCP_R_INVALIDARG;
157 1.2.2.2 pgoyette c = (omapi_connection_object_t *)h;
158 1.2.2.2 pgoyette
159 1.2.2.2 pgoyette /* See if there are enough bytes. */
160 1.2.2.2 pgoyette if (c -> in_bytes >= OMAPI_BUF_SIZE - 1 &&
161 1.2.2.2 pgoyette c -> in_bytes > c -> bytes_needed)
162 1.2.2.2 pgoyette return ISC_R_SUCCESS;
163 1.2.2.2 pgoyette
164 1.2.2.2 pgoyette
165 1.2.2.2 pgoyette if (c -> inbufs) {
166 1.2.2.2 pgoyette for (buffer = c -> inbufs; buffer -> next;
167 1.2.2.2 pgoyette buffer = buffer -> next)
168 1.2.2.2 pgoyette ;
169 1.2.2.2 pgoyette if (!BUFFER_BYTES_FREE (buffer)) {
170 1.2.2.2 pgoyette status = omapi_buffer_new (&buffer -> next, MDL);
171 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS)
172 1.2.2.2 pgoyette return status;
173 1.2.2.2 pgoyette buffer = buffer -> next;
174 1.2.2.2 pgoyette }
175 1.2.2.2 pgoyette } else {
176 1.2.2.2 pgoyette status = omapi_buffer_new (&c -> inbufs, MDL);
177 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS)
178 1.2.2.2 pgoyette return status;
179 1.2.2.2 pgoyette buffer = c -> inbufs;
180 1.2.2.2 pgoyette }
181 1.2.2.2 pgoyette
182 1.2.2.2 pgoyette bytes_to_read = BUFFER_BYTES_FREE (buffer);
183 1.2.2.2 pgoyette
184 1.2.2.2 pgoyette while (bytes_to_read) {
185 1.2.2.2 pgoyette if (buffer -> tail > buffer -> head)
186 1.2.2.2 pgoyette read_len = sizeof (buffer -> buf) - buffer -> tail;
187 1.2.2.2 pgoyette else
188 1.2.2.2 pgoyette read_len = buffer -> head - buffer -> tail;
189 1.2.2.2 pgoyette
190 1.2.2.2 pgoyette #if defined (TRACING)
191 1.2.2.2 pgoyette if (trace_playback()) {
192 1.2.2.2 pgoyette if (stuff_len) {
193 1.2.2.2 pgoyette if (read_len > stuff_len)
194 1.2.2.2 pgoyette read_len = stuff_len;
195 1.2.2.2 pgoyette if (stuff_taken)
196 1.2.2.2 pgoyette *stuff_taken += read_len;
197 1.2.2.2 pgoyette memcpy (&buffer -> buf [buffer -> tail],
198 1.2.2.2 pgoyette stuff_buf, read_len);
199 1.2.2.2 pgoyette stuff_len -= read_len;
200 1.2.2.2 pgoyette stuff_buf += read_len;
201 1.2.2.2 pgoyette read_status = read_len;
202 1.2.2.2 pgoyette } else {
203 1.2.2.2 pgoyette break;
204 1.2.2.2 pgoyette }
205 1.2.2.2 pgoyette } else
206 1.2.2.2 pgoyette #endif
207 1.2.2.2 pgoyette {
208 1.2.2.2 pgoyette read_status = read (c -> socket,
209 1.2.2.2 pgoyette &buffer -> buf [buffer -> tail],
210 1.2.2.2 pgoyette read_len);
211 1.2.2.2 pgoyette }
212 1.2.2.2 pgoyette if (read_status < 0) {
213 1.2.2.2 pgoyette if (errno == EWOULDBLOCK)
214 1.2.2.2 pgoyette break;
215 1.2.2.2 pgoyette else if (errno == EIO)
216 1.2.2.2 pgoyette return ISC_R_IOERROR;
217 1.2.2.2 pgoyette else if (errno == EINVAL)
218 1.2.2.2 pgoyette return DHCP_R_INVALIDARG;
219 1.2.2.2 pgoyette else if (errno == ECONNRESET) {
220 1.2.2.2 pgoyette omapi_disconnect (h, 1);
221 1.2.2.2 pgoyette return ISC_R_SHUTTINGDOWN;
222 1.2.2.2 pgoyette } else
223 1.2.2.2 pgoyette return ISC_R_UNEXPECTED;
224 1.2.2.2 pgoyette }
225 1.2.2.2 pgoyette
226 1.2.2.2 pgoyette /* If we got a zero-length read, as opposed to EWOULDBLOCK,
227 1.2.2.2 pgoyette the remote end closed the connection. */
228 1.2.2.2 pgoyette if (read_status == 0) {
229 1.2.2.2 pgoyette omapi_disconnect (h, 0);
230 1.2.2.2 pgoyette return ISC_R_SHUTTINGDOWN;
231 1.2.2.2 pgoyette }
232 1.2.2.2 pgoyette #if defined (TRACING)
233 1.2.2.2 pgoyette if (trace_record ()) {
234 1.2.2.2 pgoyette trace_iov_t iov [2];
235 1.2.2.2 pgoyette int32_t connect_index;
236 1.2.2.2 pgoyette
237 1.2.2.2 pgoyette connect_index = htonl (c -> index);
238 1.2.2.2 pgoyette
239 1.2.2.2 pgoyette iov [0].buf = (char *)&connect_index;
240 1.2.2.2 pgoyette iov [0].len = sizeof connect_index;
241 1.2.2.2 pgoyette iov [1].buf = &buffer -> buf [buffer -> tail];
242 1.2.2.2 pgoyette iov [1].len = read_status;
243 1.2.2.2 pgoyette
244 1.2.2.2 pgoyette status = (trace_write_packet_iov
245 1.2.2.2 pgoyette (trace_connection_input, 2, iov, MDL));
246 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS) {
247 1.2.2.2 pgoyette trace_stop ();
248 1.2.2.2 pgoyette log_error ("trace connection input: %s",
249 1.2.2.2 pgoyette isc_result_totext (status));
250 1.2.2.2 pgoyette }
251 1.2.2.2 pgoyette }
252 1.2.2.2 pgoyette #endif
253 1.2.2.2 pgoyette buffer -> tail += read_status;
254 1.2.2.2 pgoyette c -> in_bytes += read_status;
255 1.2.2.2 pgoyette if (buffer -> tail == sizeof buffer -> buf)
256 1.2.2.2 pgoyette buffer -> tail = 0;
257 1.2.2.2 pgoyette if (read_status < read_len)
258 1.2.2.2 pgoyette break;
259 1.2.2.2 pgoyette bytes_to_read -= read_status;
260 1.2.2.2 pgoyette }
261 1.2.2.2 pgoyette
262 1.2.2.2 pgoyette if (c -> bytes_needed <= c -> in_bytes) {
263 1.2.2.2 pgoyette omapi_signal (h, "ready", c);
264 1.2.2.2 pgoyette }
265 1.2.2.2 pgoyette return ISC_R_SUCCESS;
266 1.2.2.2 pgoyette }
267 1.2.2.2 pgoyette
268 1.2.2.2 pgoyette /* Put some bytes into the output buffer for a connection. */
269 1.2.2.2 pgoyette
270 1.2.2.2 pgoyette isc_result_t omapi_connection_copyin (omapi_object_t *h,
271 1.2.2.2 pgoyette const unsigned char *bufp,
272 1.2.2.2 pgoyette unsigned len)
273 1.2.2.2 pgoyette {
274 1.2.2.2 pgoyette omapi_buffer_t *buffer;
275 1.2.2.2 pgoyette isc_result_t status;
276 1.2.2.2 pgoyette int bytes_copied = 0;
277 1.2.2.2 pgoyette unsigned copy_len;
278 1.2.2.2 pgoyette int sig_flags = SIG_MODE_UPDATE;
279 1.2.2.2 pgoyette omapi_connection_object_t *c;
280 1.2.2.2 pgoyette
281 1.2.2.2 pgoyette /* no need to verify len as it's unsigned */
282 1.2.2.2 pgoyette if (!h || h -> type != omapi_type_connection)
283 1.2.2.2 pgoyette return DHCP_R_INVALIDARG;
284 1.2.2.2 pgoyette c = (omapi_connection_object_t *)h;
285 1.2.2.2 pgoyette
286 1.2.2.2 pgoyette /* If the connection is closed, return an error if the caller
287 1.2.2.2 pgoyette tries to copy in. */
288 1.2.2.2 pgoyette if (c -> state == omapi_connection_disconnecting ||
289 1.2.2.2 pgoyette c -> state == omapi_connection_closed)
290 1.2.2.2 pgoyette return ISC_R_NOTCONNECTED;
291 1.2.2.2 pgoyette
292 1.2.2.2 pgoyette if (c -> outbufs) {
293 1.2.2.2 pgoyette for (buffer = c -> outbufs;
294 1.2.2.2 pgoyette buffer -> next; buffer = buffer -> next)
295 1.2.2.2 pgoyette ;
296 1.2.2.2 pgoyette } else {
297 1.2.2.2 pgoyette status = omapi_buffer_new (&c -> outbufs, MDL);
298 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS)
299 1.2.2.2 pgoyette goto leave;
300 1.2.2.2 pgoyette buffer = c -> outbufs;
301 1.2.2.2 pgoyette }
302 1.2.2.2 pgoyette
303 1.2.2.2 pgoyette while (bytes_copied < len) {
304 1.2.2.2 pgoyette /* If there is no space available in this buffer,
305 1.2.2.2 pgoyette allocate a new one. */
306 1.2.2.2 pgoyette if (!BUFFER_BYTES_FREE (buffer)) {
307 1.2.2.2 pgoyette status = (omapi_buffer_new (&buffer -> next, MDL));
308 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS)
309 1.2.2.2 pgoyette goto leave;
310 1.2.2.2 pgoyette buffer = buffer -> next;
311 1.2.2.2 pgoyette }
312 1.2.2.2 pgoyette
313 1.2.2.2 pgoyette if (buffer -> tail > buffer -> head)
314 1.2.2.2 pgoyette copy_len = sizeof (buffer -> buf) - buffer -> tail;
315 1.2.2.2 pgoyette else
316 1.2.2.2 pgoyette copy_len = buffer -> head - buffer -> tail;
317 1.2.2.2 pgoyette
318 1.2.2.2 pgoyette if (copy_len > (len - bytes_copied))
319 1.2.2.2 pgoyette copy_len = len - bytes_copied;
320 1.2.2.2 pgoyette
321 1.2.2.2 pgoyette if (c -> out_key) {
322 1.2.2.2 pgoyette if (!c -> out_context)
323 1.2.2.2 pgoyette sig_flags |= SIG_MODE_INIT;
324 1.2.2.2 pgoyette status = omapi_connection_sign_data
325 1.2.2.2 pgoyette (sig_flags, c -> out_key, &c -> out_context,
326 1.2.2.2 pgoyette &bufp [bytes_copied], copy_len,
327 1.2.2.2 pgoyette (omapi_typed_data_t **)0);
328 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS)
329 1.2.2.2 pgoyette goto leave;
330 1.2.2.2 pgoyette }
331 1.2.2.2 pgoyette
332 1.2.2.2 pgoyette memcpy (&buffer -> buf [buffer -> tail],
333 1.2.2.2 pgoyette &bufp [bytes_copied], copy_len);
334 1.2.2.2 pgoyette buffer -> tail += copy_len;
335 1.2.2.2 pgoyette c -> out_bytes += copy_len;
336 1.2.2.2 pgoyette bytes_copied += copy_len;
337 1.2.2.2 pgoyette if (buffer -> tail == sizeof buffer -> buf)
338 1.2.2.2 pgoyette buffer -> tail = 0;
339 1.2.2.2 pgoyette }
340 1.2.2.2 pgoyette
341 1.2.2.2 pgoyette status = ISC_R_SUCCESS;
342 1.2.2.2 pgoyette
343 1.2.2.2 pgoyette leave:
344 1.2.2.2 pgoyette /*
345 1.2.2.2 pgoyette * If we have any bytes to send and we have a proper io object
346 1.2.2.2 pgoyette * inform the socket code that we would like to know when we
347 1.2.2.2 pgoyette * can send more bytes.
348 1.2.2.2 pgoyette */
349 1.2.2.2 pgoyette if (c->out_bytes != 0) {
350 1.2.2.2 pgoyette if ((c->outer != NULL) &&
351 1.2.2.2 pgoyette (c->outer->type == omapi_type_io_object)) {
352 1.2.2.2 pgoyette omapi_io_object_t *io = (omapi_io_object_t *)c->outer;
353 1.2.2.2 pgoyette isc_socket_fdwatchpoke(io->fd,
354 1.2.2.2 pgoyette ISC_SOCKFDWATCH_WRITE);
355 1.2.2.2 pgoyette }
356 1.2.2.2 pgoyette }
357 1.2.2.2 pgoyette
358 1.2.2.2 pgoyette return (status);
359 1.2.2.2 pgoyette }
360 1.2.2.2 pgoyette
361 1.2.2.2 pgoyette /* Copy some bytes from the input buffer, and advance the input buffer
362 1.2.2.2 pgoyette pointer beyond the bytes copied out. */
363 1.2.2.2 pgoyette
364 1.2.2.2 pgoyette isc_result_t omapi_connection_copyout (unsigned char *buf,
365 1.2.2.2 pgoyette omapi_object_t *h,
366 1.2.2.2 pgoyette unsigned size)
367 1.2.2.2 pgoyette {
368 1.2.2.2 pgoyette unsigned bytes_remaining;
369 1.2.2.2 pgoyette unsigned bytes_this_copy;
370 1.2.2.2 pgoyette unsigned first_byte;
371 1.2.2.2 pgoyette omapi_buffer_t *buffer;
372 1.2.2.2 pgoyette unsigned char *bufp;
373 1.2.2.2 pgoyette int sig_flags = SIG_MODE_UPDATE;
374 1.2.2.2 pgoyette omapi_connection_object_t *c;
375 1.2.2.2 pgoyette isc_result_t status;
376 1.2.2.2 pgoyette
377 1.2.2.2 pgoyette if (!h || h -> type != omapi_type_connection)
378 1.2.2.2 pgoyette return DHCP_R_INVALIDARG;
379 1.2.2.2 pgoyette c = (omapi_connection_object_t *)h;
380 1.2.2.2 pgoyette
381 1.2.2.2 pgoyette if (size > c -> in_bytes)
382 1.2.2.2 pgoyette return ISC_R_NOMORE;
383 1.2.2.2 pgoyette bufp = buf;
384 1.2.2.2 pgoyette bytes_remaining = size;
385 1.2.2.2 pgoyette buffer = c -> inbufs;
386 1.2.2.2 pgoyette
387 1.2.2.2 pgoyette while (bytes_remaining) {
388 1.2.2.2 pgoyette if (!buffer)
389 1.2.2.2 pgoyette return ISC_R_UNEXPECTED;
390 1.2.2.2 pgoyette if (BYTES_IN_BUFFER (buffer)) {
391 1.2.2.2 pgoyette if (buffer -> head == (sizeof buffer -> buf) - 1)
392 1.2.2.2 pgoyette first_byte = 0;
393 1.2.2.2 pgoyette else
394 1.2.2.2 pgoyette first_byte = buffer -> head + 1;
395 1.2.2.2 pgoyette
396 1.2.2.2 pgoyette if (first_byte > buffer -> tail) {
397 1.2.2.2 pgoyette bytes_this_copy = (sizeof buffer -> buf -
398 1.2.2.2 pgoyette first_byte);
399 1.2.2.2 pgoyette } else {
400 1.2.2.2 pgoyette bytes_this_copy =
401 1.2.2.2 pgoyette buffer -> tail - first_byte;
402 1.2.2.2 pgoyette }
403 1.2.2.2 pgoyette if (bytes_this_copy > bytes_remaining)
404 1.2.2.2 pgoyette bytes_this_copy = bytes_remaining;
405 1.2.2.2 pgoyette if (bufp) {
406 1.2.2.2 pgoyette if (c -> in_key) {
407 1.2.2.2 pgoyette if (!c -> in_context)
408 1.2.2.2 pgoyette sig_flags |= SIG_MODE_INIT;
409 1.2.2.2 pgoyette status = omapi_connection_sign_data
410 1.2.2.2 pgoyette (sig_flags,
411 1.2.2.2 pgoyette c -> in_key,
412 1.2.2.2 pgoyette &c -> in_context,
413 1.2.2.2 pgoyette (unsigned char *)
414 1.2.2.2 pgoyette &buffer -> buf [first_byte],
415 1.2.2.2 pgoyette bytes_this_copy,
416 1.2.2.2 pgoyette (omapi_typed_data_t **)0);
417 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS)
418 1.2.2.2 pgoyette return status;
419 1.2.2.2 pgoyette }
420 1.2.2.2 pgoyette
421 1.2.2.2 pgoyette memcpy (bufp, &buffer -> buf [first_byte],
422 1.2.2.2 pgoyette bytes_this_copy);
423 1.2.2.2 pgoyette bufp += bytes_this_copy;
424 1.2.2.2 pgoyette }
425 1.2.2.2 pgoyette bytes_remaining -= bytes_this_copy;
426 1.2.2.2 pgoyette buffer -> head = first_byte + bytes_this_copy - 1;
427 1.2.2.2 pgoyette c -> in_bytes -= bytes_this_copy;
428 1.2.2.2 pgoyette }
429 1.2.2.2 pgoyette
430 1.2.2.2 pgoyette if (!BYTES_IN_BUFFER (buffer))
431 1.2.2.2 pgoyette buffer = buffer -> next;
432 1.2.2.2 pgoyette }
433 1.2.2.2 pgoyette
434 1.2.2.2 pgoyette /* Get rid of any input buffers that we emptied. */
435 1.2.2.2 pgoyette buffer = (omapi_buffer_t *)0;
436 1.2.2.2 pgoyette while (c -> inbufs &&
437 1.2.2.2 pgoyette !BYTES_IN_BUFFER (c -> inbufs)) {
438 1.2.2.2 pgoyette if (c -> inbufs -> next) {
439 1.2.2.2 pgoyette omapi_buffer_reference (&buffer,
440 1.2.2.2 pgoyette c -> inbufs -> next, MDL);
441 1.2.2.2 pgoyette omapi_buffer_dereference (&c -> inbufs -> next, MDL);
442 1.2.2.2 pgoyette }
443 1.2.2.2 pgoyette omapi_buffer_dereference (&c -> inbufs, MDL);
444 1.2.2.2 pgoyette if (buffer) {
445 1.2.2.2 pgoyette omapi_buffer_reference
446 1.2.2.2 pgoyette (&c -> inbufs, buffer, MDL);
447 1.2.2.2 pgoyette omapi_buffer_dereference (&buffer, MDL);
448 1.2.2.2 pgoyette }
449 1.2.2.2 pgoyette }
450 1.2.2.2 pgoyette return ISC_R_SUCCESS;
451 1.2.2.2 pgoyette }
452 1.2.2.2 pgoyette
453 1.2.2.2 pgoyette isc_result_t omapi_connection_writer (omapi_object_t *h)
454 1.2.2.2 pgoyette {
455 1.2.2.2 pgoyette unsigned bytes_this_write;
456 1.2.2.2 pgoyette int bytes_written;
457 1.2.2.2 pgoyette unsigned first_byte;
458 1.2.2.2 pgoyette omapi_buffer_t *buffer;
459 1.2.2.2 pgoyette omapi_connection_object_t *c;
460 1.2.2.2 pgoyette
461 1.2.2.2 pgoyette if (!h || h -> type != omapi_type_connection)
462 1.2.2.2 pgoyette return DHCP_R_INVALIDARG;
463 1.2.2.2 pgoyette c = (omapi_connection_object_t *)h;
464 1.2.2.2 pgoyette
465 1.2.2.2 pgoyette /* Already flushed... */
466 1.2.2.2 pgoyette if (!c -> out_bytes)
467 1.2.2.2 pgoyette return ISC_R_SUCCESS;
468 1.2.2.2 pgoyette
469 1.2.2.2 pgoyette buffer = c -> outbufs;
470 1.2.2.2 pgoyette
471 1.2.2.2 pgoyette while (c -> out_bytes) {
472 1.2.2.2 pgoyette if (!buffer)
473 1.2.2.2 pgoyette return ISC_R_UNEXPECTED;
474 1.2.2.2 pgoyette if (BYTES_IN_BUFFER (buffer)) {
475 1.2.2.2 pgoyette if (buffer -> head == (sizeof buffer -> buf) - 1)
476 1.2.2.2 pgoyette first_byte = 0;
477 1.2.2.2 pgoyette else
478 1.2.2.2 pgoyette first_byte = buffer -> head + 1;
479 1.2.2.2 pgoyette
480 1.2.2.2 pgoyette if (first_byte > buffer -> tail) {
481 1.2.2.2 pgoyette bytes_this_write = (sizeof buffer -> buf -
482 1.2.2.2 pgoyette first_byte);
483 1.2.2.2 pgoyette } else {
484 1.2.2.2 pgoyette bytes_this_write =
485 1.2.2.2 pgoyette buffer -> tail - first_byte;
486 1.2.2.2 pgoyette }
487 1.2.2.2 pgoyette bytes_written = write (c -> socket,
488 1.2.2.2 pgoyette &buffer -> buf [first_byte],
489 1.2.2.2 pgoyette bytes_this_write);
490 1.2.2.2 pgoyette /* If the write failed with EWOULDBLOCK or we wrote
491 1.2.2.2 pgoyette zero bytes, a further write would block, so we have
492 1.2.2.2 pgoyette flushed as much as we can for now. Other errors
493 1.2.2.2 pgoyette are really errors. */
494 1.2.2.2 pgoyette if (bytes_written < 0) {
495 1.2.2.2 pgoyette if (errno == EWOULDBLOCK || errno == EAGAIN)
496 1.2.2.2 pgoyette return ISC_R_INPROGRESS;
497 1.2.2.2 pgoyette else if (errno == EPIPE)
498 1.2.2.2 pgoyette return ISC_R_NOCONN;
499 1.2.2.2 pgoyette #ifdef EDQUOT
500 1.2.2.2 pgoyette else if (errno == EFBIG || errno == EDQUOT)
501 1.2.2.2 pgoyette #else
502 1.2.2.2 pgoyette else if (errno == EFBIG)
503 1.2.2.2 pgoyette #endif
504 1.2.2.2 pgoyette return ISC_R_NORESOURCES;
505 1.2.2.2 pgoyette else if (errno == ENOSPC)
506 1.2.2.2 pgoyette return ISC_R_NOSPACE;
507 1.2.2.2 pgoyette else if (errno == EIO)
508 1.2.2.2 pgoyette return ISC_R_IOERROR;
509 1.2.2.2 pgoyette else if (errno == EINVAL)
510 1.2.2.2 pgoyette return DHCP_R_INVALIDARG;
511 1.2.2.2 pgoyette else if (errno == ECONNRESET)
512 1.2.2.2 pgoyette return ISC_R_SHUTTINGDOWN;
513 1.2.2.2 pgoyette else
514 1.2.2.2 pgoyette return ISC_R_UNEXPECTED;
515 1.2.2.2 pgoyette }
516 1.2.2.2 pgoyette if (bytes_written == 0)
517 1.2.2.2 pgoyette return ISC_R_INPROGRESS;
518 1.2.2.2 pgoyette
519 1.2.2.2 pgoyette #if defined (TRACING)
520 1.2.2.2 pgoyette if (trace_record ()) {
521 1.2.2.2 pgoyette isc_result_t status;
522 1.2.2.2 pgoyette trace_iov_t iov [2];
523 1.2.2.2 pgoyette int32_t connect_index;
524 1.2.2.2 pgoyette
525 1.2.2.2 pgoyette connect_index = htonl (c -> index);
526 1.2.2.2 pgoyette
527 1.2.2.2 pgoyette iov [0].buf = (char *)&connect_index;
528 1.2.2.2 pgoyette iov [0].len = sizeof connect_index;
529 1.2.2.2 pgoyette iov [1].buf = &buffer -> buf [buffer -> tail];
530 1.2.2.2 pgoyette iov [1].len = bytes_written;
531 1.2.2.2 pgoyette
532 1.2.2.2 pgoyette status = (trace_write_packet_iov
533 1.2.2.2 pgoyette (trace_connection_input, 2, iov,
534 1.2.2.2 pgoyette MDL));
535 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS) {
536 1.2.2.2 pgoyette trace_stop ();
537 1.2.2.2 pgoyette log_error ("trace %s output: %s",
538 1.2.2.2 pgoyette "connection",
539 1.2.2.2 pgoyette isc_result_totext (status));
540 1.2.2.2 pgoyette }
541 1.2.2.2 pgoyette }
542 1.2.2.2 pgoyette #endif
543 1.2.2.2 pgoyette
544 1.2.2.2 pgoyette buffer -> head = first_byte + bytes_written - 1;
545 1.2.2.2 pgoyette c -> out_bytes -= bytes_written;
546 1.2.2.2 pgoyette
547 1.2.2.2 pgoyette /* If we didn't finish out the write, we filled the
548 1.2.2.2 pgoyette O.S. output buffer and a further write would block,
549 1.2.2.2 pgoyette so stop trying to flush now. */
550 1.2.2.2 pgoyette if (bytes_written != bytes_this_write)
551 1.2.2.2 pgoyette return ISC_R_INPROGRESS;
552 1.2.2.2 pgoyette }
553 1.2.2.2 pgoyette
554 1.2.2.2 pgoyette if (!BYTES_IN_BUFFER (buffer))
555 1.2.2.2 pgoyette buffer = buffer -> next;
556 1.2.2.2 pgoyette }
557 1.2.2.2 pgoyette
558 1.2.2.2 pgoyette /* Get rid of any output buffers we emptied. */
559 1.2.2.2 pgoyette buffer = (omapi_buffer_t *)0;
560 1.2.2.2 pgoyette while (c -> outbufs &&
561 1.2.2.2 pgoyette !BYTES_IN_BUFFER (c -> outbufs)) {
562 1.2.2.2 pgoyette if (c -> outbufs -> next) {
563 1.2.2.2 pgoyette omapi_buffer_reference (&buffer,
564 1.2.2.2 pgoyette c -> outbufs -> next, MDL);
565 1.2.2.2 pgoyette omapi_buffer_dereference (&c -> outbufs -> next, MDL);
566 1.2.2.2 pgoyette }
567 1.2.2.2 pgoyette omapi_buffer_dereference (&c -> outbufs, MDL);
568 1.2.2.2 pgoyette if (buffer) {
569 1.2.2.2 pgoyette omapi_buffer_reference (&c -> outbufs, buffer, MDL);
570 1.2.2.2 pgoyette omapi_buffer_dereference (&buffer, MDL);
571 1.2.2.2 pgoyette }
572 1.2.2.2 pgoyette }
573 1.2.2.2 pgoyette
574 1.2.2.2 pgoyette /* If we had data left to write when we're told to disconnect,
575 1.2.2.2 pgoyette * we need recall disconnect, now that we're done writing.
576 1.2.2.2 pgoyette * See rt46767. */
577 1.2.2.2 pgoyette if (c->out_bytes == 0 && c->state == omapi_connection_disconnecting) {
578 1.2.2.2 pgoyette omapi_disconnect (h, 1);
579 1.2.2.2 pgoyette return ISC_R_SHUTTINGDOWN;
580 1.2.2.2 pgoyette }
581 1.2.2.2 pgoyette
582 1.2.2.2 pgoyette return ISC_R_SUCCESS;
583 1.2.2.2 pgoyette }
584 1.2.2.2 pgoyette
585 1.2.2.2 pgoyette isc_result_t omapi_connection_get_uint32 (omapi_object_t *c,
586 1.2.2.2 pgoyette u_int32_t *result)
587 1.2.2.2 pgoyette {
588 1.2.2.2 pgoyette u_int32_t inbuf;
589 1.2.2.2 pgoyette isc_result_t status;
590 1.2.2.2 pgoyette
591 1.2.2.2 pgoyette status = omapi_connection_copyout ((unsigned char *)&inbuf,
592 1.2.2.2 pgoyette c, sizeof inbuf);
593 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS)
594 1.2.2.2 pgoyette return status;
595 1.2.2.2 pgoyette
596 1.2.2.2 pgoyette *result = ntohl (inbuf);
597 1.2.2.2 pgoyette return ISC_R_SUCCESS;
598 1.2.2.2 pgoyette }
599 1.2.2.2 pgoyette
600 1.2.2.2 pgoyette isc_result_t omapi_connection_put_uint32 (omapi_object_t *c,
601 1.2.2.2 pgoyette u_int32_t value)
602 1.2.2.2 pgoyette {
603 1.2.2.2 pgoyette u_int32_t inbuf;
604 1.2.2.2 pgoyette
605 1.2.2.2 pgoyette inbuf = htonl (value);
606 1.2.2.2 pgoyette
607 1.2.2.2 pgoyette return omapi_connection_copyin (c, (unsigned char *)&inbuf,
608 1.2.2.2 pgoyette sizeof inbuf);
609 1.2.2.2 pgoyette }
610 1.2.2.2 pgoyette
611 1.2.2.2 pgoyette isc_result_t omapi_connection_get_uint16 (omapi_object_t *c,
612 1.2.2.2 pgoyette u_int16_t *result)
613 1.2.2.2 pgoyette {
614 1.2.2.2 pgoyette u_int16_t inbuf;
615 1.2.2.2 pgoyette isc_result_t status;
616 1.2.2.2 pgoyette
617 1.2.2.2 pgoyette status = omapi_connection_copyout ((unsigned char *)&inbuf,
618 1.2.2.2 pgoyette c, sizeof inbuf);
619 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS)
620 1.2.2.2 pgoyette return status;
621 1.2.2.2 pgoyette
622 1.2.2.2 pgoyette *result = ntohs (inbuf);
623 1.2.2.2 pgoyette return ISC_R_SUCCESS;
624 1.2.2.2 pgoyette }
625 1.2.2.2 pgoyette
626 1.2.2.2 pgoyette isc_result_t omapi_connection_put_uint16 (omapi_object_t *c,
627 1.2.2.2 pgoyette u_int32_t value)
628 1.2.2.2 pgoyette {
629 1.2.2.2 pgoyette u_int16_t inbuf;
630 1.2.2.2 pgoyette
631 1.2.2.2 pgoyette inbuf = htons (value);
632 1.2.2.2 pgoyette
633 1.2.2.2 pgoyette return omapi_connection_copyin (c, (unsigned char *)&inbuf,
634 1.2.2.2 pgoyette sizeof inbuf);
635 1.2.2.2 pgoyette }
636 1.2.2.2 pgoyette
637 1.2.2.2 pgoyette isc_result_t omapi_connection_write_typed_data (omapi_object_t *c,
638 1.2.2.2 pgoyette omapi_typed_data_t *data)
639 1.2.2.2 pgoyette {
640 1.2.2.2 pgoyette isc_result_t status;
641 1.2.2.2 pgoyette omapi_handle_t handle;
642 1.2.2.2 pgoyette
643 1.2.2.2 pgoyette /* Null data is valid. */
644 1.2.2.2 pgoyette if (!data)
645 1.2.2.2 pgoyette return omapi_connection_put_uint32 (c, 0);
646 1.2.2.2 pgoyette
647 1.2.2.2 pgoyette switch (data -> type) {
648 1.2.2.2 pgoyette case omapi_datatype_int:
649 1.2.2.2 pgoyette status = omapi_connection_put_uint32 (c, sizeof (u_int32_t));
650 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS)
651 1.2.2.2 pgoyette return status;
652 1.2.2.2 pgoyette return omapi_connection_put_uint32 (c, ((u_int32_t)
653 1.2.2.2 pgoyette (data -> u.integer)));
654 1.2.2.2 pgoyette
655 1.2.2.2 pgoyette case omapi_datatype_string:
656 1.2.2.2 pgoyette case omapi_datatype_data:
657 1.2.2.2 pgoyette status = omapi_connection_put_uint32 (c, data -> u.buffer.len);
658 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS)
659 1.2.2.2 pgoyette return status;
660 1.2.2.2 pgoyette if (data -> u.buffer.len)
661 1.2.2.2 pgoyette return omapi_connection_copyin
662 1.2.2.2 pgoyette (c, data -> u.buffer.value,
663 1.2.2.2 pgoyette data -> u.buffer.len);
664 1.2.2.2 pgoyette return ISC_R_SUCCESS;
665 1.2.2.2 pgoyette
666 1.2.2.2 pgoyette case omapi_datatype_object:
667 1.2.2.2 pgoyette if (data -> u.object) {
668 1.2.2.2 pgoyette status = omapi_object_handle (&handle,
669 1.2.2.2 pgoyette data -> u.object);
670 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS)
671 1.2.2.2 pgoyette return status;
672 1.2.2.2 pgoyette } else
673 1.2.2.2 pgoyette handle = 0;
674 1.2.2.2 pgoyette status = omapi_connection_put_uint32 (c, sizeof handle);
675 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS)
676 1.2.2.2 pgoyette return status;
677 1.2.2.2 pgoyette return omapi_connection_put_uint32 (c, handle);
678 1.2.2.2 pgoyette
679 1.2.2.2 pgoyette }
680 1.2.2.2 pgoyette return DHCP_R_INVALIDARG;
681 1.2.2.2 pgoyette }
682 1.2.2.2 pgoyette
683 1.2.2.2 pgoyette isc_result_t omapi_connection_put_name (omapi_object_t *c, const char *name)
684 1.2.2.2 pgoyette {
685 1.2.2.2 pgoyette isc_result_t status;
686 1.2.2.2 pgoyette unsigned len = strlen (name);
687 1.2.2.2 pgoyette
688 1.2.2.2 pgoyette status = omapi_connection_put_uint16 (c, len);
689 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS)
690 1.2.2.2 pgoyette return status;
691 1.2.2.2 pgoyette return omapi_connection_copyin (c, (const unsigned char *)name, len);
692 1.2.2.2 pgoyette }
693 1.2.2.2 pgoyette
694 1.2.2.2 pgoyette isc_result_t omapi_connection_put_string (omapi_object_t *c,
695 1.2.2.2 pgoyette const char *string)
696 1.2.2.2 pgoyette {
697 1.2.2.2 pgoyette isc_result_t status;
698 1.2.2.2 pgoyette unsigned len;
699 1.2.2.2 pgoyette
700 1.2.2.2 pgoyette if (string)
701 1.2.2.2 pgoyette len = strlen (string);
702 1.2.2.2 pgoyette else
703 1.2.2.2 pgoyette len = 0;
704 1.2.2.2 pgoyette
705 1.2.2.2 pgoyette status = omapi_connection_put_uint32 (c, len);
706 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS)
707 1.2.2.2 pgoyette return status;
708 1.2.2.2 pgoyette if (len)
709 1.2.2.2 pgoyette return omapi_connection_copyin
710 1.2.2.2 pgoyette (c, (const unsigned char *)string, len);
711 1.2.2.2 pgoyette return ISC_R_SUCCESS;
712 1.2.2.2 pgoyette }
713 1.2.2.2 pgoyette
714 1.2.2.2 pgoyette isc_result_t omapi_connection_put_handle (omapi_object_t *c, omapi_object_t *h)
715 1.2.2.2 pgoyette {
716 1.2.2.2 pgoyette isc_result_t status;
717 1.2.2.2 pgoyette omapi_handle_t handle;
718 1.2.2.2 pgoyette
719 1.2.2.2 pgoyette if (h) {
720 1.2.2.2 pgoyette status = omapi_object_handle (&handle, h);
721 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS)
722 1.2.2.2 pgoyette return status;
723 1.2.2.2 pgoyette } else
724 1.2.2.2 pgoyette handle = 0; /* The null handle. */
725 1.2.2.2 pgoyette status = omapi_connection_put_uint32 (c, sizeof handle);
726 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS)
727 1.2.2.2 pgoyette return status;
728 1.2.2.2 pgoyette return omapi_connection_put_uint32 (c, handle);
729 1.2.2.2 pgoyette }
730 1.2.2.2 pgoyette
731 1.2.2.2 pgoyette isc_result_t omapi_connection_put_named_uint32 (omapi_object_t *c,
732 1.2.2.2 pgoyette const char *name,
733 1.2.2.2 pgoyette u_int32_t value)
734 1.2.2.2 pgoyette {
735 1.2.2.2 pgoyette isc_result_t status;
736 1.2.2.2 pgoyette
737 1.2.2.2 pgoyette status = omapi_connection_put_name(c, name);
738 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS)
739 1.2.2.2 pgoyette return (status);
740 1.2.2.2 pgoyette
741 1.2.2.2 pgoyette status = omapi_connection_put_uint32(c, sizeof(u_int32_t));
742 1.2.2.2 pgoyette if (status != ISC_R_SUCCESS)
743 1.2.2.2 pgoyette return (status);
744 1.2.2.2 pgoyette
745 1.2.2.2 pgoyette status = omapi_connection_put_uint32(c, value);
746 1.2.2.2 pgoyette return (status);
747 1.2.2.2 pgoyette }
748 1.2.2.2 pgoyette
749