/*
* Copyright (c) 1992 Regents of the University of California.
* All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratory and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL)
*/
/*
* RPC functions used by NFS and bootparams.
* Note that bootparams requires the ability to find out the
* address of the server from which its response has come.
* This is supported by keeping the IP/UDP headers in the
* buffer space provided by the caller. (See rpc_fromaddr)
*/
/*
* Check the RPC reply status.
* The xid, dir, astatus were already checked.
*/
reply = (struct rpc_reply *)recv_head;
auth = &reply->rp_u.rpu_rok.rok_auth;
x = ntohl(auth->authlen);
if (x != 0) {
#ifdef RPC_DEBUG
if (debug)
printf("%s: reply auth != NULL\n", __func__);
#endif
errno = EBADRPC;
return -1;
}
x = ntohl(reply->rp_u.rpu_rok.rok_status);
if (x != 0) {
printf("%s: error = %d\n", __func__, x);
errno = EBADRPC;
return -1;
}
recv_head += sizeof(*reply);
return (ssize_t)(recv_tail - recv_head);
}
/*
* Returns true if packet is the one we're waiting for.
* This just checks the XID, direction, acceptance.
* Remaining checks are done by callrpc
*/
static ssize_t
recvrpc(struct iodesc *d, void *pkt, size_t len, saseconds_t tleft)
{
struct rpc_reply *reply;
ssize_t n;
int x;
errno = 0;
#ifdef RPC_DEBUG
if (debug)
printf("%s: called len=%zu\n", __func__, len);
#endif
n = readudp(d, pkt, len, tleft);
if (n <= (4 * 4))
return -1;
reply = (struct rpc_reply *)pkt;
x = ntohl(reply->rp_xid);
if (x != rpc_xid) {
#ifdef RPC_DEBUG
if (debug)
printf("%s: rp_xid %d != xid %d\n",
__func__, x, rpc_xid);
#endif
return -1;
}
x = ntohl(reply->rp_direction);
if (x != RPC_REPLY) {
#ifdef RPC_DEBUG
if (debug)
printf("%s: rp_direction %d != REPLY\n", __func__, x);
#endif
return -1;
}
/*
* RPC Portmapper cache
*/
#define PMAP_NUM 8 /* need at most 5 pmap entries */
int rpc_pmap_num;
struct pmap_list {
struct in_addr addr; /* server, net order */
u_int prog; /* host order */
u_int vers; /* host order */
int port; /* host order */
} rpc_pmap_list[PMAP_NUM];
/*
* return port number in host order, or -1.
* arguments are:
* addr .. server, net order.
* prog .. host order.
* vers .. host order.
*/
int
rpc_pmap_getcache(struct in_addr addr, u_int prog, u_int vers)
{
struct pmap_list *pl;
for (pl = rpc_pmap_list; pl < &rpc_pmap_list[rpc_pmap_num]; pl++) {
if (pl->addr.s_addr == addr.s_addr &&
pl->prog == prog && pl->vers == vers )
{
return pl->port;
}
}
return -1;
}
/*
* arguments are:
* addr .. server, net order.
* prog .. host order.
* vers .. host order.
* port .. host order.
*/
void
rpc_pmap_putcache(struct in_addr addr, u_int prog, u_int vers, int port)
{
struct pmap_list *pl;
/* Don't overflow cache... */
if (rpc_pmap_num >= PMAP_NUM) {
/* ... just re-use the last entry. */
rpc_pmap_num = PMAP_NUM - 1;
#ifdef RPC_DEBUG
printf("%s: cache overflow\n", __func__);
#endif
}
/*
* Request a port number from the port mapper.
* Returns the port in host order.
* prog and vers are host order.
*/
int
rpc_getport(struct iodesc *d, n_long prog, n_long vers)
{
struct args {
n_long prog; /* call program */
n_long vers; /* call version */
n_long proto; /* call protocol */
n_long port; /* call port (unused) */
} *args;
struct res {
n_long port;
} *res;
struct {
n_long h[RPC_HEADER_WORDS];
struct args d;
} sdata;
struct {
n_long h[RPC_HEADER_WORDS];
struct res d;
n_long pad;
} rdata;
ssize_t cc;
int port;