Annotation of libaitrpc/src/srv.c, revision 1.9.2.8
1.1 misho 1: /*************************************************************************
2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.9.2.8 ! misho 6: * $Id: srv.c,v 1.9.2.7 2012/05/15 22:47:10 misho Exp $
1.1 misho 7: *
1.2 misho 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:
1.7 misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
1.2 misho 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: */
1.1 misho 46: #include "global.h"
47:
48:
1.9.2.4 misho 49: static void *
50: closeClient(sched_task_t *task)
51: {
52: rpc_cli_t *c = TASK_ARG(task);
53: rpc_srv_t *s = c->cli_parent;
54:
1.9.2.6 misho 55: schedCancelby(s->srv_root, taskMAX, CRITERIA_ARG, TASK_ARG(task), NULL);
1.9.2.4 misho 56:
57: /* close client socket */
58: if (TASK_VAL(task))
59: shutdown(c->cli_sock, SHUT_RDWR);
60: close(c->cli_sock);
61:
62: /* free buffer */
63: AIT_FREE_VAL(&c->cli_buf);
64:
65: io_arrayDel(s->srv_clients, c->cli_id, 42);
66: return NULL;
67: }
1.7 misho 68:
69: static void *
70: txPacket(sched_task_t *task)
71: {
72: rpc_cli_t *c = TASK_ARG(task);
73: rpc_srv_t *s = c->cli_parent;
74: rpc_func_t *f = NULL;
1.9.2.4 misho 75: u_char buf[USHRT_MAX] = { 0 };
1.7 misho 76: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
77: int ret, wlen = sizeof(struct tagRPCCall);
78:
1.9.2.4 misho 79: /* copy RPC header */
80: memcpy(buf, TASK_DATA(task), wlen);
81:
1.7 misho 82: if (rpc->call_argc) {
1.9.2.6 misho 83: f = rpc_srv_getCall(s, ntohs(rpc->call_tag));
1.7 misho 84: if (!f) {
85: rpc->call_argc ^= rpc->call_argc;
86: rpc->call_rep.ret = RPC_ERROR(-1);
87: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
88: } else {
1.9.2.7 misho 89: rpc->call_argc = htons(io_arraySize(f->func_vars));
1.7 misho 90: /* Go Encapsulate variables */
1.9.2.7 misho 91: ret = io_vars2buffer(buf + wlen, sizeof buf - wlen, f->func_vars);
1.9.2.4 misho 92: /* Free return values */
1.7 misho 93: io_clrVars(f->func_vars);
94: if (ret == -1) {
95: rpc_SetErr(EBADRPC, "Prepare RPC packet failed");
96: rpc->call_argc ^= rpc->call_argc;
97: rpc->call_rep.ret = RPC_ERROR(-1);
98: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
99: } else
100: wlen += ret;
101: }
102: }
103:
1.8 misho 104: rpc->call_len = htons(wlen);
105:
1.7 misho 106: /* calculate CRC */
107: rpc->call_crc ^= rpc->call_crc;
1.8 misho 108: rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
1.7 misho 109:
110: /* send reply */
1.9.2.4 misho 111: ret = send(TASK_FD(task), buf, wlen, MSG_NOSIGNAL);
112: if (ret == -1 || ret != wlen) {
113: /* close connection */
114: schedEvent(TASK_ROOT(task), closeClient, c, 42, NULL, 0);
115: }
1.7 misho 116:
117: return NULL;
118: }
119:
120: static void *
121: execCall(sched_task_t *task)
122: {
123: rpc_cli_t *c = TASK_ARG(task);
124: rpc_srv_t *s = c->cli_parent;
125: rpc_func_t *f = NULL;
126: array_t *arr = NULL;
1.9.2.4 misho 127: u_char *buf = AIT_GET_BUF(&c->cli_buf) + TASK_VAL(task);
1.7 misho 128: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
129: int argc = ntohs(rpc->call_argc);
130:
131: /* Go decapsulate variables ... */
1.8 misho 132: if (argc) {
1.7 misho 133: arr = io_buffer2vars(buf + sizeof(struct tagRPCCall),
1.9.2.4 misho 134: AIT_LEN(&c->cli_buf) - TASK_VAL(task) - sizeof(struct tagRPCCall),
1.8 misho 135: argc, 1);
1.7 misho 136: if (!arr) {
137: rpc_SetErr(ERPCMISMATCH, "#%d - %s", io_GetErrno(), io_GetError());
138: rpc->call_argc ^= rpc->call_argc;
139: rpc->call_rep.ret = RPC_ERROR(-1);
140: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
141: return NULL;
142: }
1.9.2.8 ! misho 143: } else
! 144: arr = NULL;
1.7 misho 145:
1.9.2.6 misho 146: if (!(f = rpc_srv_getCall(s, ntohs(rpc->call_tag)))) {
1.7 misho 147: rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server");
148: rpc->call_argc ^= rpc->call_argc;
149: rpc->call_rep.ret = RPC_ERROR(-1);
150: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
151: } else {
1.8 misho 152: /* if client doesn't want reply */
153: argc = rpc->call_req.flags & RPC_NOREPLY;
1.7 misho 154: rpc->call_rep.ret = RPC_ERROR(rpc_srv_execCall(f, rpc, arr));
155: if (rpc->call_rep.ret == htonl(-1)) {
156: rpc->call_rep.eno = RPC_ERROR(errno);
157: rpc->call_argc ^= rpc->call_argc;
158: } else {
159: rpc->call_rep.eno ^= rpc->call_rep.eno;
1.8 misho 160: if (argc) {
161: /* without reply */
162: io_clrVars(f->func_vars);
163: rpc->call_argc ^= rpc->call_argc;
1.9.2.8 ! misho 164: } else {
! 165: /* reply */
1.9.2.7 misho 166: rpc->call_argc = htons(io_arraySize(f->func_vars));
1.9.2.8 ! misho 167: }
1.7 misho 168: }
169: }
170:
171: if (arr)
172: io_arrayDestroy(&arr);
173: return NULL;
174: }
175:
176: static void *
177: rxPacket(sched_task_t *task)
178: {
179: rpc_cli_t *c = TASK_ARG(task);
180: rpc_srv_t *s = c->cli_parent;
1.9.2.4 misho 181: u_char *buf = AIT_GET_BUF(&c->cli_buf);
182: int len, rlen, noreply;
1.8 misho 183: u_short crc, off = 0;
1.7 misho 184: struct tagRPCCall *rpc;
185:
1.9.2.4 misho 186: memset(buf, 0, AIT_LEN(&c->cli_buf));
187: rlen = recv(TASK_FD(task), buf, AIT_LEN(&c->cli_buf), 0);
188: if (rlen < 1) {
189: /* close connection */
190: schedEvent(TASK_ROOT(task), closeClient, c, 0, NULL, 0);
1.7 misho 191: return NULL;
1.9 misho 192: }
1.7 misho 193:
1.8 misho 194: do {
1.9.2.4 misho 195: /* check RPC packet */
1.8 misho 196: if (rlen < sizeof(struct tagRPCCall)) {
197: rpc_SetErr(ERPCMISMATCH, "Too short RPC packet");
198:
1.9.2.4 misho 199: schedReadSelf(task);
1.8 misho 200: return NULL;
201: } else
202: rpc = (struct tagRPCCall*) (buf + off);
203:
1.9.2.4 misho 204: len = ntohs(rpc->call_len);
205: rlen -= len;
1.8 misho 206:
207: /* check integrity of packet */
208: crc = ntohs(rpc->call_crc);
209: rpc->call_crc ^= rpc->call_crc;
1.9.2.4 misho 210: if (crc != crcFletcher16((u_short*) (buf + off), len / 2)) {
1.8 misho 211: rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
1.7 misho 212:
1.9.2.4 misho 213: off += len;
1.8 misho 214: if (rlen < 1)
215: break;
216: else
217: continue;
218: }
1.7 misho 219:
1.8 misho 220: noreply = rpc->call_req.flags & RPC_NOREPLY;
1.7 misho 221:
1.8 misho 222: /* check RPC packet session info */
223: if (rpc_chkPktSession(&rpc->call_session, &s->srv_session)) {
224: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
225: rpc->call_argc ^= rpc->call_argc;
226: rpc->call_rep.ret = RPC_ERROR(-1);
227: rpc->call_rep.eno = RPC_ERROR(errno);
228: } else {
229: /* execute RPC call */
1.9.2.4 misho 230: schedEvent(TASK_ROOT(task), execCall, TASK_ARG(task), off, NULL, 0);
1.8 misho 231: }
1.7 misho 232:
1.8 misho 233: /* send RPC reply */
234: if (!noreply)
1.9.2.4 misho 235: schedWrite(TASK_ROOT(task), txPacket, TASK_ARG(task), TASK_FD(task), rpc, len);
1.7 misho 236:
1.9.2.4 misho 237: off += len;
1.8 misho 238: } while (rlen > 0);
1.7 misho 239:
240: /* lets get next packet */
1.9.2.4 misho 241: schedReadSelf(task);
1.7 misho 242: return NULL;
243: }
244:
1.1 misho 245: static void *
1.9.2.4 misho 246: acceptClients(sched_task_t *task)
1.1 misho 247: {
1.9.2.4 misho 248: rpc_srv_t *srv = TASK_ARG(task);
249: rpc_cli_t *c = NULL;
250: register int i;
251: socklen_t salen = sizeof(io_sockaddr_t);
1.1 misho 252:
1.9.2.4 misho 253: /* check free slots for connect */
254: for (i = 0; i < io_arraySize(srv->srv_clients) &&
255: (c = io_array(srv->srv_clients, i, rpc_cli_t*)); i++);
256: if (c) /* no more free slots! */
257: goto end;
258: c = malloc(sizeof(rpc_cli_t));
259: if (!c) {
1.5 misho 260: LOGERR;
1.9.2.4 misho 261: srv->srv_kill = kill;
1.7 misho 262: return NULL;
263: } else {
1.9.2.4 misho 264: memset(c, 0, sizeof(rpc_cli_t));
265: io_arraySet(srv->srv_clients, i, c);
266: c->cli_id = i;
267: c->cli_parent = srv;
1.7 misho 268: }
269:
1.9.2.6 misho 270: /* alloc empty buffer */
271: AIT_SET_BUF2(&c->cli_buf, 0, srv->srv_netbuf);
272:
1.9.2.4 misho 273: /* accept client */
274: c->cli_sock = accept(srv->srv_server.cli_sock, &c->cli_sa.sa, &salen);
275: if (c->cli_sock == -1) {
276: LOGERR;
1.9.2.6 misho 277: AIT_FREE_VAL(&c->cli_buf);
1.9.2.4 misho 278: io_arrayDel(srv->srv_clients, i, 42);
279: goto end;
280: } else
281: fcntl(c->cli_sock, F_SETFL, fcntl(c->cli_sock, F_GETFL) | O_NONBLOCK);
1.7 misho 282:
1.9.2.4 misho 283: schedRead(TASK_ROOT(task), rxPacket, c, c->cli_sock, NULL, 0);
284: end:
285: schedReadSelf(task);
1.7 misho 286: return NULL;
287: }
288:
1.9.2.4 misho 289: #if 0
1.7 misho 290: static void *
291: txBLOB(sched_task_t *task)
292: {
293: u_char *buf = TASK_DATA(task);
294: struct tagBLOBHdr *blob = (struct tagBLOBHdr *) buf;
295: int wlen = sizeof(struct tagBLOBHdr);
296:
297: /* calculate CRC */
298: blob->hdr_crc ^= blob->hdr_crc;
1.8 misho 299: blob->hdr_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
1.7 misho 300:
301: /* send reply */
302: wlen = send(TASK_FD(task), buf, wlen, 0);
303: if (wlen == -1)
304: LOGERR;
305: else if (wlen != sizeof(struct tagBLOBHdr))
306: rpc_SetErr(EPROCUNAVAIL, "RPC reply, should be send %d bytes, "
307: "really sended %d bytes", sizeof(struct tagBLOBHdr), wlen);
308:
309: return NULL;
310: }
1.4 misho 311:
1.7 misho 312: static void *
313: rxBLOB(sched_task_t *task)
314: {
315: rpc_cli_t *c = TASK_ARG(task);
316: rpc_srv_t *s = c->cli_parent;
317: rpc_blob_t *b;
318: u_char *buf = TASK_DATA(task);
319: struct tagBLOBHdr *blob = (struct tagBLOBHdr *) buf;
320: int rlen;
321: u_short crc;
322: struct timespec ts;
323:
324: /* check for disable service at this moment? */
1.8 misho 325: if (!s || s->srv_blob.state == disable) {
1.7 misho 326: usleep(100000);
327: #ifdef HAVE_PTHREAD_YIELD
328: pthread_yield();
329: #endif
330: schedRead(TASK_ROOT(task), rxBLOB, TASK_ARG(task), TASK_FD(task),
331: TASK_DATA(task), TASK_DATLEN(task));
332: return NULL;
333: }
1.5 misho 334:
1.7 misho 335: memset(buf, 0, TASK_DATLEN(task));
336: rlen = recv(TASK_FD(task), buf, TASK_DATLEN(task), 0);
337: if (rlen == -1) {
338: LOGERR;
1.9 misho 339: c->cli_kill = kill;
1.7 misho 340: return NULL;
1.9 misho 341: } else if (!rlen) { /* receive EOF */
342: c->cli_kill = kill;
1.7 misho 343: return NULL;
1.9 misho 344: }
1.6 misho 345:
1.7 misho 346: if (rlen < sizeof(struct tagBLOBHdr)) {
347: rpc_SetErr(ERPCMISMATCH, "Too short BLOB packet");
348: schedRead(TASK_ROOT(task), rxBLOB, TASK_ARG(task), TASK_FD(task),
349: TASK_DATA(task), TASK_DATLEN(task));
350: return NULL;
351: }
1.1 misho 352:
1.7 misho 353: /* check integrity of packet */
354: crc = ntohs(blob->hdr_crc);
355: blob->hdr_crc ^= blob->hdr_crc;
1.8 misho 356: if (crc != crcFletcher16((u_short*) buf, rlen / 2)) {
1.7 misho 357: rpc_SetErr(ERPCMISMATCH, "Bad CRC BLOB packet");
358: schedRead(TASK_ROOT(task), rxBLOB, TASK_ARG(task), TASK_FD(task),
359: TASK_DATA(task), TASK_DATLEN(task));
360: return NULL;
361: }
1.5 misho 362:
1.7 misho 363: /* check RPC packet session info */
364: if ((crc = rpc_chkPktSession(&blob->hdr_session, &s->srv_session))) {
365: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
366: blob->hdr_cmd = error;
367: goto end;
368: } else {
369: /* change socket timeout from last packet */
370: ts.tv_sec = blob->hdr_session.sess_timeout;
371: ts.tv_nsec = 0;
372: schedPolling(TASK_ROOT(task), &ts, NULL);
373: }
374:
375: /* Go to proceed packet ... */
376: switch (blob->hdr_cmd) {
377: case get:
378: if (!(b = rpc_srv_getBLOB(s, ntohl(blob->hdr_var)))) {
379: rpc_SetErr(EINVAL, "Var=%x not found", ntohl(blob->hdr_var));
380: blob->hdr_cmd = no;
381: blob->hdr_ret = RPC_ERROR(-1);
382: break;
383: } else
384: blob->hdr_len = htonl(b->blob_len);
1.5 misho 385:
1.7 misho 386: if (rpc_srv_blobMap(s, b) != -1) {
387: /* deliver BLOB variable to client */
388: blob->hdr_ret = htonl(rpc_srv_sendBLOB(c, b));
389: rpc_srv_blobUnmap(b);
390: } else {
391: blob->hdr_cmd = error;
392: blob->hdr_ret = RPC_ERROR(-1);
393: }
394: break;
395: case set:
396: if ((b = rpc_srv_registerBLOB(s, ntohl(blob->hdr_len)))) {
397: /* set new BLOB variable for reply :) */
398: blob->hdr_var = htonl(b->blob_var);
399:
400: /* receive BLOB from client */
401: blob->hdr_ret = htonl(rpc_srv_recvBLOB(c, b));
402: rpc_srv_blobUnmap(b);
1.5 misho 403: } else {
1.7 misho 404: blob->hdr_cmd = error;
405: blob->hdr_ret = RPC_ERROR(-1);
406: }
407: break;
408: case unset:
409: if (rpc_srv_unregisterBLOB(s, blob->hdr_var) == -1) {
410: blob->hdr_cmd = error;
411: blob->hdr_ret = RPC_ERROR(-1);
1.1 misho 412: }
413: break;
1.7 misho 414: default:
415: rpc_SetErr(EPROCUNAVAIL, "Unsupported BLOB command %d", blob->hdr_cmd);
416: blob->hdr_cmd = error;
417: blob->hdr_ret = RPC_ERROR(-1);
418: }
1.1 misho 419:
1.7 misho 420: end:
421: schedWrite(TASK_ROOT(task), txBLOB, TASK_ARG(task), TASK_FD(task),
422: TASK_DATA(task), TASK_DATLEN(task));
423: schedRead(TASK_ROOT(task), rxBLOB, TASK_ARG(task), TASK_FD(task),
424: TASK_DATA(task), TASK_DATLEN(task));
425: return NULL;
1.2 misho 426: }
427:
428: static void *
429: rpc_srv_dispatchVars(void *arg)
430: {
431: rpc_cli_t *c = arg;
432: rpc_srv_t *s;
1.7 misho 433: sched_root_task_t *root;
434: u_char *buf;
435: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
436:
1.2 misho 437: if (!arg) {
1.7 misho 438: rpc_SetErr(EINVAL, "Invalid parameter can`t procced BLOB");
1.2 misho 439: return NULL;
440: } else
441: s = c->cli_parent;
442:
1.7 misho 443: /* allocate net buffer */
444: buf = malloc(sizeof(struct tagBLOBHdr));
445: if (!buf) {
446: LOGERR;
447: return NULL;
448: }
1.2 misho 449:
1.7 misho 450: root = schedBegin();
451: if (!root) {
452: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
453: free(buf);
454: return NULL;
455: } else {
456: schedTermCondition(root, kill);
457: schedPolling(root, &ts, NULL);
458: }
1.4 misho 459:
1.7 misho 460: schedRead(root, rxBLOB, c, c->cli_sock, buf, sizeof(struct tagBLOBHdr));
1.2 misho 461:
1.9 misho 462: schedRun(root, (void*) &c->cli_kill);
1.7 misho 463: schedEnd(&root);
1.2 misho 464:
465: shutdown(c->cli_sock, SHUT_RDWR);
466: close(c->cli_sock);
467: memset(c, 0, sizeof(rpc_cli_t));
1.7 misho 468: free(buf);
469: return NULL;
1.1 misho 470: }
471:
472: // -------------------------------------------------
473:
474: /*
1.7 misho 475: * rpc_srv_initBLOBServer() - Init & create BLOB Server
476: *
1.4 misho 477: * @srv = RPC server instance
1.2 misho 478: * @Port = Port for bind server, if Port == 0 default port is selected
479: * @diskDir = Disk place for BLOB file objects
480: * return: -1 == error or 0 bind and created BLOB server instance
481: */
482: int
483: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port, const char *diskDir)
484: {
485: int n = 1;
1.6 misho 486: io_sockaddr_t sa;
1.2 misho 487:
488: if (!srv) {
1.7 misho 489: rpc_SetErr(EINVAL, "Invalid parameters can`t init BLOB server");
1.2 misho 490: return -1;
491: }
492: if (srv->srv_blob.state) {
1.7 misho 493: rpc_SetErr(EPERM, "Already started BLOB server!");
1.2 misho 494: return 0;
495: }
496:
497: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
498: if (access(diskDir, R_OK | W_OK) == -1) {
499: LOGERR;
500: return -1;
501: } else
1.7 misho 502: AIT_SET_STR(&srv->srv_blob.dir, diskDir);
1.2 misho 503:
504: srv->srv_blob.server.cli_tid = pthread_self();
505: srv->srv_blob.server.cli_parent = srv;
1.4 misho 506:
507: memcpy(&sa, &srv->srv_server.cli_sa, sizeof sa);
1.6 misho 508: switch (sa.sa.sa_family) {
1.4 misho 509: case AF_INET:
1.6 misho 510: sa.sin.sin_port = htons(Port ? Port : ntohs(sa.sin.sin_port) + 1);
1.4 misho 511: break;
512: case AF_INET6:
1.6 misho 513: sa.sin6.sin6_port = htons(Port ? Port : ntohs(sa.sin6.sin6_port) + 1);
1.4 misho 514: break;
515: case AF_LOCAL:
1.6 misho 516: strlcat(sa.sun.sun_path, ".blob", sizeof sa.sun.sun_path);
1.4 misho 517: break;
518: default:
1.7 misho 519: AIT_FREE_VAL(&srv->srv_blob.dir);
1.4 misho 520: return -1;
1.2 misho 521: }
1.6 misho 522: memcpy(&srv->srv_blob.server.cli_sa, &sa, sizeof sa);
1.2 misho 523:
1.4 misho 524: /* create BLOB server socket */
1.6 misho 525: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.2 misho 526: if (srv->srv_blob.server.cli_sock == -1) {
527: LOGERR;
1.7 misho 528: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 529: return -1;
530: }
531: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
532: LOGERR;
533: close(srv->srv_blob.server.cli_sock);
1.7 misho 534: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 535: return -1;
536: }
1.5 misho 537: n = srv->srv_netbuf;
538: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
539: LOGERR;
540: close(srv->srv_blob.server.cli_sock);
1.7 misho 541: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 542: return -1;
543: }
544: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
545: LOGERR;
546: close(srv->srv_blob.server.cli_sock);
1.7 misho 547: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 548: return -1;
549: }
1.6 misho 550: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa.sa,
551: srv->srv_blob.server.cli_sa.sa.sa_len) == -1) {
1.2 misho 552: LOGERR;
553: close(srv->srv_blob.server.cli_sock);
1.7 misho 554: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 555: return -1;
556: }
557:
1.4 misho 558: /* allocate pool for concurent clients */
1.2 misho 559: srv->srv_blob.clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
560: if (!srv->srv_blob.clients) {
561: LOGERR;
562: close(srv->srv_blob.server.cli_sock);
1.7 misho 563: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 564: return -1;
565: } else
566: memset(srv->srv_blob.clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
567:
568: rpc_srv_registerCall(srv, NULL, CALL_BLOBSHUTDOWN, 0);
1.5 misho 569: rpc_srv_registerCall(srv, NULL, CALL_BLOBCLIENTS, 1);
570: rpc_srv_registerCall(srv, NULL, CALL_BLOBVARS, 1);
571: rpc_srv_registerCall(srv, NULL, CALL_BLOBSTATE, 0);
1.2 misho 572:
1.4 misho 573: srv->srv_blob.state = enable; /* enable BLOB */
1.2 misho 574: return 0;
575: }
576:
577: /*
1.7 misho 578: * rpc_srv_endBLOBServer() - Destroy BLOB server, close all opened sockets and free resources
579: *
1.2 misho 580: * @srv = RPC Server instance
581: * return: none
582: */
583: void
584: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
585: {
586: rpc_cli_t *c;
587: register int i;
588: rpc_blob_t *f;
589:
1.8 misho 590: if (!srv || srv->srv_blob.state == disable)
1.2 misho 591: return;
1.8 misho 592: else
1.4 misho 593: srv->srv_blob.state = kill;
1.2 misho 594:
595: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBSHUTDOWN);
596: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBCLIENTS);
597: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBVARS);
598: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBSTATE);
599:
1.4 misho 600: /* close all clients connections & server socket */
1.2 misho 601: for (i = 0, c = srv->srv_blob.clients; i < srv->srv_numcli && c; i++, c++)
1.6 misho 602: if (c->cli_sa.sa.sa_family)
1.2 misho 603: shutdown(c->cli_sock, SHUT_RDWR);
604: close(srv->srv_blob.server.cli_sock);
605:
606: if (srv->srv_blob.clients) {
607: free(srv->srv_blob.clients);
608: srv->srv_blob.clients = NULL;
609: }
610:
1.4 misho 611: /* detach blobs */
1.2 misho 612: while ((f = srv->srv_blob.blobs)) {
613: srv->srv_blob.blobs = f->blob_next;
614: rpc_srv_blobFree(srv, f);
615: free(f);
616: }
617:
1.7 misho 618: AIT_FREE_VAL(&srv->srv_blob.dir);
619:
1.8 misho 620: /* at final, save disable BLOB service state */
621: srv->srv_blob.state = disable;
1.2 misho 622: }
623:
624: /*
1.7 misho 625: * rpc_srv_loopBLOB() - Execute Main BLOB server loop and wait for clients requests
626: *
1.2 misho 627: * @srv = RPC Server instance
628: * return: -1 error or 0 ok, infinite loop ...
629: */
630: int
1.5 misho 631: rpc_srv_loopBLOB(rpc_srv_t * __restrict srv)
1.2 misho 632: {
1.6 misho 633: socklen_t salen = sizeof(io_sockaddr_t);
1.2 misho 634: register int i;
635: rpc_cli_t *c;
636: int ret;
1.7 misho 637: pthread_attr_t attr;
1.9 misho 638: struct pollfd pfd;
1.2 misho 639:
1.4 misho 640: if (!srv || srv->srv_blob.state == kill) {
1.7 misho 641: rpc_SetErr(EINVAL, "Invalid parameter can`t start BLOB server");
1.2 misho 642: return -1;
643: }
644:
645: if (listen(srv->srv_blob.server.cli_sock, SOMAXCONN) == -1) {
646: LOGERR;
647: return -1;
1.9 misho 648: } else
649: fcntl(srv->srv_blob.server.cli_sock, F_SETFL,
650: fcntl(srv->srv_blob.server.cli_sock, F_GETFL) | O_NONBLOCK);
1.2 misho 651:
1.7 misho 652: pthread_attr_init(&attr);
653: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
654:
1.9 misho 655: pfd.fd = srv->srv_blob.server.cli_sock;
656: pfd.events = POLLIN | POLLPRI;
1.7 misho 657: /* main BLOB loop */
1.4 misho 658: while (srv->srv_blob.state != kill && srv->srv_kill != kill) {
1.2 misho 659: for (c = srv->srv_blob.clients, i = 0; i < srv->srv_numcli && c; i++, c++)
1.6 misho 660: if (!c->cli_sa.sa.sa_family)
1.2 misho 661: break;
662: if (i >= srv->srv_numcli) {
1.5 misho 663: #ifdef HAVE_PTHREAD_YIELD
664: pthread_yield();
665: #endif
1.9.2.1 misho 666: usleep(1000000);
1.2 misho 667: continue;
668: }
669:
1.9 misho 670: if ((ret = poll(&pfd, 1, srv->srv_session.sess_timeout * 1000)) == -1 ||
671: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
1.2 misho 672: LOGERR;
673: ret = 1;
674: break;
1.9 misho 675: } else if (!ret)
1.2 misho 676: continue;
677:
1.6 misho 678: c->cli_sock = accept(srv->srv_blob.server.cli_sock, &c->cli_sa.sa, &salen);
1.2 misho 679: if (c->cli_sock == -1) {
680: LOGERR;
681: continue;
682: } else
683: c->cli_parent = srv;
684:
1.7 misho 685: /* spawn dispatch thread for BLOB client */
1.9 misho 686: c->cli_kill = enable;
1.7 misho 687: if (pthread_create(&c->cli_tid, &attr, rpc_srv_dispatchVars, c)) {
1.2 misho 688: LOGERR;
689: continue;
1.7 misho 690: }
1.2 misho 691: }
692:
1.5 misho 693: srv->srv_blob.state = kill;
1.2 misho 694:
1.7 misho 695: pthread_attr_destroy(&attr);
1.2 misho 696: return 0;
697: }
1.9.2.4 misho 698: #endif
1.2 misho 699:
700:
701: /*
1.7 misho 702: * rpc_srv_initServer() - Init & create RPC Server
703: *
1.1 misho 704: * @regProgID = ProgramID for authentication & recognition
705: * @regProcID = ProcessID for authentication & recognition
706: * @concurentClients = Concurent clients at same time to this server
1.5 misho 707: * @netBuf = Network buffer length, if =0 == BUFSIZ (also meaning max RPC packet)
1.4 misho 708: * @csHost = Host name or address for bind server, if NULL any address
1.1 misho 709: * @Port = Port for bind server, if Port == 0 default port is selected
710: * return: NULL == error or !=NULL bind and created RPC server instance
711: */
712: rpc_srv_t *
1.9.2.6 misho 713: rpc_srv_initServer(u_int regProgID, u_char regProcID, int concurentClients,
1.9.2.4 misho 714: int netBuf, const char *csHost, u_short Port)
1.1 misho 715: {
716: int n = 1;
1.9.2.4 misho 717: rpc_srv_t *srv = NULL;
1.6 misho 718: io_sockaddr_t sa;
1.1 misho 719:
1.9.2.2 misho 720: if (!concurentClients || !regProgID) {
721: rpc_SetErr(EINVAL, "Invalid parameters can`t init RPC server");
1.1 misho 722: return NULL;
723: }
1.9.2.2 misho 724: if (!io_gethostbyname(csHost, Port, &sa))
725: return NULL;
1.1 misho 726: if (!Port)
727: Port = RPC_DEFPORT;
1.5 misho 728: if (!netBuf)
729: netBuf = BUFSIZ;
1.7 misho 730: else
731: netBuf = io_align(netBuf, 1); /* align netBuf length */
1.1 misho 732:
733: srv = malloc(sizeof(rpc_srv_t));
734: if (!srv) {
735: LOGERR;
736: return NULL;
737: } else
738: memset(srv, 0, sizeof(rpc_srv_t));
739:
1.5 misho 740: srv->srv_netbuf = netBuf;
1.1 misho 741: srv->srv_session.sess_version = RPC_VERSION;
742: srv->srv_session.sess_program = regProgID;
743: srv->srv_session.sess_process = regProcID;
744:
745: srv->srv_server.cli_parent = srv;
1.9.2.4 misho 746: memcpy(&srv->srv_server.cli_sa, &sa, sizeof srv->srv_server.cli_sa);
747:
748: /* init functions list */
749: TAILQ_INIT(&srv->srv_funcs);
750:
751: /* init scheduler */
752: srv->srv_root = schedBegin();
753: if (!srv->srv_root) {
754: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
755: free(srv);
756: return NULL;
757: }
758:
759: /* init pool for clients */
760: srv->srv_clients = io_arrayInit(concurentClients);
761: if (!srv->srv_clients) {
762: rpc_SetErr(io_GetErrno(), "%s", io_GetError());
763: schedEnd(&srv->srv_root);
764: free(srv);
765: return NULL;
766: }
1.4 misho 767:
768: /* create server socket */
1.9.2.2 misho 769: srv->srv_server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.1 misho 770: if (srv->srv_server.cli_sock == -1) {
771: LOGERR;
1.9.2.4 misho 772: io_arrayDestroy(&srv->srv_clients);
773: schedEnd(&srv->srv_root);
1.1 misho 774: free(srv);
775: return NULL;
776: }
777: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
778: LOGERR;
1.9.2.4 misho 779: goto err;
1.1 misho 780: }
1.5 misho 781: n = srv->srv_netbuf;
782: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
783: LOGERR;
1.9.2.4 misho 784: goto err;
1.5 misho 785: }
786: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
787: LOGERR;
1.9.2.4 misho 788: goto err;
1.5 misho 789: }
1.6 misho 790: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa.sa,
791: srv->srv_server.cli_sa.sa.sa_len) == -1) {
1.1 misho 792: LOGERR;
1.9.2.4 misho 793: goto err;
1.1 misho 794: }
795:
796: return srv;
1.9.2.4 misho 797: err: /* error condition */
798: close(srv->srv_server.cli_sock);
799: io_arrayDestroy(&srv->srv_clients);
800: schedEnd(&srv->srv_root);
801: free(srv);
802: return NULL;
1.1 misho 803: }
804:
805: /*
1.7 misho 806: * rpc_srv_endServer() - Destroy RPC server, close all opened sockets and free resources
807: *
1.6 misho 808: * @psrv = RPC Server instance
1.1 misho 809: * return: none
810: */
811: void
1.6 misho 812: rpc_srv_endServer(rpc_srv_t ** __restrict psrv)
1.1 misho 813: {
814: rpc_cli_t *c;
815: register int i;
1.9.2.4 misho 816: rpc_func_t *f, *tmp;
1.1 misho 817:
1.6 misho 818: if (!psrv || !*psrv) {
1.9.2.3 misho 819: rpc_SetErr(EINVAL, "Can`t destroy server because parameter is null!");
1.1 misho 820: return;
821: }
822:
1.4 misho 823: /* close all clients connections & server socket */
1.9.2.4 misho 824: for (i = 0; i < io_arraySize((*psrv)->srv_clients); i++) {
825: c = io_array((*psrv)->srv_clients, i, rpc_cli_t*);
826: if (c) {
1.1 misho 827: shutdown(c->cli_sock, SHUT_RDWR);
1.2 misho 828: close(c->cli_sock);
1.9.2.6 misho 829:
1.9.2.4 misho 830: free(c);
831: io_arrayDel((*psrv)->srv_clients, i, 0);
1.2 misho 832: }
1.1 misho 833: }
1.9.2.4 misho 834: io_arrayDestroy(&(*psrv)->srv_clients);
835:
836: close((*psrv)->srv_server.cli_sock);
1.1 misho 837:
1.4 misho 838: /* detach exported calls */
1.9.2.4 misho 839: TAILQ_FOREACH_SAFE(f, &(*psrv)->srv_funcs, func_node, tmp) {
1.6 misho 840: io_freeVars(&f->func_vars);
1.7 misho 841: AIT_FREE_VAL(&f->func_name);
1.2 misho 842: free(f);
843: }
844:
1.9.2.4 misho 845: schedEnd(&(*psrv)->srv_root);
1.6 misho 846: free(*psrv);
847: *psrv = NULL;
1.1 misho 848: }
849:
850: /*
1.7 misho 851: * rpc_srv_loopServer() - Execute Main server loop and wait for clients requests
852: *
1.1 misho 853: * @srv = RPC Server instance
854: * return: -1 error or 0 ok, infinite loop ...
855: */
856: int
1.5 misho 857: rpc_srv_loopServer(rpc_srv_t * __restrict srv)
1.1 misho 858: {
859: if (!srv) {
1.9.2.3 misho 860: rpc_SetErr(EINVAL, "Invalid parameter can`t start RPC server");
1.1 misho 861: return -1;
862: }
863:
1.9.2.6 misho 864: fcntl(srv->srv_server.cli_sock, F_SETFL,
865: fcntl(srv->srv_server.cli_sock, F_GETFL) | O_NONBLOCK);
866:
1.9.2.4 misho 867: if (listen(srv->srv_server.cli_sock, io_arraySize(srv->srv_clients)) == -1) {
1.1 misho 868: LOGERR;
869: return -1;
1.9.2.6 misho 870: }
1.7 misho 871:
1.9.2.4 misho 872: if (!schedRead(srv->srv_root, acceptClients, srv, srv->srv_server.cli_sock, NULL, 0)) {
873: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
874: return -1;
1.1 misho 875: }
876:
1.9.2.4 misho 877: /* main rpc loop */
878: schedRun(srv->srv_root, &srv->srv_kill);
1.1 misho 879: return 0;
880: }
881:
882:
883: /*
884: * rpc_srv_execCall() Execute registered call from RPC server
1.7 misho 885: *
1.1 misho 886: * @call = Register RPC call
887: * @rpc = IN RPC call structure
1.5 misho 888: * @args = IN RPC calling arguments from RPC client
1.1 misho 889: * return: -1 error, !=-1 ok
890: */
891: int
1.2 misho 892: rpc_srv_execCall(rpc_func_t * __restrict call, struct tagRPCCall * __restrict rpc,
1.5 misho 893: array_t * __restrict args)
1.1 misho 894: {
895: rpc_callback_t func;
896:
1.9.2.6 misho 897: if (!call || !rpc || !call->func_parent || !AIT_ADDR(&call->func_name)) {
1.7 misho 898: rpc_SetErr(EINVAL, "Invalid parameter can`t exec function");
1.1 misho 899: return -1;
900: }
901:
1.9.2.6 misho 902: func = AIT_GET_LIKE(&call->func_name, rpc_callback_t);
903: return func(call, ntohs(rpc->call_argc), args);
1.1 misho 904: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>