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  enum clnt_stat clnt_stat;
856751Swpaul
866751Swpaul  stat_whoami_res.client_name = cln;
876751Swpaul  stat_whoami_res.domain_name = dmn;
886751Swpaul
896751Swpaul  stat_getfile_res.server_name = cln;
906751Swpaul  stat_getfile_res.server_path = path;
918857Srgrimes
9229102Scharnier  if (argc < 3)
9329102Scharnier    usage();
946751Swpaul
956751Swpaul  server = argv[1];
966751Swpaul  if ( ! strcmp(server , "all") ) broadcast = 1;
978857Srgrimes
986751Swpaul  if ( ! broadcast ) {
996751Swpaul    clnt = clnt_create(server,BOOTPARAMPROG, BOOTPARAMVERS, "udp");
100116598Sjmg    if ( clnt == NULL )
101116598Sjmg      errx(1, "could not contact bootparam server on host %s", server);
1028857Srgrimes  }
1036751Swpaul
1046751Swpaul  switch (argc) {
1056751Swpaul  case 3:
1066751Swpaul    whoami_arg.client_address.address_type = IP_ADDR_TYPE;
1076751Swpaul    the_inet_addr = inet_addr(argv[2]);
10829102Scharnier    if ( the_inet_addr == -1)
10929102Scharnier      errx(2, "bogus addr %s", argv[2]);
1106751Swpaul    bcopy(&the_inet_addr,&whoami_arg.client_address.bp_address_u.ip_addr,4);
1116751Swpaul
1126751Swpaul    if (! broadcast ) {
1136751Swpaul      whoami_res = bootparamproc_whoami_1(&whoami_arg, clnt);
1146751Swpaul      printf("Whoami returning:\n");
1156751Swpaul      if (printwhoami(whoami_res)) {
11629102Scharnier	errx(1, "bad answer returned from server %s", server);
1176751Swpaul      } else
1186751Swpaul	exit(0);
1196751Swpaul     } else {
1208857Srgrimes       clnt_stat=clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
1216751Swpaul			       BOOTPARAMPROC_WHOAMI,
122116598Sjmg			       (xdrproc_t)xdr_bp_whoami_arg,
123116598Sjmg			       (char *)&whoami_arg,
124116598Sjmg			       (xdrproc_t)xdr_bp_whoami_res,
125116598Sjmg			       (char *)&stat_whoami_res,
12674462Salfred			       (resultproc_t)eachres_whoami);
1276751Swpaul       exit(0);
1286751Swpaul     }
1296751Swpaul
1306751Swpaul  case 4:
1316751Swpaul
1326751Swpaul    getfile_arg.client_name = argv[2];
1336751Swpaul    getfile_arg.file_id = argv[3];
1348857Srgrimes
1356751Swpaul    if (! broadcast ) {
1366751Swpaul      getfile_res = bootparamproc_getfile_1(&getfile_arg,clnt);
1376751Swpaul      printf("getfile returning:\n");
1386751Swpaul      if (printgetfile(getfile_res)) {
13929102Scharnier	errx(1, "bad answer returned from server %s", server);
1406751Swpaul      } else
1418857Srgrimes	exit(0);
1426751Swpaul    } else {
1438857Srgrimes      clnt_stat=clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
1446751Swpaul			       BOOTPARAMPROC_GETFILE,
145116598Sjmg			       (xdrproc_t)xdr_bp_getfile_arg,
146116598Sjmg			       (char *)&getfile_arg,
147116598Sjmg			       (xdrproc_t)xdr_bp_getfile_res,
148116598Sjmg			       (char *)&stat_getfile_res,
14974462Salfred			       (resultproc_t)eachres_getfile);
1506751Swpaul      exit(0);
1516751Swpaul    }
1528857Srgrimes
1536751Swpaul  default:
1548857Srgrimes
15529102Scharnier    usage();
1566751Swpaul  }
1578857Srgrimes
1586751Swpaul}
1596751Swpaul
1606751Swpaul
16129102Scharnierstatic void
16229102Scharnierusage()
16329102Scharnier{
16429102Scharnier	fprintf(stderr,
16529102Scharnier		"usage: callbootd server procnum (IP-addr | host fileid)\n");
16629102Scharnier    exit(1);
16729102Scharnier}
1688857Srgrimes
16929102Scharnierint
17029102Scharnierprintwhoami(res)
1716751Swpaulbp_whoami_res *res;
1726751Swpaul{
1736751Swpaul      if ( res) {
1746751Swpaul	printf("client_name:\t%s\ndomain_name:\t%s\n",
1756751Swpaul	     res->client_name, res->domain_name);
1766751Swpaul	printf("router:\t%d.%d.%d.%d\n",
1776751Swpaul	     255 &  res->router_address.bp_address_u.ip_addr.net,
1786751Swpaul	     255 & res->router_address.bp_address_u.ip_addr.host,
1796751Swpaul	     255 &  res->router_address.bp_address_u.ip_addr.lh,
1806751Swpaul	     255 & res->router_address.bp_address_u.ip_addr.impno);
1816751Swpaul	return(0);
1826751Swpaul      } else {
18329102Scharnier	warnx("null answer!!!");
1846751Swpaul	return(1);
1856751Swpaul      }
1866751Swpaul    }
1876751Swpaul
1886751Swpaul
1896751Swpaul
1908857Srgrimes
1916751Swpaulint
1926751Swpaulprintgetfile(res)
1936751Swpaulbp_getfile_res *res;
1946751Swpaul{
1956751Swpaul      if (res) {
1966751Swpaul	printf("server_name:\t%s\nserver_address:\t%s\npath:\t%s\n",
1978857Srgrimes	       res->server_name,
19836917Speter	       inet_ntoa(*(struct in_addr *)&res->server_address.bp_address_u.ip_addr),
1996751Swpaul	       res->server_path);
2006751Swpaul	return(0);
2016751Swpaul      } else {
20229102Scharnier	warnx("null answer!!!");
2036751Swpaul	return(1);
2046751Swpaul      }
2056751Swpaul    }
206