callbootd.c revision 6751
1/*
2
3This code is not copyright, and is placed in the public domain. Feel free to
4use and modify. Please send modifications and/or suggestions + bug fixes to
5
6        Klas Heggemann <klas@nada.kth.se>
7
8	$Id$
9*/
10
11
12#include "bootparam_prot.h"
13#include <rpc/rpc.h>
14#include <sys/types.h>
15#include <sys/socket.h>
16#include <netdb.h>
17
18
19/* #define bp_address_u bp_address */
20#include <stdio.h>
21
22int broadcast;
23
24char cln[MAX_MACHINE_NAME+1];
25char dmn[MAX_MACHINE_NAME+1];
26char path[MAX_PATH_LEN+1];
27extern char *inet_ntoa();
28
29eachres_whoami(resultp, raddr)
30bp_whoami_res *resultp;
31struct sockaddr_in *raddr;
32{
33  struct hostent *he;
34
35  he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
36  printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
37  printwhoami(resultp);
38  printf("\n");
39  return(0);
40}
41
42eachres_getfile(resultp, raddr)
43bp_getfile_res *resultp;
44struct sockaddr_in *raddr;
45{
46  struct hostent *he;
47
48  he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
49  printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
50  printgetfile(resultp);
51  printf("\n");
52  return(0);
53}
54
55
56main(argc, argv)
57int argc;
58char **argv;
59{
60  int stat;
61  char *server;
62
63  bp_whoami_arg whoami_arg;
64  bp_whoami_res *whoami_res, stat_whoami_res;
65  bp_getfile_arg getfile_arg;
66  bp_getfile_res *getfile_res, stat_getfile_res;
67
68
69  long the_inet_addr;
70  CLIENT *clnt;
71  enum clnt_stat clnt_stat;
72
73  stat_whoami_res.client_name = cln;
74  stat_whoami_res.domain_name = dmn;
75
76  stat_getfile_res.server_name = cln;
77  stat_getfile_res.server_path = path;
78
79  if (argc < 3) {
80    fprintf(stderr,
81	    "Usage: %s server procnum (IP-addr | host fileid)\n", argv[0]);
82    exit(1);
83  }
84
85
86  server = argv[1];
87  if ( ! strcmp(server , "all") ) broadcast = 1;
88
89  if ( ! broadcast ) {
90    clnt = clnt_create(server,BOOTPARAMPROG, BOOTPARAMVERS, "udp");
91  }
92
93
94  switch (argc) {
95  case 3:
96    whoami_arg.client_address.address_type = IP_ADDR_TYPE;
97    the_inet_addr = inet_addr(argv[2]);
98    if ( the_inet_addr == -1) {
99      fprintf(stderr, "bogus addr %s\n", argv[2]);
100      exit(1);
101    }
102    bcopy(&the_inet_addr,&whoami_arg.client_address.bp_address_u.ip_addr,4);
103
104    if (! broadcast ) {
105      whoami_res = bootparamproc_whoami_1(&whoami_arg, clnt);
106      printf("Whoami returning:\n");
107      if (printwhoami(whoami_res)) {
108	fprintf(stderr, "Bad answer returned from server %s\n", server);
109	exit(1);
110      } else
111	exit(0);
112     } else {
113       clnt_stat=clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
114			       BOOTPARAMPROC_WHOAMI,
115			       xdr_bp_whoami_arg, &whoami_arg,
116			       xdr_bp_whoami_res, &stat_whoami_res, eachres_whoami);
117       exit(0);
118     }
119
120  case 4:
121
122    getfile_arg.client_name = argv[2];
123    getfile_arg.file_id = argv[3];
124
125    if (! broadcast ) {
126      getfile_res = bootparamproc_getfile_1(&getfile_arg,clnt);
127      printf("getfile returning:\n");
128      if (printgetfile(getfile_res)) {
129	fprintf(stderr, "Bad answer returned from server %s\n", server);
130	exit(1);
131      } else
132	exit(0);
133    } else {
134      clnt_stat=clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
135			       BOOTPARAMPROC_GETFILE,
136			       xdr_bp_getfile_arg, &getfile_arg,
137			       xdr_bp_getfile_res, &stat_getfile_res,eachres_getfile);
138      exit(0);
139    }
140
141  default:
142
143    fprintf(stderr,
144	    "Usage: %s server procnum (IP-addr | host fileid)\n", argv[0]);
145    exit(1);
146  }
147
148}
149
150
151
152int printwhoami(res)
153bp_whoami_res *res;
154{
155      if ( res) {
156	printf("client_name:\t%s\ndomain_name:\t%s\n",
157	     res->client_name, res->domain_name);
158	printf("router:\t%d.%d.%d.%d\n",
159	     255 &  res->router_address.bp_address_u.ip_addr.net,
160	     255 & res->router_address.bp_address_u.ip_addr.host,
161	     255 &  res->router_address.bp_address_u.ip_addr.lh,
162	     255 & res->router_address.bp_address_u.ip_addr.impno);
163	return(0);
164      } else {
165	fprintf(stderr,"Null answer!!!\n");
166	return(1);
167      }
168    }
169
170
171
172
173int
174printgetfile(res)
175bp_getfile_res *res;
176{
177      if (res) {
178	printf("server_name:\t%s\nserver_address:\t%s\npath:\t%s\n",
179	       res->server_name,
180	       inet_ntoa(res->server_address.bp_address_u.ip_addr),
181	       res->server_path);
182	return(0);
183      } else {
184	fprintf(stderr,"Null answer!!!\n");
185	return(1);
186      }
187    }
188