/*-
* Copyright (c) 1995 Gordon Ross, Adam Glass
* 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.
*
* partially based on:
* libnetboot/rpc.c
* @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL)
*/
/*
* What is the longest we will wait before re-sending a request?
* Note this is also the frequency of "RPC timeout" messages.
* The re-send loop count sup linearly to this maximum, so the
* first complaint will happen after (1+2+3+4+5)=15 seconds.
*/
#define MAX_RESEND_DELAY 5 /* seconds */
/*
* Call portmap to lookup a port number for a particular rpc program
* Returns non-zero error on failure.
*/
int
krpc_portmap(struct sockaddr_in *sin, u_int prog, u_int vers, u_int16_t *portp,
struct thread *td)
{
struct sdata {
u_int32_t prog; /* call program */
u_int32_t vers; /* call version */
u_int32_t proto; /* call protocol */
u_int32_t port; /* call port (unused) */
} *sdata;
struct rdata {
u_int16_t pad;
u_int16_t port;
} *rdata;
struct mbuf *m;
int error;
/* The portmapper port is fixed. */
if (prog == PMAPPROG) {
*portp = htons(PMAPPORT);
return 0;
}
/* Do the RPC to get it. */
sdata->prog = txdr_unsigned(prog);
sdata->vers = txdr_unsigned(vers);
sdata->proto = txdr_unsigned(IPPROTO_UDP);
sdata->port = 0;
/*
* Send it, repeatedly, until a reply is received,
* but delay each re-send by an increasing amount.
* If the delay hits the maximum, start complaining.
*/
timo = 0;
for (;;) {
/* Send RPC request (or re-send). */
m = m_copym(mhead, 0, M_COPYALL, M_WAITOK);
error = sosend(so, (struct sockaddr *)sa, NULL, m,
NULL, 0, td);
if (error) {
printf("krpc_call: sosend: %d\n", error);
goto out;
}
m = NULL;
/* Determine new timeout. */
if (timo < MAX_RESEND_DELAY)
timo++;
else {
saddr = ntohl(sa->sin_addr.s_addr);
printf("RPC timeout for server %d.%d.%d.%d\n",
(saddr >> 24) & 255,
(saddr >> 16) & 255,
(saddr >> 8) & 255,
saddr & 255);
}
/*
* Wait for up to timo seconds for a reply.
* The socket receive timeout was set to 1 second.
*/
secs = timo;
while (secs > 0) {
if (from) {
free(from, M_SONAME);
from = NULL;
}
m_freem(m);
m = NULL;
bzero(&auio, sizeof(auio));
auio.uio_resid = len = 1<<16;
rcvflg = 0;
error = soreceive(so, &from, &auio, &m, NULL, &rcvflg);
if (error == EWOULDBLOCK) {
secs--;
continue;
}
if (error)
goto out;
len -= auio.uio_resid;
/* Does the reply contain at least a header? */
if (len < MIN_REPLY_HDR)
continue;
if (m->m_len < MIN_REPLY_HDR)
continue;
reply = mtod(m, struct krpc_reply *);
/* Is it the right reply? */
if (reply->rp_direction != txdr_unsigned(REPLY))
continue;
if (reply->rp_xid != txdr_unsigned(xid))
continue;
/* Did the call succeed? */
if (reply->rp_status != 0) {
error = fxdr_unsigned(u_int32_t, reply->rp_status);
if (error == PROG_MISMATCH) {
error = EBADRPC;
goto out;
}
printf("rpc denied, status=%d\n", error);
continue;
}
goto gotreply; /* break two levels */
} /* while secs */
} /* forever send/receive */
error = ETIMEDOUT;
goto out;
gotreply:
/*
* Get RPC reply header into first mbuf,
* get its length, then strip it off.
*/
len = sizeof(*reply);
if (m->m_len < len) {
m = m_pullup(m, len);
if (m == NULL) {
error = ENOBUFS;
goto out;
}
}
reply = mtod(m, struct krpc_reply *);
if (reply->rp_auth.authtype != 0) {
len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
len = (len + 3) & ~3; /* XXX? */
}
m_adj(m, len);
/* result */
*data = m;
if (from_p) {
*from_p = from;
from = NULL;
}
out:
m_freem(mhead);
if (from) free(from, M_SONAME);
soclose(so);
return error;
}
/*
* eXternal Data Representation routines.
* (but with non-standard args...)
*/
/*
* String representation for RPC.
*/
struct xdr_string {
u_int32_t len; /* length without null or padding */
char data[4]; /* data (longer, of course) */
/* data is padded to a long-word boundary */
};
struct mbuf *
xdr_string_encode(char *str, int len)
{
struct mbuf *m;
struct xdr_string *xs;
int dlen; /* padded string length */
int mlen; /* message length */
dlen = (len + 3) & ~3;
mlen = dlen + 4;
if (mlen > MCLBYTES) /* If too big, we just can't do it. */
return (NULL);