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