]>
Commit | Line | Data |
---|---|---|
28f540f4 RM |
1 | /* |
2 | * clnt_tcp.c, Implements a TCP/IP based, client side RPC. | |
3 | * | |
a7ab6ec8 UD |
4 | * Copyright (c) 2010, Oracle America, Inc. |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are | |
8 | * met: | |
9 | * | |
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. | |
19 | * | |
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. | |
28f540f4 RM |
32 | * |
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). | |
38 | * | |
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.... | |
43 | * | |
44 | * Now go hang yourself. | |
45 | */ | |
46 | ||
e7fd8a39 UD |
47 | #include <netdb.h> |
48 | #include <errno.h> | |
28f540f4 | 49 | #include <stdio.h> |
e7fd8a39 | 50 | #include <unistd.h> |
4360eafd | 51 | #include <libintl.h> |
28f540f4 | 52 | #include <rpc/rpc.h> |
099a6fbd | 53 | #include <sys/poll.h> |
28f540f4 | 54 | #include <sys/socket.h> |
28f540f4 | 55 | #include <rpc/pmap_clnt.h> |
3ce1f295 | 56 | #include <wchar.h> |
82f43dd2 | 57 | #include <shlib-compat.h> |
28f540f4 | 58 | |
090ca000 UD |
59 | extern u_long _create_xid (void); |
60 | ||
e7fd8a39 | 61 | #define MCALL_MSG_SIZE 24 |
28f540f4 | 62 | |
e7fd8a39 UD |
63 | struct ct_data |
64 | { | |
65 | int ct_sock; | |
66 | bool_t ct_closeit; | |
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 */ | |
73 | XDR ct_xdrs; | |
74 | }; | |
75 | ||
76 | static int readtcp (char *, char *, int); | |
77 | static int writetcp (char *, char *, int); | |
78 | ||
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 *); | |
86 | ||
31d7b14c | 87 | static const struct clnt_ops tcp_ops = |
e7fd8a39 UD |
88 | { |
89 | clnttcp_call, | |
90 | clnttcp_abort, | |
91 | clnttcp_geterr, | |
92 | clnttcp_freeres, | |
93 | clnttcp_destroy, | |
94 | clnttcp_control | |
28f540f4 RM |
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 * | |
e7fd8a39 UD |
112 | clnttcp_create (struct sockaddr_in *raddr, u_long prog, u_long vers, |
113 | int *sockp, u_int sendsz, u_int recvsz) | |
28f540f4 | 114 | { |
e7fd8a39 | 115 | CLIENT *h; |
51028f34 | 116 | struct ct_data *ct; |
e7fd8a39 UD |
117 | struct rpc_msg call_msg; |
118 | ||
119 | h = (CLIENT *) mem_alloc (sizeof (*h)); | |
51028f34 UD |
120 | ct = (struct ct_data *) mem_alloc (sizeof (*ct)); |
121 | if (h == NULL || ct == NULL) | |
e7fd8a39 | 122 | { |
543cf8a9 | 123 | struct rpc_createerr *ce = &get_rpc_createerr (); |
1d20f7f8 | 124 | (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n")); |
543cf8a9 | 125 | ce->cf_stat = RPC_SYSTEMERROR; |
51028f34 | 126 | ce->cf_error.re_errno = ENOMEM; |
e7fd8a39 UD |
127 | goto fooy; |
128 | } | |
129 | ||
130 | /* | |
131 | * If no port number given ask the pmap for one | |
132 | */ | |
133 | if (raddr->sin_port == 0) | |
134 | { | |
135 | u_short port; | |
136 | if ((port = pmap_getport (raddr, prog, vers, IPPROTO_TCP)) == 0) | |
137 | { | |
138 | mem_free ((caddr_t) ct, sizeof (struct ct_data)); | |
139 | mem_free ((caddr_t) h, sizeof (CLIENT)); | |
140 | return ((CLIENT *) NULL); | |
28f540f4 | 141 | } |
e7fd8a39 UD |
142 | raddr->sin_port = htons (port); |
143 | } | |
144 | ||
145 | /* | |
146 | * If no socket given, open one | |
147 | */ | |
148 | if (*sockp < 0) | |
149 | { | |
50304ef0 | 150 | *sockp = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); |
e7fd8a39 UD |
151 | (void) bindresvport (*sockp, (struct sockaddr_in *) 0); |
152 | if ((*sockp < 0) | |
50304ef0 UD |
153 | || (__connect (*sockp, (struct sockaddr *) raddr, |
154 | sizeof (*raddr)) < 0)) | |
e7fd8a39 | 155 | { |
543cf8a9 UD |
156 | struct rpc_createerr *ce = &get_rpc_createerr (); |
157 | ce->cf_stat = RPC_SYSTEMERROR; | |
158 | ce->cf_error.re_errno = errno; | |
e7fd8a39 | 159 | if (*sockp >= 0) |
50304ef0 | 160 | (void) __close (*sockp); |
e7fd8a39 | 161 | goto fooy; |
28f540f4 | 162 | } |
e7fd8a39 UD |
163 | ct->ct_closeit = TRUE; |
164 | } | |
165 | else | |
166 | { | |
167 | ct->ct_closeit = FALSE; | |
168 | } | |
169 | ||
170 | /* | |
171 | * Set up private data struct | |
172 | */ | |
173 | ct->ct_sock = *sockp; | |
174 | ct->ct_wait.tv_usec = 0; | |
175 | ct->ct_waitset = FALSE; | |
176 | ct->ct_addr = *raddr; | |
177 | ||
178 | /* | |
179 | * Initialize call message | |
180 | */ | |
090ca000 | 181 | call_msg.rm_xid = _create_xid (); |
e7fd8a39 UD |
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; | |
186 | ||
187 | /* | |
188 | * pre-serialize the static part of the call msg and stash it away | |
189 | */ | |
7b57bfe5 UD |
190 | xdrmem_create (&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE, XDR_ENCODE); |
191 | if (!xdr_callhdr (&(ct->ct_xdrs), &call_msg)) | |
e7fd8a39 UD |
192 | { |
193 | if (ct->ct_closeit) | |
194 | { | |
50304ef0 | 195 | (void) __close (*sockp); |
28f540f4 | 196 | } |
e7fd8a39 UD |
197 | goto fooy; |
198 | } | |
199 | ct->ct_mpos = XDR_GETPOS (&(ct->ct_xdrs)); | |
200 | XDR_DESTROY (&(ct->ct_xdrs)); | |
201 | ||
202 | /* | |
203 | * Create a client handle which uses xdrrec for serialization | |
204 | * and authnone for authentication. | |
205 | */ | |
7b57bfe5 UD |
206 | xdrrec_create (&(ct->ct_xdrs), sendsz, recvsz, |
207 | (caddr_t) ct, readtcp, writetcp); | |
31d7b14c | 208 | h->cl_ops = (struct clnt_ops *) &tcp_ops; |
e7fd8a39 | 209 | h->cl_private = (caddr_t) ct; |
7b57bfe5 | 210 | h->cl_auth = authnone_create (); |
e7fd8a39 | 211 | return h; |
28f540f4 RM |
212 | |
213 | fooy: | |
e7fd8a39 UD |
214 | /* |
215 | * Something goofed, free stuff and barf | |
216 | */ | |
217 | mem_free ((caddr_t) ct, sizeof (struct ct_data)); | |
218 | mem_free ((caddr_t) h, sizeof (CLIENT)); | |
219 | return ((CLIENT *) NULL); | |
28f540f4 | 220 | } |
7b57bfe5 UD |
221 | #ifdef EXPORT_RPC_SYMBOLS |
222 | libc_hidden_def (clnttcp_create) | |
223 | #else | |
021db4be | 224 | libc_hidden_nolink_sunrpc (clnttcp_create, GLIBC_2_0) |
7b57bfe5 | 225 | #endif |
28f540f4 RM |
226 | |
227 | static enum clnt_stat | |
f63f2bfd JM |
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) | |
28f540f4 | 231 | { |
e7fd8a39 UD |
232 | struct ct_data *ct = (struct ct_data *) h->cl_private; |
233 | XDR *xdrs = &(ct->ct_xdrs); | |
234 | struct rpc_msg reply_msg; | |
235 | u_long x_id; | |
d9fee042 | 236 | uint32_t *msg_x_id = (uint32_t *) (ct->ct_mcall); /* yuk */ |
e7fd8a39 UD |
237 | bool_t shipnow; |
238 | int refreshes = 2; | |
239 | ||
240 | if (!ct->ct_waitset) | |
241 | { | |
242 | ct->ct_wait = timeout; | |
243 | } | |
244 | ||
245 | shipnow = | |
a69a8d9c UD |
246 | (xdr_results == (xdrproc_t) 0 && ct->ct_wait.tv_sec == 0 |
247 | && ct->ct_wait.tv_usec == 0) ? FALSE : TRUE; | |
28f540f4 RM |
248 | |
249 | call_again: | |
e7fd8a39 UD |
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))) | |
257 | { | |
258 | if (ct->ct_error.re_status == RPC_SUCCESS) | |
259 | ct->ct_error.re_status = RPC_CANTENCODEARGS; | |
7b57bfe5 | 260 | (void) xdrrec_endofrecord (xdrs, TRUE); |
e7fd8a39 UD |
261 | return (ct->ct_error.re_status); |
262 | } | |
7b57bfe5 | 263 | if (!xdrrec_endofrecord (xdrs, shipnow)) |
e7fd8a39 UD |
264 | return ct->ct_error.re_status = RPC_CANTSEND; |
265 | if (!shipnow) | |
266 | return RPC_SUCCESS; | |
267 | /* | |
268 | * Hack to provide rpc-based message passing | |
269 | */ | |
a69a8d9c | 270 | if (ct->ct_wait.tv_sec == 0 && ct->ct_wait.tv_usec == 0) |
e7fd8a39 UD |
271 | { |
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 | { | |
282 | reply_msg.acpted_rply.ar_verf = _null_auth; | |
283 | reply_msg.acpted_rply.ar_results.where = NULL; | |
7b57bfe5 UD |
284 | reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void; |
285 | if (!xdrrec_skiprecord (xdrs)) | |
e7fd8a39 UD |
286 | return (ct->ct_error.re_status); |
287 | /* now decode and validate the response header */ | |
7b57bfe5 | 288 | if (!xdr_replymsg (xdrs, &reply_msg)) |
e7fd8a39 UD |
289 | { |
290 | if (ct->ct_error.re_status == RPC_SUCCESS) | |
291 | continue; | |
292 | return ct->ct_error.re_status; | |
28f540f4 | 293 | } |
d9fee042 | 294 | if ((uint32_t) reply_msg.rm_xid == (uint32_t) x_id) |
e7fd8a39 UD |
295 | break; |
296 | } | |
297 | ||
298 | /* | |
299 | * process header | |
300 | */ | |
301 | _seterr_reply (&reply_msg, &(ct->ct_error)); | |
302 | if (ct->ct_error.re_status == RPC_SUCCESS) | |
303 | { | |
304 | if (!AUTH_VALIDATE (h->cl_auth, &reply_msg.acpted_rply.ar_verf)) | |
305 | { | |
306 | ct->ct_error.re_status = RPC_AUTHERROR; | |
307 | ct->ct_error.re_why = AUTH_INVALIDRESP; | |
28f540f4 | 308 | } |
e7fd8a39 UD |
309 | else if (!(*xdr_results) (xdrs, results_ptr)) |
310 | { | |
311 | if (ct->ct_error.re_status == RPC_SUCCESS) | |
312 | ct->ct_error.re_status = RPC_CANTDECODERES; | |
28f540f4 | 313 | } |
e7fd8a39 UD |
314 | /* free verifier ... */ |
315 | if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) | |
316 | { | |
317 | xdrs->x_op = XDR_FREE; | |
7b57bfe5 | 318 | (void) xdr_opaque_auth (xdrs, &(reply_msg.acpted_rply.ar_verf)); |
e7fd8a39 UD |
319 | } |
320 | } /* end successful completion */ | |
321 | else | |
322 | { | |
323 | /* maybe our credentials need to be refreshed ... */ | |
324 | if (refreshes-- && AUTH_REFRESH (h->cl_auth)) | |
325 | goto call_again; | |
326 | } /* end of unsuccessful completion */ | |
327 | return ct->ct_error.re_status; | |
28f540f4 RM |
328 | } |
329 | ||
330 | static void | |
9d46370c | 331 | clnttcp_geterr (CLIENT *h, struct rpc_err *errp) |
28f540f4 | 332 | { |
e7fd8a39 UD |
333 | struct ct_data *ct = |
334 | (struct ct_data *) h->cl_private; | |
28f540f4 | 335 | |
e7fd8a39 | 336 | *errp = ct->ct_error; |
28f540f4 RM |
337 | } |
338 | ||
339 | static bool_t | |
9d46370c | 340 | clnttcp_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr) |
28f540f4 | 341 | { |
e7fd8a39 UD |
342 | struct ct_data *ct = (struct ct_data *) cl->cl_private; |
343 | XDR *xdrs = &(ct->ct_xdrs); | |
28f540f4 | 344 | |
e7fd8a39 UD |
345 | xdrs->x_op = XDR_FREE; |
346 | return (*xdr_res) (xdrs, res_ptr); | |
28f540f4 RM |
347 | } |
348 | ||
349 | static void | |
60d2f8f3 | 350 | clnttcp_abort (void) |
28f540f4 RM |
351 | { |
352 | } | |
353 | ||
354 | static bool_t | |
26a60f90 | 355 | clnttcp_control (CLIENT *cl, int request, char *info) |
28f540f4 | 356 | { |
e7fd8a39 | 357 | struct ct_data *ct = (struct ct_data *) cl->cl_private; |
4efbd5cb | 358 | u_long ul; |
d9fee042 | 359 | uint32_t ui32; |
e7fd8a39 | 360 | |
26a60f90 | 361 | |
e7fd8a39 UD |
362 | switch (request) |
363 | { | |
26a60f90 UD |
364 | case CLSET_FD_CLOSE: |
365 | ct->ct_closeit = TRUE; | |
366 | break; | |
367 | case CLSET_FD_NCLOSE: | |
368 | ct->ct_closeit = FALSE; | |
369 | break; | |
e7fd8a39 UD |
370 | case CLSET_TIMEOUT: |
371 | ct->ct_wait = *(struct timeval *) info; | |
372 | ct->ct_waitset = TRUE; | |
373 | break; | |
374 | case CLGET_TIMEOUT: | |
375 | *(struct timeval *) info = ct->ct_wait; | |
376 | break; | |
377 | case CLGET_SERVER_ADDR: | |
378 | *(struct sockaddr_in *) info = ct->ct_addr; | |
379 | break; | |
26a60f90 UD |
380 | case CLGET_FD: |
381 | *(int *)info = ct->ct_sock; | |
382 | break; | |
383 | case CLGET_XID: | |
384 | /* | |
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 | |
388 | */ | |
fb1ae1ee JM |
389 | memcpy (&ui32, ct->ct_mcall, sizeof (ui32)); |
390 | ul = ntohl (ui32); | |
4efbd5cb | 391 | memcpy (info, &ul, sizeof (ul)); |
26a60f90 UD |
392 | break; |
393 | case CLSET_XID: | |
394 | /* This will set the xid of the NEXT call */ | |
fb1ae1ee JM |
395 | memcpy (&ul, info, sizeof (ul)); |
396 | ui32 = htonl (ul - 1); | |
397 | memcpy (ct->ct_mcall, &ui32, sizeof (ui32)); | |
26a60f90 | 398 | /* decrement by 1 as clnttcp_call() increments once */ |
f0ccf6ea | 399 | break; |
26a60f90 UD |
400 | case CLGET_VERS: |
401 | /* | |
402 | * This RELIES on the information that, in the call body, | |
403 | * the version number field is the fifth field from the | |
6f65e668 | 404 | * beginning of the RPC header. MUST be changed if the |
26a60f90 UD |
405 | * call_struct is changed |
406 | */ | |
fb1ae1ee JM |
407 | memcpy (&ui32, ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT, sizeof (ui32)); |
408 | ul = ntohl (ui32); | |
409 | memcpy (info, &ul, sizeof (ul)); | |
26a60f90 UD |
410 | break; |
411 | case CLSET_VERS: | |
fb1ae1ee JM |
412 | memcpy (&ul, info, sizeof (ul)); |
413 | ui32 = htonl (ul); | |
414 | memcpy (ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT, &ui32, sizeof (ui32)); | |
26a60f90 UD |
415 | break; |
416 | case CLGET_PROG: | |
417 | /* | |
418 | * This RELIES on the information that, in the call body, | |
419 | * the program number field is the field from the | |
6f65e668 | 420 | * beginning of the RPC header. MUST be changed if the |
26a60f90 UD |
421 | * call_struct is changed |
422 | */ | |
fb1ae1ee JM |
423 | memcpy (&ui32, ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT, sizeof (ui32)); |
424 | ul = ntohl (ui32); | |
425 | memcpy (info, &ul, sizeof (ul)); | |
26a60f90 UD |
426 | break; |
427 | case CLSET_PROG: | |
fb1ae1ee JM |
428 | memcpy (&ul, info, sizeof (ul)); |
429 | ui32 = htonl (ul); | |
430 | memcpy (ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT, &ui32, sizeof (ui32)); | |
26a60f90 UD |
431 | break; |
432 | /* The following are only possible with TI-RPC */ | |
433 | case CLGET_RETRY_TIMEOUT: | |
434 | case CLSET_RETRY_TIMEOUT: | |
435 | case CLGET_SVC_ADDR: | |
436 | case CLSET_SVC_ADDR: | |
437 | case CLSET_PUSH_TIMOD: | |
438 | case CLSET_POP_TIMOD: | |
e7fd8a39 UD |
439 | default: |
440 | return FALSE; | |
441 | } | |
442 | return TRUE; | |
28f540f4 RM |
443 | } |
444 | ||
445 | ||
446 | static void | |
e7fd8a39 | 447 | clnttcp_destroy (CLIENT *h) |
28f540f4 | 448 | { |
e7fd8a39 UD |
449 | struct ct_data *ct = |
450 | (struct ct_data *) h->cl_private; | |
451 | ||
452 | if (ct->ct_closeit) | |
453 | { | |
50304ef0 | 454 | (void) __close (ct->ct_sock); |
e7fd8a39 UD |
455 | } |
456 | XDR_DESTROY (&(ct->ct_xdrs)); | |
457 | mem_free ((caddr_t) ct, sizeof (struct ct_data)); | |
458 | mem_free ((caddr_t) h, sizeof (CLIENT)); | |
28f540f4 RM |
459 | } |
460 | ||
461 | /* | |
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. | |
465 | */ | |
466 | static int | |
e7fd8a39 | 467 | readtcp (char *ctptr, char *buf, int len) |
28f540f4 | 468 | { |
e7fd8a39 | 469 | struct ct_data *ct = (struct ct_data *)ctptr; |
099a6fbd UD |
470 | struct pollfd fd; |
471 | int milliseconds = (ct->ct_wait.tv_sec * 1000) + | |
472 | (ct->ct_wait.tv_usec / 1000); | |
28f540f4 | 473 | |
e7fd8a39 UD |
474 | if (len == 0) |
475 | return 0; | |
28f540f4 | 476 | |
099a6fbd UD |
477 | fd.fd = ct->ct_sock; |
478 | fd.events = POLLIN; | |
e7fd8a39 UD |
479 | while (TRUE) |
480 | { | |
099a6fbd | 481 | switch (__poll(&fd, 1, milliseconds)) |
e7fd8a39 | 482 | { |
28f540f4 | 483 | case 0: |
e7fd8a39 UD |
484 | ct->ct_error.re_status = RPC_TIMEDOUT; |
485 | return -1; | |
28f540f4 RM |
486 | |
487 | case -1: | |
e7fd8a39 UD |
488 | if (errno == EINTR) |
489 | continue; | |
490 | ct->ct_error.re_status = RPC_CANTRECV; | |
491 | ct->ct_error.re_errno = errno; | |
492 | return -1; | |
28f540f4 | 493 | } |
e7fd8a39 UD |
494 | break; |
495 | } | |
50304ef0 | 496 | switch (len = __read (ct->ct_sock, buf, len)) |
e7fd8a39 UD |
497 | { |
498 | ||
499 | case 0: | |
500 | /* premature eof */ | |
501 | ct->ct_error.re_errno = ECONNRESET; | |
502 | ct->ct_error.re_status = RPC_CANTRECV; | |
503 | len = -1; /* it's really an error */ | |
504 | break; | |
505 | ||
506 | case -1: | |
507 | ct->ct_error.re_errno = errno; | |
508 | ct->ct_error.re_status = RPC_CANTRECV; | |
509 | break; | |
510 | } | |
511 | return len; | |
28f540f4 RM |
512 | } |
513 | ||
514 | static int | |
e7fd8a39 | 515 | writetcp (char *ctptr, char *buf, int len) |
28f540f4 | 516 | { |
e7fd8a39 UD |
517 | int i, cnt; |
518 | struct ct_data *ct = (struct ct_data*)ctptr; | |
519 | ||
520 | for (cnt = len; cnt > 0; cnt -= i, buf += i) | |
521 | { | |
50304ef0 | 522 | if ((i = __write (ct->ct_sock, buf, cnt)) == -1) |
e7fd8a39 UD |
523 | { |
524 | ct->ct_error.re_errno = errno; | |
525 | ct->ct_error.re_status = RPC_CANTSEND; | |
526 | return -1; | |
28f540f4 | 527 | } |
e7fd8a39 UD |
528 | } |
529 | return len; | |
28f540f4 | 530 | } |