Annotation of libaitrpc/src/cli.c, revision 1.1.1.1.2.6
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.1.1.1.2.6! misho 6: * $Id: cli.c,v 1.1.1.1.2.5 2010/07/08 07:16:36 misho Exp $
1.1 misho 7: *
8: *************************************************************************/
9: #include "global.h"
10:
11:
12: /*
1.1.1.1.2.4 misho 13: * rpc_cli_openBLOBClient() Connect to BLOB Server
14: * @rpccli = RPC Client session
15: * @Port = Port for bind server, if Port == 0 default port is selected
16: * return: NULL == error or !=NULL connection to BLOB server established
17: */
18: rpc_cli_t *
19: rpc_cli_openBLOBClient(rpc_cli_t * __restrict rpccli, u_short Port)
20: {
21: rpc_cli_t *cli = NULL;
22: struct sockaddr_in sin;
23: struct sockaddr_in6 sin6;
24:
25: if (!rpccli || (rpccli->cli_sa.sa_family != AF_INET && rpccli->cli_sa.sa_family != AF_INET6)) {
26: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t connect to BLOB server ...\n");
27: return NULL;
28: }
29: if (!Port)
30: Port = RPC_DEFPORT + 1;
31:
32: cli = malloc(sizeof(rpc_cli_t));
33: if (!cli) {
34: LOGERR;
35: return NULL;
36: } else
37: memcpy(cli, rpccli, sizeof(rpc_cli_t));
38:
39: if (rpccli->cli_sa.sa_family == AF_INET) {
40: memcpy(&sin, &rpccli->cli_sa, sizeof sin);
41: sin.sin_port = htons(Port);
42: memcpy(&cli->cli_sa, &sin, sizeof(struct sockaddr));
43: } else {
44: memcpy(&sin6, &rpccli->cli_sa, sizeof sin6);
45: sin6.sin6_port = htons(Port);
46: memcpy(&cli->cli_sa, &sin6, sizeof(struct sockaddr));
47: }
48:
49: cli->cli_sock = socket(cli->cli_sa.sa_family, SOCK_STREAM, 0);
50: if (cli->cli_sock == -1) {
51: LOGERR;
52: free(cli);
53: return NULL;
54: }
55: if (connect(cli->cli_sock, &cli->cli_sa, sizeof cli->cli_sa) == -1) {
56: LOGERR;
57: free(cli);
58: return NULL;
59: }
60:
61: return cli;
62: }
63:
64: /*
65: * rpc_cli_closeBLOBClient() Close connection to BLOB server and free resources
66: * @cli = BLOB Client session
67: * return: none
68: */
69: void
70: rpc_cli_closeBLOBClient(rpc_cli_t * __restrict cli)
71: {
72: if (!cli) {
73: rpc_SetErr(EINVAL, "Error:: Can`t close connection because parameter is null!\n");
74: return;
75: }
76:
77: shutdown(cli->cli_sock, SHUT_RDWR);
78: close(cli->cli_sock);
79:
80: free(cli);
81: cli = NULL;
82: }
83:
84: // --------------------------------------------------------------
85:
86: /*
1.1 misho 87: * rpc_cli_openClient() Connect to RPC Server
88: * @ProgID = ProgramID for RPC session request
89: * @ProcID = ProcessID for RPC session request
90: * @family = Family socket type, AF_INET or AF_INET6
91: * @csHost = Host name or IP address for bind server
92: * @Port = Port for bind server, if Port == 0 default port is selected
93: * return: NULL == error or !=NULL connection to RPC server established
94: */
95: rpc_cli_t *
96: rpc_cli_openClient(u_int ProgID, u_int ProcID, u_short family, const char *csHost, u_short Port)
97: {
98: rpc_cli_t *cli = NULL;
99: struct hostent *host = NULL;
100: struct sockaddr_in sin;
101: struct sockaddr_in6 sin6;
102:
103: if (!csHost || (family != AF_INET && family != AF_INET6)) {
104: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t connect to RPC server ...\n");
105: return NULL;
106: }
107: if (!Port)
108: Port = RPC_DEFPORT;
109: if (csHost) {
110: host = gethostbyname2(csHost, family);
111: if (!host) {
112: rpc_SetErr(h_errno, "Error:: %s\n", hstrerror(h_errno));
113: return NULL;
114: }
115: }
116: switch (family) {
117: case AF_INET:
118: memset(&sin, 0, sizeof sin);
119: sin.sin_len = sizeof sin;
120: sin.sin_family = family;
121: sin.sin_port = htons(Port);
122: if (csHost)
123: memcpy(&sin.sin_addr, host->h_addr, host->h_length);
124: break;
125: case AF_INET6:
126: memset(&sin6, 0, sizeof sin6);
127: sin6.sin6_len = sizeof sin6;
128: sin6.sin6_family = family;
129: sin6.sin6_port = htons(Port);
130: if (csHost)
131: memcpy(&sin6.sin6_addr, host->h_addr, host->h_length);
132: break;
133: default:
134: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t connect to RPC server ...\n");
135: return NULL;
136: }
137:
138: cli = malloc(sizeof(rpc_cli_t));
139: if (!cli) {
140: LOGERR;
141: return NULL;
142: } else
143: memset(cli, 0, sizeof(rpc_cli_t));
144: cli->cli_parent = malloc(sizeof(rpc_sess_t));
145: if (!cli->cli_parent) {
146: LOGERR;
147: free(cli);
148: return NULL;
149: } else {
150: ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
151: ((rpc_sess_t*) cli->cli_parent)->sess_program = ProgID;
152: ((rpc_sess_t*) cli->cli_parent)->sess_process = ProcID;
153: }
154:
155: if (family == AF_INET)
156: memcpy(&cli->cli_sa, &sin, sizeof cli->cli_sa);
157: else
158: memcpy(&cli->cli_sa, &sin6, sizeof cli->cli_sa);
159: cli->cli_sock = socket(family, SOCK_STREAM, 0);
160: if (cli->cli_sock == -1) {
161: LOGERR;
162: free(cli->cli_parent);
163: free(cli);
164: return NULL;
165: }
166: if (connect(cli->cli_sock, &cli->cli_sa, sizeof cli->cli_sa) == -1) {
167: LOGERR;
168: free(cli->cli_parent);
169: free(cli);
170: return NULL;
171: }
172:
173: return cli;
174: }
175:
176: /*
177: * rpc_cli_closeClient() Close connection to RPC server and free resources
178: * @cli = RPC Client session
179: * return: none
180: */
181: void
182: rpc_cli_closeClient(rpc_cli_t * __restrict cli)
183: {
184: if (!cli) {
185: rpc_SetErr(EINVAL, "Error:: Can`t close connection because parameter is null!\n");
186: return;
187: }
188:
189: shutdown(cli->cli_sock, SHUT_RDWR);
190: close(cli->cli_sock);
191:
192: if (cli->cli_parent)
193: free(cli->cli_parent);
194: free(cli);
195: cli = NULL;
196: }
197:
198:
199: /*
200: * rpc_cli_execCall() Execute RPC call
201: * @cli = RPC Client session
202: * @csModule = Module name, if NULL self binary
203: * @csFunc = Function name for execute
204: * @in_argc = IN count of arguments
205: * @in_vals = IN RPC call array of rpc values
206: * @out_argc = OUT returned count of arguments
207: * @out_vals = OUT returned array of rpc values, must be free after use (see rpc_cli_freeVals())
208: * return: -1 error or != -1 ok result
209: */
210: int
211: rpc_cli_execCall(rpc_cli_t *cli, const char *csModule, const char *csFunc, int in_argc,
212: rpc_val_t * __restrict in_vals, int *out_argc, rpc_val_t ** __restrict out_vals)
213: {
214: fd_set fds;
215: u_char buf[BUFSIZ], str[MAXPATHLEN + UCHAR_MAX + 1], *data;
216: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
217: struct tagRPCRet *rrpc;
218: int ret = 0, Limit = 0;
219: register int i;
220: rpc_val_t *v;
1.1.1.1.2.2 misho 221: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.1 misho 222:
223: FD_ZERO(&fds);
224: memset(buf, 0, BUFSIZ);
225: memset(str, 0, MAXPATHLEN + UCHAR_MAX + 1);
226: if (!cli || !csFunc || (in_argc && !in_vals)) {
227: rpc_SetErr(EINVAL, "Error:: Can`t execute call because parameter is null or invalid!\n");
228: return -1;
229: }
230: if (csModule)
231: strlcpy((char*) str, csModule, MAXPATHLEN + UCHAR_MAX + 1);
232: strlcat((char*) str, "__", MAXPATHLEN + UCHAR_MAX + 1);
233: strlcat((char*) str, csFunc, MAXPATHLEN + UCHAR_MAX + 1);
234:
235: memcpy(&rpc->call_session, cli->cli_parent, sizeof rpc->call_session);
236: rpc->call_argc = in_argc;
237: rpc->call_tag = crcFletcher16((u_short*) str, (MAXPATHLEN + UCHAR_MAX + 1) / 2);
238: rpc->call_hash = hash_fnv((char*) str, MAXPATHLEN + UCHAR_MAX + 1);
239: Limit = sizeof(struct tagRPCCall);
240: if (in_argc) {
241: v = (rpc_val_t*) (buf + sizeof(struct tagRPCCall));
242: memcpy(v, in_vals, in_argc * sizeof(rpc_val_t));
243: Limit += in_argc * sizeof(rpc_val_t);
244: data = (u_char*) v + in_argc * sizeof(rpc_val_t);
245: for (i = 0; i < in_argc; i++) {
246: switch (in_vals[i].val_type) {
247: case buffer:
248: if (Limit + in_vals[i].val_len > BUFSIZ) {
249: ret = -7;
250: break;
251: }
252:
253: memcpy(data, in_vals[i].val.buffer, in_vals[i].val_len);
254: v[i].val.buffer = (uint8_t*) ((void*) data - (void*) v);
255: data += in_vals[i].val_len;
256: Limit += in_vals[i].val_len;
257: break;
258: case string:
259: if (Limit + in_vals[i].val_len + 1 > BUFSIZ) {
260: ret = -7;
261: break;
262: }
263:
264: memcpy(data, in_vals[i].val.string, in_vals[i].val_len);
265: v[i].val.string = (int8_t*) ((void*) data - (void*) v);
1.1.1.1.2.6! misho 266: data += in_vals[i].val_len + 1;
! 267: Limit += in_vals[i].val_len + 1;
1.1 misho 268: break;
269: default:
270: break;
271: }
272: }
273: if (ret < 0) {
274: rpc_SetErr(EMSGSIZE, "Error:: in prepare RPC packet (-7) ...\n");
275: return ret;
276: }
277: }
278: if ((ret = send(cli->cli_sock, buf, Limit, 0)) == -1) {
279: LOGERR;
280: return -1;
281: }
282: if (ret != Limit) {
283: rpc_SetErr(EBADMSG, "Error:: in send RPC request, should be send %d bytes, really is %d\n",
284: Limit, ret);
285: return -9;
286: }
287:
288: FD_SET(cli->cli_sock, &fds);
289: if ((ret = select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) < 1) {
290: if (ret) {
291: LOGERR;
292: } else
293: rpc_SetErr(ETIMEDOUT, "Error:: timeout, no return from RPC server?\n");
294: return -1;
295: }
296: memset(buf, 0, BUFSIZ);
297: if ((ret = recv(cli->cli_sock, buf, BUFSIZ, 0)) == -1) {
298: LOGERR;
299: return -3;
300: }
301: if (!ret) // receive EOF
302: return 0;
1.1.1.1.2.6! misho 303: if (ret < sizeof(struct tagRPCRet)) {
1.1 misho 304: rpc_SetErr(EMSGSIZE, "Error:: too short RPC packet ...\n");
305: return -4;
306: } else
307: rrpc = (struct tagRPCRet*) buf;
308: // check RPC packet session info
309: if (memcmp(&rrpc->ret_session, cli->cli_parent, sizeof rrpc->ret_session)) {
310: rpc_SetErr(EINVAL, "Error:: get invalid RPC session ...\n");
311: return -5;
1.1.1.1.2.6! misho 312: } else
! 313: Limit = sizeof(struct tagRPCRet);
1.1 misho 314: if (rrpc->ret_retcode < 0 && rrpc->ret_errno) {
1.1.1.1.2.5 misho 315: rpc_SetErr(rrpc->ret_errno, "Error::Server side: %d %s\n",
316: rrpc->ret_retcode, strerror(rrpc->ret_errno));
1.1 misho 317: return -6;
318: }
1.1.1.1.2.6! misho 319: if (rrpc->ret_argc * sizeof(rpc_val_t) > BUFSIZ - Limit) {
! 320: rpc_SetErr(EMSGSIZE, "Error:: reply RPC packet is too big ...\n");
! 321: return -7;
! 322: } else
! 323: Limit += rrpc->ret_argc * sizeof(rpc_val_t);
1.1 misho 324: // RPC is OK! Go decapsulate variables ...
325: if (rrpc->ret_argc) {
326: *out_argc = rrpc->ret_argc;
327: *out_vals = calloc(rrpc->ret_argc, sizeof(rpc_val_t));
328: if (!*out_vals) {
329: LOGERR;
330: *out_argc = 0;
331: return -1;
332: } else
1.1.1.1.2.6! misho 333: memcpy(*out_vals, buf + sizeof(struct tagRPCRet), Limit - sizeof(struct tagRPCRet));
1.1 misho 334: // RPC received variables types OK!
1.1.1.1.2.6! misho 335: data = (u_char*) buf + Limit;
1.1 misho 336: for (i = 0; i < rrpc->ret_argc; i++)
337: switch ((*out_vals)[i].val_type) {
338: case buffer:
1.1.1.1.2.6! misho 339: if ((*out_vals)[i].val_len > BUFSIZ - Limit) {
! 340: rpc_SetErr(EMSGSIZE, "Error:: Too big RPC packet ...\n");
! 341: free(*out_vals);
! 342: *out_vals = NULL;
! 343: *out_argc = 0;
! 344: return -7;
! 345: } else
! 346: Limit += (*out_vals)[i].val_len;
! 347:
1.1 misho 348: (*out_vals)[i].val.buffer = malloc((*out_vals)[i].val_len);
349: if (!(*out_vals)[i].val.buffer) {
350: rpc_SetErr(errno, "Error:: in prepare RPC reply ...\n");
351: free(*out_vals);
352: *out_vals = NULL;
353: *out_argc = 0;
354: return -1;
355: } else
356: memcpy((*out_vals)[i].val.buffer, data, (*out_vals)[i].val_len);
357: data += (*out_vals)[i].val_len;
358: break;
359: case string:
1.1.1.1.2.6! misho 360: if ((*out_vals)[i].val_len + 1 > BUFSIZ - Limit) {
! 361: rpc_SetErr(EMSGSIZE, "Error:: Too big RPC packet ...\n");
! 362: free(*out_vals);
! 363: *out_vals = NULL;
! 364: *out_argc = 0;
! 365: return -7;
! 366: } else
! 367: Limit += (*out_vals)[i].val_len + 1;
! 368:
1.1 misho 369: (*out_vals)[i].val.string = (int8_t*) strdup((char*) data);
370: if (!(*out_vals)[i].val.string) {
371: rpc_SetErr(errno, "Error:: in prepare RPC reply ...\n");
372: free(*out_vals);
373: *out_vals = NULL;
374: *out_argc = 0;
375: return -1;
376: }
377: data += (*out_vals)[i].val_len + 1;
378: break;
379: default:
380: break;
381: }
382: } else {
1.1.1.1.2.1 misho 383: if (out_argc)
384: *out_argc = 0;
385: if (out_vals)
386: *out_vals = NULL;
1.1 misho 387: }
388:
389: return rrpc->ret_retcode;
390: }
391:
392: /*
393: * rpc_cli_freeVals() Free rpc_val_t array returned from RPC call
394: * @args = Number of arguments in array
395: * @vals = Value elements
396: * return: none
397: */
398: inline void
399: rpc_cli_freeVals(int args, rpc_val_t *vals)
400: {
401: register int i;
402:
403: if (!vals)
404: return;
405:
406: for (i = 0; i < args; i++)
407: RPC_FREE_VAL(&vals[i]);
408:
409: free(vals);
410: vals = NULL;
411: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>