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