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