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.20.2.1 2015/03/09 22:01:34 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 - 2015
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: static int
52: rpcServerClients(RPC_CALL_STDARGS)
53: {
54: rpc_srv_t *srv;
55: rpc_cli_t *c;
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(cli);
63: srv = RPC_SRV_SERVER(cli);
64:
65: len = array_Size(srv->srv_clients) * STRSIZ;
66: if (!(val = e_malloc(len))) {
67: LOGERR;
68: return -1;
69: } else
70: memset(val, 0, len);
71:
72: for (i = 0; i < array_Size(srv->srv_clients); i++) {
73: c = array(srv->srv_clients, i, rpc_cli_t*);
74: if (!c)
75: continue;
76:
77: str = e_n2addr(&c->cli_sa, &v);
78: if (str)
79: strlcat(val, (char*) str, len);
80: else
81: strlcat(val, "0.0.0.0", len);
82: strlcat(val, " ", len);
83: AIT_FREE_VAL(&v);
84: }
85:
86: /* return values */
87: AIT_SET_STR(ait_getVars(&RPC_RETVARS(cli), 0), val);
88: e_free(val);
89: return 0;
90: }
91:
92: static int
93: rpcServerCalls(RPC_CALL_STDARGS)
94: {
95: rpc_srv_t *srv;
96: rpc_func_t *f;
97: register int i = 0;
98: int len;
99: char *val, str[MAXPATHLEN];
100:
101: RPC_CALLBACK_CHECK_INPUT(cli);
102: srv = RPC_SRV_SERVER(cli);
103:
104: SLIST_FOREACH(f, &srv->srv_funcs, func_next)
105: i++;
106: len = i * STRSIZ;
107:
108: if (!(val = e_malloc(len))) {
109: LOGERR;
110: return -1;
111: } else
112: memset(val, 0, len);
113:
114: SLIST_FOREACH(f, &srv->srv_funcs, func_next)
115: if (AIT_ADDR(&f->func_name)) {
116: memset(str, 0, sizeof str);
117: snprintf(str, sizeof str, "/%hu/0x%p; ", AIT_KEY(&f->func_name),
118: AIT_ADDR(&f->func_name));
119: strlcat(val, str, len);
120: }
121:
122: /* return variables */
123: AIT_SET_STR(ait_getVars(&RPC_RETVARS(cli), 0), val);
124: e_free(val);
125: return 0;
126: }
127:
128: static int
129: rpcServerSessions(RPC_CALL_STDARGS)
130: {
131: rpc_srv_t *srv;
132:
133: RPC_CALLBACK_CHECK_INPUT(cli);
134: srv = RPC_SRV_SERVER(cli);
135:
136: AIT_SET_I32(ait_getVars(&RPC_RETVARS(cli), 2), array_Size(srv->srv_clients));
137: AIT_SET_U8(ait_getVars(&RPC_RETVARS(cli), 0), srv->srv_session.sess_version);
138: AIT_SET_U8(ait_getVars(&RPC_RETVARS(cli), 1), srv->srv_session.sess_instance);
139:
140: return 0;
141: }
142:
143: static int
144: rpcServerShutdown(RPC_CALL_STDARGS)
145: {
146: rpc_srv_t *srv;
147:
148: RPC_CALLBACK_CHECK_INPUT(cli);
149: srv = RPC_SRV_SERVER(cli);
150:
151: rpc_freeCli(cli);
152:
153: srv->srv_kill = 1;
154: if (srv->srv_blob.tid)
155: pthread_kill(srv->srv_blob.tid, SIGFBLOB);
156: return 0;
157: }
158:
159: static int
160: rpcServerPing(RPC_CALL_STDARGS)
161: {
162: if (ntohl((u_long) rpc->call_req.flags) == RPC_REPLY)
163: AIT_SET_U16(ait_getVars(&RPC_RETVARS(cli), 0), random() % USHRT_MAX);
164: printf("alabala\n");
165: return 0;
166: }
167:
168: /* ---------------------------------------------------- */
169:
170: static int
171: rpcBLOBServerShutdown(RPC_CALL_STDARGS)
172: {
173: rpc_srv_t *srv;
174:
175: RPC_CALLBACK_CHECK_INPUT(cli);
176: srv = RPC_SRV_SERVER(cli);
177:
178: rpc_freeBLOBCli(cli);
179:
180: srv->srv_blob.kill = 1;
181: return 0;
182: }
183:
184: static int
185: rpcBLOBServerVars(RPC_CALL_STDARGS)
186: {
187: rpc_srv_t *srv;
188: rpc_blob_t *b;
189: register int i = 0;
190: char *val, str[64];
191: int len;
192:
193: RPC_CALLBACK_CHECK_INPUT(cli);
194: srv = RPC_SRV_SERVER(cli);
195:
196: if (srv->srv_blob.kill) {
197: AIT_SET_STR(ait_getVars(&RPC_RETVARS(cli), 0), "BLOB Server is killed");
198: return 1;
199: }
200:
201: TAILQ_FOREACH(b, &srv->srv_blob.blobs, blob_node)
202: i++;
203: len = i * sizeof str;
204:
205: if (!len) {
206: AIT_SET_STR(ait_getVars(&RPC_RETVARS(cli), 0), "");
207: return 0;
208: }
209:
210: if (!(val = e_malloc(len))) {
211: LOGERR;
212: return -1;
213: } else
214: memset(val, 0, len);
215:
216: TAILQ_FOREACH(b, &srv->srv_blob.blobs, blob_node) {
217: memset(str, 0, sizeof str);
218: snprintf(str, sizeof str, "0x%0X(%lu)=%p ", b->blob_var, (u_long) b->blob_len, b->blob_data);
219: strlcat(val, str, len);
220: }
221:
222: AIT_SET_STR(ait_getVars(&RPC_RETVARS(cli), 0), val);
223: e_free(val);
224: return 0;
225: }
226:
227: static int
228: rpcBLOBServerClients(RPC_CALL_STDARGS)
229: {
230: rpc_srv_t *srv;
231: rpc_cli_t *c;
232: register int i;
233: int len;
234: const char *str = NULL;
235: char *val;
236: ait_val_t v;
237:
238: RPC_CALLBACK_CHECK_INPUT(cli);
239: srv = RPC_SRV_SERVER(cli);
240:
241: if (srv->srv_blob.kill) {
242: AIT_SET_STR(ait_getVars(&RPC_RETVARS(cli), 0), "BLOB Server is killed");
243: return 1;
244: }
245:
246: len = array_Size(srv->srv_blob.clients) * STRSIZ;
247: if (!(val = e_malloc(len))) {
248: LOGERR;
249: return -1;
250: } else
251: memset(val, 0, len);
252:
253: for (i = 0; i < array_Size(srv->srv_clients); i++) {
254: c = array(srv->srv_blob.clients, i, rpc_cli_t*);
255: if (!c)
256: continue;
257:
258: str = e_n2addr(&c->cli_sa, &v);
259: if (str)
260: strlcat(val, (char*) str, len);
261: else
262: strlcat(val, "0.0.0.0", len);
263: strlcat(val, " ", len);
264: AIT_FREE_VAL(&v);
265: }
266:
267: AIT_SET_STR(ait_getVars(&RPC_RETVARS(cli), 0), val);
268: e_free(val);
269: return 0;
270: }
271:
272: /* ----------------------------------------------------------------- */
273:
274: /*
275: * rpc_register_srvPing() - Register ping service function
276: *
277: * @srv = RPC server instance
278: * return: -1 error or 0 ok
279: */
280: int
281: rpc_register_srvPing(rpc_srv_t * __restrict srv)
282: {
283: if (!srv)
284: return -1;
285:
286: if (rpc_srv_registerCall(srv, CALL_SRVPING, rpcServerPing) < 1)
287: return -1;
288:
289: return 0;
290: }
291:
292: /*
293: * rpc_register_srvServices() - Register internal service functions
294: *
295: * @srv = RPC server instance
296: * return: -1 error or 0 ok
297: */
298: int
299: rpc_register_srvServices(rpc_srv_t * __restrict srv)
300: {
301: if (!srv)
302: return -1;
303:
304: if (rpc_srv_registerCall(srv, CALL_SRVSHUTDOWN, rpcServerShutdown) < 1)
305: return -1;
306: if (rpc_srv_registerCall(srv, CALL_SRVCLIENTS, rpcServerClients) < 1)
307: return -1;
308: if (rpc_srv_registerCall(srv, CALL_SRVSESSIONS, rpcServerSessions) < 1)
309: return -1;
310: if (rpc_srv_registerCall(srv, CALL_SRVCALLS, rpcServerCalls) < 1)
311: return -1;
312:
313: return 0;
314: }
315:
316: /*
317: * rpc_register_blobServices() - Register internal service functions
318: *
319: * @srv = RPC server instance
320: * return: -1 error or 0 ok
321: */
322: int
323: rpc_register_blobServices(rpc_srv_t * __restrict srv)
324: {
325: if (!srv)
326: return -1;
327:
328: if (rpc_srv_registerCall(srv, CALL_BLOBSHUTDOWN, rpcBLOBServerShutdown) < 1)
329: return -1;
330: if (rpc_srv_registerCall(srv, CALL_BLOBCLIENTS, rpcBLOBServerClients) < 1)
331: return -1;
332: if (rpc_srv_registerCall(srv, CALL_BLOBVARS, rpcBLOBServerVars) < 1)
333: return -1;
334:
335: return 0;
336: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>