1166 lines
33 KiB
C
1166 lines
33 KiB
C
/*
|
|
* Copyright (C) 1996, 1997, 1998, 1999 Internet Software Consortium.
|
|
*
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
* copyright notice and this permission notice appear in all copies.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
|
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
|
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
|
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
|
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
* SOFTWARE.
|
|
*/
|
|
|
|
/* $Id: connection.c,v 1.8 2000/01/14 23:10:01 tale Exp $ */
|
|
|
|
/* Principal Author: Ted Lemon */
|
|
|
|
/*
|
|
* Subroutines for dealing with connections.
|
|
*/
|
|
#include <config.h>
|
|
|
|
#include <errno.h>
|
|
#include <stddef.h> /* NULL */
|
|
#include <string.h> /* memset */
|
|
#include <unistd.h> /* close */
|
|
|
|
#include <isc/assertions.h>
|
|
#include <isc/error.h>
|
|
#include <isc/netdb.h>
|
|
|
|
#include <omapi/private.h>
|
|
|
|
/*
|
|
* Swiped from bin/tests/sdig.c.
|
|
*/
|
|
static isc_result_t
|
|
get_address(const char *hostname, in_port_t port, isc_sockaddr_t *sockaddr) {
|
|
struct in_addr in4;
|
|
struct in6_addr in6;
|
|
struct hostent *he;
|
|
|
|
/*
|
|
* Is this an IPv6 numeric address?
|
|
*/
|
|
if (omapi_ipv6 && inet_pton(AF_INET6, hostname, &in6) == 1)
|
|
isc_sockaddr_fromin6(sockaddr, &in6, port);
|
|
|
|
/*
|
|
* What about an IPv4 numeric address?
|
|
*/
|
|
else if (inet_pton(AF_INET, hostname, &in4) == 1)
|
|
isc_sockaddr_fromin(sockaddr, &in4, port);
|
|
|
|
else {
|
|
/*
|
|
* Look up the host name.
|
|
*/
|
|
he = gethostbyname(hostname);
|
|
if (he == NULL)
|
|
return (ISC_R_NOTFOUND);
|
|
|
|
INSIST(he->h_addrtype == AF_INET);
|
|
isc_sockaddr_fromin(sockaddr,
|
|
(struct in_addr *)(he->h_addr_list[0]),
|
|
port);
|
|
}
|
|
|
|
return (ISC_R_SUCCESS);
|
|
}
|
|
|
|
/*
|
|
* Called when there are no more events are pending on the socket.
|
|
* It can be detached and data for the connection object freed.
|
|
*/
|
|
static void
|
|
free_connection(omapi_connection_object_t *connection) {
|
|
isc_buffer_t *buffer;
|
|
|
|
/*
|
|
* The mutex is locked when this routine is called. Unlock
|
|
* it so that the isc_condition_signal below will allow
|
|
* omapi_connection_wait to be able to acquire the lock.
|
|
*/
|
|
RUNTIME_CHECK(isc_mutex_unlock(&connection->mutex) == ISC_R_SUCCESS);
|
|
|
|
while ((buffer = ISC_LIST_HEAD(connection->input_buffers)) != NULL) {
|
|
ISC_LIST_UNLINK(connection->input_buffers, buffer, link);
|
|
isc_buffer_free(&buffer);
|
|
}
|
|
|
|
while ((buffer = ISC_LIST_HEAD(connection->output_buffers)) != NULL) {
|
|
ISC_LIST_UNLINK(connection->output_buffers, buffer, link);
|
|
isc_buffer_free(&buffer);
|
|
}
|
|
|
|
if (connection->task != NULL)
|
|
isc_task_destroy(&connection->task);
|
|
|
|
if (connection->socket != NULL)
|
|
isc_socket_detach(&connection->socket);
|
|
|
|
/*
|
|
* It is possible the connection was abandoned while a message
|
|
* was being assembled. That thread needs to be unblocked.
|
|
* Make sure that when it is awakened, it exits the loop by
|
|
* setting messages_expected to 0.
|
|
*/
|
|
if (connection->is_client) {
|
|
connection->messages_expected = 0;
|
|
|
|
REQUIRE(isc_condition_signal(&connection->waiter) ==
|
|
ISC_R_SUCCESS);
|
|
RUNTIME_CHECK(isc_condition_destroy(&connection->waiter) ==
|
|
ISC_R_SUCCESS);
|
|
}
|
|
|
|
/* XXXDCL ok, but what happens when omapi_connection_wait
|
|
* awakens and then tries to unlock the mutex, which is possibly
|
|
* being destroyed right here. i don't think relocking is
|
|
* a guarantee that isc_condition_wait exited and the mutex was
|
|
* unlocked in omapi_connection_wait. still, it's worth a shot.
|
|
*/
|
|
RUNTIME_CHECK(isc_mutex_lock(&connection->mutex) == ISC_R_SUCCESS);
|
|
RUNTIME_CHECK(isc_mutex_unlock(&connection->mutex) == ISC_R_SUCCESS);
|
|
RUNTIME_CHECK(isc_mutex_destroy(&connection->mutex) == ISC_R_SUCCESS);
|
|
|
|
/*
|
|
* Disconnect from I/O object, if any.
|
|
*/
|
|
if (connection->outer != NULL)
|
|
OBJECT_DEREF(&connection->outer,
|
|
"omapi_connection_disconnect");
|
|
|
|
/*
|
|
* If whatever created us registered a signal handler, send it
|
|
* a disconnect signal.
|
|
*/
|
|
omapi_signal((omapi_object_t *)connection, "disconnect", connection);
|
|
|
|
/*
|
|
* Finally, free the object itself.
|
|
*/
|
|
OBJECT_DEREF(&connection, "free_connection");
|
|
}
|
|
|
|
static void
|
|
end_connection(omapi_connection_object_t *connection, isc_event_t *event,
|
|
isc_result_t result)
|
|
{
|
|
if (event != NULL)
|
|
isc_event_free(&event);
|
|
|
|
/*
|
|
* XXXDCL would be nice to send the result as an
|
|
* omapi_signal(object, "status", result) but i don't
|
|
* think this can be done with the connection as the object.
|
|
*/
|
|
|
|
/*
|
|
* Lock the connection's mutex to examine connection->events_pending.
|
|
*/
|
|
RUNTIME_CHECK(isc_mutex_lock(&connection->mutex) == ISC_R_SUCCESS);
|
|
|
|
fprintf(stderr, "END_CONNECTION, %d events_pending\n",
|
|
connection->events_pending);
|
|
|
|
if (connection->events_pending == 0) {
|
|
free_connection(connection);
|
|
return;
|
|
}
|
|
|
|
RUNTIME_CHECK(isc_mutex_unlock(&connection->mutex) ==
|
|
ISC_R_SUCCESS);
|
|
|
|
/*
|
|
* There are events pending. Cancel them, and each will generate
|
|
* a call with ISC_R_CANCELED to this routine until finally
|
|
* events_pending is 0 and the connection is freed. The
|
|
* only time ISC_R_CANCELED should be generated is after this
|
|
* function already called isc_socket_cancel, because it is
|
|
* the only place in the omapi library that isc_socket_cancel is used.
|
|
*/
|
|
|
|
if (result != ISC_R_CANCELED)
|
|
isc_socket_cancel(connection->socket, NULL,
|
|
ISC_SOCKCANCEL_ALL);
|
|
}
|
|
|
|
/*
|
|
* This is the function that is called when a connect event is posted on
|
|
* the socket as a result of isc_socket_connect.
|
|
*/
|
|
static void
|
|
connect_done(isc_task_t *task, isc_event_t *event) {
|
|
isc_result_t result;
|
|
isc_socket_t *socket;
|
|
isc_socket_connev_t *connectevent;
|
|
omapi_connection_object_t *connection;
|
|
|
|
socket = event->sender;
|
|
connectevent = (isc_socket_connev_t *)event;
|
|
connection = event->arg;
|
|
|
|
fprintf(stderr, "CONNECT_DONE\n");
|
|
|
|
ENSURE(socket == connection->socket && task == connection->task);
|
|
|
|
RUNTIME_CHECK(isc_mutex_lock(&connection->mutex) == ISC_R_SUCCESS);
|
|
/*
|
|
* XXXDCL I may have made an unwarranted assumption about
|
|
* events_pending becoming 0 on the client when disconnecting
|
|
* only in recv_done. I'm concerned that there might be some
|
|
* sort of logic window, however small, where that isn't true.
|
|
*/
|
|
ENSURE(connection->events_pending > 0);
|
|
if (--connection->events_pending == 0 && connection->is_client &&
|
|
connection->state == omapi_connection_disconnecting)
|
|
FATAL_ERROR(__FILE__, __LINE__,
|
|
"events_pending == 0 in connect_done while "
|
|
"disconnecting, this should not happen!");
|
|
RUNTIME_CHECK(isc_mutex_unlock(&connection->mutex) == ISC_R_SUCCESS);
|
|
|
|
/*
|
|
* XXXDCL For some reason, a "connection refused" error is not
|
|
* being indicated here when it would be expected. I wonder
|
|
* how that error is indicated.
|
|
*/
|
|
|
|
if (connectevent->result != ISC_R_SUCCESS)
|
|
goto abandon;
|
|
|
|
result = isc_socket_getpeername(connection->socket,
|
|
&connection->remote_addr);
|
|
if (result != ISC_R_SUCCESS)
|
|
goto abandon;
|
|
|
|
result = isc_socket_getsockname(connection->socket,
|
|
&connection->local_addr);
|
|
if (result != ISC_R_SUCCESS)
|
|
goto abandon;
|
|
|
|
connection->state = omapi_connection_connected;
|
|
|
|
isc_event_free(&event);
|
|
|
|
return;
|
|
|
|
abandon:
|
|
end_connection(connection, event, connectevent->result);
|
|
return;
|
|
}
|
|
|
|
/*
|
|
* This is the function that is called when a recv event is posted on
|
|
* the socket as a result of isc_socket_recv*.
|
|
*/
|
|
static void
|
|
recv_done(isc_task_t *task, isc_event_t *event) {
|
|
isc_buffer_t *buffer;
|
|
isc_socket_t *socket;
|
|
isc_socketevent_t *socketevent;
|
|
omapi_connection_object_t *connection;
|
|
unsigned int original_bytes_needed;
|
|
|
|
socket = event->sender;
|
|
socketevent = (isc_socketevent_t *)event;
|
|
connection = event->arg;
|
|
|
|
fprintf(stderr, "RECV_DONE, %d bytes\n", socketevent->n);
|
|
|
|
ENSURE(socket == connection->socket && task == connection->task);
|
|
|
|
RUNTIME_CHECK(isc_mutex_lock(&connection->mutex) == ISC_R_SUCCESS);
|
|
ENSURE(connection->events_pending > 0);
|
|
connection->events_pending--;
|
|
RUNTIME_CHECK(isc_mutex_unlock(&connection->mutex) == ISC_R_SUCCESS);
|
|
|
|
/*
|
|
* Restore the input buffers to the connection object.
|
|
*/
|
|
for (buffer = ISC_LIST_HEAD(socketevent->bufferlist);
|
|
buffer != NULL;
|
|
buffer = ISC_LIST_NEXT(buffer, link))
|
|
ISC_LIST_APPEND(connection->input_buffers, buffer, link);
|
|
|
|
if (socketevent->result != ISC_R_SUCCESS)
|
|
goto abandon;
|
|
|
|
connection->in_bytes += socketevent->n;
|
|
|
|
original_bytes_needed = connection->bytes_needed;
|
|
|
|
while (connection->bytes_needed <= connection->in_bytes &&
|
|
connection->bytes_needed > 0)
|
|
if (omapi_signal((omapi_object_t *)connection, "ready",
|
|
connection) !=
|
|
ISC_R_SUCCESS)
|
|
goto abandon;
|
|
|
|
/*
|
|
* Queue up another recv request. If the bufferlist is empty,
|
|
* then, something under omapi_signal already called
|
|
* omapi_connection_require and queued the recv (which is
|
|
* what emptied the bufferlist).
|
|
*/
|
|
if (! ISC_LIST_EMPTY(connection->input_buffers))
|
|
omapi_connection_require((omapi_object_t *)connection, 0);
|
|
|
|
/*
|
|
* See if that was the last event the client was expecting, so
|
|
* that the connection can be freed.
|
|
* XXXDCL I don't *think* this has to be done in the
|
|
* send_done or connect_done handlers, because a normal
|
|
* termination will only happen
|
|
*/
|
|
if (connection->is_client) {
|
|
RUNTIME_CHECK(isc_mutex_lock(&connection->mutex) ==
|
|
ISC_R_SUCCESS);
|
|
if (connection->events_pending == 0 &&
|
|
connection->state == omapi_connection_disconnecting) {
|
|
ENSURE(connection->messages_expected == 1);
|
|
|
|
RUNTIME_CHECK(isc_mutex_unlock(&connection->mutex) ==
|
|
ISC_R_SUCCESS);
|
|
|
|
end_connection(connection, event, ISC_R_SUCCESS);
|
|
return;
|
|
}
|
|
RUNTIME_CHECK(isc_mutex_unlock(&connection->mutex) ==
|
|
ISC_R_SUCCESS);
|
|
}
|
|
|
|
isc_event_free(&event);
|
|
return;
|
|
|
|
abandon:
|
|
end_connection(connection, event, socketevent->result);
|
|
return;
|
|
}
|
|
|
|
/*
|
|
* This is the function that is called when a send event is posted on
|
|
* the socket as a result of isc_socket_send*.
|
|
*/
|
|
static void
|
|
send_done(isc_task_t *task, isc_event_t *event) {
|
|
isc_buffer_t *buffer;
|
|
isc_socket_t *socket;
|
|
isc_socketevent_t *socketevent;
|
|
omapi_connection_object_t *connection;
|
|
|
|
socket = event->sender;
|
|
socketevent = (isc_socketevent_t *)event;
|
|
connection = event->arg;
|
|
|
|
fprintf(stderr, "SEND_DONE, %d bytes\n", socketevent->n);
|
|
|
|
ENSURE(socket == connection->socket && task == connection->task);
|
|
|
|
/*
|
|
* XXXDCL I am assuming that partial writes are not done. I hope this
|
|
* does not prove to be incorrect. But the assumption can be tested ...
|
|
*/
|
|
ENSURE(socketevent->n == connection->out_bytes &&
|
|
socketevent->n ==
|
|
isc_bufferlist_usedcount(&socketevent->bufferlist));
|
|
|
|
RUNTIME_CHECK(isc_mutex_lock(&connection->mutex) == ISC_R_SUCCESS);
|
|
/*
|
|
* XXXDCL I may have made an unwarranted assumption about
|
|
* events_pending becoming 0 on the client when disconnecting
|
|
* only in recv_done. I'm concerned that there might be some
|
|
* sort of logic window, however small, where that isn't true.
|
|
*/
|
|
ENSURE(connection->events_pending > 0);
|
|
if (--connection->events_pending == 0 && connection->is_client &&
|
|
connection->state == omapi_connection_disconnecting)
|
|
FATAL_ERROR(__FILE__, __LINE__,
|
|
"events_pending == 0 in send_done while "
|
|
"disconnecting, this should not happen!");
|
|
RUNTIME_CHECK(isc_mutex_unlock(&connection->mutex) == ISC_R_SUCCESS);
|
|
|
|
/*
|
|
* Restore the head of bufferlist into the connection object, resetting
|
|
* it to have zero used space, and free the remaining buffers.
|
|
* This is done before the test of the socketevent's result so that
|
|
* end_connection() can free the buffer, if it is called below.
|
|
*/
|
|
buffer = ISC_LIST_HEAD(socketevent->bufferlist);
|
|
ISC_LIST_APPEND(connection->output_buffers, buffer, link);
|
|
isc_buffer_clear(buffer);
|
|
|
|
while ((buffer = ISC_LIST_NEXT(buffer, link)) != NULL) {
|
|
ISC_LIST_UNLINK(socketevent->bufferlist, buffer, link);
|
|
isc_buffer_free(&buffer);
|
|
}
|
|
|
|
if (socketevent->result != ISC_R_SUCCESS)
|
|
goto abandon;
|
|
|
|
connection->out_bytes -= socketevent->n;
|
|
|
|
isc_event_free(&event);
|
|
return;
|
|
|
|
abandon:
|
|
end_connection(connection, event, socketevent->result);
|
|
return;
|
|
}
|
|
|
|
void
|
|
connection_send(omapi_connection_object_t *connection) {
|
|
REQUIRE(connection != NULL &&
|
|
connection->type == omapi_type_connection);
|
|
|
|
REQUIRE(connection->state == omapi_connection_connected);
|
|
|
|
if (connection->out_bytes > 0) {
|
|
ENSURE(!ISC_LIST_EMPTY(connection->output_buffers));
|
|
|
|
RUNTIME_CHECK(isc_mutex_lock(&connection->mutex) ==
|
|
ISC_R_SUCCESS);
|
|
connection->events_pending++;
|
|
RUNTIME_CHECK(isc_mutex_unlock(&connection->mutex) ==
|
|
ISC_R_SUCCESS);
|
|
|
|
isc_socket_sendv(connection->socket,
|
|
&connection->output_buffers, connection->task,
|
|
send_done, connection);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Make an outgoing connection to an OMAPI server.
|
|
*/
|
|
isc_result_t
|
|
omapi_connection_toserver(omapi_object_t *protocol, const char *server_name,
|
|
int port)
|
|
{
|
|
isc_result_t result;
|
|
isc_sockaddr_t sockaddr;
|
|
isc_buffer_t *ibuffer = NULL, *obuffer = NULL;
|
|
isc_task_t *task = NULL;
|
|
omapi_connection_object_t *connection = NULL;
|
|
|
|
result = get_address(server_name, port, &sockaddr);
|
|
if (result != ISC_R_SUCCESS)
|
|
return (result);
|
|
|
|
/* XXXDCL Make cleanup better */
|
|
/*
|
|
* Prepare the task that will wait for the connection to be made.
|
|
*/
|
|
result = isc_task_create(omapi_taskmgr, NULL, 0, &task);
|
|
if (result != ISC_R_SUCCESS)
|
|
return (result);
|
|
|
|
result = isc_buffer_allocate(omapi_mctx, &ibuffer, OMAPI_BUFFER_SIZE,
|
|
ISC_BUFFERTYPE_BINARY);
|
|
if (result != ISC_R_SUCCESS)
|
|
goto free_task;
|
|
|
|
result = isc_buffer_allocate(omapi_mctx, &obuffer, OMAPI_BUFFER_SIZE,
|
|
ISC_BUFFERTYPE_BINARY);
|
|
if (result != ISC_R_SUCCESS)
|
|
goto free_ibuffer;
|
|
|
|
/*
|
|
* Create a new connection object.
|
|
*/
|
|
result = omapi_object_new((omapi_object_t **)&connection,
|
|
omapi_type_connection, sizeof(*connection));
|
|
if (result != ISC_R_SUCCESS)
|
|
goto free_obuffer;
|
|
|
|
connection->is_client = ISC_TRUE;
|
|
|
|
connection->task = task;
|
|
|
|
ISC_LIST_INIT(connection->input_buffers);
|
|
ISC_LIST_APPEND(connection->input_buffers, ibuffer, link);
|
|
ISC_LIST_INIT(connection->output_buffers);
|
|
ISC_LIST_APPEND(connection->output_buffers, obuffer, link);
|
|
|
|
RUNTIME_CHECK(isc_mutex_init(&connection->mutex) == ISC_R_SUCCESS);
|
|
RUNTIME_CHECK(isc_condition_init(&connection->waiter) ==
|
|
ISC_R_SUCCESS);
|
|
|
|
/*
|
|
* An introductory message is expected from the server.
|
|
* It is not necessary to lock the mutex here because there
|
|
* will be no recv() tasks that could possibly compete for the
|
|
* messages_expected variable, since isc_socket_create has
|
|
* not even been called yet.
|
|
*/
|
|
connection->messages_expected = 1;
|
|
|
|
/*
|
|
* Tie the new connection object to the protocol object.
|
|
*/
|
|
OBJECT_REF(&protocol->outer, connection, "omapi_connection_toserver");
|
|
OBJECT_REF(&connection->inner, protocol, "omapi_connection_toserver");
|
|
|
|
/*
|
|
* Create a socket on which to communicate.
|
|
*/
|
|
result = isc_socket_create(omapi_socketmgr, isc_sockaddr_pf(&sockaddr),
|
|
isc_sockettype_tcp, &connection->socket);
|
|
if (result != ISC_R_SUCCESS)
|
|
goto free_object;
|
|
|
|
#if 0
|
|
/*
|
|
* Set the SO_REUSEADDR flag (this should not fail).
|
|
* XXXDCL is this needed? isc_socket_* does not support it.
|
|
*/
|
|
flag = 1;
|
|
if (setsockopt(connection->socket, SOL_SOCKET, SO_REUSEADDR,
|
|
(char *)&flag, sizeof(flag)) < 0) {
|
|
OBJECT_DEREF(&connection, "omapi_connect");
|
|
return (ISC_R_UNEXPECTED);
|
|
}
|
|
#endif
|
|
|
|
RUNTIME_CHECK(isc_mutex_lock(&connection->mutex) == ISC_R_SUCCESS);
|
|
connection->events_pending++;
|
|
RUNTIME_CHECK(isc_mutex_unlock(&connection->mutex) == ISC_R_SUCCESS);
|
|
|
|
result = isc_socket_connect(connection->socket, &sockaddr, task,
|
|
connect_done, connection);
|
|
if (result != ISC_R_SUCCESS) {
|
|
end_connection(connection, NULL, result);
|
|
return (result);
|
|
}
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
free_object:
|
|
OBJECT_DEREF(&connection, "omapi_connection_toserver");
|
|
OBJECT_DEREF(&protocol->outer, "omapi_connection_toserver");
|
|
return (result);
|
|
|
|
free_obuffer:
|
|
isc_buffer_free(&obuffer);
|
|
free_ibuffer:
|
|
isc_buffer_free(&ibuffer);
|
|
free_task:
|
|
isc_task_destroy(&task);
|
|
|
|
return (result);
|
|
}
|
|
|
|
/*
|
|
* Put some bytes into the output buffer for a connection.
|
|
*/
|
|
isc_result_t
|
|
omapi_connection_copyin(omapi_object_t *generic, unsigned char *src,
|
|
unsigned int len)
|
|
{
|
|
omapi_connection_object_t *connection;
|
|
isc_buffer_t *buffer;
|
|
isc_bufferlist_t bufferlist;
|
|
isc_result_t result;
|
|
unsigned int space_available;
|
|
|
|
REQUIRE(generic != NULL && generic->type == omapi_type_connection);
|
|
|
|
connection = (omapi_connection_object_t *)generic;
|
|
|
|
/*
|
|
* Check for enough space in the output buffers.
|
|
*/
|
|
bufferlist = connection->output_buffers;
|
|
space_available = isc_bufferlist_availablecount(&bufferlist);
|
|
|
|
while (space_available < len) {
|
|
/*
|
|
* Add new buffers until there is sufficient space.
|
|
*/
|
|
buffer = NULL;
|
|
result = isc_buffer_allocate(omapi_mctx, &buffer,
|
|
OMAPI_BUFFER_SIZE,
|
|
ISC_BUFFERTYPE_BINARY);
|
|
if (result != ISC_R_SUCCESS)
|
|
return (result);
|
|
|
|
space_available += OMAPI_BUFFER_SIZE;
|
|
ISC_LIST_APPEND(bufferlist, buffer, link);
|
|
}
|
|
|
|
/*
|
|
* XXXDCL out_bytes hardly seems needed as it is easy to get a
|
|
* total of how much data is in the output buffers.
|
|
*/
|
|
connection->out_bytes += len;
|
|
|
|
/*
|
|
* Copy the data into the buffers, splitting across buffers
|
|
* as necessary.
|
|
*/
|
|
for (buffer = ISC_LIST_HEAD(bufferlist); len > 0;
|
|
buffer = ISC_LIST_NEXT(buffer, link)) {
|
|
|
|
space_available = ISC_BUFFER_AVAILABLECOUNT(buffer);
|
|
if (space_available > len)
|
|
space_available = len;
|
|
|
|
isc_buffer_putmem(buffer, src, space_available);
|
|
|
|
src += space_available;
|
|
len -= space_available;
|
|
}
|
|
|
|
return (ISC_R_SUCCESS);
|
|
}
|
|
|
|
/*
|
|
* Copy some bytes from the input buffer, and advance the input buffer
|
|
* pointer beyond the bytes copied out.
|
|
*/
|
|
isc_result_t
|
|
omapi_connection_copyout(unsigned char *dst, omapi_object_t *generic,
|
|
unsigned int size)
|
|
{
|
|
omapi_connection_object_t *connection;
|
|
isc_buffer_t *buffer;
|
|
unsigned int copy_bytes;
|
|
|
|
REQUIRE(generic != NULL && generic->type == omapi_type_connection);
|
|
|
|
connection = (omapi_connection_object_t *)generic;
|
|
|
|
if (size > connection->in_bytes)
|
|
return (ISC_R_NOMORE);
|
|
|
|
connection->bytes_needed -= size;
|
|
|
|
buffer = ISC_LIST_HEAD(connection->input_buffers);
|
|
|
|
/*
|
|
* The data could potentially be split across multiple buffers,
|
|
* so rather than a simple memcpy, a loop is needed.
|
|
*/
|
|
while (size > 0) {
|
|
copy_bytes = buffer->used - buffer->current;
|
|
if (copy_bytes > size)
|
|
copy_bytes = size;
|
|
|
|
/*
|
|
* When dst == NULL, this function is being used to skip
|
|
* over uninteresting input.
|
|
*/
|
|
if (dst != NULL)
|
|
(void)memcpy(dst, buffer->base + buffer->current,
|
|
copy_bytes);
|
|
|
|
isc_buffer_forward(buffer, copy_bytes);
|
|
|
|
size -= copy_bytes;
|
|
connection->in_bytes -= copy_bytes;
|
|
|
|
buffer = ISC_LIST_NEXT(buffer, link);
|
|
}
|
|
|
|
return (ISC_R_SUCCESS);
|
|
}
|
|
|
|
/*
|
|
* Disconnect a connection object from the remote end. If force is true,
|
|
* close the connection immediately. Otherwise, shut down the receiving end
|
|
* but allow any unsent data to be sent before actually closing the socket.
|
|
*
|
|
* This routine is called in the following situations:
|
|
*
|
|
* The client wants to exit normally after all its transactions are
|
|
* processed. Closing the connection causes an ISC_R_EOF event result
|
|
* to be given to the server's recv_done, which then causes the
|
|
* server's recv_done to close its side of the connection.
|
|
*
|
|
* The client got some sort of error it could not handle gracefully, so
|
|
* it wants to just tear down the connection. This can be caused either
|
|
* internally in the omapi library, or by the calling program.
|
|
*
|
|
* The server is dropping the connection. This is always asynchronous;
|
|
* the server will never block waiting for a connection to be completed
|
|
* because it never initiates a "normal" close of the connection.
|
|
* (Receipt of ISC_R_EOF is always treated as though it were an error,
|
|
* no matter what the client had been intending; it's the nature of
|
|
* the protocol.)
|
|
*
|
|
* The client might or might not want to block on the disconnection.
|
|
* Also, if the error is being thrown from the library, the client
|
|
* might *already* be waiting on (or intending to wait on) whatever messages
|
|
* it has already sent, so it needs to be awakened. That will be handled
|
|
* by free_connection after all of the cancelled events are processed.
|
|
*/
|
|
void
|
|
omapi_connection_disconnect(omapi_object_t *generic, isc_boolean_t force) {
|
|
omapi_connection_object_t *connection;
|
|
|
|
REQUIRE(generic != NULL);
|
|
|
|
connection = (omapi_connection_object_t *)generic;
|
|
|
|
REQUIRE(connection->type == omapi_type_connection);
|
|
|
|
/*
|
|
* Only the client can request an unforced disconnection. The server's
|
|
* disconnection will always happen when the client goes away.
|
|
* XXXDCL ... hmm, can timeouts of the client on the server be handled?
|
|
*/
|
|
REQUIRE(force || connection->is_client);
|
|
|
|
/*
|
|
* XXXDCL this has to be fixed up when isc_socket_shutdown is
|
|
* available, because then the shutdown can be done asynchronously.
|
|
* It is currently done synchronously.
|
|
*/
|
|
|
|
if (! force) {
|
|
/*
|
|
* Client wants a clean disconnect.
|
|
*
|
|
* Increment the count of messages expected. Even though
|
|
* no message is really expected, this will keep
|
|
* omapi_connection_wait from exiting until free_connection()
|
|
* signals it.
|
|
*/
|
|
RUNTIME_CHECK(isc_mutex_lock(&connection->mutex) ==
|
|
ISC_R_SUCCESS);
|
|
|
|
connection->state = omapi_connection_disconnecting;
|
|
connection->messages_expected++;
|
|
|
|
/*
|
|
* If there are no other messages expected for the socket,
|
|
* the end_connection can be done right now. Otherwise,
|
|
* when recv_done gets the last output from the server,
|
|
* then it will then end the connection.
|
|
*/
|
|
if (connection->messages_expected > 1) {
|
|
RUNTIME_CHECK(isc_mutex_unlock(&connection->mutex) ==
|
|
ISC_R_SUCCESS);
|
|
return;
|
|
}
|
|
/*
|
|
* ... else fall through.
|
|
*/
|
|
RUNTIME_CHECK(isc_mutex_unlock(&connection->mutex) ==
|
|
ISC_R_SUCCESS);
|
|
}
|
|
|
|
/*
|
|
* XXXDCL
|
|
* This might be improved if the 'force' argument to this function
|
|
* were instead an isc_reault_t argument. Then omapi_signal could send
|
|
* a "status" back up to a signal handler that could set a waitresult.
|
|
*/
|
|
end_connection(connection, NULL,
|
|
force ? ISC_R_UNEXPECTED : ISC_R_SUCCESS);
|
|
}
|
|
|
|
/*
|
|
* The caller wants a specific amount of bytes to be read. Queue up a
|
|
* recv for the socket.
|
|
*/
|
|
isc_result_t
|
|
omapi_connection_require(omapi_object_t *generic, unsigned int bytes) {
|
|
omapi_connection_object_t *connection;
|
|
|
|
REQUIRE(generic != NULL && generic->type == omapi_type_connection);
|
|
|
|
connection = (omapi_connection_object_t *)generic;
|
|
|
|
/*
|
|
* There is a race condition here that is really, REALLY
|
|
* irritating me. Here's the latest attempt at fixing the
|
|
* whole dang disconnect problem. It is doomed to fail ...
|
|
* but maybe it will do the job for now.
|
|
* XXXDCL
|
|
*/
|
|
RUNTIME_CHECK(isc_mutex_lock(&connection->mutex) == ISC_R_SUCCESS);
|
|
if (connection->state == omapi_connection_disconnecting &&
|
|
connection->messages_expected == 1) {
|
|
RUNTIME_CHECK(isc_mutex_unlock(&connection->mutex) ==
|
|
ISC_R_SUCCESS);
|
|
return (OMAPI_R_NOTYET);
|
|
}
|
|
RUNTIME_CHECK(isc_mutex_unlock(&connection->mutex) == ISC_R_SUCCESS);
|
|
|
|
ENSURE(connection->state == omapi_connection_connected ||
|
|
connection->state == omapi_connection_disconnecting);
|
|
|
|
connection->bytes_needed += bytes;
|
|
|
|
if (connection->bytes_needed <= connection->in_bytes)
|
|
return (ISC_R_SUCCESS);
|
|
|
|
if (connection->bytes_needed >
|
|
isc_bufferlist_availablecount(&connection->input_buffers)) {
|
|
/*
|
|
* Not enough space to put the required volume of information.
|
|
* See if the space can be attained by getting rid of the
|
|
* used buffer space.
|
|
*
|
|
* This could be made more efficient by not freeing
|
|
* the completely used buffers, but honestly the free/allocate
|
|
* code will probably *never* be used in practice; to even test
|
|
* the free/allocate stuff OMAPI_BUFFER_SIZE has to be set to
|
|
* an absurdly low value (like 4).
|
|
*/
|
|
isc_bufferlist_t bufferlist = connection->input_buffers;
|
|
isc_buffer_t *buffer;
|
|
isc_result_t result;
|
|
|
|
buffer = ISC_LIST_HEAD(bufferlist);
|
|
|
|
/*
|
|
* Lop off any completely used buffers, except the last one.
|
|
*/
|
|
while (ISC_BUFFER_AVAILABLECOUNT(buffer) == 0 &&
|
|
buffer != ISC_LIST_TAIL(bufferlist)) {
|
|
|
|
ISC_LIST_UNLINK(bufferlist, buffer, link);
|
|
isc_buffer_free(&buffer);
|
|
|
|
buffer = ISC_LIST_HEAD(bufferlist);
|
|
}
|
|
|
|
/*
|
|
* Reclaim any used space. (Any buffers after this one,
|
|
* if they exist at all, will be empty.)
|
|
*/
|
|
isc_buffer_compact(buffer);
|
|
|
|
/*
|
|
* Create as many new buffers as necessary to fit the
|
|
* entire size requirement.
|
|
*/
|
|
while (connection->bytes_needed >
|
|
isc_bufferlist_availablecount(&bufferlist)) {
|
|
|
|
buffer = NULL;
|
|
result = isc_buffer_allocate(omapi_mctx, &buffer,
|
|
OMAPI_BUFFER_SIZE,
|
|
ISC_BUFFERTYPE_BINARY);
|
|
if (result != ISC_R_SUCCESS)
|
|
return (result);
|
|
|
|
ISC_LIST_APPEND(bufferlist, buffer, link);
|
|
}
|
|
}
|
|
|
|
RUNTIME_CHECK(isc_mutex_lock(&connection->mutex) == ISC_R_SUCCESS);
|
|
/*
|
|
* Don't queue any more read requests if the connection is
|
|
* disconnecting and no more messages are expected. This situation
|
|
* can happen when omapi_connection_disconnect is called before
|
|
* omapi_protocol_signal_handler has had a chance to go back to
|
|
* its omapi_protocol_header_wait condition where it calls
|
|
* omapi_connection_require. It is possible the isc_socket_cancel
|
|
* was already done.
|
|
*/
|
|
if (connection->state == omapi_connection_disconnecting &&
|
|
connection->messages_expected == 0) {
|
|
/*
|
|
* Test the above comment's claim that this only happens
|
|
* when requesting to wait for the header of the next message.
|
|
* XXXDCL
|
|
* (omapi_protocol_object_t *)p->inner->header_size should
|
|
* be what is checked against, but at the present time
|
|
* the protocol_object_t exists only in protocol.c
|
|
*/
|
|
ENSURE(bytes == 24);
|
|
} else {
|
|
/*
|
|
* Queue the receive task.
|
|
* XXXDCL The "minimum" arg has not been fully thought out.
|
|
*/
|
|
connection->events_pending++;
|
|
isc_socket_recvv(connection->socket,
|
|
&connection->input_buffers,
|
|
(connection->bytes_needed -
|
|
connection->in_bytes),
|
|
connection->task, recv_done, connection);
|
|
}
|
|
|
|
RUNTIME_CHECK(isc_mutex_unlock(&connection->mutex) ==
|
|
ISC_R_SUCCESS);
|
|
|
|
return (OMAPI_R_NOTYET);
|
|
}
|
|
|
|
/*
|
|
* This function is meant to pause the client until it has received
|
|
* a message from the server, either the introductory message or a response
|
|
* to a message it has sent. Because the socket library is multithreaded,
|
|
* those events can happen before omapi_connection_wait is ever called.
|
|
* So a counter needs to be set for every expected message, and this
|
|
* function can only return when that counter is 0.
|
|
*/
|
|
isc_result_t
|
|
omapi_connection_wait(omapi_object_t *object,
|
|
omapi_object_t *connection_handle,
|
|
isc_time_t *timeout)
|
|
{
|
|
/*
|
|
* XXXDCL 'object' is not really used.
|
|
*/
|
|
omapi_connection_object_t *connection;
|
|
isc_result_t result, wait_result;
|
|
|
|
REQUIRE(object != NULL && connection_handle != NULL);
|
|
REQUIRE(connection_handle->type == omapi_type_connection);
|
|
|
|
connection = (omapi_connection_object_t *)connection_handle;
|
|
/*
|
|
* This routine is not valid for server connections.
|
|
*/
|
|
REQUIRE(connection->is_client);
|
|
|
|
result = isc_mutex_lock(&connection->mutex);
|
|
if (result != ISC_R_SUCCESS)
|
|
return (result);
|
|
|
|
wait_result = ISC_R_SUCCESS;
|
|
|
|
while (connection->messages_expected > 0 &&
|
|
wait_result == ISC_R_SUCCESS)
|
|
|
|
if (timeout == NULL)
|
|
wait_result = isc_condition_wait(&connection->waiter,
|
|
&connection->mutex);
|
|
else
|
|
wait_result =
|
|
isc_condition_waituntil(&connection->waiter,
|
|
&connection->mutex,
|
|
timeout);
|
|
|
|
RUNTIME_CHECK(wait_result == ISC_R_SUCCESS ||
|
|
wait_result == ISC_R_TIMEDOUT);
|
|
|
|
RUNTIME_CHECK(isc_mutex_unlock(&connection->mutex) == ISC_R_SUCCESS);
|
|
|
|
return (wait_result);
|
|
}
|
|
|
|
isc_result_t
|
|
omapi_connection_setvalue(omapi_object_t *connection, omapi_object_t *id,
|
|
omapi_data_string_t *name, omapi_typed_data_t *value)
|
|
{
|
|
REQUIRE(connection != NULL &&
|
|
connection->type == omapi_type_connection);
|
|
|
|
PASS_SETVALUE(connection);
|
|
}
|
|
|
|
isc_result_t
|
|
omapi_connection_getvalue(omapi_object_t *connection, omapi_object_t *id,
|
|
omapi_data_string_t *name, omapi_value_t **value)
|
|
{
|
|
REQUIRE(connection != NULL &&
|
|
connection->type == omapi_type_connection);
|
|
|
|
PASS_GETVALUE(connection);
|
|
}
|
|
|
|
void
|
|
omapi_connection_destroy(omapi_object_t *handle, const char *name) {
|
|
omapi_connection_object_t *connection;
|
|
|
|
REQUIRE(handle != NULL && handle->type == omapi_type_connection);
|
|
|
|
(void)name;
|
|
|
|
connection = (omapi_connection_object_t *)handle;
|
|
|
|
if (connection->state == omapi_connection_connected)
|
|
omapi_connection_disconnect(handle, OMAPI_FORCE_DISCONNECT);
|
|
}
|
|
|
|
isc_result_t
|
|
omapi_connection_signalhandler(omapi_object_t *connection, const char *name,
|
|
va_list ap)
|
|
{
|
|
REQUIRE(connection != NULL &&
|
|
connection->type == omapi_type_connection);
|
|
|
|
PASS_SIGNAL(connection);
|
|
}
|
|
|
|
/*
|
|
* Write all the published values associated with the object through the
|
|
* specified connection.
|
|
*/
|
|
isc_result_t
|
|
omapi_connection_stuffvalues(omapi_object_t *connection, omapi_object_t *id,
|
|
omapi_object_t *handle)
|
|
{
|
|
REQUIRE(connection != NULL &&
|
|
connection->type == omapi_type_connection);
|
|
|
|
PASS_STUFFVALUES(handle);
|
|
}
|
|
|
|
/*
|
|
* XXXDCL These could potentially use the isc_buffer_* integer functions
|
|
*/
|
|
isc_result_t
|
|
omapi_connection_getuint32(omapi_object_t *c, isc_uint32_t *value) {
|
|
isc_uint32_t inbuf;
|
|
isc_result_t result;
|
|
|
|
result = omapi_connection_copyout((unsigned char *)&inbuf, c,
|
|
sizeof(inbuf));
|
|
if (result != ISC_R_SUCCESS)
|
|
return (result);
|
|
|
|
*value = ntohl(inbuf);
|
|
return (ISC_R_SUCCESS);
|
|
}
|
|
|
|
isc_result_t
|
|
omapi_connection_putuint32(omapi_object_t *c, isc_uint32_t value) {
|
|
isc_uint32_t inbuf;
|
|
|
|
inbuf = htonl(value);
|
|
|
|
return (omapi_connection_copyin(c, (unsigned char *)&inbuf,
|
|
sizeof(inbuf)));
|
|
}
|
|
|
|
isc_result_t
|
|
omapi_connection_getuint16(omapi_object_t *c, isc_uint16_t *value) {
|
|
isc_uint16_t inbuf;
|
|
isc_result_t result;
|
|
|
|
result = omapi_connection_copyout((unsigned char *)&inbuf, c,
|
|
sizeof(inbuf));
|
|
if (result != ISC_R_SUCCESS)
|
|
return (result);
|
|
|
|
*value = ntohs(inbuf);
|
|
return (ISC_R_SUCCESS);
|
|
}
|
|
|
|
isc_result_t
|
|
omapi_connection_putuint16(omapi_object_t *c, isc_uint32_t value) {
|
|
isc_uint16_t inbuf;
|
|
|
|
REQUIRE(value < 65536);
|
|
|
|
inbuf = htons((isc_uint16_t)value);
|
|
|
|
return (omapi_connection_copyin(c, (unsigned char *)&inbuf,
|
|
sizeof(inbuf)));
|
|
}
|
|
|
|
isc_result_t
|
|
omapi_connection_puttypeddata(omapi_object_t *c, omapi_typed_data_t *data)
|
|
{
|
|
isc_result_t result;
|
|
omapi_handle_t handle;
|
|
|
|
REQUIRE(data != NULL &&
|
|
(data->type == omapi_datatype_int ||
|
|
data->type == omapi_datatype_string ||
|
|
data->type == omapi_datatype_data ||
|
|
data->type == omapi_datatype_object));
|
|
|
|
switch (data->type) {
|
|
case omapi_datatype_int:
|
|
result = omapi_connection_putuint32(c, sizeof(isc_uint32_t));
|
|
if (result != ISC_R_SUCCESS)
|
|
return (result);
|
|
return (omapi_connection_putuint32(c, ((isc_uint32_t)
|
|
(data->u.integer))));
|
|
|
|
case omapi_datatype_string:
|
|
case omapi_datatype_data:
|
|
result = omapi_connection_putuint32(c, data->u.buffer.len);
|
|
if (result != ISC_R_SUCCESS)
|
|
return (result);
|
|
if (data->u.buffer.len > 0)
|
|
return (omapi_connection_copyin(c,
|
|
data->u.buffer.value,
|
|
data->u.buffer.len));
|
|
return (ISC_R_SUCCESS);
|
|
|
|
case omapi_datatype_object:
|
|
if (data->u.object != NULL) {
|
|
result = omapi_object_handle(&handle, data->u.object);
|
|
if (result != ISC_R_SUCCESS)
|
|
return (result);
|
|
} else
|
|
handle = 0;
|
|
result = omapi_connection_putuint32(c, sizeof(handle));
|
|
if (result != ISC_R_SUCCESS)
|
|
return (result);
|
|
return (omapi_connection_putuint32(c, handle));
|
|
}
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
"unknown type in omapi_connection_puttypeddata: "
|
|
"%d\n", data->type);
|
|
return (ISC_R_UNEXPECTED);
|
|
}
|
|
|
|
isc_result_t
|
|
omapi_connection_putname(omapi_object_t *c, const char *name) {
|
|
isc_result_t result;
|
|
unsigned int len = strlen(name);
|
|
|
|
if (len > 65535)
|
|
/* XXXDCL better error? */
|
|
return (ISC_R_FAILURE);
|
|
|
|
result = omapi_connection_putuint16(c, len);
|
|
if (result != ISC_R_SUCCESS)
|
|
return (result);
|
|
|
|
return (omapi_connection_copyin(c, (char *)name, len));
|
|
}
|
|
|
|
isc_result_t
|
|
omapi_connection_putstring(omapi_object_t *c, const char *string) {
|
|
isc_result_t result;
|
|
unsigned int len;
|
|
|
|
if (string != NULL)
|
|
len = strlen(string);
|
|
else
|
|
len = 0;
|
|
|
|
result = omapi_connection_putuint32(c, len);
|
|
|
|
if (result == ISC_R_SUCCESS && len > 0)
|
|
result = omapi_connection_copyin(c, (char *)string, len);
|
|
return (result);
|
|
}
|
|
|
|
isc_result_t
|
|
omapi_connection_puthandle(omapi_object_t *c, omapi_object_t *h) {
|
|
isc_result_t result;
|
|
omapi_handle_t handle;
|
|
|
|
if (h != NULL) {
|
|
result = omapi_object_handle(&handle, h);
|
|
if (result != ISC_R_SUCCESS)
|
|
return (result);
|
|
} else
|
|
handle = 0; /* The null handle. */
|
|
|
|
result = omapi_connection_putuint32(c, sizeof(handle));
|
|
|
|
if (result == ISC_R_SUCCESS)
|
|
result = omapi_connection_putuint32(c, handle);
|
|
|
|
return (result);
|
|
}
|