callbootd.c revision 74462
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*/
9
10#ifndef lint
11static const char rcsid[] =
12  "$FreeBSD: head/usr.sbin/bootparamd/callbootd/callbootd.c 74462 2001-03-19 12:50:13Z alfred $";
13#endif /* not lint */
14
15#include "bootparam_prot.h"
16#include <rpc/rpc.h>
17#include <sys/types.h>
18#include <sys/socket.h>
19#include <netinet/in.h>
20#include <arpa/inet.h>
21#include <err.h>
22#include <netdb.h>
23
24
25/* #define bp_address_u bp_address */
26#include <stdio.h>
27#include <string.h>
28
29int broadcast;
30
31char cln[MAX_MACHINE_NAME+1];
32char dmn[MAX_MACHINE_NAME+1];
33char path[MAX_PATH_LEN+1];
34extern char *inet_ntoa();
35static void usage __P((void));
36int printgetfile __P((bp_getfile_res *));
37int printwhoami __P((bp_whoami_res *));
38
39int
40eachres_whoami(resultp, raddr)
41bp_whoami_res *resultp;
42struct sockaddr_in *raddr;
43{
44  struct hostent *he;
45
46  he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
47  printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
48  printwhoami(resultp);
49  printf("\n");
50  return(0);
51}
52
53eachres_getfile(resultp, raddr)
54bp_getfile_res *resultp;
55struct sockaddr_in *raddr;
56{
57  struct hostent *he;
58
59  he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
60  printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
61  printgetfile(resultp);
62  printf("\n");
63  return(0);
64}
65
66
67int
68main(argc, argv)
69int argc;
70char **argv;
71{
72  char *server;
73
74  bp_whoami_arg whoami_arg;
75  bp_whoami_res *whoami_res, stat_whoami_res;
76  bp_getfile_arg getfile_arg;
77  bp_getfile_res *getfile_res, stat_getfile_res;
78
79
80  long the_inet_addr;
81  CLIENT *clnt;
82  enum clnt_stat clnt_stat;
83
84  stat_whoami_res.client_name = cln;
85  stat_whoami_res.domain_name = dmn;
86
87  stat_getfile_res.server_name = cln;
88  stat_getfile_res.server_path = path;
89
90  if (argc < 3)
91    usage();
92
93  server = argv[1];
94  if ( ! strcmp(server , "all") ) broadcast = 1;
95
96  if ( ! broadcast ) {
97    clnt = clnt_create(server,BOOTPARAMPROG, BOOTPARAMVERS, "udp");
98  }
99
100  if ( clnt == NULL )
101     errx(1, "could not contact bootparam server on host %s", server);
102
103  switch (argc) {
104  case 3:
105    whoami_arg.client_address.address_type = IP_ADDR_TYPE;
106    the_inet_addr = inet_addr(argv[2]);
107    if ( the_inet_addr == -1)
108      errx(2, "bogus addr %s", argv[2]);
109    bcopy(&the_inet_addr,&whoami_arg.client_address.bp_address_u.ip_addr,4);
110
111    if (! broadcast ) {
112      whoami_res = bootparamproc_whoami_1(&whoami_arg, clnt);
113      printf("Whoami returning:\n");
114      if (printwhoami(whoami_res)) {
115	errx(1, "bad answer returned from server %s", server);
116      } else
117	exit(0);
118     } else {
119       clnt_stat=clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
120			       BOOTPARAMPROC_WHOAMI,
121			       (xdrproc_t)xdr_bp_whoami_arg, (char *)&whoami_arg,
122			       xdr_bp_whoami_res, (char *)&stat_whoami_res,
123			       (resultproc_t)eachres_whoami);
124       exit(0);
125     }
126
127  case 4:
128
129    getfile_arg.client_name = argv[2];
130    getfile_arg.file_id = argv[3];
131
132    if (! broadcast ) {
133      getfile_res = bootparamproc_getfile_1(&getfile_arg,clnt);
134      printf("getfile returning:\n");
135      if (printgetfile(getfile_res)) {
136	errx(1, "bad answer returned from server %s", server);
137      } else
138	exit(0);
139    } else {
140      clnt_stat=clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
141			       BOOTPARAMPROC_GETFILE,
142			       xdr_bp_getfile_arg, (char *)&getfile_arg,
143			       xdr_bp_getfile_res, (char *)&stat_getfile_res,
144			       (resultproc_t)eachres_getfile);
145      exit(0);
146    }
147
148  default:
149
150    usage();
151  }
152
153}
154
155
156static void
157usage()
158{
159	fprintf(stderr,
160		"usage: callbootd server procnum (IP-addr | host fileid)\n");
161    exit(1);
162}
163
164int
165printwhoami(res)
166bp_whoami_res *res;
167{
168      if ( res) {
169	printf("client_name:\t%s\ndomain_name:\t%s\n",
170	     res->client_name, res->domain_name);
171	printf("router:\t%d.%d.%d.%d\n",
172	     255 &  res->router_address.bp_address_u.ip_addr.net,
173	     255 & res->router_address.bp_address_u.ip_addr.host,
174	     255 &  res->router_address.bp_address_u.ip_addr.lh,
175	     255 & res->router_address.bp_address_u.ip_addr.impno);
176	return(0);
177      } else {
178	warnx("null answer!!!");
179	return(1);
180      }
181    }
182
183
184
185
186int
187printgetfile(res)
188bp_getfile_res *res;
189{
190      if (res) {
191	printf("server_name:\t%s\nserver_address:\t%s\npath:\t%s\n",
192	       res->server_name,
193	       inet_ntoa(*(struct in_addr *)&res->server_address.bp_address_u.ip_addr),
194	       res->server_path);
195	return(0);
196      } else {
197	warnx("null answer!!!");
198	return(1);
199      }
200    }
201