xref: /titanic_52/usr/src/boot/lib/libstand/rpc.c (revision 4a5d661a82b942b6538acd26209d959ce98b593a)
1*4a5d661aSToomas Soome /*	$NetBSD: rpc.c,v 1.18 1998/01/23 19:27:45 thorpej Exp $	*/
2*4a5d661aSToomas Soome 
3*4a5d661aSToomas Soome /*
4*4a5d661aSToomas Soome  * Copyright (c) 1992 Regents of the University of California.
5*4a5d661aSToomas Soome  * All rights reserved.
6*4a5d661aSToomas Soome  *
7*4a5d661aSToomas Soome  * This software was developed by the Computer Systems Engineering group
8*4a5d661aSToomas Soome  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9*4a5d661aSToomas Soome  * contributed to Berkeley.
10*4a5d661aSToomas Soome  *
11*4a5d661aSToomas Soome  * Redistribution and use in source and binary forms, with or without
12*4a5d661aSToomas Soome  * modification, are permitted provided that the following conditions
13*4a5d661aSToomas Soome  * are met:
14*4a5d661aSToomas Soome  * 1. Redistributions of source code must retain the above copyright
15*4a5d661aSToomas Soome  *    notice, this list of conditions and the following disclaimer.
16*4a5d661aSToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
17*4a5d661aSToomas Soome  *    notice, this list of conditions and the following disclaimer in the
18*4a5d661aSToomas Soome  *    documentation and/or other materials provided with the distribution.
19*4a5d661aSToomas Soome  * 4. Neither the name of the University nor the names of its contributors
20*4a5d661aSToomas Soome  *    may be used to endorse or promote products derived from this software
21*4a5d661aSToomas Soome  *    without specific prior written permission.
22*4a5d661aSToomas Soome  *
23*4a5d661aSToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24*4a5d661aSToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*4a5d661aSToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*4a5d661aSToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27*4a5d661aSToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*4a5d661aSToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*4a5d661aSToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*4a5d661aSToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*4a5d661aSToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*4a5d661aSToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*4a5d661aSToomas Soome  * SUCH DAMAGE.
34*4a5d661aSToomas Soome  *
35*4a5d661aSToomas Soome  * @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp  (LBL)
36*4a5d661aSToomas Soome  */
37*4a5d661aSToomas Soome 
38*4a5d661aSToomas Soome #include <sys/cdefs.h>
39*4a5d661aSToomas Soome __FBSDID("$FreeBSD$");
40*4a5d661aSToomas Soome 
41*4a5d661aSToomas Soome /*
42*4a5d661aSToomas Soome  * RPC functions used by NFS and bootparams.
43*4a5d661aSToomas Soome  * Note that bootparams requires the ability to find out the
44*4a5d661aSToomas Soome  * address of the server from which its response has come.
45*4a5d661aSToomas Soome  * This is supported by keeping the IP/UDP headers in the
46*4a5d661aSToomas Soome  * buffer space provided by the caller.  (See rpc_fromaddr)
47*4a5d661aSToomas Soome  */
48*4a5d661aSToomas Soome 
49*4a5d661aSToomas Soome #include <sys/param.h>
50*4a5d661aSToomas Soome #include <sys/socket.h>
51*4a5d661aSToomas Soome 
52*4a5d661aSToomas Soome #include <netinet/in.h>
53*4a5d661aSToomas Soome #include <netinet/in_systm.h>
54*4a5d661aSToomas Soome 
55*4a5d661aSToomas Soome #include <string.h>
56*4a5d661aSToomas Soome 
57*4a5d661aSToomas Soome #include "rpcv2.h"
58*4a5d661aSToomas Soome 
59*4a5d661aSToomas Soome #include "stand.h"
60*4a5d661aSToomas Soome #include "net.h"
61*4a5d661aSToomas Soome #include "netif.h"
62*4a5d661aSToomas Soome #include "rpc.h"
63*4a5d661aSToomas Soome 
64*4a5d661aSToomas Soome struct auth_info {
65*4a5d661aSToomas Soome 	int32_t 	authtype;	/* auth type */
66*4a5d661aSToomas Soome 	u_int32_t	authlen;	/* auth length */
67*4a5d661aSToomas Soome };
68*4a5d661aSToomas Soome 
69*4a5d661aSToomas Soome struct auth_unix {
70*4a5d661aSToomas Soome 	int32_t   ua_time;
71*4a5d661aSToomas Soome 	int32_t   ua_hostname;	/* null */
72*4a5d661aSToomas Soome 	int32_t   ua_uid;
73*4a5d661aSToomas Soome 	int32_t   ua_gid;
74*4a5d661aSToomas Soome 	int32_t   ua_gidlist;	/* null */
75*4a5d661aSToomas Soome };
76*4a5d661aSToomas Soome 
77*4a5d661aSToomas Soome struct rpc_call {
78*4a5d661aSToomas Soome 	u_int32_t	rp_xid;		/* request transaction id */
79*4a5d661aSToomas Soome 	int32_t 	rp_direction;	/* call direction (0) */
80*4a5d661aSToomas Soome 	u_int32_t	rp_rpcvers;	/* rpc version (2) */
81*4a5d661aSToomas Soome 	u_int32_t	rp_prog;	/* program */
82*4a5d661aSToomas Soome 	u_int32_t	rp_vers;	/* version */
83*4a5d661aSToomas Soome 	u_int32_t	rp_proc;	/* procedure */
84*4a5d661aSToomas Soome };
85*4a5d661aSToomas Soome 
86*4a5d661aSToomas Soome struct rpc_reply {
87*4a5d661aSToomas Soome 	u_int32_t	rp_xid;		/* request transaction id */
88*4a5d661aSToomas Soome 	int32_t 	rp_direction;	/* call direction (1) */
89*4a5d661aSToomas Soome 	int32_t 	rp_astatus;	/* accept status (0: accepted) */
90*4a5d661aSToomas Soome 	union {
91*4a5d661aSToomas Soome 		u_int32_t	rpu_errno;
92*4a5d661aSToomas Soome 		struct {
93*4a5d661aSToomas Soome 			struct auth_info rok_auth;
94*4a5d661aSToomas Soome 			u_int32_t	rok_status;
95*4a5d661aSToomas Soome 		} rpu_rok;
96*4a5d661aSToomas Soome 	} rp_u;
97*4a5d661aSToomas Soome };
98*4a5d661aSToomas Soome 
99*4a5d661aSToomas Soome /* Local forwards */
100*4a5d661aSToomas Soome static	ssize_t recvrpc(struct iodesc *, void *, size_t, time_t);
101*4a5d661aSToomas Soome static	int rpc_getport(struct iodesc *, n_long, n_long);
102*4a5d661aSToomas Soome 
103*4a5d661aSToomas Soome int rpc_xid;
104*4a5d661aSToomas Soome int rpc_port = 0x400;	/* predecrement */
105*4a5d661aSToomas Soome 
106*4a5d661aSToomas Soome /*
107*4a5d661aSToomas Soome  * Make a rpc call; return length of answer
108*4a5d661aSToomas Soome  * Note: Caller must leave room for headers.
109*4a5d661aSToomas Soome  */
110*4a5d661aSToomas Soome ssize_t
111*4a5d661aSToomas Soome rpc_call(struct iodesc *d, n_long prog, n_long vers, n_long proc,
112*4a5d661aSToomas Soome 	void *sdata, size_t slen, void *rdata, size_t rlen)
113*4a5d661aSToomas Soome {
114*4a5d661aSToomas Soome 	ssize_t cc;
115*4a5d661aSToomas Soome 	struct auth_info *auth;
116*4a5d661aSToomas Soome 	struct rpc_call *call;
117*4a5d661aSToomas Soome 	struct rpc_reply *reply;
118*4a5d661aSToomas Soome 	char *send_head, *send_tail;
119*4a5d661aSToomas Soome 	char *recv_head, *recv_tail;
120*4a5d661aSToomas Soome 	n_long x;
121*4a5d661aSToomas Soome 	int port;	/* host order */
122*4a5d661aSToomas Soome 
123*4a5d661aSToomas Soome #ifdef RPC_DEBUG
124*4a5d661aSToomas Soome 	if (debug)
125*4a5d661aSToomas Soome 		printf("rpc_call: prog=0x%x vers=%d proc=%d\n",
126*4a5d661aSToomas Soome 		    prog, vers, proc);
127*4a5d661aSToomas Soome #endif
128*4a5d661aSToomas Soome 
129*4a5d661aSToomas Soome 	port = rpc_getport(d, prog, vers);
130*4a5d661aSToomas Soome 	if (port == -1)
131*4a5d661aSToomas Soome 		return (-1);
132*4a5d661aSToomas Soome 
133*4a5d661aSToomas Soome 	d->destport = htons(port);
134*4a5d661aSToomas Soome 
135*4a5d661aSToomas Soome 	/*
136*4a5d661aSToomas Soome 	 * Prepend authorization stuff and headers.
137*4a5d661aSToomas Soome 	 * Note, must prepend things in reverse order.
138*4a5d661aSToomas Soome 	 */
139*4a5d661aSToomas Soome 	send_head = sdata;
140*4a5d661aSToomas Soome 	send_tail = (char *)sdata + slen;
141*4a5d661aSToomas Soome 
142*4a5d661aSToomas Soome 	/* Auth verifier is always auth_null */
143*4a5d661aSToomas Soome 	send_head -= sizeof(*auth);
144*4a5d661aSToomas Soome 	auth = (struct auth_info *)send_head;
145*4a5d661aSToomas Soome 	auth->authtype = htonl(RPCAUTH_NULL);
146*4a5d661aSToomas Soome 	auth->authlen = 0;
147*4a5d661aSToomas Soome 
148*4a5d661aSToomas Soome #if 1
149*4a5d661aSToomas Soome 	/* Auth credentials: always auth unix (as root) */
150*4a5d661aSToomas Soome 	send_head -= sizeof(struct auth_unix);
151*4a5d661aSToomas Soome 	bzero(send_head, sizeof(struct auth_unix));
152*4a5d661aSToomas Soome 	send_head -= sizeof(*auth);
153*4a5d661aSToomas Soome 	auth = (struct auth_info *)send_head;
154*4a5d661aSToomas Soome 	auth->authtype = htonl(RPCAUTH_UNIX);
155*4a5d661aSToomas Soome 	auth->authlen = htonl(sizeof(struct auth_unix));
156*4a5d661aSToomas Soome #else
157*4a5d661aSToomas Soome 	/* Auth credentials: always auth_null (XXX OK?) */
158*4a5d661aSToomas Soome 	send_head -= sizeof(*auth);
159*4a5d661aSToomas Soome 	auth = send_head;
160*4a5d661aSToomas Soome 	auth->authtype = htonl(RPCAUTH_NULL);
161*4a5d661aSToomas Soome 	auth->authlen = 0;
162*4a5d661aSToomas Soome #endif
163*4a5d661aSToomas Soome 
164*4a5d661aSToomas Soome 	/* RPC call structure. */
165*4a5d661aSToomas Soome 	send_head -= sizeof(*call);
166*4a5d661aSToomas Soome 	call = (struct rpc_call *)send_head;
167*4a5d661aSToomas Soome 	rpc_xid++;
168*4a5d661aSToomas Soome 	call->rp_xid       = htonl(rpc_xid);
169*4a5d661aSToomas Soome 	call->rp_direction = htonl(RPC_CALL);
170*4a5d661aSToomas Soome 	call->rp_rpcvers   = htonl(RPC_VER2);
171*4a5d661aSToomas Soome 	call->rp_prog = htonl(prog);
172*4a5d661aSToomas Soome 	call->rp_vers = htonl(vers);
173*4a5d661aSToomas Soome 	call->rp_proc = htonl(proc);
174*4a5d661aSToomas Soome 
175*4a5d661aSToomas Soome 	/* Make room for the rpc_reply header. */
176*4a5d661aSToomas Soome 	recv_head = rdata;
177*4a5d661aSToomas Soome 	recv_tail = (char *)rdata + rlen;
178*4a5d661aSToomas Soome 	recv_head -= sizeof(*reply);
179*4a5d661aSToomas Soome 
180*4a5d661aSToomas Soome 	cc = sendrecv(d,
181*4a5d661aSToomas Soome 	    sendudp, send_head, send_tail - send_head,
182*4a5d661aSToomas Soome 	    recvrpc, recv_head, recv_tail - recv_head);
183*4a5d661aSToomas Soome 
184*4a5d661aSToomas Soome #ifdef RPC_DEBUG
185*4a5d661aSToomas Soome 	if (debug)
186*4a5d661aSToomas Soome 		printf("callrpc: cc=%ld rlen=%lu\n", (long)cc, (u_long)rlen);
187*4a5d661aSToomas Soome #endif
188*4a5d661aSToomas Soome 	if (cc == -1)
189*4a5d661aSToomas Soome 		return (-1);
190*4a5d661aSToomas Soome 
191*4a5d661aSToomas Soome 	if (cc <= sizeof(*reply)) {
192*4a5d661aSToomas Soome 		errno = EBADRPC;
193*4a5d661aSToomas Soome 		return (-1);
194*4a5d661aSToomas Soome 	}
195*4a5d661aSToomas Soome 
196*4a5d661aSToomas Soome 	recv_tail = recv_head + cc;
197*4a5d661aSToomas Soome 
198*4a5d661aSToomas Soome 	/*
199*4a5d661aSToomas Soome 	 * Check the RPC reply status.
200*4a5d661aSToomas Soome 	 * The xid, dir, astatus were already checked.
201*4a5d661aSToomas Soome 	 */
202*4a5d661aSToomas Soome 	reply = (struct rpc_reply *)recv_head;
203*4a5d661aSToomas Soome 	auth = &reply->rp_u.rpu_rok.rok_auth;
204*4a5d661aSToomas Soome 	x = ntohl(auth->authlen);
205*4a5d661aSToomas Soome 	if (x != 0) {
206*4a5d661aSToomas Soome #ifdef RPC_DEBUG
207*4a5d661aSToomas Soome 		if (debug)
208*4a5d661aSToomas Soome 			printf("callrpc: reply auth != NULL\n");
209*4a5d661aSToomas Soome #endif
210*4a5d661aSToomas Soome 		errno = EBADRPC;
211*4a5d661aSToomas Soome 		return(-1);
212*4a5d661aSToomas Soome 	}
213*4a5d661aSToomas Soome 	x = ntohl(reply->rp_u.rpu_rok.rok_status);
214*4a5d661aSToomas Soome 	if (x != 0) {
215*4a5d661aSToomas Soome 		printf("callrpc: error = %ld\n", (long)x);
216*4a5d661aSToomas Soome 		errno = EBADRPC;
217*4a5d661aSToomas Soome 		return(-1);
218*4a5d661aSToomas Soome 	}
219*4a5d661aSToomas Soome 	recv_head += sizeof(*reply);
220*4a5d661aSToomas Soome 
221*4a5d661aSToomas Soome 	return (ssize_t)(recv_tail - recv_head);
222*4a5d661aSToomas Soome }
223*4a5d661aSToomas Soome 
224*4a5d661aSToomas Soome /*
225*4a5d661aSToomas Soome  * Returns true if packet is the one we're waiting for.
226*4a5d661aSToomas Soome  * This just checks the XID, direction, acceptance.
227*4a5d661aSToomas Soome  * Remaining checks are done by callrpc
228*4a5d661aSToomas Soome  */
229*4a5d661aSToomas Soome static ssize_t
230*4a5d661aSToomas Soome recvrpc(struct iodesc *d, void *pkt, size_t len, time_t tleft)
231*4a5d661aSToomas Soome {
232*4a5d661aSToomas Soome 	struct rpc_reply *reply;
233*4a5d661aSToomas Soome 	ssize_t	n;
234*4a5d661aSToomas Soome 	int	x;
235*4a5d661aSToomas Soome 
236*4a5d661aSToomas Soome 	errno = 0;
237*4a5d661aSToomas Soome #ifdef RPC_DEBUG
238*4a5d661aSToomas Soome 	if (debug)
239*4a5d661aSToomas Soome 		printf("recvrpc: called len=%lu\n", (u_long)len);
240*4a5d661aSToomas Soome #endif
241*4a5d661aSToomas Soome 
242*4a5d661aSToomas Soome 	n = readudp(d, pkt, len, tleft);
243*4a5d661aSToomas Soome 	if (n <= (4 * 4))
244*4a5d661aSToomas Soome 		return -1;
245*4a5d661aSToomas Soome 
246*4a5d661aSToomas Soome 	reply = (struct rpc_reply *)pkt;
247*4a5d661aSToomas Soome 
248*4a5d661aSToomas Soome 	x = ntohl(reply->rp_xid);
249*4a5d661aSToomas Soome 	if (x != rpc_xid) {
250*4a5d661aSToomas Soome #ifdef RPC_DEBUG
251*4a5d661aSToomas Soome 		if (debug)
252*4a5d661aSToomas Soome 			printf("recvrpc: rp_xid %d != xid %d\n", x, rpc_xid);
253*4a5d661aSToomas Soome #endif
254*4a5d661aSToomas Soome 		return -1;
255*4a5d661aSToomas Soome 	}
256*4a5d661aSToomas Soome 
257*4a5d661aSToomas Soome 	x = ntohl(reply->rp_direction);
258*4a5d661aSToomas Soome 	if (x != RPC_REPLY) {
259*4a5d661aSToomas Soome #ifdef RPC_DEBUG
260*4a5d661aSToomas Soome 		if (debug)
261*4a5d661aSToomas Soome 			printf("recvrpc: rp_direction %d != REPLY\n", x);
262*4a5d661aSToomas Soome #endif
263*4a5d661aSToomas Soome 		return -1;
264*4a5d661aSToomas Soome 	}
265*4a5d661aSToomas Soome 
266*4a5d661aSToomas Soome 	x = ntohl(reply->rp_astatus);
267*4a5d661aSToomas Soome 	if (x != RPC_MSGACCEPTED) {
268*4a5d661aSToomas Soome 		errno = ntohl(reply->rp_u.rpu_errno);
269*4a5d661aSToomas Soome 		printf("recvrpc: reject, astat=%d, errno=%d\n", x, errno);
270*4a5d661aSToomas Soome 		return -1;
271*4a5d661aSToomas Soome 	}
272*4a5d661aSToomas Soome 
273*4a5d661aSToomas Soome 	/* Return data count (thus indicating success) */
274*4a5d661aSToomas Soome 	return (n);
275*4a5d661aSToomas Soome }
276*4a5d661aSToomas Soome 
277*4a5d661aSToomas Soome /*
278*4a5d661aSToomas Soome  * Given a pointer to a reply just received,
279*4a5d661aSToomas Soome  * dig out the IP address/port from the headers.
280*4a5d661aSToomas Soome  */
281*4a5d661aSToomas Soome void
282*4a5d661aSToomas Soome rpc_fromaddr(void *pkt, struct in_addr *addr, u_short *port)
283*4a5d661aSToomas Soome {
284*4a5d661aSToomas Soome 	struct hackhdr {
285*4a5d661aSToomas Soome 		/* Tail of IP header: just IP addresses */
286*4a5d661aSToomas Soome 		n_long ip_src;
287*4a5d661aSToomas Soome 		n_long ip_dst;
288*4a5d661aSToomas Soome 		/* UDP header: */
289*4a5d661aSToomas Soome 		u_int16_t uh_sport;		/* source port */
290*4a5d661aSToomas Soome 		u_int16_t uh_dport;		/* destination port */
291*4a5d661aSToomas Soome 		int16_t	  uh_ulen;		/* udp length */
292*4a5d661aSToomas Soome 		u_int16_t uh_sum;		/* udp checksum */
293*4a5d661aSToomas Soome 		/* RPC reply header: */
294*4a5d661aSToomas Soome 		struct rpc_reply rpc;
295*4a5d661aSToomas Soome 	} *hhdr;
296*4a5d661aSToomas Soome 
297*4a5d661aSToomas Soome 	hhdr = ((struct hackhdr *)pkt) - 1;
298*4a5d661aSToomas Soome 	addr->s_addr = hhdr->ip_src;
299*4a5d661aSToomas Soome 	*port = hhdr->uh_sport;
300*4a5d661aSToomas Soome }
301*4a5d661aSToomas Soome 
302*4a5d661aSToomas Soome /*
303*4a5d661aSToomas Soome  * RPC Portmapper cache
304*4a5d661aSToomas Soome  */
305*4a5d661aSToomas Soome #define PMAP_NUM 8			/* need at most 5 pmap entries */
306*4a5d661aSToomas Soome 
307*4a5d661aSToomas Soome int rpc_pmap_num;
308*4a5d661aSToomas Soome struct pmap_list {
309*4a5d661aSToomas Soome 	struct in_addr	addr;	/* server, net order */
310*4a5d661aSToomas Soome 	u_int	prog;		/* host order */
311*4a5d661aSToomas Soome 	u_int	vers;		/* host order */
312*4a5d661aSToomas Soome 	int 	port;		/* host order */
313*4a5d661aSToomas Soome } rpc_pmap_list[PMAP_NUM];
314*4a5d661aSToomas Soome 
315*4a5d661aSToomas Soome /*
316*4a5d661aSToomas Soome  * return port number in host order, or -1.
317*4a5d661aSToomas Soome  * arguments are:
318*4a5d661aSToomas Soome  *  addr .. server, net order.
319*4a5d661aSToomas Soome  *  prog .. host order.
320*4a5d661aSToomas Soome  *  vers .. host order.
321*4a5d661aSToomas Soome  */
322*4a5d661aSToomas Soome int
323*4a5d661aSToomas Soome rpc_pmap_getcache(struct in_addr addr, u_int prog, u_int vers)
324*4a5d661aSToomas Soome {
325*4a5d661aSToomas Soome 	struct pmap_list *pl;
326*4a5d661aSToomas Soome 
327*4a5d661aSToomas Soome 	for (pl = rpc_pmap_list; pl < &rpc_pmap_list[rpc_pmap_num]; pl++) {
328*4a5d661aSToomas Soome 		if (pl->addr.s_addr == addr.s_addr &&
329*4a5d661aSToomas Soome 			pl->prog == prog && pl->vers == vers )
330*4a5d661aSToomas Soome 		{
331*4a5d661aSToomas Soome 			return (pl->port);
332*4a5d661aSToomas Soome 		}
333*4a5d661aSToomas Soome 	}
334*4a5d661aSToomas Soome 	return (-1);
335*4a5d661aSToomas Soome }
336*4a5d661aSToomas Soome 
337*4a5d661aSToomas Soome /*
338*4a5d661aSToomas Soome  * arguments are:
339*4a5d661aSToomas Soome  *  addr .. server, net order.
340*4a5d661aSToomas Soome  *  prog .. host order.
341*4a5d661aSToomas Soome  *  vers .. host order.
342*4a5d661aSToomas Soome  *  port .. host order.
343*4a5d661aSToomas Soome  */
344*4a5d661aSToomas Soome void
345*4a5d661aSToomas Soome rpc_pmap_putcache(struct in_addr addr, u_int prog, u_int vers, int port)
346*4a5d661aSToomas Soome {
347*4a5d661aSToomas Soome 	struct pmap_list *pl;
348*4a5d661aSToomas Soome 
349*4a5d661aSToomas Soome 	/* Don't overflow cache... */
350*4a5d661aSToomas Soome 	if (rpc_pmap_num >= PMAP_NUM) {
351*4a5d661aSToomas Soome 		/* ... just re-use the last entry. */
352*4a5d661aSToomas Soome 		rpc_pmap_num = PMAP_NUM - 1;
353*4a5d661aSToomas Soome #ifdef	RPC_DEBUG
354*4a5d661aSToomas Soome 		printf("rpc_pmap_putcache: cache overflow\n");
355*4a5d661aSToomas Soome #endif
356*4a5d661aSToomas Soome 	}
357*4a5d661aSToomas Soome 
358*4a5d661aSToomas Soome 	pl = &rpc_pmap_list[rpc_pmap_num];
359*4a5d661aSToomas Soome 	rpc_pmap_num++;
360*4a5d661aSToomas Soome 
361*4a5d661aSToomas Soome 	/* Cache answer */
362*4a5d661aSToomas Soome 	pl->addr = addr;
363*4a5d661aSToomas Soome 	pl->prog = prog;
364*4a5d661aSToomas Soome 	pl->vers = vers;
365*4a5d661aSToomas Soome 	pl->port = port;
366*4a5d661aSToomas Soome }
367*4a5d661aSToomas Soome 
368*4a5d661aSToomas Soome 
369*4a5d661aSToomas Soome /*
370*4a5d661aSToomas Soome  * Request a port number from the port mapper.
371*4a5d661aSToomas Soome  * Returns the port in host order.
372*4a5d661aSToomas Soome  * prog and vers are host order.
373*4a5d661aSToomas Soome  */
374*4a5d661aSToomas Soome int
375*4a5d661aSToomas Soome rpc_getport(struct iodesc *d, n_long prog, n_long vers)
376*4a5d661aSToomas Soome {
377*4a5d661aSToomas Soome 	struct args {
378*4a5d661aSToomas Soome 		n_long	prog;		/* call program */
379*4a5d661aSToomas Soome 		n_long	vers;		/* call version */
380*4a5d661aSToomas Soome 		n_long	proto;		/* call protocol */
381*4a5d661aSToomas Soome 		n_long	port;		/* call port (unused) */
382*4a5d661aSToomas Soome 	} *args;
383*4a5d661aSToomas Soome 	struct res {
384*4a5d661aSToomas Soome 		n_long port;
385*4a5d661aSToomas Soome 	} *res;
386*4a5d661aSToomas Soome 	struct {
387*4a5d661aSToomas Soome 		n_long	h[RPC_HEADER_WORDS];
388*4a5d661aSToomas Soome 		struct args d;
389*4a5d661aSToomas Soome 	} sdata;
390*4a5d661aSToomas Soome 	struct {
391*4a5d661aSToomas Soome 		n_long	h[RPC_HEADER_WORDS];
392*4a5d661aSToomas Soome 		struct res d;
393*4a5d661aSToomas Soome 		n_long  pad;
394*4a5d661aSToomas Soome 	} rdata;
395*4a5d661aSToomas Soome 	ssize_t cc;
396*4a5d661aSToomas Soome 	int port;
397*4a5d661aSToomas Soome 
398*4a5d661aSToomas Soome #ifdef RPC_DEBUG
399*4a5d661aSToomas Soome 	if (debug)
400*4a5d661aSToomas Soome 		printf("%s: prog=0x%x vers=%d\n", __func__, prog, vers);
401*4a5d661aSToomas Soome #endif
402*4a5d661aSToomas Soome 
403*4a5d661aSToomas Soome 	/* This one is fixed forever. */
404*4a5d661aSToomas Soome 	if (prog == PMAPPROG) {
405*4a5d661aSToomas Soome 		port = PMAPPORT;
406*4a5d661aSToomas Soome 		goto out;
407*4a5d661aSToomas Soome 	}
408*4a5d661aSToomas Soome 
409*4a5d661aSToomas Soome 	/* Try for cached answer first */
410*4a5d661aSToomas Soome 	port = rpc_pmap_getcache(d->destip, prog, vers);
411*4a5d661aSToomas Soome 	if (port != -1)
412*4a5d661aSToomas Soome 		goto out;
413*4a5d661aSToomas Soome 
414*4a5d661aSToomas Soome 	args = &sdata.d;
415*4a5d661aSToomas Soome 	args->prog = htonl(prog);
416*4a5d661aSToomas Soome 	args->vers = htonl(vers);
417*4a5d661aSToomas Soome 	args->proto = htonl(IPPROTO_UDP);
418*4a5d661aSToomas Soome 	args->port = 0;
419*4a5d661aSToomas Soome 	res = &rdata.d;
420*4a5d661aSToomas Soome 
421*4a5d661aSToomas Soome 	cc = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
422*4a5d661aSToomas Soome 		args, sizeof(*args), res, sizeof(*res));
423*4a5d661aSToomas Soome 	if (cc < sizeof(*res)) {
424*4a5d661aSToomas Soome 		printf("getport: %s", strerror(errno));
425*4a5d661aSToomas Soome 		errno = EBADRPC;
426*4a5d661aSToomas Soome 		return (-1);
427*4a5d661aSToomas Soome 	}
428*4a5d661aSToomas Soome 	port = (int)ntohl(res->port);
429*4a5d661aSToomas Soome 
430*4a5d661aSToomas Soome 	rpc_pmap_putcache(d->destip, prog, vers, port);
431*4a5d661aSToomas Soome 
432*4a5d661aSToomas Soome out:
433*4a5d661aSToomas Soome #ifdef RPC_DEBUG
434*4a5d661aSToomas Soome 	if (debug)
435*4a5d661aSToomas Soome 		printf("%s: port=%u\n", __func__, port);
436*4a5d661aSToomas Soome #endif
437*4a5d661aSToomas Soome 	return (port);
438*4a5d661aSToomas Soome }
439