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