33 #include <arpa/inet.h> 38 static void trace_connect_input (
trace_type_t *,
unsigned,
char *);
40 static void trace_disconnect_input (
trace_type_t *,
unsigned,
char *);
46 static isc_result_t omapi_connection_connect_internal (
omapi_object_t *);
52 const
char *server_name,
62 log_debug (
"omapi_connect(%s, port=%d)", server_name, port);
65 if (!inet_aton (server_name, &foo)) {
68 he = gethostbyname (server_name);
71 for (i = 0; he -> h_addr_list [i]; i++)
78 if (status != ISC_R_SUCCESS)
80 for (i = 0; i < hix; i++) {
81 addrs -> addresses [i].addrtype = he -> h_addrtype;
82 addrs -> addresses [i].addrlen = he -> h_length;
83 memcpy (addrs -> addresses [i].address,
84 he -> h_addr_list [i],
85 (
unsigned)he -> h_length);
86 addrs -> addresses [i].port = port;
90 if (status != ISC_R_SUCCESS)
92 addrs -> addresses [0].addrtype = AF_INET;
93 addrs -> addresses [0].addrlen =
sizeof foo;
94 memcpy (addrs -> addresses [0].address, &foo,
sizeof foo);
95 addrs -> addresses [0].port = port;
109 struct sockaddr_in local_sin;
112 status = omapi_connection_allocate (&obj,
MDL);
113 if (status != ISC_R_SUCCESS)
118 if (status != ISC_R_SUCCESS) {
119 omapi_connection_dereference (&obj,
MDL);
123 if (status != ISC_R_SUCCESS) {
124 omapi_connection_dereference (&obj,
MDL);
133 #if defined (TRACING) 140 socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
141 if (obj -> socket < 0) {
142 omapi_connection_dereference (&obj,
MDL);
143 if (errno == EMFILE || errno == ENFILE
145 return ISC_R_NORESOURCES;
146 return ISC_R_UNEXPECTED;
152 if (local_addr -> addrtype != AF_INET) {
153 omapi_connection_dereference (&obj,
MDL);
156 local_sin.sin_port = htons (local_addr -> port);
157 memcpy (&local_sin.sin_addr,
158 local_addr -> address,
159 local_addr -> addrlen);
160 #if defined (HAVE_SA_LEN) 161 local_sin.sin_len =
sizeof local_addr;
163 local_sin.sin_family = AF_INET;
164 memset (&local_sin.sin_zero, 0,
165 sizeof local_sin.sin_zero);
167 if (bind (obj -> socket, (
struct sockaddr *)&local_sin,
168 sizeof local_sin) < 0) {
172 if (errno == EADDRINUSE)
173 return ISC_R_ADDRINUSE;
174 if (errno == EADDRNOTAVAIL)
175 return ISC_R_ADDRNOTAVAIL;
178 return ISC_R_UNEXPECTED;
180 obj -> local_addr = local_sin;
184 if (fcntl (obj -> socket, F_SETFD, 1) < 0) {
185 close (obj -> socket);
186 omapi_connection_dereference (&obj,
MDL);
187 return ISC_R_UNEXPECTED;
193 if (setsockopt (obj -> socket, SOL_SOCKET, SO_REUSEADDR,
194 (
char *)&flag,
sizeof flag) < 0) {
195 omapi_connection_dereference (&obj,
MDL);
196 return ISC_R_UNEXPECTED;
200 if (fcntl (obj -> socket, F_SETFL, O_NONBLOCK) < 0) {
201 omapi_connection_dereference (&obj,
MDL);
202 return ISC_R_UNEXPECTED;
211 if (setsockopt(obj->socket, SOL_SOCKET, SO_NOSIGPIPE,
212 (
char *)&flag,
sizeof(flag)) < 0) {
213 omapi_connection_dereference (&obj,
MDL);
214 return ISC_R_UNEXPECTED;
223 if (status != ISC_R_SUCCESS)
234 if (status == ISC_R_INPROGRESS) {
235 status = ISC_R_SUCCESS;
237 #if defined (TRACING) 243 omapi_connection_dereference (&obj,
MDL);
247 #if defined (TRACING) 255 trace_connect_stop,
MDL);
257 trace_disconnect_input,
258 trace_disconnect_stop,
MDL);
267 int32_t connect_index, listener_index;
268 static int32_t index;
270 if (!omapi_connections) {
271 status = omapi_connection_array_allocate (&omapi_connections,
273 if (status != ISC_R_SUCCESS)
277 status = omapi_connection_array_extend (omapi_connections, obj,
278 (
int *)0,
file, line);
279 if (status != ISC_R_SUCCESS) {
284 #if defined (TRACING) 295 connect_index = htonl (index);
298 listener_index = htonl (obj -> listener -> index);
300 listener_index = htonl (-1);
301 iov [iov_count].
buf = (
char *)&connect_index;
302 iov [iov_count++].
len =
sizeof connect_index;
303 iov [iov_count].
buf = (
char *)&listener_index;
304 iov [iov_count++].
len =
sizeof listener_index;
305 iov [iov_count].
buf = (
char *)&obj -> remote_addr.sin_port;
306 iov [iov_count++].
len =
sizeof obj -> remote_addr.sin_port;
307 iov [iov_count].
buf = (
char *)&obj -> local_addr.sin_port;
308 iov [iov_count++].
len =
sizeof obj -> local_addr.sin_port;
309 iov [iov_count].
buf = (
char *)&obj -> remote_addr.sin_addr;
310 iov [iov_count++].
len =
sizeof obj -> remote_addr.sin_addr;
311 iov [iov_count].
buf = (
char *)&obj -> local_addr.sin_addr;
312 iov [iov_count++].
len =
sizeof obj -> local_addr.sin_addr;
315 iov_count, iov,
file, line);
321 unsigned length,
char *buf)
323 struct sockaddr_in remote, local;
324 int32_t connect_index, listener_index;
330 if (length != ((
sizeof connect_index) +
331 (
sizeof remote.sin_port) +
332 (
sizeof remote.sin_addr)) * 2) {
333 log_error (
"Trace connect: invalid length %d", length);
337 memset (&remote, 0,
sizeof remote);
338 memset (&local, 0,
sizeof local);
339 memcpy (&connect_index, s,
sizeof connect_index);
340 s +=
sizeof connect_index;
341 memcpy (&listener_index, s,
sizeof listener_index);
342 s +=
sizeof listener_index;
343 memcpy (&remote.sin_port, s,
sizeof remote.sin_port);
344 s +=
sizeof remote.sin_port;
345 memcpy (&local.sin_port, s,
sizeof local.sin_port);
346 s +=
sizeof local.sin_port;
347 memcpy (&remote.sin_addr, s,
sizeof remote.sin_addr);
348 s +=
sizeof remote.sin_addr;
349 memcpy (&local.sin_addr, s,
sizeof local.sin_addr);
350 s +=
sizeof local.sin_addr;
353 connect_index = ntohl (connect_index);
354 listener_index = ntohl (listener_index);
358 if (listener_index != -1) {
363 if (lp -> address.sin_port == local.sin_port) {
364 omapi_listener_reference (&listener, lp,
MDL);
365 omapi_listener_dereference (&lp,
MDL);
372 "Spurious traced listener connect - index ",
373 (
long int)listener_index,
374 inet_ntoa (local.sin_addr),
375 ntohs (local.sin_port));
380 if (status != ISC_R_SUCCESS) {
381 log_error (
"traced listener connect: %s",
382 isc_result_totext (status));
385 omapi_connection_dereference (&obj,
MDL);
386 omapi_listener_dereference (&listener,
MDL);
393 for (i = 0; (lp->connect_list &&
394 i < lp->connect_list->count); i++) {
395 if (!memcmp (&remote.sin_addr,
396 &lp->connect_list->addresses[i].address,
397 sizeof remote.sin_addr) &&
398 (ntohs (remote.sin_port) ==
399 lp->connect_list->addresses[i].port)) {
401 lp->remote_addr = remote;
402 lp->remote_addr.sin_family = AF_INET;
404 lp->index = connect_index;
407 omapi_connection_dereference (&lp,
MDL);
414 log_error (
"Spurious traced connect - index %ld, addr %s, port %d",
415 (
long int)connect_index, inet_ntoa (remote.sin_addr),
416 ntohs (remote.sin_port));
420 static void trace_connect_stop (
trace_type_t *ttype) { }
422 static void trace_disconnect_input (
trace_type_t *ttype,
423 unsigned length,
char *buf)
426 if (length !=
sizeof *index) {
427 log_error (
"trace disconnect: wrong length %d", length);
431 index = (int32_t *)buf;
435 if (lp -> index == ntohl (*index)) {
437 omapi_connection_dereference (&lp,
MDL);
443 log_error (
"trace disconnect: no connection matching index %ld",
444 (
long int)ntohl (*index));
447 static void trace_disconnect_stop (
trace_type_t *ttype) { }
459 #ifdef DEBUG_PROTOCOL 460 log_debug (
"omapi_disconnect(%s)", force ?
"force" :
"");
467 #if defined (TRACING) 472 index = htonl (c -> index);
474 sizeof index, (
char *)&index,
476 if (status != ISC_R_SUCCESS) {
479 isc_result_totext (status));
488 return ISC_R_SUCCESS;
495 if (!shutdown (c -> socket,
SHUT_RD)) {
496 if (c -> out_bytes > 0) {
499 return ISC_R_SUCCESS;
504 #if defined (TRACING) 521 if (h -> outer -> inner)
536 if (h->inner != NULL) {
537 if (h->inner->outer != NULL) {
556 return ISC_R_SUCCESS;
567 c -> bytes_needed = bytes;
568 if (c -> bytes_needed <= c -> in_bytes) {
569 return ISC_R_SUCCESS;
614 status = omapi_connection_connect_internal (h);
615 if (status == ISC_R_INPROGRESS)
616 return ISC_R_INPROGRESS;
618 if (status != ISC_R_SUCCESS)
621 return ISC_R_SUCCESS;
624 static isc_result_t omapi_connection_connect_internal (
omapi_object_t *h)
637 if (getsockopt (c -> socket, SOL_SOCKET, SO_ERROR,
638 (
char *)&error, &sl) < 0) {
640 return ISC_R_SUCCESS;
647 if (c -> cptr >= c -> connect_list -> count) {
650 status = ISC_R_CONNREFUSED;
653 status = ISC_R_NETUNREACH;
663 if (c -> connect_list -> addresses [c -> cptr].addrtype !=
669 memcpy (&c -> remote_addr.sin_addr,
670 &c -> connect_list -> addresses [c -> cptr].address,
671 sizeof c -> remote_addr.sin_addr);
672 c -> remote_addr.sin_family = AF_INET;
673 c -> remote_addr.sin_port =
674 htons (c -> connect_list -> addresses [c -> cptr].port);
675 #if defined (HAVE_SA_LEN) 676 c -> remote_addr.sin_len =
sizeof c -> remote_addr;
678 memset (&c -> remote_addr.sin_zero, 0,
679 sizeof c -> remote_addr.sin_zero);
682 error = connect (c -> socket,
683 (
struct sockaddr *)&c -> remote_addr,
684 sizeof c -> remote_addr);
687 if (error != EINPROGRESS) {
691 status = ISC_R_CONNREFUSED;
694 status = ISC_R_NETUNREACH;
710 sl =
sizeof (c -> local_addr);
711 if (getsockname (c -> socket,
712 (
struct sockaddr *)&c -> local_addr, &sl) < 0) {
726 if (status != ISC_R_SUCCESS) {
733 return ISC_R_INPROGRESS;
749 c -> out_bytes == 0) {
750 #ifdef DEBUG_PROTOCOL 751 log_debug (
"omapi_connection_reaper(): disconnect");
756 #ifdef DEBUG_PROTOCOL 757 log_debug (
"omapi_connection_reaper(): closed");
759 return ISC_R_NOTCONNECTED;
761 return ISC_R_SUCCESS;
768 char *name_str = NULL;
769 isc_result_t status = ISC_R_SUCCESS;
771 if (status == ISC_R_SUCCESS)
775 if (status == ISC_R_SUCCESS)
779 if (status == ISC_R_SUCCESS)
783 if (status == ISC_R_SUCCESS) {
793 if (status == ISC_R_SUCCESS) {
794 name_str =
dmalloc (name -> value -> u.buffer.len + 1,
MDL);
796 status = ISC_R_NOMEMORY;
799 if (status == ISC_R_SUCCESS) {
801 name -> value -> u.buffer.
value,
802 name -> value -> u.buffer.len);
803 name_str [name -> value -> u.buffer.len] = 0;
812 status = ISC_R_NOMEMORY;
830 const unsigned char *data,
836 dst_context_t **dctx = (dst_context_t **)context;
841 if (status != ISC_R_SUCCESS) {
849 region.base = (
unsigned char *)data;
851 dst_context_adddata(*dctx, ®ion);
856 unsigned int sigsize;
859 status = dst_key_sigsize(key, &sigsize);
860 if (status != ISC_R_SUCCESS) {
867 if (status != ISC_R_SUCCESS) {
872 status = dst_context_sign(*dctx, &sigbuf);
873 if (status != ISC_R_SUCCESS) {
888 dst_context_destroy(dctx);
892 return ISC_R_SUCCESS;
905 return ISC_R_NOTFOUND;
907 return(dst_key_sigsize(c->
out_key, l));
926 if (c -> in_context) {
939 status = make_dst_key (&c -> in_key,
941 if (status != ISC_R_SUCCESS)
945 return ISC_R_SUCCESS;
951 if (c -> out_context) {
964 status = make_dst_key (&c -> out_key,
966 if (status != ISC_R_SUCCESS)
970 return ISC_R_SUCCESS;
973 if (h -> inner && h -> inner -> type -> set_value)
974 return (*(h -> inner -> type -> set_value))
975 (h -> inner, id, name, value);
976 return ISC_R_NOTFOUND;
987 unsigned int sigsize;
994 if (!c -> in_key || !c -> in_context)
995 return ISC_R_NOTFOUND;
1001 if (status != ISC_R_SUCCESS)
1010 return ISC_R_NOTFOUND;
1012 status = dst_key_sigsize(c->
in_key, &sigsize);
1013 if (status != ISC_R_SUCCESS) {
1020 if (!c -> out_key || !c -> out_context)
1021 return ISC_R_NOTFOUND;
1027 if (status != ISC_R_SUCCESS)
1036 return ISC_R_NOTFOUND;
1039 status = dst_key_sigsize(c->
out_key, &sigsize);
1040 if (status != ISC_R_SUCCESS) {
1047 if (h -> inner && h -> inner -> type -> get_value)
1048 return (*(h -> inner -> type -> get_value))
1049 (h -> inner, id, name, value);
1050 return ISC_R_NOTFOUND;
1058 #ifdef DEBUG_PROTOCOL 1059 log_debug (
"omapi_connection_destroy()");
1063 return ISC_R_UNEXPECTED;
1068 omapi_listener_dereference (&c -> listener, file, line);
1069 if (c -> connect_list)
1071 return ISC_R_SUCCESS;
1075 const char *name, va_list ap)
1080 #ifdef DEBUG_PROTOCOL 1081 log_debug (
"omapi_connection_signal_handler(%s)", name);
1084 if (h -> inner && h -> inner -> type -> signal_handler)
1085 return (*(h -> inner -> type -> signal_handler)) (h -> inner,
1087 return ISC_R_NOTFOUND;
1100 if (m -> inner && m -> inner -> type -> stuff_values)
1101 return (*(m -> inner -> type -> stuff_values)) (c, id,
1103 return ISC_R_SUCCESS;
isc_result_t omapi_reregister_io_object(omapi_object_t *, int(*)(omapi_object_t *), int(*)(omapi_object_t *), isc_result_t(*)(omapi_object_t *), isc_result_t(*)(omapi_object_t *), isc_result_t(*)(omapi_object_t *))
isc_result_t omapi_disconnect(omapi_object_t *h, int force)
isc_result_t omapi_typed_data_new(const char *, int, omapi_typed_data_t **, omapi_datatype_t,...)
isc_result_t omapi_connection_reader(omapi_object_t *)
#define omapi_array_foreach_end(array, stype, var)
isc_result_t omapi_connection_sign_data(int mode, dst_key_t *key, void **context, const unsigned char *data, const unsigned len, omapi_typed_data_t **result)
omapi_object_type_t * omapi_type_connection
isc_result_t omapi_register_io_object(omapi_object_t *, int(*)(omapi_object_t *), int(*)(omapi_object_t *), isc_result_t(*)(omapi_object_t *), isc_result_t(*)(omapi_object_t *), isc_result_t(*)(omapi_object_t *))
isc_result_t omapi_make_int_value(omapi_value_t **, omapi_data_string_t *, int, const char *, int)
isc_result_t omapi_buffer_dereference(omapi_buffer_t **, const char *, int)
isc_result_t omapi_object_reference(omapi_object_t **, omapi_object_t *, const char *, int)
#define NS_TSIG_ALG_HMAC_MD5
void * dmalloc(unsigned, const char *, int)
dhcp_context_t dhcp_gbl_ctx
#define DHCP_R_INVALIDARG
omapi_typed_data_t * value
int int int log_debug(const char *,...) __attribute__((__format__(__printf__
isc_result_t omapi_signal_in(omapi_object_t *, const char *,...)
isc_result_t trace_write_packet_iov(trace_type_t *, int, trace_iov_t *, const char *, int)
int log_error(const char *,...) __attribute__((__format__(__printf__
#define DHCP_R_HOSTUNKNOWN
void omapi_connection_trace_setup(void)
#define omapi_array_foreach_begin(array, stype, var)
struct omapi_typed_data_t::@3::@4 buffer
isc_result_t omapi_connection_output_auth_length(omapi_object_t *h, unsigned *l)
isc_result_t omapi_get_value_str(omapi_object_t *, omapi_object_t *, const char *, omapi_value_t **)
isc_result_t omapi_connection_set_value(omapi_object_t *h, omapi_object_t *id, omapi_data_string_t *name, omapi_typed_data_t *value)
OMAPI_OBJECT_ALLOC(omapi_connection, omapi_connection_object_t, omapi_type_connection)
isc_result_t omapi_addr_list_reference(omapi_addr_list_t **, omapi_addr_list_t *, const char *, int)
trace_type_t * trace_type_register(const char *, void *, void(*)(trace_type_t *, unsigned, char *), void(*)(trace_type_t *), const char *, int)
isc_result_t omapi_connection_writer(omapi_object_t *)
isc_result_t omapi_connect_list(omapi_object_t *c, omapi_addr_list_t *remote_addrs, omapi_addr_t *local_addr)
isc_result_t omapi_object_dereference(omapi_object_t **, const char *, int)
isc_result_t omapi_signal(omapi_object_t *, const char *,...)
isc_result_t omapi_connection_reaper(omapi_object_t *h)
isc_result_t omapi_connection_require(omapi_object_t *h, unsigned bytes)
void dfree(void *, const char *, int)
isc_result_t omapi_make_value(omapi_value_t **, omapi_data_string_t *, omapi_typed_data_t *, const char *, int)
union omapi_typed_data_t::@3 u
void omapi_connection_register(omapi_connection_object_t *, const char *, int)
#define OMAPI_ARRAY_TYPE(name, stype)
isc_result_t omapi_addr_list_dereference(omapi_addr_list_t **, const char *, int)
isc_result_t uerr2isc(int)
isc_result_t omapi_connect(omapi_object_t *, const char *, unsigned)
isc_result_t omapi_typed_data_reference(omapi_typed_data_t **, omapi_typed_data_t *, const char *, int)
isc_result_t omapi_value_dereference(omapi_value_t **, const char *, int)
isc_result_t omapi_connection_stuff_values(omapi_object_t *c, omapi_object_t *id, omapi_object_t *m)
isc_result_t isclib_make_dst_key(char *inname, char *algorithm, unsigned char *secret, int length, dst_key_t **dstkey)
#define DHCP_HMAC_MD5_NAME
isc_result_t omapi_connection_get_value(omapi_object_t *h, omapi_object_t *id, omapi_data_string_t *name, omapi_value_t **value)
int omapi_ds_strcmp(omapi_data_string_t *, const char *)
isc_result_t omapi_connection_connect(omapi_object_t *h)
isc_result_t omapi_unregister_io_object(omapi_object_t *)
isc_result_t trace_write_packet(trace_type_t *, unsigned, const char *, const char *, int)
isc_result_t omapi_connection_signal_handler(omapi_object_t *h, const char *name, va_list ap)
isc_result_t omapi_listener_connect(omapi_connection_object_t **obj, omapi_listener_object_t *listener, int socket, struct sockaddr_in *remote_addr)
#define DHCP_R_INCOMPLETE
int omapi_connection_readfd(omapi_object_t *h)
int omapi_connection_writefd(omapi_object_t *h)
isc_result_t omapi_connection_destroy(omapi_object_t *h, const char *file, int line)
isc_result_t omapi_typed_data_dereference(omapi_typed_data_t **, const char *, int)
isc_result_t omapi_addr_list_new(omapi_addr_list_t **, unsigned, const char *, int)