#include <sys/types.h>
#include <rpc/types.h>
#include <sys/errno.h>
#include <rpc/auth.h>
#include <rpc/xdr.h>
#include <rpc/rpc_msg.h>
#include <sys/t_lock.h>
#include "clnt.h"
#include <rpc/rpc.h>
#include <sys/utsname.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>
#include <sys/promif.h>
#include <rpcsvc/bootparam.h>
#include "pmap.h"
#include "brpc.h"
#include "socket_inet.h"
#include "ipv4.h"
#include <sys/salib.h>
#include <sys/bootdebug.h>
extern int errno;
static struct bp_whoami_res bp;
static char bp_hostname[SYS_NMLN+1];
static char bp_domainname[SYS_NMLN+1];
static struct in_addr responder;
static const char *noserver =
"No bootparam (%s) server responding; still trying...\n";
#define GETFILE_BTIMEO 1
#define GETFILE_BRETRIES 2
#define dprintf if (boothowto & RB_DEBUG) printf
bool_t
whoami(void)
{
struct bp_whoami_arg arg;
struct sockaddr_in to, from;
struct in_addr ipaddr;
enum clnt_stat stat;
bool_t retval = TRUE;
int rexmit;
int resp_wait;
int namelen;
int printed_waiting_msg;
to.sin_family = AF_INET;
to.sin_addr.s_addr = htonl(INADDR_BROADCAST);
to.sin_port = htons(0);
arg.client_address.address_type = IP_ADDR_TYPE;
ipv4_getipaddr(&ipaddr);
ipaddr.s_addr = htonl(ipaddr.s_addr);
bcopy((caddr_t)&ipaddr,
(caddr_t)&arg.client_address.bp_address_u.ip_addr,
sizeof (ipaddr));
rexmit = 0;
resp_wait = 16;
bp.client_name = &bp_hostname[0];
bp.domain_name = &bp_domainname[0];
printed_waiting_msg = 0;
do {
stat = bpmap_rmtcall((rpcprog_t)BOOTPARAMPROG,
(rpcvers_t)BOOTPARAMVERS, (rpcproc_t)BOOTPARAMPROC_WHOAMI,
xdr_bp_whoami_arg, (caddr_t)&arg,
xdr_bp_whoami_res, (caddr_t)&bp, rexmit, resp_wait,
&to, &from, AUTH_NONE);
if (stat == RPC_TIMEDOUT && !printed_waiting_msg) {
dprintf(noserver, "whoami");
printed_waiting_msg = 1;
}
rexmit = resp_wait;
resp_wait = 0;
} while (stat == RPC_TIMEDOUT);
if (stat != RPC_SUCCESS) {
dprintf("whoami RPC call failed with rpc status: %d\n", stat);
retval = FALSE;
goto done;
} else {
if (printed_waiting_msg && (boothowto & RB_VERBOSE))
printf("Bootparam response received\n");
responder.s_addr = from.sin_addr.s_addr;
}
namelen = strlen(bp.client_name);
if (namelen > SYS_NMLN) {
dprintf("whoami: hostname too long");
retval = FALSE;
goto done;
}
if (namelen > 0) {
if (boothowto & RB_VERBOSE)
printf("hostname: %s\n", bp.client_name);
(void) sethostname(bp.client_name, namelen);
} else {
dprintf("whoami: no host name\n");
retval = FALSE;
goto done;
}
namelen = strlen(bp.domain_name);
if (namelen > SYS_NMLN) {
dprintf("whoami: domainname too long");
retval = FALSE;
goto done;
}
if (namelen > 0)
if (boothowto & RB_VERBOSE)
printf("domainname: %s\n", bp.domain_name);
else
dprintf("whoami: no domain name\n");
if (bp.router_address.address_type == IP_ADDR_TYPE) {
bcopy((caddr_t)&bp.router_address.bp_address_u.ip_addr,
(caddr_t)&ipaddr, sizeof (ipaddr));
if (ntohl(ipaddr.s_addr) != INADDR_ANY) {
dprintf("whoami: Router ip is: %s\n",
inet_ntoa(ipaddr));
(void) ipv4_route(IPV4_ADD_ROUTE, RT_DEFAULT, NULL,
&ipaddr);
}
} else
dprintf("whoami: unknown gateway addr family %d\n",
bp.router_address.address_type);
done:
return (retval);
}
bool_t
getfile(char *fileid, char *server_name, struct in_addr *server_ip,
char *server_path)
{
struct bp_getfile_arg arg;
struct bp_getfile_res res;
enum clnt_stat stat;
struct sockaddr_in to, from;
int rexmit;
int wait;
uint_t max_retries = 0xFFFFFFFF;
int def_rexmit = 0;
int def_wait = 32;
int printed_waiting_msg;
if (strcmp(fileid, "root") != 0) {
def_wait = GETFILE_BTIMEO;
def_rexmit = GETFILE_BTIMEO;
max_retries = GETFILE_BRETRIES;
}
arg.client_name = bp.client_name;
arg.file_id = fileid;
res.server_name = (bp_machine_name_t)bkmem_zalloc(SYS_NMLN + 1);
res.server_path = (bp_path_t)bkmem_zalloc(SYS_NMLN + 1);
if (res.server_name == NULL || res.server_path == NULL) {
dprintf("getfile: rpc_call failed: No memory\n");
errno = ENOMEM;
if (res.server_name != NULL)
bkmem_free(res.server_name, SYS_NMLN + 1);
if (res.server_path != NULL)
bkmem_free(res.server_path, SYS_NMLN + 1);
return (FALSE);
}
to.sin_family = AF_INET;
to.sin_addr.s_addr = responder.s_addr;
to.sin_port = htons(0);
rexmit = def_rexmit;
wait = def_wait;
stat = brpc_call((rpcprog_t)BOOTPARAMPROG, (rpcvers_t)BOOTPARAMVERS,
(rpcproc_t)BOOTPARAMPROC_GETFILE, xdr_bp_getfile_arg, (caddr_t)&arg,
xdr_bp_getfile_res, (caddr_t)&res, rexmit, wait,
&to, &from, AUTH_NONE);
if (stat == RPC_TIMEDOUT) {
to.sin_addr.s_addr = htonl(INADDR_BROADCAST);
to.sin_port = htons(0);
rexmit = def_rexmit;
wait = def_wait;
printed_waiting_msg = 0;
do {
if (max_retries-- == 0)
break;
stat = bpmap_rmtcall((rpcprog_t)BOOTPARAMPROG,
(rpcvers_t)BOOTPARAMVERS,
(rpcproc_t)BOOTPARAMPROC_GETFILE,
xdr_bp_getfile_arg, (caddr_t)&arg,
xdr_bp_getfile_res, (caddr_t)&res, rexmit,
wait, &to, &from, AUTH_NONE);
if (stat == RPC_SUCCESS) {
responder.s_addr = from.sin_addr.s_addr;
if (printed_waiting_msg &&
(boothowto & RB_VERBOSE)) {
printf(
"Bootparam response received.\n");
}
break;
}
if (stat == RPC_TIMEDOUT && !printed_waiting_msg) {
dprintf(noserver, "getfile");
printed_waiting_msg = 1;
}
rexmit = wait;
wait = def_wait;
} while (stat == RPC_TIMEDOUT);
}
if (stat == RPC_SUCCESS) {
bcopy(res.server_name, server_name, strlen(res.server_name));
bcopy(res.server_path, server_path, strlen(res.server_path));
switch (res.server_address.address_type) {
case IP_ADDR_TYPE:
bcopy((caddr_t)&res.server_address.bp_address_u.ip_addr,
(caddr_t)server_ip, sizeof (struct in_addr));
break;
default:
dprintf("getfile: unknown address type %d\n",
res.server_address.address_type);
server_ip->s_addr = htonl(INADDR_ANY);
bkmem_free(res.server_name, SYS_NMLN + 1);
bkmem_free(res.server_path, SYS_NMLN + 1);
return (FALSE);
}
} else {
dprintf("getfile: rpc_call failed.\n");
bkmem_free(res.server_name, SYS_NMLN + 1);
bkmem_free(res.server_path, SYS_NMLN + 1);
return (FALSE);
}
bkmem_free(res.server_name, SYS_NMLN + 1);
bkmem_free(res.server_path, SYS_NMLN + 1);
return (TRUE);
}