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