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