Change #ifdef sparc to #ifdef sun for more portability.
[krb5.git] / src / lib / rpc / clnt_udp.c
1 /* @(#)clnt_udp.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.
9  * 
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.
13  * 
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.
17  * 
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.
21  * 
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.
25  * 
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_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro";
32 #endif
33
34 /*
35  * clnt_udp.c, Implements a UDP/IP based, client side RPC.
36  *
37  * Copyright (C) 1984, Sun Microsystems, Inc.
38  */
39
40 #include <stdio.h>
41 #include <rpc/rpc.h>
42 #include <sys/socket.h>
43 #include <sys/ioctl.h>
44 #if defined(sun)
45 #include <sys/filio.h>
46 #endif
47 #include <netdb.h>
48 #include <errno.h>
49 #include <rpc/pmap_clnt.h>
50
51 extern int errno;
52
53 /*
54  * UDP bases client side rpc operations
55  */
56 static enum clnt_stat   clntudp_call();
57 static void             clntudp_abort();
58 static void             clntudp_geterr();
59 static bool_t           clntudp_freeres();
60 static bool_t           clntudp_control();
61 static void             clntudp_destroy();
62
63 static struct clnt_ops udp_ops = {
64         clntudp_call,
65         clntudp_abort,
66         clntudp_geterr,
67         clntudp_freeres,
68         clntudp_destroy,
69         clntudp_control
70 };
71
72 /* 
73  * Private data kept per client handle
74  */
75 struct cu_data {
76         int                cu_sock;
77         bool_t             cu_closeit;
78         struct sockaddr_in cu_raddr;
79         int                cu_rlen;
80         struct timeval     cu_wait;
81         struct timeval     cu_total;
82         struct rpc_err     cu_error;
83         XDR                cu_outxdrs;
84         unsigned int               cu_xdrpos;
85         unsigned int               cu_sendsz;
86         char               *cu_outbuf;
87         unsigned int               cu_recvsz;
88         char               cu_inbuf[1];
89 };
90
91 /*
92  * Create a UDP based client handle.
93  * If *sockp<0, *sockp is set to a newly created UPD socket.
94  * If raddr->sin_port is 0 a binder on the remote machine
95  * is consulted for the correct port number.
96  * NB: It is the clients responsibility to close *sockp.
97  * NB: The rpch->cl_auth is initialized to null authentication.
98  *     Caller may wish to set this something more useful.
99  *
100  * wait is the amount of time used between retransmitting a call if
101  * no response has been heard;  retransmition occurs until the actual
102  * rpc call times out.
103  *
104  * sendsz and recvsz are the maximum allowable packet sizes that can be
105  * sent and received.
106  */
107 CLIENT *
108 clntudp_bufcreate(raddr, program, version, wait, sockp, sendsz, recvsz)
109         struct sockaddr_in *raddr;
110         rpc_u_int32 program;
111         rpc_u_int32 version;
112         struct timeval wait;
113         register int *sockp;
114         unsigned int sendsz;
115         unsigned int recvsz;
116 {
117         CLIENT *cl;
118         register struct cu_data *cu;
119         struct timeval now;
120         struct rpc_msg call_msg;
121
122         cl = (CLIENT *)mem_alloc(sizeof(CLIENT));
123         if (cl == NULL) {
124                 (void) fprintf(stderr, "clntudp_create: out of memory\n");
125                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
126                 rpc_createerr.cf_error.re_errno = errno;
127                 goto fooy;
128         }
129         sendsz = ((sendsz + 3) / 4) * 4;
130         recvsz = ((recvsz + 3) / 4) * 4;
131         cu = (struct cu_data *)mem_alloc(sizeof(*cu) + sendsz + recvsz);
132         if (cu == NULL) {
133                 (void) fprintf(stderr, "clntudp_create: out of memory\n");
134                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
135                 rpc_createerr.cf_error.re_errno = errno;
136                 goto fooy;
137         }
138         cu->cu_outbuf = &cu->cu_inbuf[recvsz];
139
140         (void)gettimeofday(&now, (struct timezone *)0);
141         if (raddr->sin_port == 0) {
142                 unsigned short port;
143                 if ((port =
144                     pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
145                         goto fooy;
146                 }
147                 raddr->sin_port = htons(port);
148         }
149         cl->cl_ops = &udp_ops;
150         cl->cl_private = (caddr_t)cu;
151         cu->cu_raddr = *raddr;
152         cu->cu_rlen = sizeof (cu->cu_raddr);
153         cu->cu_wait = wait;
154         cu->cu_total.tv_sec = -1;
155         cu->cu_total.tv_usec = -1;
156         cu->cu_sendsz = sendsz;
157         cu->cu_recvsz = recvsz;
158         call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec;
159         call_msg.rm_direction = CALL;
160         call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
161         call_msg.rm_call.cb_prog = program;
162         call_msg.rm_call.cb_vers = version;
163         xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf,
164             sendsz, XDR_ENCODE);
165         if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
166                 goto fooy;
167         }
168         cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
169         if (*sockp < 0) {
170                 int dontblock = 1;
171
172                 *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
173                 if (*sockp < 0) {
174                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
175                         rpc_createerr.cf_error.re_errno = errno;
176                         goto fooy;
177                 }
178                 /* attempt to bind to prov port */
179                 (void)bindresvport(*sockp, (struct sockaddr_in *)0);
180                 /* the sockets rpc controls are non-blocking */
181                 (void)ioctl(*sockp, FIONBIO, (char *) &dontblock);
182                 cu->cu_closeit = TRUE;
183         } else {
184                 cu->cu_closeit = FALSE;
185         }
186         cu->cu_sock = *sockp;
187         cl->cl_auth = authnone_create();
188         return (cl);
189 fooy:
190         if (cu)
191                 mem_free((caddr_t)cu, sizeof(*cu) + sendsz + recvsz);
192         if (cl)
193                 mem_free((caddr_t)cl, sizeof(CLIENT));
194         return ((CLIENT *)NULL);
195 }
196
197 CLIENT *
198 clntudp_create(raddr, program, version, wait, sockp)
199         struct sockaddr_in *raddr;
200         rpc_u_int32 program;
201         rpc_u_int32 version;
202         struct timeval wait;
203         register int *sockp;
204 {
205
206         return(clntudp_bufcreate(raddr, program, version, wait, sockp,
207             UDPMSGSIZE, UDPMSGSIZE));
208 }
209
210 static enum clnt_stat 
211 clntudp_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout)
212         register CLIENT *cl;            /* client handle */
213         rpc_u_int32             proc;           /* procedure number */
214         xdrproc_t       xargs;          /* xdr routine for args */
215         caddr_t         argsp;          /* pointer to args */
216         xdrproc_t       xresults;       /* xdr routine for results */
217         caddr_t         resultsp;       /* pointer to results */
218         struct timeval  utimeout;       /* seconds to wait before giving up */
219 {
220         register struct cu_data *cu = (struct cu_data *)cl->cl_private;
221         register XDR *xdrs;
222         register int outlen;
223         register int inlen;
224         int fromlen;
225 #ifdef FD_SETSIZE
226         fd_set readfds;
227         fd_set mask;
228 #else
229         int readfds;
230         register int mask;
231 #endif /* def FD_SETSIZE */
232         struct sockaddr_in from;
233         struct rpc_msg reply_msg;
234         XDR reply_xdrs;
235         struct timeval time_waited;
236         bool_t ok;
237         int nrefreshes = 2;     /* number of times to refresh cred */
238         struct timeval timeout;
239         long procl = proc;
240
241         if (cu->cu_total.tv_usec == -1) {
242                 timeout = utimeout;     /* use supplied timeout */
243         } else {
244                 timeout = cu->cu_total; /* use default timeout */
245         }
246
247         time_waited.tv_sec = 0;
248         time_waited.tv_usec = 0;
249 call_again:
250         xdrs = &(cu->cu_outxdrs);
251         xdrs->x_op = XDR_ENCODE;
252         XDR_SETPOS(xdrs, cu->cu_xdrpos);
253         /*
254          * the transaction is the first thing in the out buffer
255          */
256         (*(unsigned short *)(cu->cu_outbuf))++;
257         if ((! XDR_PUTLONG(xdrs, &procl)) ||
258             (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
259             (! AUTH_WRAP(cl->cl_auth, xdrs, xargs, argsp)))
260                 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
261         outlen = (int)XDR_GETPOS(xdrs);
262
263 send_again:
264         if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0,
265             (struct sockaddr *)&(cu->cu_raddr), cu->cu_rlen)
266             != outlen) {
267                 cu->cu_error.re_errno = errno;
268                 return (cu->cu_error.re_status = RPC_CANTSEND);
269         }
270
271         /*
272          * Hack to provide rpc-based message passing
273          */
274         if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
275                 return (cu->cu_error.re_status = RPC_TIMEDOUT);
276         }
277         /*
278          * sub-optimal code appears here because we have
279          * some clock time to spare while the packets are in flight.
280          * (We assume that this is actually only executed once.)
281          */
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 = xdr_void;
285 #ifdef FD_SETSIZE
286         FD_ZERO(&mask);
287         FD_SET(cu->cu_sock, &mask);
288 #else
289         mask = 1 << cu->cu_sock;
290 #endif /* def FD_SETSIZE */
291         for (;;) {
292                 readfds = mask;
293                 switch (select(_rpc_dtablesize(), &readfds, (fd_set *)NULL, 
294                                (fd_set *)NULL, &(cu->cu_wait))) {
295
296                 case 0:
297                         time_waited.tv_sec += cu->cu_wait.tv_sec;
298                         time_waited.tv_usec += cu->cu_wait.tv_usec;
299                         while (time_waited.tv_usec >= 1000000) {
300                                 time_waited.tv_sec++;
301                                 time_waited.tv_usec -= 1000000;
302                         }
303                         if ((time_waited.tv_sec < timeout.tv_sec) ||
304                                 ((time_waited.tv_sec == timeout.tv_sec) &&
305                                 (time_waited.tv_usec < timeout.tv_usec)))
306                                 goto send_again;        
307                         return (cu->cu_error.re_status = RPC_TIMEDOUT);
308
309                 /*
310                  * buggy in other cases because time_waited is not being
311                  * updated.
312                  */
313                 case -1:
314                         if (errno == EINTR)
315                                 continue;       
316                         cu->cu_error.re_errno = errno;
317                         return (cu->cu_error.re_status = RPC_CANTRECV);
318                 }
319                 do {
320                         fromlen = sizeof(struct sockaddr);
321                         inlen = recvfrom(cu->cu_sock, cu->cu_inbuf, 
322                                 (int) cu->cu_recvsz, 0,
323                                 (struct sockaddr *)&from, &fromlen);
324                 } while (inlen < 0 && errno == EINTR);
325                 if (inlen < 0) {
326                         if (errno == EWOULDBLOCK)
327                                 continue;       
328                         cu->cu_error.re_errno = errno;
329                         return (cu->cu_error.re_status = RPC_CANTRECV);
330                 }
331                 if (inlen < sizeof(rpc_u_int32))
332                         continue;       
333                 /* see if reply transaction id matches sent id */
334                 if (*((rpc_u_int32 *)(cu->cu_inbuf)) != *((rpc_u_int32 *)(cu->cu_outbuf)))
335                         continue;       
336                 /* we now assume we have the proper reply */
337                 break;
338         }
339
340         /*
341          * now decode and validate the response
342          */
343         xdrmem_create(&reply_xdrs, cu->cu_inbuf, (unsigned int)inlen, XDR_DECODE);
344         ok = xdr_replymsg(&reply_xdrs, &reply_msg);
345         /* XDR_DESTROY(&reply_xdrs);  save a few cycles on noop destroy */
346         if (ok) {
347                 sunrpc_seterr_reply(&reply_msg, &(cu->cu_error));
348                 if (cu->cu_error.re_status == RPC_SUCCESS) {
349                         if (! AUTH_VALIDATE(cl->cl_auth,
350                                 &reply_msg.acpted_rply.ar_verf)) {
351                                 cu->cu_error.re_status = RPC_AUTHERROR;
352                                 cu->cu_error.re_why = AUTH_INVALIDRESP;
353                         } else if (! AUTH_UNWRAP(cl->cl_auth, &reply_xdrs,
354                                                  xresults, resultsp)) {
355                              if (cu->cu_error.re_status == RPC_SUCCESS)
356                                   cu->cu_error.re_status = RPC_CANTDECODERES;
357                         }
358                 }  /* end successful completion */
359                 else {
360                         /* maybe our credentials need to be refreshed ... */
361                         if (nrefreshes > 0 &&
362                             AUTH_REFRESH(cl->cl_auth, &reply_msg)) {
363                                 nrefreshes--;
364                                 goto call_again;
365                         }
366                 }  /* end of unsuccessful completion */
367                 /* free verifier */
368                 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
369                     (reply_msg.acpted_rply.ar_verf.oa_base != NULL)) {
370                     xdrs->x_op = XDR_FREE;
371                     (void)xdr_opaque_auth(xdrs,
372                                           &(reply_msg.acpted_rply.ar_verf));
373                 } 
374         }  /* end of valid reply message */
375         else {
376                 cu->cu_error.re_status = RPC_CANTDECODERES;
377         }
378         return (cu->cu_error.re_status);
379 }
380
381 static void
382 clntudp_geterr(cl, errp)
383         CLIENT *cl;
384         struct rpc_err *errp;
385 {
386         register struct cu_data *cu = (struct cu_data *)cl->cl_private;
387
388         *errp = cu->cu_error;
389 }
390
391
392 static bool_t
393 clntudp_freeres(cl, xdr_res, res_ptr)
394         CLIENT *cl;
395         xdrproc_t xdr_res;
396         caddr_t res_ptr;
397 {
398         register struct cu_data *cu = (struct cu_data *)cl->cl_private;
399         register XDR *xdrs = &(cu->cu_outxdrs);
400
401         xdrs->x_op = XDR_FREE;
402         return ((*xdr_res)(xdrs, res_ptr));
403 }
404
405 static void 
406 clntudp_abort(/*h*/)
407         /*CLIENT *h;*/
408 {
409 }
410
411 static bool_t
412 clntudp_control(cl, request, info)
413         CLIENT *cl;
414         int request;
415         char *info;
416 {
417         register struct cu_data *cu = (struct cu_data *)cl->cl_private;
418         int len;
419         
420         switch (request) {
421         case CLSET_TIMEOUT:
422                 cu->cu_total = *(struct timeval *)info;
423                 break;
424         case CLGET_TIMEOUT:
425                 *(struct timeval *)info = cu->cu_total;
426                 break;
427         case CLSET_RETRY_TIMEOUT:
428                 cu->cu_wait = *(struct timeval *)info;
429                 break;
430         case CLGET_RETRY_TIMEOUT:
431                 *(struct timeval *)info = cu->cu_wait;
432                 break;
433         case CLGET_SERVER_ADDR:
434                 *(struct sockaddr_in *)info = cu->cu_raddr;
435                 break;
436         case CLGET_LOCAL_ADDR:
437                 len = sizeof(struct sockaddr);
438                 if (getsockname(cu->cu_sock, (struct sockaddr*)info, &len) < 0)
439                      return FALSE;
440                 else
441                      return TRUE;
442         default:
443                 return (FALSE);
444         }
445         return (TRUE);
446 }
447         
448 static void
449 clntudp_destroy(cl)
450         CLIENT *cl;
451 {
452         register struct cu_data *cu = (struct cu_data *)cl->cl_private;
453
454         if (cu->cu_closeit) {
455                 (void)close(cu->cu_sock);
456         }
457         XDR_DESTROY(&(cu->cu_outxdrs));
458         mem_free((caddr_t)cu, (sizeof(*cu) + cu->cu_sendsz + cu->cu_recvsz));
459         mem_free((caddr_t)cl, sizeof(CLIENT));
460 }