1 /* @(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC */
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.
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.
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.
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.
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.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
31 static char sccsid[] = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
35 * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
37 * Copyright (C) 1984, Sun Microsystems, Inc.
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).
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....
50 * Now go hang yourself.
53 #define authnone_create __authnone_create
54 #define xdrrec_create __xdrrec_create
55 #define xdrrec_endofrecord __xdrrec_endofrecord
56 #define xdrrec_skiprecord __xdrrec_skiprecord
57 #define xdr_callhdr __xdr_callhdr
58 #define xdr_replymsg __xdr_replymsg
59 #define xdr_opaque_auth __xdr_opaque_auth
60 #define xdrmem_create __xdrmem_create
61 #define pmap_getport __pmap_getport
72 #include <sys/socket.h>
73 #include <rpc/pmap_clnt.h>
78 extern u_long _create_xid (void) attribute_hidden;
80 #define MCALL_MSG_SIZE 24
86 struct timeval ct_wait;
87 bool_t ct_waitset; /* wait set by clnt_control? */
88 struct sockaddr_in ct_addr;
89 struct rpc_err ct_error;
90 char ct_mcall[MCALL_MSG_SIZE]; /* marshalled callmsg */
91 u_int ct_mpos; /* pos after marshal */
95 static int readtcp (char *, char *, int);
96 static int writetcp (char *, char *, int);
98 static enum clnt_stat clnttcp_call (CLIENT *, u_long, xdrproc_t, caddr_t,
99 xdrproc_t, caddr_t, struct timeval);
100 static void clnttcp_abort (void);
101 static void clnttcp_geterr (CLIENT *, struct rpc_err *);
102 static bool_t clnttcp_freeres (CLIENT *, xdrproc_t, caddr_t);
103 static bool_t clnttcp_control (CLIENT *, int, char *);
104 static void clnttcp_destroy (CLIENT *);
106 static struct clnt_ops tcp_ops =
117 * Create a client handle for a tcp/ip connection.
118 * If *sockp<0, *sockp is set to a newly created TCP socket and it is
119 * connected to raddr. If *sockp non-negative then
120 * raddr is ignored. The rpc/tcp package does buffering
121 * similar to stdio, so the client must pick send and receive buffer sizes,];
122 * 0 => use the default.
123 * If raddr->sin_port is 0, then a binder on the remote machine is
124 * consulted for the right port number.
125 * NB: *sockp is copied into a private area.
126 * NB: It is the clients responsibility to close *sockp.
127 * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this
128 * something more useful.
130 CLIENT attribute_hidden *
131 __clnttcp_create (struct sockaddr_in *raddr, u_long prog, u_long vers,
132 int *sockp, u_int sendsz, u_int recvsz)
136 struct rpc_msg call_msg;
138 h = (CLIENT *) mem_alloc (sizeof (*h));
139 ct = (struct ct_data *) mem_alloc (sizeof (*ct));
140 if (h == NULL || ct == NULL)
142 struct rpc_createerr *ce = &get_rpc_createerr ();
144 if (_IO_fwide (stderr, 0) > 0)
145 (void) __fwprintf (stderr, L"%s",
146 _("clnttcp_create: out of memory\n"));
149 (void) fputs (_("clnttcp_create: out of memory\n"), stderr);
150 ce->cf_stat = RPC_SYSTEMERROR;
151 ce->cf_error.re_errno = ENOMEM;
156 * If no port number given ask the pmap for one
158 if (raddr->sin_port == 0)
161 if ((port = pmap_getport (raddr, prog, vers, IPPROTO_TCP)) == 0)
163 mem_free ((caddr_t) ct, sizeof (struct ct_data));
164 mem_free ((caddr_t) h, sizeof (CLIENT));
165 return ((CLIENT *) NULL);
167 raddr->sin_port = htons (port);
171 * If no socket given, open one
175 *sockp = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
176 (void) bindresvport (*sockp, (struct sockaddr_in *) 0);
178 || (connect (*sockp, (struct sockaddr *) raddr,
179 sizeof (*raddr)) < 0))
181 struct rpc_createerr *ce = &get_rpc_createerr ();
182 ce->cf_stat = RPC_SYSTEMERROR;
183 ce->cf_error.re_errno = errno;
185 (void) __close (*sockp);
188 ct->ct_closeit = TRUE;
192 ct->ct_closeit = FALSE;
196 * Set up private data struct
198 ct->ct_sock = *sockp;
199 ct->ct_wait.tv_usec = 0;
200 ct->ct_waitset = FALSE;
201 ct->ct_addr = *raddr;
204 * Initialize call message
206 call_msg.rm_xid = _create_xid ();
207 call_msg.rm_direction = CALL;
208 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
209 call_msg.rm_call.cb_prog = prog;
210 call_msg.rm_call.cb_vers = vers;
213 * pre-serialize the static part of the call msg and stash it away
215 xdrmem_create (&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE,
217 if (!xdr_callhdr (&(ct->ct_xdrs), &call_msg))
221 (void) __close (*sockp);
225 ct->ct_mpos = XDR_GETPOS (&(ct->ct_xdrs));
226 XDR_DESTROY (&(ct->ct_xdrs));
229 * Create a client handle which uses xdrrec for serialization
230 * and authnone for authentication.
232 xdrrec_create (&(ct->ct_xdrs), sendsz, recvsz,
233 (caddr_t) ct, readtcp, writetcp);
234 h->cl_ops = &tcp_ops;
235 h->cl_private = (caddr_t) ct;
236 h->cl_auth = authnone_create ();
241 * Something goofed, free stuff and barf
243 mem_free ((caddr_t) ct, sizeof (struct ct_data));
244 mem_free ((caddr_t) h, sizeof (CLIENT));
245 return ((CLIENT *) NULL);
247 strong_alias(__clnttcp_create,clnttcp_create)
249 static enum clnt_stat
250 clnttcp_call (h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
255 xdrproc_t xdr_results;
257 struct timeval timeout;
259 struct ct_data *ct = (struct ct_data *) h->cl_private;
260 XDR *xdrs = &(ct->ct_xdrs);
261 struct rpc_msg reply_msg;
263 u_int32_t *msg_x_id = (u_int32_t *) (ct->ct_mcall); /* yuk */
269 ct->ct_wait = timeout;
273 (xdr_results == (xdrproc_t) 0 && ct->ct_wait.tv_sec == 0
274 && ct->ct_wait.tv_usec == 0) ? FALSE : TRUE;
277 xdrs->x_op = XDR_ENCODE;
278 ct->ct_error.re_status = RPC_SUCCESS;
279 x_id = ntohl (--(*msg_x_id));
280 if ((!XDR_PUTBYTES (xdrs, ct->ct_mcall, ct->ct_mpos)) ||
281 (!XDR_PUTLONG (xdrs, (long *) &proc)) ||
282 (!AUTH_MARSHALL (h->cl_auth, xdrs)) ||
283 (!(*xdr_args) (xdrs, args_ptr)))
285 if (ct->ct_error.re_status == RPC_SUCCESS)
286 ct->ct_error.re_status = RPC_CANTENCODEARGS;
287 (void) xdrrec_endofrecord (xdrs, TRUE);
288 return (ct->ct_error.re_status);
290 if (!xdrrec_endofrecord (xdrs, shipnow))
291 return ct->ct_error.re_status = RPC_CANTSEND;
295 * Hack to provide rpc-based message passing
297 if (ct->ct_wait.tv_sec == 0 && ct->ct_wait.tv_usec == 0)
299 return ct->ct_error.re_status = RPC_TIMEDOUT;
304 * Keep receiving until we get a valid transaction id
306 xdrs->x_op = XDR_DECODE;
309 reply_msg.acpted_rply.ar_verf = _null_auth;
310 reply_msg.acpted_rply.ar_results.where = NULL;
311 reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
312 if (!xdrrec_skiprecord (xdrs))
313 return (ct->ct_error.re_status);
314 /* now decode and validate the response header */
315 if (!xdr_replymsg (xdrs, &reply_msg))
317 if (ct->ct_error.re_status == RPC_SUCCESS)
319 return ct->ct_error.re_status;
321 if ((u_int32_t) reply_msg.rm_xid == (u_int32_t) x_id)
328 _seterr_reply (&reply_msg, &(ct->ct_error));
329 if (ct->ct_error.re_status == RPC_SUCCESS)
331 if (!AUTH_VALIDATE (h->cl_auth, &reply_msg.acpted_rply.ar_verf))
333 ct->ct_error.re_status = RPC_AUTHERROR;
334 ct->ct_error.re_why = AUTH_INVALIDRESP;
336 else if (!(*xdr_results) (xdrs, results_ptr))
338 if (ct->ct_error.re_status == RPC_SUCCESS)
339 ct->ct_error.re_status = RPC_CANTDECODERES;
341 /* free verifier ... */
342 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
344 xdrs->x_op = XDR_FREE;
345 (void) xdr_opaque_auth (xdrs, &(reply_msg.acpted_rply.ar_verf));
347 } /* end successful completion */
350 /* maybe our credentials need to be refreshed ... */
351 if (refreshes-- && AUTH_REFRESH (h->cl_auth))
353 } /* end of unsuccessful completion */
354 return ct->ct_error.re_status;
358 clnttcp_geterr (h, errp)
360 struct rpc_err *errp;
363 (struct ct_data *) h->cl_private;
365 *errp = ct->ct_error;
369 clnttcp_freeres (cl, xdr_res, res_ptr)
374 struct ct_data *ct = (struct ct_data *) cl->cl_private;
375 XDR *xdrs = &(ct->ct_xdrs);
377 xdrs->x_op = XDR_FREE;
378 return (*xdr_res) (xdrs, res_ptr);
387 clnttcp_control (CLIENT *cl, int request, char *info)
389 struct ct_data *ct = (struct ct_data *) cl->cl_private;
395 ct->ct_closeit = TRUE;
397 case CLSET_FD_NCLOSE:
398 ct->ct_closeit = FALSE;
401 ct->ct_wait = *(struct timeval *) info;
402 ct->ct_waitset = TRUE;
405 *(struct timeval *) info = ct->ct_wait;
407 case CLGET_SERVER_ADDR:
408 *(struct sockaddr_in *) info = ct->ct_addr;
411 *(int *)info = ct->ct_sock;
415 * use the knowledge that xid is the
416 * first element in the call structure *.
417 * This will get the xid of the PREVIOUS call
419 *(u_long *)info = ntohl (*(u_long *)ct->ct_mcall);
422 /* This will set the xid of the NEXT call */
423 *(u_long *)ct->ct_mcall = htonl (*(u_long *)info - 1);
424 /* decrement by 1 as clnttcp_call() increments once */
427 * This RELIES on the information that, in the call body,
428 * the version number field is the fifth field from the
429 * begining of the RPC header. MUST be changed if the
430 * call_struct is changed
432 *(u_long *)info = ntohl (*(u_long *)(ct->ct_mcall +
433 4 * BYTES_PER_XDR_UNIT));
436 *(u_long *)(ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT)
437 = htonl (*(u_long *)info);
441 * This RELIES on the information that, in the call body,
442 * the program number field is the field from the
443 * begining of the RPC header. MUST be changed if the
444 * call_struct is changed
446 *(u_long *)info = ntohl(*(u_long *)(ct->ct_mcall +
447 3 * BYTES_PER_XDR_UNIT));
450 *(u_long *)(ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT)
451 = htonl(*(u_long *)info);
453 /* The following are only possible with TI-RPC */
454 case CLGET_RETRY_TIMEOUT:
455 case CLSET_RETRY_TIMEOUT:
458 case CLSET_PUSH_TIMOD:
459 case CLSET_POP_TIMOD:
468 clnttcp_destroy (CLIENT *h)
471 (struct ct_data *) h->cl_private;
475 (void) __close (ct->ct_sock);
477 XDR_DESTROY (&(ct->ct_xdrs));
478 mem_free ((caddr_t) ct, sizeof (struct ct_data));
479 mem_free ((caddr_t) h, sizeof (CLIENT));
483 * Interface between xdr serializer and tcp connection.
484 * Behaves like the system calls, read & write, but keeps some error state
485 * around for the rpc level.
488 readtcp (char *ctptr, char *buf, int len)
490 struct ct_data *ct = (struct ct_data *)ctptr;
492 int milliseconds = (ct->ct_wait.tv_sec * 1000) +
493 (ct->ct_wait.tv_usec / 1000);
502 switch (poll(&fd, 1, milliseconds))
505 ct->ct_error.re_status = RPC_TIMEDOUT;
511 ct->ct_error.re_status = RPC_CANTRECV;
512 ct->ct_error.re_errno = errno;
517 switch (len = __read (ct->ct_sock, buf, len))
522 ct->ct_error.re_errno = ECONNRESET;
523 ct->ct_error.re_status = RPC_CANTRECV;
524 len = -1; /* it's really an error */
528 ct->ct_error.re_errno = errno;
529 ct->ct_error.re_status = RPC_CANTRECV;
536 writetcp (char *ctptr, char *buf, int len)
539 struct ct_data *ct = (struct ct_data*)ctptr;
541 for (cnt = len; cnt > 0; cnt -= i, buf += i)
543 if ((i = __write (ct->ct_sock, buf, cnt)) == -1)
545 ct->ct_error.re_errno = errno;
546 ct->ct_error.re_status = RPC_CANTSEND;