Annotation of libaitrpc/src/blob.c, revision 1.7
1.2 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.7 ! misho 6: * $Id: blob.c,v 1.6.2.3 2012/05/11 13:09:15 misho Exp $
1.2 misho 7: *
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.5 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: */
46: #include "global.h"
47:
48:
49: /*
1.5 misho 50: * rpc_srv_blobCreate() - Create map blob to memory region and return object
51: *
1.2 misho 52: * @srv = RPC Server instance
53: * @len = BLOB length object
54: * return: NULL error or !=NULL allocated BLOB object
55: */
56: inline rpc_blob_t *
57: rpc_srv_blobCreate(rpc_srv_t * __restrict srv, int len)
58: {
59: rpc_blob_t *blob = NULL;
60: char szFName[MAXPATHLEN];
61: int f;
62: u_int rnd;
63:
64: #ifdef HAVE_SRANDOMDEV
65: srandomdev();
66: #else
67: time_t tim;
68:
69: srandom((time(&tim) ^ getpid()));
70: #endif
71: again:
72: rnd = random() % UINT_MAX;
73:
1.3 misho 74: memset(szFName, 0, sizeof szFName);
1.5 misho 75: snprintf(szFName, sizeof szFName, BLOB_FILE, AIT_GET_STR(&srv->srv_blob.dir), rnd);
1.2 misho 76: f = open(szFName, O_CREAT | O_EXCL | O_RDWR, 0600);
77: if (f == -1) {
78: if (errno == EEXIST)
79: goto again;
80:
81: LOGERR;
82: return NULL;
83: }
84: if (lseek(f, len - 1, SEEK_SET) == -1) {
85: LOGERR;
86: close(f);
87: unlink(szFName);
88: return NULL;
89: } else
90: write(f, "", 1);
91:
92: blob = malloc(sizeof(rpc_blob_t));
93: if (!blob) {
94: LOGERR;
95: close(f);
96: unlink(szFName);
97: return NULL;
98: }
99:
100: blob->blob_data = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, f, 0);
101: if (blob->blob_data == MAP_FAILED) {
102: LOGERR;
103: free(blob);
104: close(f);
105: unlink(szFName);
106: return NULL;
107: } else
108: close(f);
109:
110: blob->blob_len = len;
111: blob->blob_var = rnd;
112: return blob;
113: }
114:
115: /*
1.5 misho 116: * rpc_srv_blobMap() - Map blob to memory region
117: *
1.2 misho 118: * @srv = RPC Server instance
119: * @blob = Map to this BLOB element
120: * return: -1 error or 0 ok
121: */
122: inline int
123: rpc_srv_blobMap(rpc_srv_t * __restrict srv, rpc_blob_t * __restrict blob)
124: {
125: int f;
126: char szFName[MAXPATHLEN];
127:
128: if (!blob) {
1.5 misho 129: rpc_SetErr(EINVAL, "Invalid argument BLOB");
1.2 misho 130: return -1;
131: }
1.4 misho 132: if (blob->blob_data) {
1.5 misho 133: rpc_SetErr(EPERM, "Already mmapped object found!");
1.4 misho 134: return -1;
135: }
1.2 misho 136:
1.3 misho 137: memset(szFName, 0, sizeof szFName);
1.5 misho 138: snprintf(szFName, sizeof szFName, BLOB_FILE, AIT_GET_STR(&srv->srv_blob.dir), blob->blob_var);
1.2 misho 139: f = open(szFName, O_RDWR);
140: if (f == -1) {
141: LOGERR;
142: return -1;
143: }
144:
145: blob->blob_data = mmap(NULL, blob->blob_len, PROT_READ | PROT_WRITE, MAP_SHARED, f, 0);
146: if (blob->blob_data == MAP_FAILED) {
147: LOGERR;
148: close(f);
149: blob->blob_data = NULL;
150: return -1;
151: } else {
152: close(f);
153:
154: madvise(blob->blob_data, blob->blob_len, MADV_SEQUENTIAL);
155: }
156:
157: return 0;
158: }
159:
160: /*
1.5 misho 161: * rpc_srv_blobUnmap() - Unmap blob memory region
162: *
1.2 misho 163: * @blob = Mapped BLOB element
164: * return: none
165: */
166: inline void
167: rpc_srv_blobUnmap(rpc_blob_t * __restrict blob)
168: {
169: if (!blob || !blob->blob_data)
1.5 misho 170: rpc_SetErr(EINVAL, "Invalid arguments");
1.2 misho 171: else {
172: munmap(blob->blob_data, blob->blob_len);
173: blob->blob_data = NULL;
174: }
175: }
176:
177: /*
1.5 misho 178: * rpc_srv_blobFree() - Free blob from disk & memory
179: *
1.2 misho 180: * @srv = RPC Server instance
181: * @blob = Mapped BLOB element
182: * return: -1 error or 0 ok
183: */
184: inline int
185: rpc_srv_blobFree(rpc_srv_t * __restrict srv, rpc_blob_t * __restrict blob)
186: {
187: char szFName[MAXPATHLEN];
188:
189: if (!blob) {
1.5 misho 190: rpc_SetErr(EINVAL, "Invalid argument BLOB");
1.2 misho 191: return -1;
192: }
193:
194: if (blob->blob_data)
195: rpc_srv_blobUnmap(blob);
196:
1.3 misho 197: memset(szFName, 0, sizeof szFName);
1.5 misho 198: snprintf(szFName, sizeof szFName, BLOB_FILE, AIT_GET_STR(&srv->srv_blob.dir), blob->blob_var);
1.3 misho 199: if (unlink(szFName) == -1) {
1.2 misho 200: LOGERR;
201: return -1;
202: }
203:
204: return 0;
205: }
206:
207: // ------------------------------------------------------------
208:
209: /*
1.5 misho 210: * rpc_srv_sendBLOB() - Send mapped BLOB to client
211: *
1.2 misho 212: * @cli = Client instance
213: * @blob = Mapped BLOB element
214: * return: -1 error, 0 ok
215: */
216: int
217: rpc_srv_sendBLOB(rpc_cli_t * __restrict cli, rpc_blob_t * __restrict blob)
218: {
219: int ret, len;
220: uint8_t *pos;
221:
222: if (!cli || !blob || !blob->blob_data) {
1.5 misho 223: rpc_SetErr(EINVAL, "Invalid arguments");
1.2 misho 224: return -1;
225: }
226:
1.3 misho 227: for (ret = blob->blob_len, pos = blob->blob_data; ret > 0; ret -= len, pos += len) {
228: len = send(cli->cli_sock, pos, ret, 0);
229: if (len == -1) {
1.2 misho 230: LOGERR;
231: return -1;
232: }
1.3 misho 233: }
1.2 misho 234:
235: return ret;
236: }
237:
238: /*
1.5 misho 239: * rpc_srv_recvBLOB() - Receive BLOB from client
240: *
1.2 misho 241: * @cli = Client instance
242: * @blob = Mapped BLOB element
243: * return: -1 error, 0 ok, >0 unreceived data from client, may be error?
244: */
245: int
246: rpc_srv_recvBLOB(rpc_cli_t * __restrict cli, rpc_blob_t * __restrict blob)
247: {
248: int ret, len;
249: uint8_t *pos;
1.7 ! misho 250: struct pollfd pfd;
1.2 misho 251:
252: if (!cli || !blob || !blob->blob_data) {
1.5 misho 253: rpc_SetErr(EINVAL, "Invalid arguments");
1.2 misho 254: return -1;
1.7 ! misho 255: }
1.2 misho 256:
1.7 ! misho 257: pfd.fd = cli->cli_sock;
! 258: pfd.events = POLLIN | POLLPRI;
1.2 misho 259: for (ret = blob->blob_len, pos = blob->blob_data; ret > 0; ret -= len, pos += len) {
1.7 ! misho 260: if ((len = poll(&pfd, 1, ((rpc_sess_t*) cli->cli_parent)->sess_timeout * 1000)) < 1 ||
! 261: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
1.2 misho 262: LOGERR;
263: return -1;
264: }
265:
1.3 misho 266: len = recv(cli->cli_sock, pos, ret, 0);
267: if (len == -1) {
1.2 misho 268: LOGERR;
269: return -1;
270: }
271: }
272:
273: return ret;
274: }
275:
276: // ------------------------------------------------------------
277:
278: /*
1.5 misho 279: * rpc_cli_sendBLOB() - Send BLOB to server
280: *
1.2 misho 281: * @cli = Client instance
282: * @var = BLOB variable
283: * @data = BLOB data
284: * return: -1 error, 0 ok, 1 remote error
285: */
286: int
1.4 misho 287: rpc_cli_sendBLOB(rpc_cli_t * __restrict cli, ait_val_t * __restrict var, void * __restrict data)
1.2 misho 288: {
289: int ret, len;
290: uint8_t *pos;
291: struct tagBLOBHdr hdr;
1.7 ! misho 292: struct pollfd pfd;
1.2 misho 293:
294: if (!cli || !var || !data) {
1.5 misho 295: rpc_SetErr(EINVAL, "Invalid arguments");
1.2 misho 296: return -1;
1.7 ! misho 297: }
1.2 misho 298:
1.5 misho 299: rpc_addPktSession(&hdr.hdr_session, cli->cli_parent);
1.2 misho 300: hdr.hdr_cmd = set;
301: hdr.hdr_var = 0;
302: hdr.hdr_ret = 0;
1.5 misho 303: hdr.hdr_len = htonl(AIT_LEN(var));
304: /* calculate CRC */
305: hdr.hdr_crc ^= hdr.hdr_crc;
1.6 misho 306: hdr.hdr_crc = htons(crcFletcher16((u_short*) &hdr, sizeof hdr / 2));
1.5 misho 307:
308: /* send SET request */
1.2 misho 309: if (send(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
310: LOGERR;
311: return -1;
312: }
313:
1.4 misho 314: /* send BLOB to server */
315: for (ret = AIT_LEN(var), pos = data; ret > 0; ret -= len, pos += len)
1.3 misho 316: if ((len = send(cli->cli_sock, pos, ret, 0)) == -1) {
1.2 misho 317: LOGERR;
318: return -1;
319: }
320:
1.5 misho 321: /* wait for reply */
1.7 ! misho 322: pfd.fd = cli->cli_sock;
! 323: pfd.events = POLLIN | POLLPRI;
! 324: if ((ret = poll(&pfd, 1, ((rpc_sess_t*) cli->cli_parent)->sess_timeout * 1000)) < 1 ||
! 325: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
! 326: if (ret)
1.2 misho 327: LOGERR;
1.7 ! misho 328: else
! 329: rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond");
! 330: return -1;
1.2 misho 331: }
332: if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
333: LOGERR;
1.5 misho 334: return 1;
335: }
336: /* check CRC */
337: ret = ntohs(hdr.hdr_crc);
338: hdr.hdr_crc ^= hdr.hdr_crc;
1.6 misho 339: if (ret != crcFletcher16((u_short*) &hdr, sizeof hdr / 2)) {
1.5 misho 340: rpc_SetErr(ERPCMISMATCH, "Bad CRC BLOB packet");
341: return 1;
1.2 misho 342: }
1.5 misho 343:
1.2 misho 344: if (hdr.hdr_cmd != error) {
1.5 misho 345: if (ntohl(hdr.hdr_len) != AIT_LEN(var)) {
346: rpc_SetErr(ECANCELED, "Bad return length packet");
347: return 1;
1.2 misho 348: }
349:
1.5 misho 350: var->val.blob = ntohl(hdr.hdr_var);
1.2 misho 351: }
352:
353: return hdr.hdr_cmd == error;
354: }
355:
356: /*
1.5 misho 357: * rpc_cli_recvBLOB() - Receive BLOB from server
358: *
1.2 misho 359: * @cli = Client instance
360: * @var = BLOB variable
361: * @data = BLOB data, must be free after use!
362: * return: -1 error, 0 ok, 1 remote error
363: */
364: int
1.4 misho 365: rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t * __restrict var, void ** __restrict data)
1.2 misho 366: {
367: int ret, len;
368: uint8_t *pos;
1.7 ! misho 369: struct pollfd pfd;
1.2 misho 370: struct tagBLOBHdr hdr;
371:
372: if (!cli || !var || !data) {
1.5 misho 373: rpc_SetErr(EINVAL, "Invalid arguments");
1.2 misho 374: return -1;
1.7 ! misho 375: }
1.2 misho 376:
1.4 misho 377: *data = malloc(AIT_LEN(var));
1.2 misho 378: if (!*data) {
379: LOGERR;
380: return -1;
381: } else
1.4 misho 382: memset(*data, 0, AIT_LEN(var));
1.2 misho 383:
1.5 misho 384: rpc_addPktSession(&hdr.hdr_session, cli->cli_parent);
1.2 misho 385: hdr.hdr_cmd = get;
1.5 misho 386: hdr.hdr_var = htonl((uint32_t) AIT_GET_BLOB(var));
1.2 misho 387: hdr.hdr_ret = 0;
388: hdr.hdr_len = 0;
1.5 misho 389: /* calculate CRC */
390: hdr.hdr_crc ^= hdr.hdr_crc;
1.6 misho 391: hdr.hdr_crc = htons(crcFletcher16((u_short*) &hdr, sizeof hdr / 2));
1.5 misho 392:
393: /* send GET request */
1.2 misho 394: if (send(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
395: LOGERR;
396: free(*data);
397: *data = NULL;
398: return -1;
399: }
400:
1.4 misho 401: /* receive BLOB from server */
1.7 ! misho 402: pfd.fd = cli->cli_sock;
! 403: pfd.events = POLLIN | POLLPRI;
1.4 misho 404: for (ret = AIT_LEN(var), pos = *data; ret > 0; ret -= len, pos += len) {
1.7 ! misho 405: if ((len = poll(&pfd, 1, ((rpc_sess_t*) cli->cli_parent)->sess_timeout * 1000)) < 1 ||
! 406: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
1.2 misho 407: LOGERR;
408: free(*data);
409: *data = NULL;
410: return -1;
411: }
412:
1.3 misho 413: if ((len = recv(cli->cli_sock, pos, ret, 0)) == -1) {
1.2 misho 414: LOGERR;
415: free(*data);
416: *data = NULL;
417: return -1;
418: }
419: }
420:
1.5 misho 421: /* wait for reply */
1.7 ! misho 422: if ((len = poll(&pfd, 1, ((rpc_sess_t*) cli->cli_parent)->sess_timeout * 1000)) < 1 ||
! 423: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
! 424: if (len)
1.2 misho 425: LOGERR;
1.7 ! misho 426: else
! 427: rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond");
! 428: free(*data);
! 429: *data = NULL;
! 430: return 1;
1.2 misho 431: }
432: if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
433: LOGERR;
434: free(*data);
435: *data = NULL;
1.5 misho 436: return 1;
437: }
438: /* check CRC */
439: ret = ntohs(hdr.hdr_crc);
440: hdr.hdr_crc ^= hdr.hdr_crc;
1.6 misho 441: if (ret != crcFletcher16((u_short*) &hdr, sizeof hdr / 2)) {
1.5 misho 442: rpc_SetErr(ERPCMISMATCH, "Bad CRC BLOB packet");
443: free(*data);
444: *data = NULL;
445: return 1;
1.2 misho 446: }
447: if (hdr.hdr_cmd != error) {
1.5 misho 448: if (ntohl(hdr.hdr_len) != AIT_LEN(var)) {
449: rpc_SetErr(ECANCELED, "Bad return length packet");
1.2 misho 450: free(*data);
451: *data = NULL;
1.5 misho 452: return 1;
1.2 misho 453: }
454: }
455:
456: return hdr.hdr_cmd == error;
457: }
458:
459: /*
1.5 misho 460: * rpc_cli_delBLOB() - Delete BLOB from server
461: *
1.2 misho 462: * @cli = Client instance
463: * @var = BLOB variable
464: * return: -1 error, 0 ok, 1 remote error
465: */
466: int
1.4 misho 467: rpc_cli_delBLOB(rpc_cli_t * __restrict cli, ait_val_t * __restrict var)
1.2 misho 468: {
469: struct tagBLOBHdr hdr;
1.7 ! misho 470: struct pollfd pfd;
1.5 misho 471: int ret;
1.2 misho 472:
473: if (!cli || !var) {
1.5 misho 474: rpc_SetErr(EINVAL, "Invalid arguments");
1.2 misho 475: return -1;
1.7 ! misho 476: }
1.2 misho 477:
1.5 misho 478: rpc_addPktSession(&hdr.hdr_session, cli->cli_parent);
1.2 misho 479: hdr.hdr_cmd = unset;
1.5 misho 480: hdr.hdr_var = htonl((uint32_t) AIT_GET_BLOB(var));
1.2 misho 481: hdr.hdr_ret = 0;
482: hdr.hdr_len = 0;
1.5 misho 483: /* calculate CRC */
484: hdr.hdr_crc ^= hdr.hdr_crc;
1.6 misho 485: hdr.hdr_crc = htons(crcFletcher16((u_short*) &hdr, sizeof hdr / 2));
1.5 misho 486:
487: /* send UNSET request */
1.2 misho 488: if (send(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
489: LOGERR;
490: return -1;
491: }
492:
1.5 misho 493: /* wait for reply */
1.7 ! misho 494: pfd.fd = cli->cli_sock;
! 495: pfd.events = POLLIN | POLLPRI;
! 496: if ((ret = poll(&pfd, 1, ((rpc_sess_t*) cli->cli_parent)->sess_timeout * 1000)) < 1 ||
! 497: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
! 498: if (ret)
1.2 misho 499: LOGERR;
1.7 ! misho 500: else
! 501: rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond");
! 502: return 1;
1.2 misho 503: }
504: if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
505: LOGERR;
1.5 misho 506: return 1;
507: }
508: /* check CRC */
509: ret = ntohs(hdr.hdr_crc);
510: hdr.hdr_crc ^= hdr.hdr_crc;
1.6 misho 511: if (ret != crcFletcher16((u_short*) &hdr, sizeof hdr / 2)) {
1.5 misho 512: rpc_SetErr(ERPCMISMATCH, "Bad CRC BLOB packet");
513: return 1;
1.2 misho 514: }
515:
516: return hdr.hdr_cmd == error;
517: }
518:
519: /*
1.5 misho 520: * rpc_cli_getBLOB() - Receive BLOB from server and Delete after that
521: *
1.2 misho 522: * @cli = Client instance
523: * @var = BLOB variable
524: * @data = BLOB data, must be free after use!
525: * return: -1 error, 0 ok, 1 remote error
526: */
527: inline int
1.4 misho 528: rpc_cli_getBLOB(rpc_cli_t * __restrict cli, ait_val_t * __restrict var, void ** __restrict data)
1.2 misho 529: {
530: int ret;
531:
532: ret = rpc_cli_recvBLOB(cli, var, data);
533: ret |= rpc_cli_delBLOB(cli, var) > 0 ? 2 : 0;
534:
535: return ret;
536: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>