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