callbootd.c revision 29102
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[] =
1229102Scharnier	"$Id$";
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>
1929102Scharnier#include <err.h>
206751Swpaul#include <netdb.h>
216751Swpaul
226751Swpaul
236751Swpaul/* #define bp_address_u bp_address */
246751Swpaul#include <stdio.h>
2529102Scharnier#include <string.h>
266751Swpaul
276751Swpaulint broadcast;
286751Swpaul
296751Swpaulchar cln[MAX_MACHINE_NAME+1];
306751Swpaulchar dmn[MAX_MACHINE_NAME+1];
316751Swpaulchar path[MAX_PATH_LEN+1];
326751Swpaulextern char *inet_ntoa();
3329102Scharnierstatic void usage __P((void));
3429102Scharnierint printgetfile __P((bp_getfile_res *));
3529102Scharnierint printwhoami __P((bp_whoami_res *));
366751Swpaul
3729102Scharnierint
386751Swpauleachres_whoami(resultp, raddr)
396751Swpaulbp_whoami_res *resultp;
406751Swpaulstruct sockaddr_in *raddr;
416751Swpaul{
426751Swpaul  struct hostent *he;
436751Swpaul
446751Swpaul  he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
456751Swpaul  printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
466751Swpaul  printwhoami(resultp);
476751Swpaul  printf("\n");
486751Swpaul  return(0);
496751Swpaul}
506751Swpaul
516751Swpauleachres_getfile(resultp, raddr)
526751Swpaulbp_getfile_res *resultp;
536751Swpaulstruct sockaddr_in *raddr;
546751Swpaul{
556751Swpaul  struct hostent *he;
566751Swpaul
576751Swpaul  he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
586751Swpaul  printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
596751Swpaul  printgetfile(resultp);
606751Swpaul  printf("\n");
616751Swpaul  return(0);
626751Swpaul}
636751Swpaul
646751Swpaul
6529102Scharnierint
666751Swpaulmain(argc, argv)
676751Swpaulint argc;
686751Swpaulchar **argv;
696751Swpaul{
706751Swpaul  char *server;
718857Srgrimes
726751Swpaul  bp_whoami_arg whoami_arg;
736751Swpaul  bp_whoami_res *whoami_res, stat_whoami_res;
746751Swpaul  bp_getfile_arg getfile_arg;
756751Swpaul  bp_getfile_res *getfile_res, stat_getfile_res;
766751Swpaul
778857Srgrimes
786751Swpaul  long the_inet_addr;
796751Swpaul  CLIENT *clnt;
806751Swpaul  enum clnt_stat clnt_stat;
816751Swpaul
826751Swpaul  stat_whoami_res.client_name = cln;
836751Swpaul  stat_whoami_res.domain_name = dmn;
846751Swpaul
856751Swpaul  stat_getfile_res.server_name = cln;
866751Swpaul  stat_getfile_res.server_path = path;
878857Srgrimes
8829102Scharnier  if (argc < 3)
8929102Scharnier    usage();
906751Swpaul
916751Swpaul  server = argv[1];
926751Swpaul  if ( ! strcmp(server , "all") ) broadcast = 1;
938857Srgrimes
946751Swpaul  if ( ! broadcast ) {
956751Swpaul    clnt = clnt_create(server,BOOTPARAMPROG, BOOTPARAMVERS, "udp");
968857Srgrimes  }
976751Swpaul
9829102Scharnier  if ( clnt == NULL )
9929102Scharnier     errx(1, "could not contact bootparam server on host %s", server);
1006751Swpaul
1016751Swpaul  switch (argc) {
1026751Swpaul  case 3:
1036751Swpaul    whoami_arg.client_address.address_type = IP_ADDR_TYPE;
1046751Swpaul    the_inet_addr = inet_addr(argv[2]);
10529102Scharnier    if ( the_inet_addr == -1)
10629102Scharnier      errx(2, "bogus addr %s", argv[2]);
1076751Swpaul    bcopy(&the_inet_addr,&whoami_arg.client_address.bp_address_u.ip_addr,4);
1086751Swpaul
1096751Swpaul    if (! broadcast ) {
1106751Swpaul      whoami_res = bootparamproc_whoami_1(&whoami_arg, clnt);
1116751Swpaul      printf("Whoami returning:\n");
1126751Swpaul      if (printwhoami(whoami_res)) {
11329102Scharnier	errx(1, "bad answer returned from server %s", server);
1146751Swpaul      } else
1156751Swpaul	exit(0);
1166751Swpaul     } else {
1178857Srgrimes       clnt_stat=clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
1186751Swpaul			       BOOTPARAMPROC_WHOAMI,
1198857Srgrimes			       xdr_bp_whoami_arg, &whoami_arg,
1206751Swpaul			       xdr_bp_whoami_res, &stat_whoami_res, eachres_whoami);
1216751Swpaul       exit(0);
1226751Swpaul     }
1236751Swpaul
1246751Swpaul  case 4:
1256751Swpaul
1266751Swpaul    getfile_arg.client_name = argv[2];
1276751Swpaul    getfile_arg.file_id = argv[3];
1288857Srgrimes
1296751Swpaul    if (! broadcast ) {
1306751Swpaul      getfile_res = bootparamproc_getfile_1(&getfile_arg,clnt);
1316751Swpaul      printf("getfile returning:\n");
1326751Swpaul      if (printgetfile(getfile_res)) {
13329102Scharnier	errx(1, "bad answer returned from server %s", server);
1346751Swpaul      } else
1358857Srgrimes	exit(0);
1366751Swpaul    } else {
1378857Srgrimes      clnt_stat=clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
1386751Swpaul			       BOOTPARAMPROC_GETFILE,
1398857Srgrimes			       xdr_bp_getfile_arg, &getfile_arg,
1406751Swpaul			       xdr_bp_getfile_res, &stat_getfile_res,eachres_getfile);
1416751Swpaul      exit(0);
1426751Swpaul    }
1438857Srgrimes
1446751Swpaul  default:
1458857Srgrimes
14629102Scharnier    usage();
1476751Swpaul  }
1488857Srgrimes
1496751Swpaul}
1506751Swpaul
1516751Swpaul
15229102Scharnierstatic void
15329102Scharnierusage()
15429102Scharnier{
15529102Scharnier	fprintf(stderr,
15629102Scharnier		"usage: callbootd server procnum (IP-addr | host fileid)\n");
15729102Scharnier    exit(1);
15829102Scharnier}
1598857Srgrimes
16029102Scharnierint
16129102Scharnierprintwhoami(res)
1626751Swpaulbp_whoami_res *res;
1636751Swpaul{
1646751Swpaul      if ( res) {
1656751Swpaul	printf("client_name:\t%s\ndomain_name:\t%s\n",
1666751Swpaul	     res->client_name, res->domain_name);
1676751Swpaul	printf("router:\t%d.%d.%d.%d\n",
1686751Swpaul	     255 &  res->router_address.bp_address_u.ip_addr.net,
1696751Swpaul	     255 & res->router_address.bp_address_u.ip_addr.host,
1706751Swpaul	     255 &  res->router_address.bp_address_u.ip_addr.lh,
1716751Swpaul	     255 & res->router_address.bp_address_u.ip_addr.impno);
1726751Swpaul	return(0);
1736751Swpaul      } else {
17429102Scharnier	warnx("null answer!!!");
1756751Swpaul	return(1);
1766751Swpaul      }
1776751Swpaul    }
1786751Swpaul
1796751Swpaul
1806751Swpaul
1818857Srgrimes
1826751Swpaulint
1836751Swpaulprintgetfile(res)
1846751Swpaulbp_getfile_res *res;
1856751Swpaul{
1866751Swpaul      if (res) {
1876751Swpaul	printf("server_name:\t%s\nserver_address:\t%s\npath:\t%s\n",
1888857Srgrimes	       res->server_name,
1896751Swpaul	       inet_ntoa(res->server_address.bp_address_u.ip_addr),
1906751Swpaul	       res->server_path);
1916751Swpaul	return(0);
1926751Swpaul      } else {
19329102Scharnier	warnx("null answer!!!");
1946751Swpaul	return(1);
1956751Swpaul      }
1966751Swpaul    }
197