2 * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * TCP based RPC supports 'batched calls'.
34 * A sequence of calls may be batched-up in a send buffer. The rpc call
35 * return immediately to the client even though the call was not necessarily
36 * sent. The batching occurs if the results' xdr routine is NULL (0) AND
37 * the rpc timeout value is zero (see clnt.h, rpc).
39 * Clients should NOT casually batch calls that in fact return results; that is,
40 * the server side should be aware that a call is batched and not produce any
41 * return message. Batched calls that produce many result messages can
42 * deadlock (netlock) the client and the server....
44 * Now go hang yourself.
54 #include <sys/socket.h>
55 #include <rpc/pmap_clnt.h>
57 #include <shlib-compat.h>
59 extern u_long
_create_xid (void);
61 #define MCALL_MSG_SIZE 24
67 struct timeval ct_wait
;
68 bool_t ct_waitset
; /* wait set by clnt_control? */
69 struct sockaddr_in ct_addr
;
70 struct rpc_err ct_error
;
71 char ct_mcall
[MCALL_MSG_SIZE
]; /* marshalled callmsg */
72 u_int ct_mpos
; /* pos after marshal */
76 static int readtcp (char *, char *, int);
77 static int writetcp (char *, char *, int);
79 static enum clnt_stat
clnttcp_call (CLIENT
*, u_long
, xdrproc_t
, caddr_t
,
80 xdrproc_t
, caddr_t
, struct timeval
);
81 static void clnttcp_abort (void);
82 static void clnttcp_geterr (CLIENT
*, struct rpc_err
*);
83 static bool_t
clnttcp_freeres (CLIENT
*, xdrproc_t
, caddr_t
);
84 static bool_t
clnttcp_control (CLIENT
*, int, char *);
85 static void clnttcp_destroy (CLIENT
*);
87 static const struct clnt_ops tcp_ops
=
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.
112 clnttcp_create (struct sockaddr_in
*raddr
, u_long prog
, u_long vers
,
113 int *sockp
, u_int sendsz
, u_int recvsz
)
117 struct rpc_msg call_msg
;
119 h
= (CLIENT
*) mem_alloc (sizeof (*h
));
120 ct
= (struct ct_data
*) mem_alloc (sizeof (*ct
));
121 if (h
== NULL
|| ct
== NULL
)
123 struct rpc_createerr
*ce
= &get_rpc_createerr ();
124 (void) __fxprintf (NULL
, "%s: %s", __func__
, _("out of memory\n"));
125 ce
->cf_stat
= RPC_SYSTEMERROR
;
126 ce
->cf_error
.re_errno
= ENOMEM
;
131 * If no port number given ask the pmap for one
133 if (raddr
->sin_port
== 0)
136 if ((port
= pmap_getport (raddr
, prog
, vers
, IPPROTO_TCP
)) == 0)
138 mem_free ((caddr_t
) ct
, sizeof (struct ct_data
));
139 mem_free ((caddr_t
) h
, sizeof (CLIENT
));
140 return ((CLIENT
*) NULL
);
142 raddr
->sin_port
= htons (port
);
146 * If no socket given, open one
150 *sockp
= __socket (AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
151 (void) bindresvport (*sockp
, (struct sockaddr_in
*) 0);
153 || (__connect (*sockp
, (struct sockaddr
*) raddr
,
154 sizeof (*raddr
)) < 0))
156 struct rpc_createerr
*ce
= &get_rpc_createerr ();
157 ce
->cf_stat
= RPC_SYSTEMERROR
;
158 ce
->cf_error
.re_errno
= errno
;
160 (void) __close (*sockp
);
163 ct
->ct_closeit
= TRUE
;
167 ct
->ct_closeit
= FALSE
;
171 * Set up private data struct
173 ct
->ct_sock
= *sockp
;
174 ct
->ct_wait
.tv_usec
= 0;
175 ct
->ct_waitset
= FALSE
;
176 ct
->ct_addr
= *raddr
;
179 * Initialize call message
181 call_msg
.rm_xid
= _create_xid ();
182 call_msg
.rm_direction
= CALL
;
183 call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
184 call_msg
.rm_call
.cb_prog
= prog
;
185 call_msg
.rm_call
.cb_vers
= vers
;
188 * pre-serialize the static part of the call msg and stash it away
190 xdrmem_create (&(ct
->ct_xdrs
), ct
->ct_mcall
, MCALL_MSG_SIZE
, XDR_ENCODE
);
191 if (!xdr_callhdr (&(ct
->ct_xdrs
), &call_msg
))
195 (void) __close (*sockp
);
199 ct
->ct_mpos
= XDR_GETPOS (&(ct
->ct_xdrs
));
200 XDR_DESTROY (&(ct
->ct_xdrs
));
203 * Create a client handle which uses xdrrec for serialization
204 * and authnone for authentication.
206 xdrrec_create (&(ct
->ct_xdrs
), sendsz
, recvsz
,
207 (caddr_t
) ct
, readtcp
, writetcp
);
208 h
->cl_ops
= (struct clnt_ops
*) &tcp_ops
;
209 h
->cl_private
= (caddr_t
) ct
;
210 h
->cl_auth
= authnone_create ();
215 * Something goofed, free stuff and barf
217 mem_free ((caddr_t
) ct
, sizeof (struct ct_data
));
218 mem_free ((caddr_t
) h
, sizeof (CLIENT
));
219 return ((CLIENT
*) NULL
);
221 #ifdef EXPORT_RPC_SYMBOLS
222 libc_hidden_def (clnttcp_create
)
224 libc_hidden_nolink_sunrpc (clnttcp_create
, GLIBC_2_0
)
227 static enum clnt_stat
228 clnttcp_call (CLIENT
*h
, u_long proc
, xdrproc_t xdr_args
, caddr_t args_ptr
,
229 xdrproc_t xdr_results
, caddr_t results_ptr
,
230 struct timeval timeout
)
232 struct ct_data
*ct
= (struct ct_data
*) h
->cl_private
;
233 XDR
*xdrs
= &(ct
->ct_xdrs
);
234 struct rpc_msg reply_msg
;
236 u_int32_t
*msg_x_id
= (u_int32_t
*) (ct
->ct_mcall
); /* yuk */
242 ct
->ct_wait
= timeout
;
246 (xdr_results
== (xdrproc_t
) 0 && ct
->ct_wait
.tv_sec
== 0
247 && ct
->ct_wait
.tv_usec
== 0) ? FALSE
: TRUE
;
250 xdrs
->x_op
= XDR_ENCODE
;
251 ct
->ct_error
.re_status
= RPC_SUCCESS
;
252 x_id
= ntohl (--(*msg_x_id
));
253 if ((!XDR_PUTBYTES (xdrs
, ct
->ct_mcall
, ct
->ct_mpos
)) ||
254 (!XDR_PUTLONG (xdrs
, (long *) &proc
)) ||
255 (!AUTH_MARSHALL (h
->cl_auth
, xdrs
)) ||
256 (!(*xdr_args
) (xdrs
, args_ptr
)))
258 if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
259 ct
->ct_error
.re_status
= RPC_CANTENCODEARGS
;
260 (void) xdrrec_endofrecord (xdrs
, TRUE
);
261 return (ct
->ct_error
.re_status
);
263 if (!xdrrec_endofrecord (xdrs
, shipnow
))
264 return ct
->ct_error
.re_status
= RPC_CANTSEND
;
268 * Hack to provide rpc-based message passing
270 if (ct
->ct_wait
.tv_sec
== 0 && ct
->ct_wait
.tv_usec
== 0)
272 return ct
->ct_error
.re_status
= RPC_TIMEDOUT
;
277 * Keep receiving until we get a valid transaction id
279 xdrs
->x_op
= XDR_DECODE
;
282 reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
283 reply_msg
.acpted_rply
.ar_results
.where
= NULL
;
284 reply_msg
.acpted_rply
.ar_results
.proc
= (xdrproc_t
)xdr_void
;
285 if (!xdrrec_skiprecord (xdrs
))
286 return (ct
->ct_error
.re_status
);
287 /* now decode and validate the response header */
288 if (!xdr_replymsg (xdrs
, &reply_msg
))
290 if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
292 return ct
->ct_error
.re_status
;
294 if ((u_int32_t
) reply_msg
.rm_xid
== (u_int32_t
) x_id
)
301 _seterr_reply (&reply_msg
, &(ct
->ct_error
));
302 if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
304 if (!AUTH_VALIDATE (h
->cl_auth
, &reply_msg
.acpted_rply
.ar_verf
))
306 ct
->ct_error
.re_status
= RPC_AUTHERROR
;
307 ct
->ct_error
.re_why
= AUTH_INVALIDRESP
;
309 else if (!(*xdr_results
) (xdrs
, results_ptr
))
311 if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
312 ct
->ct_error
.re_status
= RPC_CANTDECODERES
;
314 /* free verifier ... */
315 if (reply_msg
.acpted_rply
.ar_verf
.oa_base
!= NULL
)
317 xdrs
->x_op
= XDR_FREE
;
318 (void) xdr_opaque_auth (xdrs
, &(reply_msg
.acpted_rply
.ar_verf
));
320 } /* end successful completion */
323 /* maybe our credentials need to be refreshed ... */
324 if (refreshes
-- && AUTH_REFRESH (h
->cl_auth
))
326 } /* end of unsuccessful completion */
327 return ct
->ct_error
.re_status
;
331 clnttcp_geterr (CLIENT
*h
, struct rpc_err
*errp
)
334 (struct ct_data
*) h
->cl_private
;
336 *errp
= ct
->ct_error
;
340 clnttcp_freeres (CLIENT
*cl
, xdrproc_t xdr_res
, caddr_t res_ptr
)
342 struct ct_data
*ct
= (struct ct_data
*) cl
->cl_private
;
343 XDR
*xdrs
= &(ct
->ct_xdrs
);
345 xdrs
->x_op
= XDR_FREE
;
346 return (*xdr_res
) (xdrs
, res_ptr
);
355 clnttcp_control (CLIENT
*cl
, int request
, char *info
)
357 struct ct_data
*ct
= (struct ct_data
*) cl
->cl_private
;
365 ct
->ct_closeit
= TRUE
;
367 case CLSET_FD_NCLOSE
:
368 ct
->ct_closeit
= FALSE
;
371 ct
->ct_wait
= *(struct timeval
*) info
;
372 ct
->ct_waitset
= TRUE
;
375 *(struct timeval
*) info
= ct
->ct_wait
;
377 case CLGET_SERVER_ADDR
:
378 *(struct sockaddr_in
*) info
= ct
->ct_addr
;
381 *(int *)info
= ct
->ct_sock
;
385 * use the knowledge that xid is the
386 * first element in the call structure *.
387 * This will get the xid of the PREVIOUS call
389 memcpy (&ui32
, ct
->ct_mcall
, sizeof (ui32
));
391 memcpy (info
, &ul
, sizeof (ul
));
394 /* This will set the xid of the NEXT call */
395 memcpy (&ul
, info
, sizeof (ul
));
396 ui32
= htonl (ul
- 1);
397 memcpy (ct
->ct_mcall
, &ui32
, sizeof (ui32
));
398 /* decrement by 1 as clnttcp_call() increments once */
402 * This RELIES on the information that, in the call body,
403 * the version number field is the fifth field from the
404 * beginning of the RPC header. MUST be changed if the
405 * call_struct is changed
407 memcpy (&ui32
, ct
->ct_mcall
+ 4 * BYTES_PER_XDR_UNIT
, sizeof (ui32
));
409 memcpy (info
, &ul
, sizeof (ul
));
412 memcpy (&ul
, info
, sizeof (ul
));
414 memcpy (ct
->ct_mcall
+ 4 * BYTES_PER_XDR_UNIT
, &ui32
, sizeof (ui32
));
418 * This RELIES on the information that, in the call body,
419 * the program number field is the field from the
420 * beginning of the RPC header. MUST be changed if the
421 * call_struct is changed
423 memcpy (&ui32
, ct
->ct_mcall
+ 3 * BYTES_PER_XDR_UNIT
, sizeof (ui32
));
425 memcpy (info
, &ul
, sizeof (ul
));
428 memcpy (&ul
, info
, sizeof (ul
));
430 memcpy (ct
->ct_mcall
+ 3 * BYTES_PER_XDR_UNIT
, &ui32
, sizeof (ui32
));
432 /* The following are only possible with TI-RPC */
433 case CLGET_RETRY_TIMEOUT
:
434 case CLSET_RETRY_TIMEOUT
:
437 case CLSET_PUSH_TIMOD
:
438 case CLSET_POP_TIMOD
:
447 clnttcp_destroy (CLIENT
*h
)
450 (struct ct_data
*) h
->cl_private
;
454 (void) __close (ct
->ct_sock
);
456 XDR_DESTROY (&(ct
->ct_xdrs
));
457 mem_free ((caddr_t
) ct
, sizeof (struct ct_data
));
458 mem_free ((caddr_t
) h
, sizeof (CLIENT
));
462 * Interface between xdr serializer and tcp connection.
463 * Behaves like the system calls, read & write, but keeps some error state
464 * around for the rpc level.
467 readtcp (char *ctptr
, char *buf
, int len
)
469 struct ct_data
*ct
= (struct ct_data
*)ctptr
;
471 int milliseconds
= (ct
->ct_wait
.tv_sec
* 1000) +
472 (ct
->ct_wait
.tv_usec
/ 1000);
481 switch (__poll(&fd
, 1, milliseconds
))
484 ct
->ct_error
.re_status
= RPC_TIMEDOUT
;
490 ct
->ct_error
.re_status
= RPC_CANTRECV
;
491 ct
->ct_error
.re_errno
= errno
;
496 switch (len
= __read (ct
->ct_sock
, buf
, len
))
501 ct
->ct_error
.re_errno
= ECONNRESET
;
502 ct
->ct_error
.re_status
= RPC_CANTRECV
;
503 len
= -1; /* it's really an error */
507 ct
->ct_error
.re_errno
= errno
;
508 ct
->ct_error
.re_status
= RPC_CANTRECV
;
515 writetcp (char *ctptr
, char *buf
, int len
)
518 struct ct_data
*ct
= (struct ct_data
*)ctptr
;
520 for (cnt
= len
; cnt
> 0; cnt
-= i
, buf
+= i
)
522 if ((i
= __write (ct
->ct_sock
, buf
, cnt
)) == -1)
524 ct
->ct_error
.re_errno
= errno
;
525 ct
->ct_error
.re_status
= RPC_CANTSEND
;