📄 protocol.c
字号:
/* protocol.c Functions supporting the object management protocol... *//* * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") * Copyright (c) 1999-2003 by 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 ISC DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC 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. * * Internet Systems Consortium, Inc. * 950 Charter Street * Redwood City, CA 94063 * <info@isc.org> * http://www.isc.org/ * * This software has been written for Internet Systems Consortium * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc. * To learn more about Internet Systems 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 some number of bytes were read, and that number of bytes has in fact been read. */ switch (p -> state) { case omapi_protocol_intro_wait: /* Get protocol version and header size in network byte order. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -