]>
Commit | Line | Data |
---|---|---|
28f540f4 RM |
1 | /* @(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC */ |
2 | /* | |
3 | * Sun RPC is a product of Sun Microsystems, Inc. and is provided for | |
4 | * unrestricted use provided that this legend is included on all tape | |
5 | * media and as a part of the software program in whole or part. Users | |
6 | * may copy or modify Sun RPC without charge, but are not authorized | |
7 | * to license or distribute it to anyone else except as part of a product or | |
8 | * program developed by the user. | |
c28fb3c8 | 9 | * |
28f540f4 RM |
10 | * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE |
11 | * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR | |
12 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. | |
c28fb3c8 | 13 | * |
28f540f4 RM |
14 | * Sun RPC is provided with no support and without any obligation on the |
15 | * part of Sun Microsystems, Inc. to assist in its use, correction, | |
16 | * modification or enhancement. | |
c28fb3c8 | 17 | * |
28f540f4 RM |
18 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE |
19 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC | |
20 | * OR ANY PART THEREOF. | |
c28fb3c8 | 21 | * |
28f540f4 RM |
22 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue |
23 | * or profits or other special, indirect and consequential damages, even if | |
24 | * Sun has been advised of the possibility of such damages. | |
c28fb3c8 | 25 | * |
28f540f4 RM |
26 | * Sun Microsystems, Inc. |
27 | * 2550 Garcia Avenue | |
28 | * Mountain View, California 94043 | |
29 | */ | |
30 | #if !defined(lint) && defined(SCCSIDS) | |
31 | static char sccsid[] = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro"; | |
32 | #endif | |
c28fb3c8 | 33 | |
28f540f4 RM |
34 | /* |
35 | * clnt_tcp.c, Implements a TCP/IP based, client side RPC. | |
36 | * | |
37 | * Copyright (C) 1984, Sun Microsystems, Inc. | |
38 | * | |
39 | * TCP based RPC supports 'batched calls'. | |
40 | * A sequence of calls may be batched-up in a send buffer. The rpc call | |
41 | * return immediately to the client even though the call was not necessarily | |
42 | * sent. The batching occurs if the results' xdr routine is NULL (0) AND | |
43 | * the rpc timeout value is zero (see clnt.h, rpc). | |
44 | * | |
45 | * Clients should NOT casually batch calls that in fact return results; that is, | |
46 | * the server side should be aware that a call is batched and not produce any | |
47 | * return message. Batched calls that produce many result messages can | |
48 | * deadlock (netlock) the client and the server.... | |
49 | * | |
50 | * Now go hang yourself. | |
51 | */ | |
52 | ||
53 | #include <stdio.h> | |
54 | #include <rpc/rpc.h> | |
55 | #include <sys/socket.h> | |
56 | #include <netdb.h> | |
57 | #include <errno.h> | |
58 | #include <rpc/pmap_clnt.h> | |
59 | ||
60 | #define MCALL_MSG_SIZE 24 | |
61 | ||
c4029823 | 62 | #ifndef errno |
28f540f4 | 63 | extern int errno; |
c4029823 | 64 | #endif |
28f540f4 RM |
65 | |
66 | static int readtcp(); | |
67 | static int writetcp(); | |
68 | ||
69 | static enum clnt_stat clnttcp_call(); | |
70 | static void clnttcp_abort(); | |
71 | static void clnttcp_geterr(); | |
72 | static bool_t clnttcp_freeres(); | |
73 | static bool_t clnttcp_control(); | |
74 | static void clnttcp_destroy(); | |
75 | ||
76 | static struct clnt_ops tcp_ops = { | |
77 | clnttcp_call, | |
78 | clnttcp_abort, | |
79 | clnttcp_geterr, | |
80 | clnttcp_freeres, | |
81 | clnttcp_destroy, | |
82 | clnttcp_control | |
83 | }; | |
84 | ||
85 | struct ct_data { | |
86 | int ct_sock; | |
87 | bool_t ct_closeit; | |
88 | struct timeval ct_wait; | |
89 | bool_t ct_waitset; /* wait set by clnt_control? */ | |
c28fb3c8 | 90 | struct sockaddr_in ct_addr; |
28f540f4 RM |
91 | struct rpc_err ct_error; |
92 | char ct_mcall[MCALL_MSG_SIZE]; /* marshalled callmsg */ | |
93 | u_int ct_mpos; /* pos after marshal */ | |
94 | XDR ct_xdrs; | |
95 | }; | |
96 | ||
97 | /* | |
98 | * Create a client handle for a tcp/ip connection. | |
99 | * If *sockp<0, *sockp is set to a newly created TCP socket and it is | |
100 | * connected to raddr. If *sockp non-negative then | |
101 | * raddr is ignored. The rpc/tcp package does buffering | |
102 | * similar to stdio, so the client must pick send and receive buffer sizes,]; | |
103 | * 0 => use the default. | |
104 | * If raddr->sin_port is 0, then a binder on the remote machine is | |
105 | * consulted for the right port number. | |
106 | * NB: *sockp is copied into a private area. | |
107 | * NB: It is the clients responsibility to close *sockp. | |
108 | * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this | |
109 | * something more useful. | |
110 | */ | |
111 | CLIENT * | |
112 | clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz) | |
113 | struct sockaddr_in *raddr; | |
114 | u_long prog; | |
115 | u_long vers; | |
116 | register int *sockp; | |
117 | u_int sendsz; | |
118 | u_int recvsz; | |
119 | { | |
120 | CLIENT *h; | |
121 | register struct ct_data *ct; | |
122 | struct timeval now; | |
123 | struct rpc_msg call_msg; | |
124 | ||
125 | h = (CLIENT *)mem_alloc(sizeof(*h)); | |
126 | if (h == NULL) { | |
127 | (void)fprintf(stderr, "clnttcp_create: out of memory\n"); | |
128 | rpc_createerr.cf_stat = RPC_SYSTEMERROR; | |
129 | rpc_createerr.cf_error.re_errno = errno; | |
130 | goto fooy; | |
131 | } | |
132 | ct = (struct ct_data *)mem_alloc(sizeof(*ct)); | |
133 | if (ct == NULL) { | |
134 | (void)fprintf(stderr, "clnttcp_create: out of memory\n"); | |
135 | rpc_createerr.cf_stat = RPC_SYSTEMERROR; | |
136 | rpc_createerr.cf_error.re_errno = errno; | |
137 | goto fooy; | |
138 | } | |
139 | ||
140 | /* | |
141 | * If no port number given ask the pmap for one | |
142 | */ | |
143 | if (raddr->sin_port == 0) { | |
144 | u_short port; | |
145 | if ((port = pmap_getport(raddr, prog, vers, IPPROTO_TCP)) == 0) { | |
146 | mem_free((caddr_t)ct, sizeof(struct ct_data)); | |
147 | mem_free((caddr_t)h, sizeof(CLIENT)); | |
148 | return ((CLIENT *)NULL); | |
149 | } | |
150 | raddr->sin_port = htons(port); | |
151 | } | |
152 | ||
153 | /* | |
154 | * If no socket given, open one | |
155 | */ | |
156 | if (*sockp < 0) { | |
157 | *sockp = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | |
158 | (void)bindresvport(*sockp, (struct sockaddr_in *)0); | |
159 | if ((*sockp < 0) | |
160 | || (connect(*sockp, (struct sockaddr *)raddr, | |
161 | sizeof(*raddr)) < 0)) { | |
162 | rpc_createerr.cf_stat = RPC_SYSTEMERROR; | |
163 | rpc_createerr.cf_error.re_errno = errno; | |
c28fb3c8 RM |
164 | if (*sockp >= 0) |
165 | (void)close(*sockp); | |
28f540f4 RM |
166 | goto fooy; |
167 | } | |
168 | ct->ct_closeit = TRUE; | |
169 | } else { | |
170 | ct->ct_closeit = FALSE; | |
171 | } | |
172 | ||
173 | /* | |
174 | * Set up private data struct | |
175 | */ | |
176 | ct->ct_sock = *sockp; | |
177 | ct->ct_wait.tv_usec = 0; | |
178 | ct->ct_waitset = FALSE; | |
179 | ct->ct_addr = *raddr; | |
180 | ||
181 | /* | |
182 | * Initialize call message | |
183 | */ | |
184 | (void)gettimeofday(&now, (struct timezone *)0); | |
185 | call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec; | |
186 | call_msg.rm_direction = CALL; | |
187 | call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; | |
188 | call_msg.rm_call.cb_prog = prog; | |
189 | call_msg.rm_call.cb_vers = vers; | |
190 | ||
191 | /* | |
6d52618b | 192 | * pre-serialize the static part of the call msg and stash it away |
28f540f4 RM |
193 | */ |
194 | xdrmem_create(&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE, | |
195 | XDR_ENCODE); | |
196 | if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) { | |
197 | if (ct->ct_closeit) { | |
198 | (void)close(*sockp); | |
199 | } | |
200 | goto fooy; | |
201 | } | |
202 | ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs)); | |
203 | XDR_DESTROY(&(ct->ct_xdrs)); | |
204 | ||
205 | /* | |
206 | * Create a client handle which uses xdrrec for serialization | |
207 | * and authnone for authentication. | |
208 | */ | |
209 | xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz, | |
210 | (caddr_t)ct, readtcp, writetcp); | |
211 | h->cl_ops = &tcp_ops; | |
212 | h->cl_private = (caddr_t) ct; | |
213 | h->cl_auth = authnone_create(); | |
214 | return (h); | |
215 | ||
216 | fooy: | |
217 | /* | |
218 | * Something goofed, free stuff and barf | |
219 | */ | |
220 | mem_free((caddr_t)ct, sizeof(struct ct_data)); | |
221 | mem_free((caddr_t)h, sizeof(CLIENT)); | |
222 | return ((CLIENT *)NULL); | |
223 | } | |
224 | ||
225 | static enum clnt_stat | |
226 | clnttcp_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout) | |
227 | register CLIENT *h; | |
228 | u_long proc; | |
229 | xdrproc_t xdr_args; | |
230 | caddr_t args_ptr; | |
231 | xdrproc_t xdr_results; | |
232 | caddr_t results_ptr; | |
233 | struct timeval timeout; | |
234 | { | |
235 | register struct ct_data *ct = (struct ct_data *) h->cl_private; | |
236 | register XDR *xdrs = &(ct->ct_xdrs); | |
237 | struct rpc_msg reply_msg; | |
238 | u_long x_id; | |
b20e47cb | 239 | u_int32_t *msg_x_id = (u_int32_t *)(ct->ct_mcall); /* yuk */ |
28f540f4 RM |
240 | register bool_t shipnow; |
241 | int refreshes = 2; | |
242 | ||
243 | if (!ct->ct_waitset) { | |
244 | ct->ct_wait = timeout; | |
245 | } | |
246 | ||
247 | shipnow = | |
248 | (xdr_results == (xdrproc_t)0 && timeout.tv_sec == 0 | |
249 | && timeout.tv_usec == 0) ? FALSE : TRUE; | |
250 | ||
251 | call_again: | |
252 | xdrs->x_op = XDR_ENCODE; | |
253 | ct->ct_error.re_status = RPC_SUCCESS; | |
254 | x_id = ntohl(--(*msg_x_id)); | |
255 | if ((! XDR_PUTBYTES(xdrs, ct->ct_mcall, ct->ct_mpos)) || | |
256 | (! XDR_PUTLONG(xdrs, (long *)&proc)) || | |
257 | (! AUTH_MARSHALL(h->cl_auth, xdrs)) || | |
258 | (! (*xdr_args)(xdrs, args_ptr))) { | |
259 | if (ct->ct_error.re_status == RPC_SUCCESS) | |
260 | ct->ct_error.re_status = RPC_CANTENCODEARGS; | |
261 | (void)xdrrec_endofrecord(xdrs, TRUE); | |
262 | return (ct->ct_error.re_status); | |
263 | } | |
264 | if (! xdrrec_endofrecord(xdrs, shipnow)) | |
265 | return (ct->ct_error.re_status = RPC_CANTSEND); | |
266 | if (! shipnow) | |
267 | return (RPC_SUCCESS); | |
268 | /* | |
269 | * Hack to provide rpc-based message passing | |
270 | */ | |
271 | if (timeout.tv_sec == 0 && timeout.tv_usec == 0) { | |
272 | return(ct->ct_error.re_status = RPC_TIMEDOUT); | |
273 | } | |
274 | ||
275 | ||
276 | /* | |
277 | * Keep receiving until we get a valid transaction id | |
278 | */ | |
279 | xdrs->x_op = XDR_DECODE; | |
280 | while (TRUE) { | |
281 | reply_msg.acpted_rply.ar_verf = _null_auth; | |
282 | reply_msg.acpted_rply.ar_results.where = NULL; | |
283 | reply_msg.acpted_rply.ar_results.proc = xdr_void; | |
284 | if (! xdrrec_skiprecord(xdrs)) | |
285 | return (ct->ct_error.re_status); | |
286 | /* now decode and validate the response header */ | |
287 | if (! xdr_replymsg(xdrs, &reply_msg)) { | |
288 | if (ct->ct_error.re_status == RPC_SUCCESS) | |
289 | continue; | |
290 | return (ct->ct_error.re_status); | |
291 | } | |
292 | if (reply_msg.rm_xid == x_id) | |
293 | break; | |
294 | } | |
295 | ||
296 | /* | |
297 | * process header | |
298 | */ | |
299 | _seterr_reply(&reply_msg, &(ct->ct_error)); | |
300 | if (ct->ct_error.re_status == RPC_SUCCESS) { | |
301 | if (! AUTH_VALIDATE(h->cl_auth, &reply_msg.acpted_rply.ar_verf)) { | |
302 | ct->ct_error.re_status = RPC_AUTHERROR; | |
303 | ct->ct_error.re_why = AUTH_INVALIDRESP; | |
304 | } else if (! (*xdr_results)(xdrs, results_ptr)) { | |
305 | if (ct->ct_error.re_status == RPC_SUCCESS) | |
306 | ct->ct_error.re_status = RPC_CANTDECODERES; | |
307 | } | |
308 | /* free verifier ... */ | |
309 | if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) { | |
310 | xdrs->x_op = XDR_FREE; | |
311 | (void)xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf)); | |
312 | } | |
313 | } /* end successful completion */ | |
314 | else { | |
315 | /* maybe our credentials need to be refreshed ... */ | |
316 | if (refreshes-- && AUTH_REFRESH(h->cl_auth)) | |
317 | goto call_again; | |
318 | } /* end of unsuccessful completion */ | |
319 | return (ct->ct_error.re_status); | |
320 | } | |
321 | ||
322 | static void | |
323 | clnttcp_geterr(h, errp) | |
324 | CLIENT *h; | |
325 | struct rpc_err *errp; | |
326 | { | |
327 | register struct ct_data *ct = | |
328 | (struct ct_data *) h->cl_private; | |
329 | ||
330 | *errp = ct->ct_error; | |
331 | } | |
332 | ||
333 | static bool_t | |
334 | clnttcp_freeres(cl, xdr_res, res_ptr) | |
335 | CLIENT *cl; | |
336 | xdrproc_t xdr_res; | |
337 | caddr_t res_ptr; | |
338 | { | |
339 | register struct ct_data *ct = (struct ct_data *)cl->cl_private; | |
340 | register XDR *xdrs = &(ct->ct_xdrs); | |
341 | ||
342 | xdrs->x_op = XDR_FREE; | |
343 | return ((*xdr_res)(xdrs, res_ptr)); | |
344 | } | |
345 | ||
346 | static void | |
347 | clnttcp_abort() | |
348 | { | |
349 | } | |
350 | ||
351 | static bool_t | |
352 | clnttcp_control(cl, request, info) | |
353 | CLIENT *cl; | |
354 | int request; | |
355 | char *info; | |
356 | { | |
357 | register struct ct_data *ct = (struct ct_data *)cl->cl_private; | |
358 | ||
359 | switch (request) { | |
360 | case CLSET_TIMEOUT: | |
361 | ct->ct_wait = *(struct timeval *)info; | |
362 | ct->ct_waitset = TRUE; | |
363 | break; | |
364 | case CLGET_TIMEOUT: | |
365 | *(struct timeval *)info = ct->ct_wait; | |
366 | break; | |
367 | case CLGET_SERVER_ADDR: | |
368 | *(struct sockaddr_in *)info = ct->ct_addr; | |
369 | break; | |
370 | default: | |
371 | return (FALSE); | |
372 | } | |
373 | return (TRUE); | |
374 | } | |
375 | ||
376 | ||
377 | static void | |
378 | clnttcp_destroy(h) | |
379 | CLIENT *h; | |
380 | { | |
381 | register struct ct_data *ct = | |
382 | (struct ct_data *) h->cl_private; | |
383 | ||
384 | if (ct->ct_closeit) { | |
385 | (void)close(ct->ct_sock); | |
386 | } | |
387 | XDR_DESTROY(&(ct->ct_xdrs)); | |
388 | mem_free((caddr_t)ct, sizeof(struct ct_data)); | |
389 | mem_free((caddr_t)h, sizeof(CLIENT)); | |
390 | } | |
391 | ||
392 | /* | |
393 | * Interface between xdr serializer and tcp connection. | |
394 | * Behaves like the system calls, read & write, but keeps some error state | |
395 | * around for the rpc level. | |
396 | */ | |
397 | static int | |
398 | readtcp(ct, buf, len) | |
399 | register struct ct_data *ct; | |
400 | caddr_t buf; | |
401 | register int len; | |
402 | { | |
403 | #ifdef FD_SETSIZE | |
404 | fd_set mask; | |
405 | fd_set readfds; | |
406 | ||
407 | if (len == 0) | |
408 | return (0); | |
409 | FD_ZERO(&mask); | |
410 | FD_SET(ct->ct_sock, &mask); | |
411 | #else | |
412 | register int mask = 1 << (ct->ct_sock); | |
413 | int readfds; | |
414 | ||
415 | if (len == 0) | |
416 | return (0); | |
417 | ||
418 | #endif /* def FD_SETSIZE */ | |
419 | while (TRUE) { | |
7cc27f44 | 420 | struct timeval timeout = ct->ct_wait; |
28f540f4 RM |
421 | readfds = mask; |
422 | switch (select(_rpc_dtablesize(), &readfds, (int*)NULL, (int*)NULL, | |
7cc27f44 | 423 | &timeout)) { |
28f540f4 RM |
424 | case 0: |
425 | ct->ct_error.re_status = RPC_TIMEDOUT; | |
426 | return (-1); | |
427 | ||
428 | case -1: | |
429 | if (errno == EINTR) | |
430 | continue; | |
431 | ct->ct_error.re_status = RPC_CANTRECV; | |
432 | ct->ct_error.re_errno = errno; | |
433 | return (-1); | |
434 | } | |
435 | break; | |
436 | } | |
437 | switch (len = read(ct->ct_sock, buf, len)) { | |
438 | ||
439 | case 0: | |
440 | /* premature eof */ | |
441 | ct->ct_error.re_errno = ECONNRESET; | |
442 | ct->ct_error.re_status = RPC_CANTRECV; | |
443 | len = -1; /* it's really an error */ | |
444 | break; | |
445 | ||
446 | case -1: | |
447 | ct->ct_error.re_errno = errno; | |
448 | ct->ct_error.re_status = RPC_CANTRECV; | |
449 | break; | |
450 | } | |
451 | return (len); | |
452 | } | |
453 | ||
454 | static int | |
455 | writetcp(ct, buf, len) | |
456 | struct ct_data *ct; | |
457 | caddr_t buf; | |
458 | int len; | |
459 | { | |
460 | register int i, cnt; | |
461 | ||
462 | for (cnt = len; cnt > 0; cnt -= i, buf += i) { | |
463 | if ((i = write(ct->ct_sock, buf, cnt)) == -1) { | |
464 | ct->ct_error.re_errno = errno; | |
465 | ct->ct_error.re_status = RPC_CANTSEND; | |
466 | return (-1); | |
467 | } | |
468 | } | |
469 | return (len); | |
470 | } |