connection.c revision fa9a40718023a494943e25a99c9ef75bf6b18638
/*
* Copyright (C) 1996-2000 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.34 2000/08/26 01:42:32 bwelling Exp $ */
/* Principal Author: DCL */
/*
* Subroutines for dealing with connections.
*/
#include <config.h>
#include <isc/bufferlist.h>
/*
*/
static isc_result_t
/*
* Is this an IPv6 numeric address?
*/
if (isc_net_probeipv6 == ISC_R_SUCCESS &&
/*
* What about an IPv4 numeric address?
*/
else {
/*
* Look up the host name.
*/
return (ISC_R_NOTFOUND);
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
}
}
if (connection->is_client) {
/*
* Break the link between the protocol object and its parent
* (usually a generic object); this is done so the client's
* reference to its managing object does not prevent the
* connection object and protocol object from being destroyed.
*/
} else {
/*
* Ensure that the protocol object has no parent, and
* signal the listener that the connection is ended.
*/
}
/*
* Finally, free the object itself.
*/
}
static void
if (connection->events_pending == 0) {
/*
* The client connection will be waiting if the error was
* triggered in one of the socket event handlers. It will
* not be waiting an error happened in omapi_meesgae_send
* or send_intro.
*
* The server connection will never be waiting.
*/
if (connection->waiting) {
/*
* Signal connection_wait and have it do the cleanup.
* free_connection can't be called directly here
* because it can't be sure that the mutex has been
* finished being touched by connection_wait even if
* free_connection has signaled it. (Nasty little race
* condition with the lock.)
*/
} else
return;
}
/*
* This function is only expected to be called by the client side
* when events_pending is 0. On the server side, this function
* could possibly be called with an event pending if
* omapi_listener_shutdown was called and had to resort to
* forced disconnects to blow away outstanding connections.
*/
/*
* It is also expected that the library only has one event
* outstanding at any given time.
*/
/*
* Cancel the outstanding event. It will generate an ISC_R_CANCELED
* result for either recv_done or send_done, which will decrement
* events_pending to 0 and call end_connection again.
*/
}
/*
* 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. This is
* necessary because the underlying socket library is multithreaded, and
* it is possible that reading incoming data would trigger an error
* that causes the connection to be destroyed --- while the client program
* is still trying to use it.
*
* This problem does not exist in the server, because everything in the
* server happens in the socket event functions, and as soon as one
* detects an error the connection is destroyed and no further attempt
* is made to use it. The server has its own mechanism for making sure
* destroyed connections are gone via omapi_listener_shutdown.
*/
static isc_result_t
/*
* This routine is not valid for server connections.
*/
while (connection->events_pending > 0)
/*
* An error occurred and end_connection needs to have
* free_connection called now that we're done looking
* at connection->events_pending.
*/
}
return (result);
}
/*
* This is the function that is called when a connect event is posted on
* the socket as a result of isc_socket_connect. It is only called
* on the client side.
*/
static void
/*
* Acquire the wait_lock before proceeding, to guarantee that
* connection_wait was entered in connection_toserver.
*/
if (result == ISC_R_SUCCESS)
if (result == ISC_R_SUCCESS)
&connection->local_addr);
if (result == ISC_R_SUCCESS) {
/*
* Unblock omapi_protocol_connect so it can send the intro.
*/
} else {
/*
* Set the state to disconnecting and unblock connection_wait
* to free the connection.
*/
}
}
/*
* This is the function that is called when a recv event is posted on
* the socket as a result of isc_socket_recv*. It is called by both the
* client and the server.
*/
static void
unsigned int bytes_read;
bytes_read = socketevent->n;
/*
* Acquire the wait_lock before proceeding, to guarantee that
* connection_wait was entered in connection_send.
*/
if (connection->is_client) {
}
/*
* Restore the input buffers to the connection object.
*/
if (result == ISC_R_SUCCESS) {
/*
* Signal protocol_signalhandler that the bytes it requested
* are present. Though this is set up as a loop, it should
* only execute once because protocol_signalhandler will
* loop calling dispatch_messages as long as there is
* input available.
*/
connection->bytes_needed > 0) {
"ready", connection);
if (result != ISC_R_SUCCESS)
break;
}
}
if (result == ISC_R_SUCCESS) {
if (connection->is_client)
/*
* Attempt to unblock connection_send. It might
* be the case that not all the bytes the client
* needs have yet been read, and so it would
* have had connection_require queue another recv,
* so events_pending will be 1 and connection_wait
* will not yet continue.
*/
} else {
/*
* Set the state to disconnecting and unblock connection_wait
* to free the connection.
*/
}
}
/*
* This is the function that is called when a send event is posted on
* the socket as a result of isc_socket_send*. It is called by both the
* client and the server.
*/
static void
unsigned int sent_bytes;
sent_bytes = socketevent->n;
/*
* Check the validity of the assumption that partial
* writes are not done.
*/
/*
* Acquire the wait_lock before proceeding, to guarantee that
* connection_wait was entered in connection_send.
*/
if (connection->is_client) {
}
/*
* 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.
*/
}
/*
* Both the server and client are allowed to have only
* one event outstanding on a client at a time. Each has
* already set up the number of bytes it expects to read
* next, but not queued the isc_socket_recv yet. Calling
* connection_require for 0 bytes will enable the recv.
*/
} else
/*
* Set the state to disconnecting and unblock connection_wait
* to free the connection.
*/
}
/*
* This does not need to be locked, because the only thing that
* decrements events_pending is the socket event handlers, and the
* design is to have only one event outstanding at a time.
*/
/*
* Block the send event from posting before the wait is established.
*/
if (connection->is_client)
if (connection->is_client)
/*
* Wait for the server's response to be processed. If
* the result is not ISC_R_SUCCESS, connection_wait
* has freed the connection.
*/
else
return (result);
}
/*
* Make an outgoing connection to an OMAPI server.
*/
{
if (result != ISC_R_SUCCESS)
return (result);
/* XXXDCL Make cleanup better */
/*
* Prepare the task that will wait for the connection to be made.
*/
if (result != ISC_R_SUCCESS)
return (result);
if (result != ISC_R_SUCCESS)
goto free_task;
if (result != ISC_R_SUCCESS)
goto free_ibuffer;
/*
* Create a new connection object.
*/
sizeof(*connection));
if (result != ISC_R_SUCCESS)
goto free_obuffer;
/*
* Tie the new connection object to the protocol object.
*/
/*
* Create a socket on which to communicate.
*/
if (result == ISC_R_SUCCESS) {
/*
* Lock before requesting the connection; this way
* connection_wait can safely block on connection->waiter
* before some connect error comes in and blows away the
* connection structure.
*/
}
if (result == ISC_R_SUCCESS)
/*
* Wait for the connection event. If result != ISC_R_SUCCESS,
* the connection was already abandoned via connect_done, so
* it does not need to be freed.
*/
else
/*
* There was an error calling isc_socket_create or
* isc_socket_connect. Tear down the connection.
*/
return (result);
return (result);
}
/*
* Put some bytes into the output buffer for a connection.
*/
unsigned int len)
{
unsigned int space_available;
connection = (omapi_connection_t *)c;
/*
* XXX make the auth stuff a part of the connection object instead?
*/
if (protocol->dst_update) {
(isc_region_t *)®ion);
if (result != ISC_R_SUCCESS)
return (result);
}
/*
* Check for enough space in the output buffers.
*/
while (space_available < len) {
/*
* Add new buffers until there is sufficient space.
*/
if (result != ISC_R_SUCCESS)
return (result);
}
/*
* Copy the data into the buffers, splitting across buffers
* as necessary.
*/
if (space_available > len)
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.
*/
void
unsigned int size)
{
unsigned int copy_bytes;
/*
* The data could potentially be split across multiple buffers,
* so rather than a simple memcpy, a loop is needed.
*/
while (size > 0) {
if (copy_bytes > size)
copy_bytes = size;
/*
* When dst == NULL, this function is being used to skip
* over uninteresting input.
*/
if (protocol->dst_update &&
size -= copy_bytes;
}
}
/*
* 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.)
*/
void
/*
* Only the client can request an unforced disconnection. The server's
* "normal" (non-error) disconnection will always happen when the
* client goes away, and the only time it calls this function
* is to forcibly blow away a connection while trying to shut down.
* XXXDCL ... hmm, can timeouts of the client on the server be handled?
*/
/*
* 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.
*
* Since this *must* have been called from the client driving
* thread, and the client never gets control back until all
* outstanding events have been posted, and the connection must
* still be valid for it to have been passed here, the
* following *must* be true.
*/
/*
* Fall through.
*/
}
}
/*
* The caller wants a specific amount of bytes to be read. Queue up a
* recv for the socket.
*/
return (ISC_R_SUCCESS);
if (connection->bytes_needed >
/*
* 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
* code will probably *never* be used in practice; to even test
* an absurdly low value (like 4).
*/
/*
* Lop off any completely used buffers, except the last one.
*/
while (isc_buffer_availablelength(buffer) == 0 &&
}
/*
* Reclaim any used space. (Any buffers after this one,
* if they exist at all, will be empty.)
*/
/*
* Create as many new buffers as necessary to fit the
* entire size requirement.
*/
while (connection->bytes_needed >
if (result != ISC_R_SUCCESS)
return (result);
}
}
/*
* Queue the receive task.
*/
/*
* The client should already be waiting.
*/
if (connection->is_client)
/*
* XXXDCL The "minimum" arg has not been fully thought out.
*/
return (OMAPI_R_NOTYET);
}
void
{
}
void
isc_uint16_t *value) {
}
return (omapi_connection_putmem(c, (unsigned char *)&inbuf,
sizeof(inbuf)));
}
return (omapi_connection_putmem(c, (unsigned char *)&inbuf,
sizeof(inbuf)));
}
case omapi_datatype_int:
if (result != ISC_R_SUCCESS)
return (result);
return (omapi_connection_putuint32(c, ((isc_uint32_t)
case omapi_datatype_string:
case omapi_datatype_data:
if (result != ISC_R_SUCCESS)
return (result);
return (omapi_connection_putmem(c,
return (ISC_R_SUCCESS);
case omapi_datatype_object:
if (result != ISC_R_SUCCESS)
return (result);
} else
handle = 0;
if (result != ISC_R_SUCCESS)
return (result);
return (omapi_connection_putuint32(c, handle));
}
"unknown type in omapi_connection_putdata: "
return (ISC_R_UNEXPECTED);
}
if (len > 65535)
/* XXXDCL better error? */
return (ISC_R_FAILURE);
if (result != ISC_R_SUCCESS)
return (result);
}
unsigned int len;
else
len = 0;
(const unsigned char *)string,
len);
return (result);
}
if (h != NULL) {
if (result != ISC_R_SUCCESS)
return (result);
} else
handle = 0; /* The null handle. */
if (result == ISC_R_SUCCESS)
return (result);
}
static isc_result_t
{
}
static isc_result_t
{
}
static void
/*
* end_connection is the proper entry point for removing a
* connection, so it should have been called to do all the cleanup.
*/
/*
* XXXDCL somehow, not all memory is being destroyed with abnormal
* drops. run the omapi_test program listener. then run the
* omapi_test as a client, and break at the end of omapi_auth_use.
* when the debugger stops, exit the debugger. only two blocks
* of memory are freed, but i suspect there are more than those
* associated with the connection.
*/
"Unexpected path to connection_destroy - "
"the connection object was dereferenced "
"without a previous disconnect");
}
}
static isc_result_t
{
}
/*
* Write all the published values associated with the object through the
* specified connection.
*/
static isc_result_t
{
}
connection_init(void) {
}