Merge from vendor branch OPENSSH:
[dragonfly.git] / contrib / dhcp-3.0 / dhcpctl / callback.c
1 /* callback.c
2
3    The dhcpctl callback object. */
4
5 /*
6  * Copyright (c) 2004 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  *   http://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  * ``http://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 #ifndef lint
36 static char copyright[] =
37 "$Id: callback.c,v 1.5.2.2 2004/06/10 17:59:23 dhankins Exp $ Copyright (c) 2004 Internet Systems Consortium.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #include <omapip/omapip_p.h>
41 #include "dhcpctl.h"
42
43 /* dhcpctl_set_callback
44
45    synchronous, with asynchronous aftereffect
46    handle is some object upon which some kind of process has been
47    started - e.g., an open, an update or a refresh.
48    data is an anonymous pointer containing some information that
49    the callback will use to figure out what event completed.
50    return value of 0 means callback was successfully set, a nonzero
51    status code is returned otherwise.
52    Upon completion of whatever task is in process, the callback
53    will be passed the handle to the object, a status code
54    indicating what happened, and the anonymous pointer passed to  */
55
56 dhcpctl_status dhcpctl_set_callback (dhcpctl_handle h, void *data,
57                                      void (*func) (dhcpctl_handle,
58                                                    dhcpctl_status, void *))
59 {
60         dhcpctl_callback_object_t *callback;
61         omapi_object_t *inner;
62         isc_result_t status;
63
64         callback = dmalloc (sizeof *callback, MDL);
65         if (!callback)
66                 return ISC_R_NOMEMORY;
67
68         /* Tie the callback object to the innermost object in the chain. */
69         for (inner = h; inner -> inner; inner = inner -> inner)
70                 ;
71         omapi_object_reference (&inner -> inner,
72                                 (omapi_object_t *)callback, MDL);
73         omapi_object_reference ((omapi_object_t **)&callback -> outer,
74                                 inner, MDL);
75
76         /* Save the actual handle pointer we were passed for the callback. */
77         omapi_object_reference (&callback -> object, h, MDL);
78         callback -> data = data;
79         callback -> callback = func;
80         
81         return ISC_R_SUCCESS;
82 }
83
84 /* Callback methods (not meant to be called directly) */
85
86 isc_result_t dhcpctl_callback_set_value (omapi_object_t *h,
87                                          omapi_object_t *id,
88                                          omapi_data_string_t *name,
89                                          omapi_typed_data_t *value)
90 {
91         if (h -> type != dhcpctl_callback_type)
92                 return ISC_R_INVALIDARG;
93
94         if (h -> inner && h -> inner -> type -> set_value)
95                 return (*(h -> inner -> type -> set_value))
96                         (h -> inner, id, name, value);
97         return ISC_R_NOTFOUND;
98 }
99
100 isc_result_t dhcpctl_callback_get_value (omapi_object_t *h,
101                                          omapi_object_t *id,
102                                          omapi_data_string_t *name,
103                                          omapi_value_t **value)
104 {
105         if (h -> type != dhcpctl_callback_type)
106                 return ISC_R_INVALIDARG;
107         
108         if (h -> inner && h -> inner -> type -> get_value)
109                 return (*(h -> inner -> type -> get_value))
110                         (h -> inner, id, name, value);
111         return ISC_R_NOTFOUND;
112 }
113
114 isc_result_t dhcpctl_callback_signal_handler (omapi_object_t *o,
115                                               const char *name, va_list ap)
116 {
117         dhcpctl_callback_object_t *p;
118         isc_result_t waitstatus;
119
120         if (o -> type != dhcpctl_callback_type)
121                 return ISC_R_INVALIDARG;
122         p = (dhcpctl_callback_object_t *)o;
123
124         /* Not a signal we recognize? */
125         if (strcmp (name, "ready")) {
126                 if (p -> inner && p -> inner -> type -> signal_handler)
127                         return (*(p -> inner -> type -> signal_handler))
128                                 (p -> inner, name, ap);
129                 return ISC_R_NOTFOUND;
130         }
131
132         if (p -> object -> type == dhcpctl_remote_type) {
133                 waitstatus = (((dhcpctl_remote_object_t *)
134                                (p -> object)) -> waitstatus);
135         } else
136                 waitstatus = ISC_R_SUCCESS;
137
138         /* Do the callback. */
139         if (p -> callback)
140                 (*(p -> callback)) (p -> object, waitstatus, p -> data);
141
142         return ISC_R_SUCCESS;
143 }
144
145 isc_result_t dhcpctl_callback_destroy (omapi_object_t *h,
146                                        const char *file, int line)
147 {
148         dhcpctl_callback_object_t *p;
149         if (h -> type != dhcpctl_callback_type)
150                 return ISC_R_INVALIDARG;
151         p = (dhcpctl_callback_object_t *)h;
152         if (p -> handle)
153                 omapi_object_dereference ((omapi_object_t **)&p -> handle,
154                                           file, line);
155         return ISC_R_SUCCESS;
156 }
157
158 /* Write all the published values associated with the object through the
159    specified connection. */
160
161 isc_result_t dhcpctl_callback_stuff_values (omapi_object_t *c,
162                                             omapi_object_t *id,
163                                             omapi_object_t *p)
164 {
165         int i;
166
167         if (p -> type != dhcpctl_callback_type)
168                 return ISC_R_INVALIDARG;
169
170         if (p -> inner && p -> inner -> type -> stuff_values)
171                 return (*(p -> inner -> type -> stuff_values)) (c, id,
172                                                                 p -> inner);
173         return ISC_R_SUCCESS;
174 }
175