16751Swpaul/*
26751Swpaul
38857SrgrimesThis code is not copyright, and is placed in the public domain. Feel free to
46751Swpauluse and modify. Please send modifications and/or suggestions + bug fixes to
56751Swpaul
66751Swpaul        Klas Heggemann <klas@nada.kth.se>
76751Swpaul
86751Swpaul*/
96751Swpaul
1029102Scharnier#ifndef lint
1129102Scharnierstatic const char rcsid[] =
1250479Speter  "$FreeBSD$";
1329102Scharnier#endif /* not lint */
146751Swpaul
156751Swpaul#include "bootparam_prot.h"
166751Swpaul#include <rpc/rpc.h>
176751Swpaul#include <sys/types.h>
186751Swpaul#include <sys/socket.h>
1936917Speter#include <netinet/in.h>
2036917Speter#include <arpa/inet.h>
2129102Scharnier#include <err.h>
226751Swpaul#include <netdb.h>
23116598Sjmg#include <stdlib.h>
246751Swpaul
256751Swpaul
266751Swpaul/* #define bp_address_u bp_address */
276751Swpaul#include <stdio.h>
2829102Scharnier#include <string.h>
296751Swpaul
306751Swpaulint broadcast;
316751Swpaul
326751Swpaulchar cln[MAX_MACHINE_NAME+1];
336751Swpaulchar dmn[MAX_MACHINE_NAME+1];
346751Swpaulchar path[MAX_PATH_LEN+1];
356751Swpaulextern char *inet_ntoa();
36173412Skevlostatic void usage(void);
37173412Skevloint printgetfile(bp_getfile_res *);
38173412Skevloint printwhoami(bp_whoami_res *);
396751Swpaul
40116598Sjmgbool_t
416751Swpauleachres_whoami(resultp, raddr)
426751Swpaulbp_whoami_res *resultp;
436751Swpaulstruct sockaddr_in *raddr;
446751Swpaul{
456751Swpaul  struct hostent *he;
466751Swpaul
476751Swpaul  he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
486751Swpaul  printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
496751Swpaul  printwhoami(resultp);
506751Swpaul  printf("\n");
516751Swpaul  return(0);
526751Swpaul}
536751Swpaul
54116598Sjmgbool_t
556751Swpauleachres_getfile(resultp, raddr)
566751Swpaulbp_getfile_res *resultp;
576751Swpaulstruct sockaddr_in *raddr;
586751Swpaul{
596751Swpaul  struct hostent *he;
606751Swpaul
616751Swpaul  he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
626751Swpaul  printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
636751Swpaul  printgetfile(resultp);
646751Swpaul  printf("\n");
656751Swpaul  return(0);
666751Swpaul}
676751Swpaul
686751Swpaul
6929102Scharnierint
706751Swpaulmain(argc, argv)
716751Swpaulint argc;
726751Swpaulchar **argv;
736751Swpaul{
746751Swpaul  char *server;
758857Srgrimes
766751Swpaul  bp_whoami_arg whoami_arg;
776751Swpaul  bp_whoami_res *whoami_res, stat_whoami_res;
786751Swpaul  bp_getfile_arg getfile_arg;
796751Swpaul  bp_getfile_res *getfile_res, stat_getfile_res;
806751Swpaul
818857Srgrimes
826751Swpaul  long the_inet_addr;
836751Swpaul  CLIENT *clnt;
846751Swpaul
856751Swpaul  stat_whoami_res.client_name = cln;
866751Swpaul  stat_whoami_res.domain_name = dmn;
876751Swpaul
886751Swpaul  stat_getfile_res.server_name = cln;
896751Swpaul  stat_getfile_res.server_path = path;
908857Srgrimes
9129102Scharnier  if (argc < 3)
9229102Scharnier    usage();
936751Swpaul
946751Swpaul  server = argv[1];
956751Swpaul  if ( ! strcmp(server , "all") ) broadcast = 1;
968857Srgrimes
976751Swpaul  if ( ! broadcast ) {
986751Swpaul    clnt = clnt_create(server,BOOTPARAMPROG, BOOTPARAMVERS, "udp");
99116598Sjmg    if ( clnt == NULL )
100116598Sjmg      errx(1, "could not contact bootparam server on host %s", server);
1018857Srgrimes  }
1026751Swpaul
1036751Swpaul  switch (argc) {
1046751Swpaul  case 3:
1056751Swpaul    whoami_arg.client_address.address_type = IP_ADDR_TYPE;
1066751Swpaul    the_inet_addr = inet_addr(argv[2]);
10729102Scharnier    if ( the_inet_addr == -1)
10829102Scharnier      errx(2, "bogus addr %s", argv[2]);
1096751Swpaul    bcopy(&the_inet_addr,&whoami_arg.client_address.bp_address_u.ip_addr,4);
1106751Swpaul
1116751Swpaul    if (! broadcast ) {
1126751Swpaul      whoami_res = bootparamproc_whoami_1(&whoami_arg, clnt);
1136751Swpaul      printf("Whoami returning:\n");
1146751Swpaul      if (printwhoami(whoami_res)) {
11529102Scharnier	errx(1, "bad answer returned from server %s", server);
1166751Swpaul      } else
1176751Swpaul	exit(0);
1186751Swpaul     } else {
119230361Seadler       (void)clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
1206751Swpaul			       BOOTPARAMPROC_WHOAMI,
121116598Sjmg			       (xdrproc_t)xdr_bp_whoami_arg,
122116598Sjmg			       (char *)&whoami_arg,
123116598Sjmg			       (xdrproc_t)xdr_bp_whoami_res,
124116598Sjmg			       (char *)&stat_whoami_res,
12574462Salfred			       (resultproc_t)eachres_whoami);
1266751Swpaul       exit(0);
1276751Swpaul     }
1286751Swpaul
1296751Swpaul  case 4:
1306751Swpaul
1316751Swpaul    getfile_arg.client_name = argv[2];
1326751Swpaul    getfile_arg.file_id = argv[3];
1338857Srgrimes
1346751Swpaul    if (! broadcast ) {
1356751Swpaul      getfile_res = bootparamproc_getfile_1(&getfile_arg,clnt);
1366751Swpaul      printf("getfile returning:\n");
1376751Swpaul      if (printgetfile(getfile_res)) {
13829102Scharnier	errx(1, "bad answer returned from server %s", server);
1396751Swpaul      } else
1408857Srgrimes	exit(0);
1416751Swpaul    } else {
142230361Seadler      (void)clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
1436751Swpaul			       BOOTPARAMPROC_GETFILE,
144116598Sjmg			       (xdrproc_t)xdr_bp_getfile_arg,
145116598Sjmg			       (char *)&getfile_arg,
146116598Sjmg			       (xdrproc_t)xdr_bp_getfile_res,
147116598Sjmg			       (char *)&stat_getfile_res,
14874462Salfred			       (resultproc_t)eachres_getfile);
1496751Swpaul      exit(0);
1506751Swpaul    }
1518857Srgrimes
1526751Swpaul  default:
1538857Srgrimes
15429102Scharnier    usage();
1556751Swpaul  }
1568857Srgrimes
1576751Swpaul}
1586751Swpaul
1596751Swpaul
16029102Scharnierstatic void
16129102Scharnierusage()
16229102Scharnier{
16329102Scharnier	fprintf(stderr,
16429102Scharnier		"usage: callbootd server procnum (IP-addr | host fileid)\n");
16529102Scharnier    exit(1);
16629102Scharnier}
1678857Srgrimes
16829102Scharnierint
16929102Scharnierprintwhoami(res)
1706751Swpaulbp_whoami_res *res;
1716751Swpaul{
1726751Swpaul      if ( res) {
1736751Swpaul	printf("client_name:\t%s\ndomain_name:\t%s\n",
1746751Swpaul	     res->client_name, res->domain_name);
1756751Swpaul	printf("router:\t%d.%d.%d.%d\n",
1766751Swpaul	     255 &  res->router_address.bp_address_u.ip_addr.net,
1776751Swpaul	     255 & res->router_address.bp_address_u.ip_addr.host,
1786751Swpaul	     255 &  res->router_address.bp_address_u.ip_addr.lh,
1796751Swpaul	     255 & res->router_address.bp_address_u.ip_addr.impno);
1806751Swpaul	return(0);
1816751Swpaul      } else {
18229102Scharnier	warnx("null answer!!!");
1836751Swpaul	return(1);
1846751Swpaul      }
1856751Swpaul    }
1866751Swpaul
1876751Swpaul
1886751Swpaul
1898857Srgrimes
1906751Swpaulint
1916751Swpaulprintgetfile(res)
1926751Swpaulbp_getfile_res *res;
1936751Swpaul{
1946751Swpaul      if (res) {
1956751Swpaul	printf("server_name:\t%s\nserver_address:\t%s\npath:\t%s\n",
1968857Srgrimes	       res->server_name,
19736917Speter	       inet_ntoa(*(struct in_addr *)&res->server_address.bp_address_u.ip_addr),
1986751Swpaul	       res->server_path);
1996751Swpaul	return(0);
2006751Swpaul      } else {
20129102Scharnier	warnx("null answer!!!");
2026751Swpaul	return(1);
2036751Swpaul      }
2046751Swpaul    }
205