1: /*************************************************************************
2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
6: * $Id: builtin.c,v 1.7.2.2 2012/05/16 13:32:47 misho Exp $
7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
46: #include "global.h"
47:
48:
49: /* builtin RPC server functions */
50:
51: int
52: rpcServerClients(rpc_func_t *call, int ic, array_t *iv)
53: {
54: rpc_srv_t *srv;
55: rpc_cli_t *cli;
56: register int i;
57: int len;
58: const char *str = NULL;
59: char *val;
60: ait_val_t v;
61:
62: RPC_CALLBACK_CHECK_INPUT(call);
63: if (!call->func_parent)
64: return -1;
65: else
66: srv = call->func_parent;
67:
68: len = io_arraySize(srv->srv_clients) * STRSIZ;
69: if (!(val = malloc(len))) {
70: LOGERR;
71: return -1;
72: } else
73: memset(val, 0, len);
74:
75: for (i = 0; i < io_arraySize(srv->srv_clients); i++) {
76: cli = io_array(srv->srv_clients, i, rpc_cli_t*);
77: if (!cli)
78: continue;
79:
80: str = io_n2addr(&cli->cli_sa, &v);
81: if (str)
82: strlcat(val, (char*) str, len);
83: else
84: strlcat(val, "0.0.0.0", len);
85: strlcat(val, " ", len);
86: AIT_FREE_VAL(&v);
87: }
88:
89: AIT_SET_STR(io_array(call->func_vars, 0, ait_val_t*), val);
90: free(val);
91: return 0;
92: }
93:
94: int
95: rpcServerCalls(rpc_func_t *call, int ic, array_t *iv)
96: {
97: rpc_srv_t *srv;
98: rpc_func_t *f;
99: register int i = 0;
100: int len;
101: char *val, str[MAXPATHLEN];
102:
103: RPC_CALLBACK_CHECK_INPUT(call);
104: if (!call->func_parent)
105: return -1;
106: else
107: srv = call->func_parent;
108:
109: TAILQ_FOREACH(f, &srv->srv_funcs, func_node)
110: i++;
111: len = i * STRSIZ;
112:
113: if (!(val = malloc(len))) {
114: LOGERR;
115: return -1;
116: } else
117: memset(val, 0, len);
118:
119: TAILQ_FOREACH(f, &srv->srv_funcs, func_node)
120: if (AIT_ADDR(&f->func_name)) {
121: memset(str, 0, sizeof str);
122: snprintf(str, sizeof str, "/%hu/0x%p(%d); ", AIT_KEY(&f->func_name),
123: AIT_ADDR(&f->func_name), io_arraySize(f->func_vars));
124: strlcat(val, str, len);
125: }
126:
127: AIT_SET_STR(io_array(f->func_vars, 0, ait_val_t*), val);
128: free(val);
129: return 0;
130: }
131:
132: int
133: rpcServerSessions(rpc_func_t *call, int ic, array_t *iv)
134: {
135: rpc_srv_t *srv;
136: ait_val_t *v;
137:
138: RPC_CALLBACK_CHECK_INPUT(call);
139: if (!call->func_parent)
140: return -1;
141: else
142: srv = call->func_parent;
143:
144: v = io_array(call->func_vars, 0, ait_val_t*);
145: AIT_SET_U8(v, srv->srv_session.sess_version);
146: v = io_array(call->func_vars, 1, ait_val_t*);
147: AIT_SET_U32(v, srv->srv_session.sess_program);
148: v = io_array(call->func_vars, 2, ait_val_t*);
149: AIT_SET_U8(v, srv->srv_session.sess_process);
150: v = io_array(call->func_vars, 3, ait_val_t*);
151: AIT_SET_I32(v, io_arraySize(srv->srv_clients));
152:
153: return 0;
154: }
155:
156: int
157: rpcServerShutdown(rpc_func_t *call, int ic, array_t *iv)
158: {
159: rpc_srv_t *srv;
160:
161: RPC_CALLBACK_CHECK_INPUT(call);
162: if (!call->func_parent)
163: return -1;
164: else
165: srv = call->func_parent;
166:
167: srv->srv_kill = 1;
168:
169: return 0;
170: }
171:
172: /* ---------------------------------------------------- */
173:
174: #if 0
175: int
176: rpcBLOBServerShutdown(rpc_func_t *call, int ic, array_t *iv)
177: {
178: rpc_srv_t *srv;
179:
180: RPC_CALLBACK_CHECK_INPUT(call);
181: if (!call->func_parent)
182: return -1;
183: else
184: srv = call->func_parent;
185:
186: srv->srv_blob.state = kill;
187:
188: return 0;
189: }
190:
191: int
192: rpcBLOBServerVars(rpc_func_t *call, int ic, array_t *iv)
193: {
194: rpc_srv_t *srv;
195: array_t *vals;
196: rpc_blob_t *b;
197: register int i;
198: char *val, str[64];
199: int len;
200:
201: RPC_CALLBACK_CHECK_INPUT(call);
202: if (!call->func_parent)
203: return -1;
204: else
205: srv = call->func_parent;
206:
207: rpc_srv_getVars(call, &vals);
208:
209: if (srv->srv_blob.state == kill) {
210: AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is killed");
211: return 1;
212: }
213: if (srv->srv_blob.state == disable) {
214: AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is disabled");
215: return 1;
216: }
217:
218: for (i = 0, b = srv->srv_blob.blobs; b; i++, b = b->blob_next);
219: len = i * sizeof str;
220:
221: if (!len) {
222: AIT_SET_STR(io_array(vals, 0, ait_val_t*), "");
223: return 0;
224: }
225:
226: if (!(val = malloc(len))) {
227: LOGERR;
228: return -1;
229: } else
230: memset(val, 0, len);
231:
232: for (b = srv->srv_blob.blobs, i = 0; b; i++, b = b->blob_next) {
233: memset(str, 0, sizeof str);
234: snprintf(str, sizeof str, "0x%0X(%lu)=%p ", b->blob_var, (u_long) b->blob_len, b->blob_data);
235: strlcat(val, str, len);
236: }
237:
238: AIT_SET_STR(io_array(vals, 0, ait_val_t*), val);
239: free(val);
240: return 0;
241: }
242:
243: int
244: rpcBLOBServerState(rpc_func_t *call, int ic, array_t *iv)
245: {
246: rpc_srv_t *srv;
247:
248: RPC_CALLBACK_CHK_ARGS(call, ic);
249: if (!call->func_parent)
250: return -1;
251: else
252: srv = call->func_parent;
253:
254: if (AIT_TYPE(io_array(iv, 0, ait_val_t*)) != i32)
255: return -1;
256:
257: srv->srv_blob.state = AIT_GET_I32(io_array(iv, 0, ait_val_t*));
258: return 0;
259: }
260:
261: int
262: rpcBLOBServerClients(rpc_func_t *call, int ic, array_t *iv)
263: {
264: rpc_srv_t *srv;
265: array_t *vals;
266: rpc_cli_t *cli;
267: register int i;
268: int len;
269: const char *str = NULL;
270: char *val, wrk[INET6_ADDRSTRLEN];
271:
272: RPC_CALLBACK_CHECK_INPUT(call);
273: if (!call->func_parent)
274: return -1;
275: else
276: srv = call->func_parent;
277:
278: rpc_srv_getVars(call, &vals);
279:
280: if (srv->srv_blob.state == kill) {
281: AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is killed");
282: return 1;
283: }
284: if (srv->srv_blob.state == disable) {
285: AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is disabled");
286: return 1;
287: }
288:
289: len = srv->srv_numcli * STRSIZ;
290: if (!(val = malloc(len))) {
291: LOGERR;
292: return -1;
293: } else
294: memset(val, 0, len);
295:
296: for (i = 0, cli = srv->srv_blob.clients; i < srv->srv_numcli; i++, cli++) {
297: if (!cli->cli_sa.sa.sa_family)
298: continue;
299:
300: switch (cli->cli_sa.sa.sa_family) {
301: case AF_INET:
302: str = inet_ntop(cli->cli_sa.sa.sa_family, &cli->cli_sa.sin.sin_addr, wrk, sizeof wrk);
303: break;
304: case AF_INET6:
305: str = inet_ntop(cli->cli_sa.sa.sa_family, &cli->cli_sa.sin6.sin6_addr, wrk, sizeof wrk);
306: break;
307: case AF_LOCAL:
308: str = cli->cli_sa.sun.sun_path;
309: break;
310: }
311: if (str)
312: strlcat(val, (char*) str, len);
313: else
314: strlcat(val, "0.0.0.0", len);
315: strlcat(val, " ", len);
316: }
317:
318: AIT_SET_STR(io_array(vals, 0, ait_val_t*), val);
319: free(val);
320: return 0;
321: }
322: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>