199767f8919635c4928607450d9e0abb932109ceToomas Soome/* $NetBSD: bootparam.c,v 1.11 1997/06/26 19:11:32 drochner Exp $ */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1995 Gordon W. Ross
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 3. The name of the author may not be used to endorse or promote products
199767f8919635c4928607450d9e0abb932109ceToomas Soome * derived from this software without specific prior written permission.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 4. All advertising materials mentioning features or use of this software
199767f8919635c4928607450d9e0abb932109ceToomas Soome * must display the following acknowledgement:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This product includes software developed by Gordon W. Ross
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * RPC/bootparams
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/param.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/socket.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <net/if.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netinet/in.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netinet/in_systm.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "rpcv2.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "stand.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "net.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "netif.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "rpc.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "bootparam.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DEBUG_RPC
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define RPC_PRINTF(a) printf a
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define RPC_PRINTF(a)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct in_addr bp_server_addr; /* net order */
199767f8919635c4928607450d9e0abb932109ceToomas Soomen_short bp_server_port; /* net order */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * RPC definitions for bootparamd
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define BOOTPARAM_PROG 100026
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define BOOTPARAM_VERS 1
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define BOOTPARAM_WHOAMI 1
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define BOOTPARAM_GETFILE 2
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Inet address in RPC messages
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (Note, really four ints, NOT chars. Blech.)
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct xdr_inaddr {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t atype;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int32_t addr[4];
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint xdr_inaddr_encode(char **p, struct in_addr ia);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint xdr_inaddr_decode(char **p, struct in_addr *ia);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint xdr_string_encode(char **p, char *str, int len);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint xdr_string_decode(char **p, char *str, int *len_p);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * RPC: bootparam/whoami
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Given client IP address, get:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * client name (hostname)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * domain name (domainname)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * gateway address
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The hostname and domainname are set here for convenience.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note - bpsin is initialized to the broadcast address,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and will be replaced with the bootparam server address
199767f8919635c4928607450d9e0abb932109ceToomas Soome * after this call is complete. Have to use PMAP_PROC_CALL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to make sure we get responses only from a servers that
199767f8919635c4928607450d9e0abb932109ceToomas Soome * know about us (don't want to broadcast a getport call).
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomebp_whoami(sockfd)
199767f8919635c4928607450d9e0abb932109ceToomas Soome int sockfd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* RPC structures for PMAPPROC_CALLIT */
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct args {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t prog;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t vers;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t proc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t arglen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct xdr_inaddr xina;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } *args;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct repl {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t _pad;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t port;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t encap_len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* encapsulated data here */
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long capsule[64];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } *repl;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct args d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } sdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct repl d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } rdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *send_tail, *recv_head;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct iodesc *d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len, x;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome RPC_PRINTF(("bp_whoami: myip=%s\n", inet_ntoa(myip)));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!(d = socktodesc(sockfd))) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome RPC_PRINTF(("bp_whoami: bad socket. %d\n", sockfd));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome args = &sdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome repl = &rdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Build request args for PMAPPROC_CALLIT.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->prog = htonl(BOOTPARAM_PROG);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->vers = htonl(BOOTPARAM_VERS);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->proc = htonl(BOOTPARAM_WHOAMI);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->arglen = htonl(sizeof(struct xdr_inaddr));
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_tail = (char*) &args->xina;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * append encapsulated data (client IP address)
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (xdr_inaddr_encode(&send_tail, myip))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* RPC: portmap/callit */
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->myport = htons(--rpc_port);
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->destip.s_addr = INADDR_BROADCAST; /* XXX: subnet bcast? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* rpc_call will set d->destport */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_CALLIT,
199767f8919635c4928607450d9e0abb932109ceToomas Soome args, send_tail - (char*)args,
199767f8919635c4928607450d9e0abb932109ceToomas Soome repl, sizeof(*repl));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len < 8) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("bootparamd: 'whoami' call failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Save bootparam server address (from IP header). */
199767f8919635c4928607450d9e0abb932109ceToomas Soome rpc_fromaddr(repl, &bp_server_addr, &bp_server_port);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note that bp_server_port is now 111 due to the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * indirect call (using PMAPPROC_CALLIT), so get the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * actual port number from the reply data.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome bp_server_port = repl->port;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome RPC_PRINTF(("bp_whoami: server at %s:%d\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome inet_ntoa(bp_server_addr), ntohs(bp_server_port)));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* We have just done a portmap call, so cache the portnum. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome rpc_pmap_putcache(bp_server_addr,
199767f8919635c4928607450d9e0abb932109ceToomas Soome BOOTPARAM_PROG,
199767f8919635c4928607450d9e0abb932109ceToomas Soome BOOTPARAM_VERS,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (int)ntohs(bp_server_port));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Parse the encapsulated results from bootparam/whoami
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome x = ntohl(repl->encap_len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len < x) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("bp_whoami: short reply, %d < %d\n", len, x);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome recv_head = (char*) repl->capsule;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* client name */
199767f8919635c4928607450d9e0abb932109ceToomas Soome hostnamelen = MAXHOSTNAMELEN-1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (xdr_string_decode(&recv_head, hostname, &hostnamelen)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome RPC_PRINTF(("bp_whoami: bad hostname\n"));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* domain name */
199767f8919635c4928607450d9e0abb932109ceToomas Soome domainnamelen = MAXHOSTNAMELEN-1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (xdr_string_decode(&recv_head, domainname, &domainnamelen)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome RPC_PRINTF(("bp_whoami: bad domainname\n"));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* gateway address */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (xdr_inaddr_decode(&recv_head, &gateip)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome RPC_PRINTF(("bp_whoami: bad gateway\n"));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* success */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * RPC: bootparam/getfile
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Given client name and file "key", get:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * server name
199767f8919635c4928607450d9e0abb932109ceToomas Soome * server IP address
199767f8919635c4928607450d9e0abb932109ceToomas Soome * server pathname
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomebp_getfile(sockfd, key, serv_addr, pathname)
199767f8919635c4928607450d9e0abb932109ceToomas Soome int sockfd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *key;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *pathname;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct in_addr *serv_addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long d[64];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } sdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long d[128];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } rdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char serv_name[FNAME_SIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *send_tail, *recv_head;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* misc... */
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct iodesc *d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int sn_len, path_len, rlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!(d = socktodesc(sockfd))) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome RPC_PRINTF(("bp_getfile: bad socket. %d\n", sockfd));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_tail = (char*) sdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome recv_head = (char*) rdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Build request message.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* client name (hostname) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (xdr_string_encode(&send_tail, hostname, hostnamelen)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome RPC_PRINTF(("bp_getfile: bad client\n"));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* key name (root or swap) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (xdr_string_encode(&send_tail, key, strlen(key))) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome RPC_PRINTF(("bp_getfile: bad key\n"));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* RPC: bootparam/getfile */
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->myport = htons(--rpc_port);
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->destip = bp_server_addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* rpc_call will set d->destport */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rlen = rpc_call(d,
199767f8919635c4928607450d9e0abb932109ceToomas Soome BOOTPARAM_PROG, BOOTPARAM_VERS, BOOTPARAM_GETFILE,
199767f8919635c4928607450d9e0abb932109ceToomas Soome sdata.d, send_tail - (char*)sdata.d,
199767f8919635c4928607450d9e0abb932109ceToomas Soome rdata.d, sizeof(rdata.d));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rlen < 4) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome RPC_PRINTF(("bp_getfile: short reply\n"));
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = EBADRPC;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome recv_head = (char*) rdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Parse result message.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* server name */
199767f8919635c4928607450d9e0abb932109ceToomas Soome sn_len = FNAME_SIZE-1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (xdr_string_decode(&recv_head, serv_name, &sn_len)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome RPC_PRINTF(("bp_getfile: bad server name\n"));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* server IP address (mountd/NFS) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (xdr_inaddr_decode(&recv_head, serv_addr)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome RPC_PRINTF(("bp_getfile: bad server addr\n"));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* server pathname */
199767f8919635c4928607450d9e0abb932109ceToomas Soome path_len = MAXPATHLEN-1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (xdr_string_decode(&recv_head, pathname, &path_len)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome RPC_PRINTF(("bp_getfile: bad server path\n"));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* success */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * eXternal Data Representation routines.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (but with non-standard args...)
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomexdr_string_encode(pkt, str, len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome char **pkt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *str;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t *lenp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *datap;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int padlen = (len + 3) & ~3; /* padded length */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The data will be int aligned. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome lenp = (u_int32_t*) *pkt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *pkt += sizeof(*lenp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *lenp = htonl(len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome datap = *pkt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *pkt += padlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(str, datap, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomexdr_string_decode(pkt, str, len_p)
199767f8919635c4928607450d9e0abb932109ceToomas Soome char **pkt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *str;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int *len_p; /* bufsize - 1 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t *lenp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *datap;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int slen; /* string length */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int plen; /* padded length */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The data will be int aligned. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome lenp = (u_int32_t*) *pkt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *pkt += sizeof(*lenp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome slen = ntohl(*lenp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome plen = (slen + 3) & ~3;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (slen > *len_p)
199767f8919635c4928607450d9e0abb932109ceToomas Soome slen = *len_p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome datap = *pkt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *pkt += plen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(datap, str, slen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome str[slen] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome *len_p = slen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomexdr_inaddr_encode(pkt, ia)
199767f8919635c4928607450d9e0abb932109ceToomas Soome char **pkt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct in_addr ia; /* network order */
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct xdr_inaddr *xi;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char *cp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int32_t *ip;
199767f8919635c4928607450d9e0abb932109ceToomas Soome union {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long l; /* network order */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char c[4];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } uia;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The data will be int aligned. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome xi = (struct xdr_inaddr *) *pkt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *pkt += sizeof(*xi);
199767f8919635c4928607450d9e0abb932109ceToomas Soome xi->atype = htonl(1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome uia.l = ia.s_addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = uia.c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ip = xi->addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note: the htonl() calls below DO NOT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * imply that uia.l is in host order.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * In fact this needs it in net order.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome *ip++ = htonl((unsigned int)*cp++);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *ip++ = htonl((unsigned int)*cp++);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *ip++ = htonl((unsigned int)*cp++);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *ip++ = htonl((unsigned int)*cp++);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomexdr_inaddr_decode(pkt, ia)
199767f8919635c4928607450d9e0abb932109ceToomas Soome char **pkt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct in_addr *ia; /* network order */
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct xdr_inaddr *xi;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char *cp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int32_t *ip;
199767f8919635c4928607450d9e0abb932109ceToomas Soome union {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long l; /* network order */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char c[4];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } uia;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The data will be int aligned. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome xi = (struct xdr_inaddr *) *pkt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *pkt += sizeof(*xi);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (xi->atype != htonl(1)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome RPC_PRINTF(("xdr_inaddr_decode: bad addrtype=%d\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome ntohl(xi->atype)));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = uia.c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ip = xi->addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note: the ntohl() calls below DO NOT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * imply that uia.l is in host order.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * In fact this needs it in net order.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome *cp++ = ntohl(*ip++);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *cp++ = ntohl(*ip++);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *cp++ = ntohl(*ip++);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *cp++ = ntohl(*ip++);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ia->s_addr = uia.l;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}