199767f8919635c4928607450d9e0abb932109ceToomas Soome/* $NetBSD: bootparam.c,v 1.11 1997/06/26 19:11:32 drochner Exp $ */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1995 Gordon W. Ross
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
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 * 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 * 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 * RPC definitions for bootparamd
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Inet address in RPC messages
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (Note, really four ints, NOT chars. Blech.)
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint xdr_inaddr_encode(char **p, struct in_addr ia);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint xdr_inaddr_decode(char **p, struct in_addr *ia);
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 * Given client IP address, get:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * client name (hostname)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * domain name (domainname)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * gateway address
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The hostname and domainname are set here for convenience.
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 /* RPC structures for PMAPPROC_CALLIT */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* encapsulated data here */
199767f8919635c4928607450d9e0abb932109ceToomas Soome RPC_PRINTF(("bp_whoami: myip=%s\n", inet_ntoa(myip)));
199767f8919635c4928607450d9e0abb932109ceToomas Soome RPC_PRINTF(("bp_whoami: bad socket. %d\n", sockfd));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Build request args for PMAPPROC_CALLIT.
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->arglen = htonl(sizeof(struct xdr_inaddr));
199767f8919635c4928607450d9e0abb932109ceToomas Soome * append encapsulated data (client IP address)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->destip.s_addr = INADDR_BROADCAST; /* XXX: subnet bcast? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* rpc_call will set d->destport */
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_CALLIT,
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Save bootparam server address (from IP header). */
199767f8919635c4928607450d9e0abb932109ceToomas Soome rpc_fromaddr(repl, &bp_server_addr, &bp_server_port);
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 inet_ntoa(bp_server_addr), ntohs(bp_server_port)));
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* We have just done a portmap call, so cache the portnum. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Parse the encapsulated results from bootparam/whoami
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("bp_whoami: short reply, %d < %d\n", len, x);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* client name */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (xdr_string_decode(&recv_head, hostname, &hostnamelen)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* domain name */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (xdr_string_decode(&recv_head, domainname, &domainnamelen)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* gateway address */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* success */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Given client name and file "key", get:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * server name
199767f8919635c4928607450d9e0abb932109ceToomas Soome * server IP address
199767f8919635c4928607450d9e0abb932109ceToomas Soome * server pathname
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* misc... */
199767f8919635c4928607450d9e0abb932109ceToomas Soome RPC_PRINTF(("bp_getfile: bad socket. %d\n", sockfd));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Build request message.
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* client name (hostname) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (xdr_string_encode(&send_tail, hostname, hostnamelen)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* key name (root or swap) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (xdr_string_encode(&send_tail, key, strlen(key))) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* rpc_call will set d->destport */
199767f8919635c4928607450d9e0abb932109ceToomas Soome BOOTPARAM_PROG, BOOTPARAM_VERS, BOOTPARAM_GETFILE,
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Parse result message.
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* server name */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (xdr_string_decode(&recv_head, serv_name, &sn_len)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* server IP address (mountd/NFS) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (xdr_inaddr_decode(&recv_head, serv_addr)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* server pathname */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (xdr_string_decode(&recv_head, pathname, &path_len)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* success */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * eXternal Data Representation routines.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (but with non-standard args...)
199767f8919635c4928607450d9e0abb932109ceToomas Soome int padlen = (len + 3) & ~3; /* padded length */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The data will be int aligned. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The data will be int aligned. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The data will be int aligned. */
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 /* The data will be int aligned. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome RPC_PRINTF(("xdr_inaddr_decode: bad addrtype=%d\n",
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.