File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / dhcp / omapip / support.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Oct 9 09:06:54 2012 UTC (12 years, 5 months ago) by misho
Branches: dhcp, MAIN
CVS tags: v4_1_R7p0, v4_1_R7, v4_1_R4, HEAD
dhcp 4.1 r7

    1: /* support.c
    2: 
    3:    Subroutines providing general support for objects. */
    4: 
    5: /*
    6:  * Copyright (c) 2004-2007,2009 by Internet Systems Consortium, Inc. ("ISC")
    7:  * Copyright (c) 1999-2003 by Internet Software Consortium
    8:  *
    9:  * Permission to use, copy, modify, and distribute this software for any
   10:  * purpose with or without fee is hereby granted, provided that the above
   11:  * copyright notice and this permission notice appear in all copies.
   12:  *
   13:  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
   14:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   15:  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
   16:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   17:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   18:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
   19:  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   20:  *
   21:  *   Internet Systems Consortium, Inc.
   22:  *   950 Charter Street
   23:  *   Redwood City, CA 94063
   24:  *   <info@isc.org>
   25:  *   https://www.isc.org/
   26:  *
   27:  * This software has been written for Internet Systems Consortium
   28:  * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
   29:  * To learn more about Internet Systems Consortium, see
   30:  * ``https://www.isc.org/''.  To learn more about Vixie Enterprises,
   31:  * see ``http://www.vix.com''.   To learn more about Nominum, Inc., see
   32:  * ``http://www.nominum.com''.
   33:  */
   34: 
   35: #include "dhcpd.h"
   36: 
   37: #include <omapip/omapip_p.h>
   38: 
   39: omapi_object_type_t *omapi_type_connection;
   40: omapi_object_type_t *omapi_type_listener;
   41: omapi_object_type_t *omapi_type_io_object;
   42: omapi_object_type_t *omapi_type_datagram;
   43: omapi_object_type_t *omapi_type_generic;
   44: omapi_object_type_t *omapi_type_protocol;
   45: omapi_object_type_t *omapi_type_protocol_listener;
   46: omapi_object_type_t *omapi_type_waiter;
   47: omapi_object_type_t *omapi_type_remote;
   48: omapi_object_type_t *omapi_type_message;
   49: omapi_object_type_t *omapi_type_auth_key;
   50: 
   51: omapi_object_type_t *omapi_object_types;
   52: int omapi_object_type_count;
   53: 
   54: #if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
   55: void omapi_type_relinquish ()
   56: {
   57: 	omapi_object_type_t *t, *n;
   58: 
   59: 	for (t = omapi_object_types; t; t = n) {
   60: 		n = t -> next;
   61: 		dfree (t, MDL);
   62: 	}
   63: 	omapi_object_types = (omapi_object_type_t *)0;
   64: }
   65: #endif
   66: 
   67: isc_result_t omapi_init (void)
   68: {
   69: 	isc_result_t status;
   70: 
   71: 	dst_init();
   72: 
   73: 	/* Register all the standard object types... */
   74: 	status = omapi_object_type_register (&omapi_type_connection,
   75: 					     "connection",
   76: 					     omapi_connection_set_value,
   77: 					     omapi_connection_get_value,
   78: 					     omapi_connection_destroy,
   79: 					     omapi_connection_signal_handler,
   80: 					     omapi_connection_stuff_values,
   81: 					     0, 0, 0, 0, 0, 0,
   82: 					     sizeof
   83: 					     (omapi_connection_object_t), 0,
   84: 					     RC_MISC);
   85: 	if (status != ISC_R_SUCCESS)
   86: 		return status;
   87: 
   88: 	status = omapi_object_type_register (&omapi_type_listener,
   89: 					     "listener",
   90: 					     omapi_listener_set_value,
   91: 					     omapi_listener_get_value,
   92: 					     omapi_listener_destroy,
   93: 					     omapi_listener_signal_handler,
   94: 					     omapi_listener_stuff_values,
   95: 					     0, 0, 0, 0, 0, 0,
   96: 					     sizeof (omapi_listener_object_t),
   97: 					     0, RC_MISC);
   98: 	if (status != ISC_R_SUCCESS)
   99: 		return status;
  100: 
  101: 	status = omapi_object_type_register (&omapi_type_io_object,
  102: 					     "io",
  103: 					     omapi_io_set_value,
  104: 					     omapi_io_get_value,
  105: 					     omapi_io_destroy,
  106: 					     omapi_io_signal_handler,
  107: 					     omapi_io_stuff_values,
  108: 					     0, 0, 0, 0, 0, 0,
  109: 					     sizeof (omapi_io_object_t),
  110: 					     0, RC_MISC);
  111: 	if (status != ISC_R_SUCCESS)
  112: 		return status;
  113: 
  114: 	status = omapi_object_type_register (&omapi_type_generic,
  115: 					     "generic",
  116: 					     omapi_generic_set_value,
  117: 					     omapi_generic_get_value,
  118: 					     omapi_generic_destroy,
  119: 					     omapi_generic_signal_handler,
  120: 					     omapi_generic_stuff_values,
  121: 					     0, 0, 0, 0, 0, 0,
  122: 					     sizeof (omapi_generic_object_t),
  123: 					     0, RC_MISC);
  124: 	if (status != ISC_R_SUCCESS)
  125: 		return status;
  126: 
  127: 	status = omapi_object_type_register (&omapi_type_protocol,
  128: 					     "protocol",
  129: 					     omapi_protocol_set_value,
  130: 					     omapi_protocol_get_value,
  131: 					     omapi_protocol_destroy,
  132: 					     omapi_protocol_signal_handler,
  133: 					     omapi_protocol_stuff_values,
  134: 					     0, 0, 0, 0, 0, 0,
  135: 					     sizeof (omapi_protocol_object_t),
  136: 					     0, RC_MISC);
  137: 	if (status != ISC_R_SUCCESS)
  138: 		return status;
  139: 
  140: 	status = (omapi_object_type_register
  141: 		  (&omapi_type_protocol_listener, "protocol-listener",
  142: 		   omapi_protocol_listener_set_value,
  143: 		   omapi_protocol_listener_get_value,
  144: 		   omapi_protocol_listener_destroy,
  145: 		   omapi_protocol_listener_signal,
  146: 		   omapi_protocol_listener_stuff,
  147: 		   0, 0, 0, 0, 0, 0,
  148: 		   sizeof (omapi_protocol_listener_object_t), 0, RC_MISC));
  149: 	if (status != ISC_R_SUCCESS)
  150: 		return status;
  151: 
  152: 	status = omapi_object_type_register (&omapi_type_message,
  153: 					     "message",
  154: 					     omapi_message_set_value,
  155: 					     omapi_message_get_value,
  156: 					     omapi_message_destroy,
  157: 					     omapi_message_signal_handler,
  158: 					     omapi_message_stuff_values,
  159: 					     0, 0, 0, 0, 0, 0,
  160: 					     sizeof (omapi_message_object_t),
  161: 					     0, RC_MISC);
  162: 	if (status != ISC_R_SUCCESS)
  163: 		return status;
  164: 
  165: 	status = omapi_object_type_register (&omapi_type_waiter,
  166: 					     "waiter",
  167: 					     0,
  168: 					     0,
  169: 					     0,
  170: 					     omapi_waiter_signal_handler, 0,
  171: 					     0, 0, 0, 0, 0, 0,
  172: 					     sizeof (omapi_waiter_object_t),
  173: 					     0, RC_MISC);
  174: 	if (status != ISC_R_SUCCESS)
  175: 		return status;
  176: 
  177: 	status = omapi_object_type_register (&omapi_type_auth_key,
  178: 					     "authenticator",
  179: 					     0,
  180: 					     omapi_auth_key_get_value,
  181: 					     omapi_auth_key_destroy,
  182: 					     0,
  183: 					     omapi_auth_key_stuff_values,
  184: 					     omapi_auth_key_lookup,
  185: 					     0, 0, 0, 0, 0,
  186: 					     sizeof (omapi_auth_key_t), 0,
  187: 					     RC_MISC);
  188: 	if (status != ISC_R_SUCCESS)
  189: 		return status;
  190: 
  191: #if defined (TRACING)
  192: 	omapi_listener_trace_setup ();
  193: 	omapi_connection_trace_setup ();
  194: 	omapi_buffer_trace_setup ();
  195: 	trace_mr_init ();
  196: #endif
  197: 
  198: 	/* This seems silly, but leave it. */
  199: 	return ISC_R_SUCCESS;
  200: }
  201: 
  202: isc_result_t omapi_object_type_register (omapi_object_type_t **type,
  203: 					 const char *name,
  204: 					 isc_result_t (*set_value)
  205: 						 (omapi_object_t *,
  206: 						  omapi_object_t *,
  207: 						  omapi_data_string_t *,
  208: 						  omapi_typed_data_t *),
  209: 					 isc_result_t (*get_value)
  210: 						(omapi_object_t *,
  211: 						 omapi_object_t *,
  212: 						 omapi_data_string_t *,
  213: 						 omapi_value_t **),
  214: 					 isc_result_t (*destroy)
  215: 						(omapi_object_t *,
  216: 						 const char *, int),
  217: 					 isc_result_t (*signal_handler)
  218: 						 (omapi_object_t *,
  219: 						  const char *, va_list),
  220: 					 isc_result_t (*stuff_values)
  221: 						(omapi_object_t *,
  222: 						 omapi_object_t *,
  223: 						 omapi_object_t *),
  224: 					 isc_result_t (*lookup)
  225: 						(omapi_object_t **,
  226: 						 omapi_object_t *,
  227: 						 omapi_object_t *),
  228: 					 isc_result_t (*create)
  229: 						(omapi_object_t **,
  230: 						 omapi_object_t *),
  231: 					 isc_result_t (*remove)
  232: 						(omapi_object_t *,
  233: 						 omapi_object_t *),
  234: 					 isc_result_t (*freer)
  235: 						(omapi_object_t *,
  236: 						 const char *, int),
  237: 					 isc_result_t (*allocator)
  238: 						(omapi_object_t **,
  239: 						 const char *, int),
  240: 					 isc_result_t (*sizer) (size_t),
  241: 					 size_t size,
  242: 					 isc_result_t (*initialize)
  243: 						(omapi_object_t *,
  244: 						 const char *, int),
  245: 					 int rc_flag)
  246: {
  247: 	omapi_object_type_t *t;
  248: 
  249: 	t = dmalloc (sizeof *t, MDL);
  250: 	if (!t)
  251: 		return ISC_R_NOMEMORY;
  252: 	memset (t, 0, sizeof *t);
  253: 
  254: 	t -> name = name;
  255: 	t -> set_value = set_value;
  256: 	t -> get_value = get_value;
  257: 	t -> destroy = destroy;
  258: 	t -> signal_handler = signal_handler;
  259: 	t -> stuff_values = stuff_values;
  260: 	t -> lookup = lookup;
  261: 	t -> create = create;
  262: 	t -> remove = remove;
  263: 	t -> next = omapi_object_types;
  264: 	t -> sizer = sizer;
  265: 	t -> size = size;
  266: 	t -> freer = freer;
  267: 	t -> allocator = allocator;
  268: 	t -> initialize = initialize;
  269: 	t -> rc_flag = rc_flag;
  270: 	omapi_object_types = t;
  271: 	if (type)
  272: 		*type = t;
  273: 	return ISC_R_SUCCESS;
  274: }
  275: 
  276: isc_result_t omapi_signal (omapi_object_t *handle, const char *name, ...)
  277: {
  278: 	va_list ap;
  279: 	omapi_object_t *outer;
  280: 	isc_result_t status;
  281: 
  282: 	va_start (ap, name);
  283: 	for (outer = handle; outer -> outer; outer = outer -> outer)
  284: 		;
  285: 	if (outer -> type -> signal_handler)
  286: 		status = (*(outer -> type -> signal_handler)) (outer,
  287: 							       name, ap);
  288: 	else
  289: 		status = ISC_R_NOTFOUND;
  290: 	va_end (ap);
  291: 	return status;
  292: }
  293: 
  294: isc_result_t omapi_signal_in (omapi_object_t *handle, const char *name, ...)
  295: {
  296: 	va_list ap;
  297: 	isc_result_t status;
  298: 
  299: 	if (!handle)
  300: 		return ISC_R_NOTFOUND;
  301: 	va_start (ap, name);
  302: 
  303: 	if (handle -> type -> signal_handler)
  304: 		status = (*(handle -> type -> signal_handler)) (handle,
  305: 								name, ap);
  306: 	else
  307: 		status = ISC_R_NOTFOUND;
  308: 	va_end (ap);
  309: 	return status;
  310: }
  311: 
  312: isc_result_t omapi_set_value (omapi_object_t *h,
  313: 			      omapi_object_t *id,
  314: 			      omapi_data_string_t *name,
  315: 			      omapi_typed_data_t *value)
  316: {
  317: 	omapi_object_t *outer;
  318: 	isc_result_t status;
  319: 
  320: #if defined (DEBUG)
  321: 	if (!value) {
  322: 		log_info ("omapi_set_value (%.*s, NULL)",
  323: 			  (int)name -> len, name -> value);
  324: 	} else if (value -> type == omapi_datatype_int) {
  325: 		log_info ("omapi_set_value (%.*s, %ld)",
  326: 			  (int)name -> len, name -> value,
  327: 			  (long)value -> u.integer);
  328: 	} else if (value -> type == omapi_datatype_string) {
  329: 		log_info ("omapi_set_value (%.*s, %.*s)",
  330: 			  (int)name -> len, name -> value,
  331: 			  (int)value -> u.buffer.len, value -> u.buffer.value);
  332: 	} else if (value -> type == omapi_datatype_data) {
  333: 		log_info ("omapi_set_value (%.*s, %ld %lx)",
  334: 			  (int)name -> len, name -> value,
  335: 			  (long)value -> u.buffer.len,
  336: 			  (unsigned long)value -> u.buffer.value);
  337: 	} else if (value -> type == omapi_datatype_object) {
  338: 		log_info ("omapi_set_value (%.*s, %s)",
  339: 			  (int)name -> len, name -> value,
  340: 			  value -> u.object
  341: 			  ? (value -> u.object -> type
  342: 			     ? value -> u.object -> type -> name
  343: 			     : "(unknown object)")
  344: 			  : "(unknown object)");
  345: 	}
  346: #endif
  347: 
  348: 	for (outer = h; outer -> outer; outer = outer -> outer)
  349: 		;
  350: 	if (outer -> type -> set_value)
  351: 		status = (*(outer -> type -> set_value)) (outer,
  352: 							  id, name, value);
  353: 	else
  354: 		status = ISC_R_NOTFOUND;
  355: #if defined (DEBUG)
  356: 	log_info (" ==> %s", isc_result_totext (status));
  357: #endif
  358: 	return status;
  359: }
  360: 
  361: isc_result_t omapi_set_value_str (omapi_object_t *h,
  362: 				  omapi_object_t *id,
  363: 				  const char *name,
  364: 				  omapi_typed_data_t *value)
  365: {
  366: 	omapi_data_string_t *nds;
  367: 	isc_result_t status;
  368: 
  369: 	nds = (omapi_data_string_t *)0;
  370: 	status = omapi_data_string_new (&nds, strlen (name), MDL);
  371: 	if (status != ISC_R_SUCCESS)
  372: 		return status;
  373: 	memcpy (nds -> value, name, strlen (name));
  374: 
  375: 	status = omapi_set_value (h, id, nds, value);
  376: 	omapi_data_string_dereference (&nds, MDL);
  377: 	return status;
  378: }
  379: 
  380: isc_result_t omapi_set_boolean_value (omapi_object_t *h, omapi_object_t *id,
  381: 				      const char *name, int value)
  382: {
  383: 	isc_result_t status;
  384: 	omapi_typed_data_t *tv = (omapi_typed_data_t *)0;
  385: 	omapi_data_string_t *n = (omapi_data_string_t *)0;
  386: 
  387: 	status = omapi_data_string_new (&n, strlen (name), MDL);
  388: 	if (status != ISC_R_SUCCESS)
  389: 		return status;
  390: 	memcpy (n -> value, name, strlen (name));
  391: 
  392: 	status = omapi_typed_data_new (MDL, &tv, omapi_datatype_int, value);
  393: 	if (status != ISC_R_SUCCESS) {
  394: 		omapi_data_string_dereference (&n, MDL);
  395: 		return status;
  396: 	}
  397: 
  398: 	status = omapi_set_value (h, id, n, tv);
  399: 	omapi_data_string_dereference (&n, MDL);
  400: 	omapi_typed_data_dereference (&tv, MDL);
  401: 	return status;
  402: }
  403: 
  404: isc_result_t omapi_set_int_value (omapi_object_t *h, omapi_object_t *id,
  405: 				  const char *name, int value)
  406: {
  407: 	isc_result_t status;
  408: 	omapi_typed_data_t *tv = (omapi_typed_data_t *)0;
  409: 	omapi_data_string_t *n = (omapi_data_string_t *)0;
  410: 
  411: 	status = omapi_data_string_new (&n, strlen (name), MDL);
  412: 	if (status != ISC_R_SUCCESS)
  413: 		return status;
  414: 	memcpy (n -> value, name, strlen (name));
  415: 
  416: 	status = omapi_typed_data_new (MDL, &tv, omapi_datatype_int, value);
  417: 	if (status != ISC_R_SUCCESS) {
  418: 		omapi_data_string_dereference (&n, MDL);
  419: 		return status;
  420: 	}
  421: 
  422: 	status = omapi_set_value (h, id, n, tv);
  423: 	omapi_data_string_dereference (&n, MDL);
  424: 	omapi_typed_data_dereference (&tv, MDL);
  425: 	return status;
  426: }
  427: 
  428: isc_result_t omapi_set_object_value (omapi_object_t *h, omapi_object_t *id,
  429: 				     const char *name, omapi_object_t *value)
  430: {
  431: 	isc_result_t status;
  432: 	omapi_typed_data_t *tv = (omapi_typed_data_t *)0;
  433: 	omapi_data_string_t *n = (omapi_data_string_t *)0;
  434: 
  435: 	status = omapi_data_string_new (&n, strlen (name), MDL);
  436: 	if (status != ISC_R_SUCCESS)
  437: 		return status;
  438: 	memcpy (n -> value, name, strlen (name));
  439: 
  440: 	status = omapi_typed_data_new (MDL, &tv, omapi_datatype_object, value);
  441: 	if (status != ISC_R_SUCCESS) {
  442: 		omapi_data_string_dereference (&n, MDL);
  443: 		return status;
  444: 	}
  445: 
  446: 	status = omapi_set_value (h, id, n, tv);
  447: 	omapi_data_string_dereference (&n, MDL);
  448: 	omapi_typed_data_dereference (&tv, MDL);
  449: 	return status;
  450: }
  451: 
  452: isc_result_t omapi_set_string_value (omapi_object_t *h, omapi_object_t *id,
  453: 				     const char *name, const char *value)
  454: {
  455: 	isc_result_t status;
  456: 	omapi_typed_data_t *tv = (omapi_typed_data_t *)0;
  457: 	omapi_data_string_t *n = (omapi_data_string_t *)0;
  458: 
  459: 	status = omapi_data_string_new (&n, strlen (name), MDL);
  460: 	if (status != ISC_R_SUCCESS)
  461: 		return status;
  462: 	memcpy (n -> value, name, strlen (name));
  463: 
  464: 	status = omapi_typed_data_new (MDL, &tv, omapi_datatype_string, value);
  465: 	if (status != ISC_R_SUCCESS) {
  466: 		omapi_data_string_dereference (&n, MDL);
  467: 		return status;
  468: 	}
  469: 
  470: 	status = omapi_set_value (h, id, n, tv);
  471: 	omapi_data_string_dereference (&n, MDL);
  472: 	omapi_typed_data_dereference (&tv, MDL);
  473: 	return status;
  474: }
  475: 
  476: isc_result_t omapi_get_value (omapi_object_t *h,
  477: 			      omapi_object_t *id,
  478: 			      omapi_data_string_t *name,
  479: 			      omapi_value_t **value)
  480: {
  481: 	omapi_object_t *outer;
  482: 
  483: 	for (outer = h; outer -> outer; outer = outer -> outer)
  484: 		;
  485: 	if (outer -> type -> get_value)
  486: 		return (*(outer -> type -> get_value)) (outer,
  487: 							id, name, value);
  488: 	return ISC_R_NOTFOUND;
  489: }
  490: 
  491: isc_result_t omapi_get_value_str (omapi_object_t *h,
  492: 				  omapi_object_t *id,
  493: 				  const char *name,
  494: 				  omapi_value_t **value)
  495: {
  496: 	omapi_object_t *outer;
  497: 	omapi_data_string_t *nds;
  498: 	isc_result_t status;
  499: 
  500: 	nds = (omapi_data_string_t *)0;
  501: 	status = omapi_data_string_new (&nds, strlen (name), MDL);
  502: 	if (status != ISC_R_SUCCESS)
  503: 		return status;
  504: 	memcpy (nds -> value, name, strlen (name));
  505: 
  506: 	for (outer = h; outer -> outer; outer = outer -> outer)
  507: 		;
  508: 	if (outer -> type -> get_value)
  509: 		status = (*(outer -> type -> get_value)) (outer,
  510: 							  id, nds, value);
  511: 	else
  512: 		status = ISC_R_NOTFOUND;
  513: 	omapi_data_string_dereference (&nds, MDL);
  514: 	return status;
  515: }
  516: 
  517: isc_result_t omapi_stuff_values (omapi_object_t *c,
  518: 				 omapi_object_t *id,
  519: 				 omapi_object_t *o)
  520: {
  521: 	omapi_object_t *outer;
  522: 
  523: 	for (outer = o; outer -> outer; outer = outer -> outer)
  524: 		;
  525: 	if (outer -> type -> stuff_values)
  526: 		return (*(outer -> type -> stuff_values)) (c, id, outer);
  527: 	return ISC_R_NOTFOUND;
  528: }
  529: 
  530: isc_result_t omapi_object_create (omapi_object_t **obj, omapi_object_t *id,
  531: 				  omapi_object_type_t *type)
  532: {
  533: 	if (!type -> create)
  534: 		return ISC_R_NOTIMPLEMENTED;
  535: 	return (*(type -> create)) (obj, id);
  536: }
  537: 
  538: isc_result_t omapi_object_update (omapi_object_t *obj, omapi_object_t *id,
  539: 				  omapi_object_t *src, omapi_handle_t handle)
  540: {
  541: 	omapi_generic_object_t *gsrc;
  542: 	isc_result_t status;
  543: 	int i;
  544: 
  545: 	if (!src)
  546: 		return ISC_R_INVALIDARG;
  547: 	if (src -> type != omapi_type_generic)
  548: 		return ISC_R_NOTIMPLEMENTED;
  549: 	gsrc = (omapi_generic_object_t *)src;
  550: 	for (i = 0; i < gsrc -> nvalues; i++) {
  551: 		status = omapi_set_value (obj, id,
  552: 					  gsrc -> values [i] -> name,
  553: 					  gsrc -> values [i] -> value);
  554: 		if (status != ISC_R_SUCCESS && status != ISC_R_UNCHANGED)
  555: 			return status;
  556: 	}
  557: 	if (handle)
  558: 		omapi_set_int_value (obj, id, "remote-handle", (int)handle);
  559: 	status = omapi_signal (obj, "updated");
  560: 	if (status != ISC_R_NOTFOUND)
  561: 		return status;
  562: 	return ISC_R_SUCCESS;
  563: }
  564: 
  565: int omapi_data_string_cmp (omapi_data_string_t *s1, omapi_data_string_t *s2)
  566: {
  567: 	unsigned len;
  568: 	int rv;
  569: 
  570: 	if (s1 -> len > s2 -> len)
  571: 		len = s2 -> len;
  572: 	else
  573: 		len = s1 -> len;
  574: 	rv = memcmp (s1 -> value, s2 -> value, len);
  575: 	if (rv)
  576: 		return rv;
  577: 	if (s1 -> len > s2 -> len)
  578: 		return 1;
  579: 	else if (s1 -> len < s2 -> len)
  580: 		return -1;
  581: 	return 0;
  582: }
  583: 
  584: int omapi_ds_strcmp (omapi_data_string_t *s1, const char *s2)
  585: {
  586: 	unsigned len, slen;
  587: 	int rv;
  588: 
  589: 	slen = strlen (s2);
  590: 	if (slen > s1 -> len)
  591: 		len = s1 -> len;
  592: 	else
  593: 		len = slen;
  594: 	rv = memcmp (s1 -> value, s2, len);
  595: 	if (rv)
  596: 		return rv;
  597: 	if (s1 -> len > slen)
  598: 		return 1;
  599: 	else if (s1 -> len < slen)
  600: 		return -1;
  601: 	return 0;
  602: }
  603: 
  604: int omapi_td_strcmp (omapi_typed_data_t *s1, const char *s2)
  605: {
  606: 	unsigned len, slen;
  607: 	int rv;
  608: 
  609: 	/* If the data type is not compatible, never equal. */
  610: 	if (s1 -> type != omapi_datatype_data &&
  611: 	    s1 -> type != omapi_datatype_string)
  612: 		return -1;
  613: 
  614: 	slen = strlen (s2);
  615: 	if (slen > s1 -> u.buffer.len)
  616: 		len = s1 -> u.buffer.len;
  617: 	else
  618: 		len = slen;
  619: 	rv = memcmp (s1 -> u.buffer.value, s2, len);
  620: 	if (rv)
  621: 		return rv;
  622: 	if (s1 -> u.buffer.len > slen)
  623: 		return 1;
  624: 	else if (s1 -> u.buffer.len < slen)
  625: 		return -1;
  626: 	return 0;
  627: }
  628: 
  629: int omapi_td_strcasecmp (omapi_typed_data_t *s1, const char *s2)
  630: {
  631: 	unsigned len, slen;
  632: 	int rv;
  633: 
  634: 	/* If the data type is not compatible, never equal. */
  635: 	if (s1 -> type != omapi_datatype_data &&
  636: 	    s1 -> type != omapi_datatype_string)
  637: 		return -1;
  638: 
  639: 	slen = strlen (s2);
  640: 	if (slen > s1 -> u.buffer.len)
  641: 		len = s1 -> u.buffer.len;
  642: 	else
  643: 		len = slen;
  644: 	rv = casecmp (s1 -> u.buffer.value, s2, len);
  645: 	if (rv)
  646: 		return rv;
  647: 	if (s1 -> u.buffer.len > slen)
  648: 		return 1;
  649: 	else if (s1 -> u.buffer.len < slen)
  650: 		return -1;
  651: 	return 0;
  652: }
  653: 
  654: isc_result_t omapi_make_value (omapi_value_t **vp,
  655: 			       omapi_data_string_t *name,
  656: 			       omapi_typed_data_t *value,
  657: 			       const char *file, int line)
  658: {
  659: 	isc_result_t status;
  660: 
  661: 	status = omapi_value_new (vp, file, line);
  662: 	if (status != ISC_R_SUCCESS)
  663: 		return status;
  664: 
  665: 	status = omapi_data_string_reference (&(*vp) -> name,
  666: 					      name, file, line);
  667: 	if (status != ISC_R_SUCCESS) {
  668: 		omapi_value_dereference (vp, file, line);
  669: 		return status;
  670: 	}
  671: 	if (value) {
  672: 		status = omapi_typed_data_reference (&(*vp) -> value,
  673: 						     value, file, line);
  674: 		if (status != ISC_R_SUCCESS) {
  675: 			omapi_value_dereference (vp, file, line);
  676: 			return status;
  677: 		}
  678: 	}
  679: 	return ISC_R_SUCCESS;
  680: }
  681: 
  682: isc_result_t omapi_make_const_value (omapi_value_t **vp,
  683: 				     omapi_data_string_t *name,
  684: 				     const unsigned char *value,
  685: 				     unsigned len,
  686: 				     const char *file, int line)
  687: {
  688: 	isc_result_t status;
  689: 
  690: 	status = omapi_value_new (vp, file, line);
  691: 	if (status != ISC_R_SUCCESS)
  692: 		return status;
  693: 
  694: 	status = omapi_data_string_reference (&(*vp) -> name,
  695: 					      name, file, line);
  696: 	if (status != ISC_R_SUCCESS) {
  697: 		omapi_value_dereference (vp, file, line);
  698: 		return status;
  699: 	}
  700: 	if (value) {
  701: 		status = omapi_typed_data_new (file, line, &(*vp) -> value,
  702: 					       omapi_datatype_data, len);
  703: 		if (status != ISC_R_SUCCESS) {
  704: 			omapi_value_dereference (vp, file, line);
  705: 			return status;
  706: 		}
  707: 		memcpy ((*vp) -> value -> u.buffer.value, value, len);
  708: 	}
  709: 	return ISC_R_SUCCESS;
  710: }
  711: 
  712: isc_result_t omapi_make_int_value (omapi_value_t **vp,
  713: 				   omapi_data_string_t *name,
  714: 				   int value, const char *file, int line)
  715: {
  716: 	isc_result_t status;
  717: 
  718: 	status = omapi_value_new (vp, file, line);
  719: 	if (status != ISC_R_SUCCESS)
  720: 		return status;
  721: 
  722: 	status = omapi_data_string_reference (&(*vp) -> name,
  723: 					      name, file, line);
  724: 	if (status != ISC_R_SUCCESS) {
  725: 		omapi_value_dereference (vp, file, line);
  726: 		return status;
  727: 	}
  728: 	status = omapi_typed_data_new (file, line, &(*vp) -> value,
  729: 				       omapi_datatype_int, value);
  730: 	if (status != ISC_R_SUCCESS) {
  731: 		omapi_value_dereference (vp, file, line);
  732: 		return status;
  733: 	}
  734: 	return ISC_R_SUCCESS;
  735: }
  736: 
  737: isc_result_t omapi_make_uint_value (omapi_value_t **vp,
  738: 				    omapi_data_string_t *name,
  739: 				    unsigned int value,
  740: 				    const char *file, int line)
  741: {
  742: 	return omapi_make_int_value (vp, name, (int)value, file, line);
  743: }
  744: 
  745: isc_result_t omapi_make_object_value (omapi_value_t **vp,
  746: 				      omapi_data_string_t *name,
  747: 				      omapi_object_t *value,
  748: 				      const char *file, int line)
  749: {
  750: 	isc_result_t status;
  751: 	
  752: 	status = omapi_value_new (vp, file, line);
  753: 	if (status != ISC_R_SUCCESS)
  754: 		return status;
  755: 	
  756: 	status = omapi_data_string_reference (&(*vp) -> name,
  757:                                               name, file, line);
  758: 	if (status != ISC_R_SUCCESS) {
  759: 		omapi_value_dereference (vp, file, line);
  760: 		return status;
  761: 	}
  762: 	
  763: 	if (value) {
  764: 		status = omapi_typed_data_new (file, line, &(*vp) -> value,
  765: 					       omapi_datatype_object, value);
  766: 		if (status != ISC_R_SUCCESS) {
  767: 			omapi_value_dereference (vp, file, line);
  768: 			return status;
  769: 		}
  770: 	}
  771: 	
  772: 	return ISC_R_SUCCESS;
  773: }
  774: 
  775: isc_result_t omapi_make_handle_value (omapi_value_t **vp,
  776: 				      omapi_data_string_t *name,
  777: 				      omapi_object_t *value,
  778: 				      const char *file, int line)
  779: {
  780: 	isc_result_t status;
  781: 
  782: 	status = omapi_value_new (vp, file, line);
  783: 	if (status != ISC_R_SUCCESS)
  784: 		return status;
  785: 
  786: 	status = omapi_data_string_reference (&(*vp) -> name,
  787: 					      name, file, line);
  788: 	if (status != ISC_R_SUCCESS) {
  789: 		omapi_value_dereference (vp, file, line);
  790: 		return status;
  791: 	}
  792: 	if (value) {
  793: 		status = omapi_typed_data_new (file, line, &(*vp) -> value,
  794: 					       omapi_datatype_int);
  795: 		if (status != ISC_R_SUCCESS) {
  796: 			omapi_value_dereference (vp, file, line);
  797: 			return status;
  798: 		}
  799: 		status = (omapi_object_handle
  800: 			  ((omapi_handle_t *)&(*vp) -> value -> u.integer,
  801: 			   value));
  802: 		if (status != ISC_R_SUCCESS) {
  803: 			omapi_value_dereference (vp, file, line);
  804: 			return status;
  805: 		}
  806: 	}
  807: 	return ISC_R_SUCCESS;
  808: }
  809: 
  810: isc_result_t omapi_make_string_value (omapi_value_t **vp,
  811: 				      omapi_data_string_t *name,
  812: 				      const char *value,
  813: 				      const char *file, int line)
  814: {
  815: 	isc_result_t status;
  816: 
  817: 	status = omapi_value_new (vp, file, line);
  818: 	if (status != ISC_R_SUCCESS)
  819: 		return status;
  820: 
  821: 	status = omapi_data_string_reference (&(*vp) -> name,
  822: 					      name, file, line);
  823: 	if (status != ISC_R_SUCCESS) {
  824: 		omapi_value_dereference (vp, file, line);
  825: 		return status;
  826: 	}
  827: 	if (value) {
  828: 		status = omapi_typed_data_new (file, line, &(*vp) -> value,
  829: 					       omapi_datatype_string, value);
  830: 		if (status != ISC_R_SUCCESS) {
  831: 			omapi_value_dereference (vp, file, line);
  832: 			return status;
  833: 		}
  834: 	}
  835: 	return ISC_R_SUCCESS;
  836: }
  837: 
  838: isc_result_t omapi_get_int_value (unsigned long *v, omapi_typed_data_t *t)
  839: {
  840: 	u_int32_t rv;
  841: 
  842: 	if (t -> type == omapi_datatype_int) {
  843: 		*v = t -> u.integer;
  844: 		return ISC_R_SUCCESS;
  845: 	} else if (t -> type == omapi_datatype_string ||
  846: 		   t -> type == omapi_datatype_data) {
  847: 		if (t -> u.buffer.len != sizeof (rv))
  848: 			return ISC_R_INVALIDARG;
  849: 		memcpy (&rv, t -> u.buffer.value, sizeof rv);
  850: 		*v = ntohl (rv);
  851: 		return ISC_R_SUCCESS;
  852: 	}
  853: 	return ISC_R_INVALIDARG;
  854: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>