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