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