📄 protocol.c
字号:
/* protocol.c Functions supporting the object management protocol... *//* * Copyright (c) 1999-2000 Internet Software Consortium. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of The Internet Software Consortium nor the names * of its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * This software has been written for the Internet Software Consortium * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc. * To learn more about the Internet Software Consortium, see * ``http://www.isc.org/''. To learn more about Vixie Enterprises, * see ``http://www.vix.com''. To learn more about Nominum, Inc., see * ``http://www.nominum.com''. */#include <omapip/omapip_p.h>OMAPI_OBJECT_ALLOC (omapi_protocol, omapi_protocol_object_t, omapi_type_protocol)OMAPI_OBJECT_ALLOC (omapi_protocol_listener, omapi_protocol_listener_object_t, omapi_type_protocol_listener)isc_result_t omapi_protocol_connect (omapi_object_t *h, const char *server_name, unsigned port, omapi_object_t *a){ isc_result_t rstatus, status; omapi_protocol_object_t *obj;#ifdef DEBUG_PROTOCOL log_debug ("omapi_protocol_connect(%s port=%d)", server_name, port);#endif obj = (omapi_protocol_object_t *)0; status = omapi_protocol_allocate (&obj, MDL); if (status != ISC_R_SUCCESS) return status; rstatus = omapi_connect ((omapi_object_t *)obj, server_name, port); if (rstatus != ISC_R_SUCCESS && rstatus != ISC_R_INCOMPLETE) { omapi_protocol_dereference (&obj, MDL); return rstatus; } status = omapi_object_reference (&h -> outer, (omapi_object_t *)obj, MDL); if (status != ISC_R_SUCCESS) { omapi_protocol_dereference (&obj, MDL); return status; } status = omapi_object_reference (&obj -> inner, h, MDL); if (status != ISC_R_SUCCESS) { omapi_protocol_dereference (&obj, MDL); return status; } /* If we were passed a default authenticator, store it now. We'll open it once we're connected. */ if (a) { obj -> default_auth = dmalloc (sizeof(omapi_remote_auth_t), MDL); if (!obj -> default_auth) { omapi_protocol_dereference (&obj, MDL); return ISC_R_NOMEMORY; } obj -> default_auth -> next = (omapi_remote_auth_t *)0; status = omapi_object_reference (&obj -> default_auth -> a, a, MDL); if (status != ISC_R_SUCCESS) { dfree (obj -> default_auth, MDL); omapi_protocol_dereference (&obj, MDL); return status; } obj -> insecure = 0; rstatus = ISC_R_INCOMPLETE; } else { obj -> insecure = 1;#if 0 status = ISC_R_SUCCESS;#endif } omapi_protocol_dereference (&obj, MDL); return rstatus;}/* Send the protocol introduction message. */isc_result_t omapi_protocol_send_intro (omapi_object_t *h, unsigned ver, unsigned hsize){ isc_result_t status; omapi_protocol_object_t *p;#ifdef DEBUG_PROTOCOL log_debug ("omapi_protocol_send_intro()");#endif if (h -> type != omapi_type_protocol) return ISC_R_INVALIDARG; p = (omapi_protocol_object_t *)h; if (!h -> outer || h -> outer -> type != omapi_type_connection) return ISC_R_NOTCONNECTED; status = omapi_connection_put_uint32 (h -> outer, ver); if (status != ISC_R_SUCCESS) return status; status = omapi_connection_put_uint32 (h -> outer, hsize); if (status != ISC_R_SUCCESS) return status; /* Require the other end to send an intro - this kicks off the protocol input state machine. */ p -> state = omapi_protocol_intro_wait; status = omapi_connection_require (h -> outer, 8); if (status != ISC_R_SUCCESS && status != ISC_R_NOTYET) return status; /* Make up an initial transaction ID for this connection. */ p -> next_xid = random (); return ISC_R_SUCCESS;}isc_result_t omapi_protocol_send_message (omapi_object_t *po, omapi_object_t *id, omapi_object_t *mo, omapi_object_t *omo){ omapi_protocol_object_t *p; omapi_object_t *c; omapi_message_object_t *m, *om; omapi_remote_auth_t *ra; omapi_value_t *signature; isc_result_t status; u_int32_t foo; unsigned auth_len; if (po -> type != omapi_type_protocol || !po -> outer || po -> outer -> type != omapi_type_connection || mo -> type != omapi_type_message) return ISC_R_INVALIDARG; if (omo && omo -> type != omapi_type_message) return ISC_R_INVALIDARG; p = (omapi_protocol_object_t *)po; c = (omapi_object_t *)(po -> outer); m = (omapi_message_object_t *)mo; om = (omapi_message_object_t *)omo;#ifdef DEBUG_PROTOCOL log_debug ("omapi_protocol_send_message()" "op=%ld handle=%#lx id=%#lx rid=%#lx", (long)m -> op, (long)(m -> object ? m -> object -> handle : m -> handle), (long)p -> next_xid, (long)m -> rid);#endif /* Find the authid to use for this message. */ if (id) { for (ra = p -> remote_auth_list; ra; ra = ra -> next) { if (ra -> a == id) { break; } } if (!ra) return ISC_R_KEY_UNKNOWN; } else if (p -> remote_auth_list) { ra = p -> default_auth; } else { ra = (omapi_remote_auth_t *)0; } if (ra) { m -> authid = ra -> remote_handle; status = omapi_object_reference (&m -> id_object, ra -> a, MDL); if (status != ISC_R_SUCCESS) return status; } /* Write the ID of the authentication key we're using. */ status = omapi_connection_put_uint32 (c, ra ? ra -> remote_handle : 0); if (status != ISC_R_SUCCESS) { omapi_disconnect (c, 1); return status; } /* Activate the authentication key on the connection. */ auth_len = 0; if (ra) { status = omapi_set_object_value (c, (omapi_object_t *)0, "output-authenticator", ra -> a); if (status != ISC_R_SUCCESS) { omapi_disconnect (c, 1); return status; } status = omapi_connection_output_auth_length (c, &auth_len); if (status != ISC_R_SUCCESS) { omapi_disconnect (c, 1); return status; } } /* Write the authenticator length */ status = omapi_connection_put_uint32 (c, auth_len); if (status != ISC_R_SUCCESS) { omapi_disconnect (c, 1); return status; } /* Write the opcode. */ status = omapi_connection_put_uint32 (c, m -> op); if (status != ISC_R_SUCCESS) { omapi_disconnect (c, 1); return status; } /* Write the handle. If we've been given an explicit handle, use that. Otherwise, use the handle of the object we're sending. The caller is responsible for arranging for one of these handles to be set (or not). */ status = omapi_connection_put_uint32 (c, (m -> h ? m -> h : (m -> object ? m -> object -> handle : 0))); if (status != ISC_R_SUCCESS) { omapi_disconnect (c, 1); return status; } /* Set and write the transaction ID. */ m -> id = p -> next_xid++; status = omapi_connection_put_uint32 (c, m -> id); if (status != ISC_R_SUCCESS) { omapi_disconnect (c, 1); return status; } /* Write the transaction ID of the message to which this is a response, if there is such a message. */ status = omapi_connection_put_uint32 (c, om ? om -> id : m -> rid); if (status != ISC_R_SUCCESS) { omapi_disconnect (c, 1); return status; } /* Stuff out the name/value pairs specific to this message. */ status = omapi_stuff_values (c, id, (omapi_object_t *)m); if (status != ISC_R_SUCCESS) { omapi_disconnect (c, 1); return status; } /* Write the zero-length name that terminates the list of name/value pairs specific to the message. */ status = omapi_connection_put_uint16 (c, 0); if (status != ISC_R_SUCCESS) { omapi_disconnect (c, 1); return status; } /* Stuff out all the published name/value pairs in the object that's being sent in the message, if there is one. */ if (m -> object) { status = omapi_stuff_values (c, id, m -> object); if (status != ISC_R_SUCCESS) { omapi_disconnect (c, 1); return status; } } /* Write the zero-length name that terminates the list of name/value pairs for the associated object. */ status = omapi_connection_put_uint16 (c, 0); if (status != ISC_R_SUCCESS) { omapi_disconnect (c, 1); return status; } if (ra) { /* Calculate the message signature. */ signature = (omapi_value_t *)0; status = omapi_get_value_str (c, (omapi_object_t *)0, "output-signature", &signature); if (status != ISC_R_SUCCESS) { omapi_disconnect (c, 1); return status; } /* Write the authenticator... */ status = (omapi_connection_copyin (c, signature -> value -> u.buffer.value, signature -> value -> u.buffer.len)); omapi_value_dereference (&signature, MDL); if (status != ISC_R_SUCCESS) { omapi_disconnect (c, 1); return status; } /* Dectivate the authentication key on the connection. */ status = omapi_set_value_str (c, (omapi_object_t *)0, "output-authenticator", (omapi_typed_data_t *)0); if (status != ISC_R_SUCCESS) { omapi_disconnect (c, 1); return status; } } if (!omo) { omapi_protocol_reference (&m -> protocol_object, p, MDL); } return ISC_R_SUCCESS;} isc_result_t omapi_protocol_signal_handler (omapi_object_t *h, const char *name, va_list ap){ isc_result_t status; omapi_protocol_object_t *p; omapi_object_t *c; omapi_message_object_t *m; omapi_value_t *signature; u_int16_t nlen; u_int32_t vlen; u_int32_t th;#if defined (DEBUG_MEMORY_LEAKAGE) unsigned long previous_outstanding = 0xDEADBEEF; unsigned long connect_outstanding = 0xDEADBEEF;#endif if (h -> type != omapi_type_protocol) { /* XXX shouldn't happen. Put an assert here? */ return ISC_R_UNEXPECTED; } p = (omapi_protocol_object_t *)h; if (!strcmp (name, "connect")) {#if defined (DEBUG_MEMORY_LEAKAGE) connect_outstanding = dmalloc_outstanding;#endif /* Send the introductory message. */ status = omapi_protocol_send_intro (h, OMAPI_PROTOCOL_VERSION, sizeof (omapi_protocol_header_t)); if (status != ISC_R_SUCCESS) { omapi_disconnect (p -> outer, 1); return status; } return ISC_R_SUCCESS; } /* Should only receive these when opening the initial authenticator. */ if (!strcmp (name, "status")) { status = va_arg (ap, isc_result_t); if (status != ISC_R_SUCCESS) { omapi_signal_in (h -> inner, "status", status, (omapi_object_t *)0); omapi_disconnect (p -> outer, 1); return status; } else { return omapi_signal_in (h -> inner, "ready"); } } /* If we get a disconnect, dump memory usage. */ if (!strcmp (name, "disconnect")) {#if defined (DEBUG_MEMORY_LEAKAGE) if (connect_outstanding != 0xDEADBEEF) { log_info ("generation %ld: %ld new, %ld outstanding, %ld%s", dmalloc_generation, dmalloc_outstanding - previous_outstanding, dmalloc_outstanding, dmalloc_longterm, " long-term"); }#endif#if defined (DEBUG_MEMORY_LEAKAGE) dmalloc_dump_outstanding ();#endif#if defined (DEBUG_RC_HISTORY_EXHAUSTIVELY) dump_rc_history ();#endif for (m = omapi_registered_messages; m; m = m -> next) { if (m -> protocol_object == p) { if (m -> object) omapi_signal (m -> object, "disconnect"); } } } /* Not a signal we recognize? */ if (strcmp (name, "ready")) { if (p -> inner && p -> inner -> type -> signal_handler) return (*(p -> inner -> type -> signal_handler)) (h, name, ap); return ISC_R_NOTFOUND; } if (!p -> outer || p -> outer -> type != omapi_type_connection) return ISC_R_INVALIDARG; c = p -> outer; /* We get here because we requested that we be woken up after
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -