xref: /freebsd/usr.sbin/bootparamd/callbootd/callbootd.c (revision df7f5d4de4592a8948a25ce01e5bddfbb7ce39dc)
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 	$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 
22 int broadcast;
23 
24 char cln[MAX_MACHINE_NAME+1];
25 char dmn[MAX_MACHINE_NAME+1];
26 char path[MAX_PATH_LEN+1];
27 extern char *inet_ntoa();
28 
29 eachres_whoami(resultp, raddr)
30 bp_whoami_res *resultp;
31 struct 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 
42 eachres_getfile(resultp, raddr)
43 bp_getfile_res *resultp;
44 struct 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 
56 main(argc, argv)
57 int argc;
58 char **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   if ( clnt == NULL ) {
94      fprintf (stderr, "%s: could not contact bootparam server on host %s\n",
95 			argv[0], server);
96      exit (1);
97   }
98 
99   switch (argc) {
100   case 3:
101     whoami_arg.client_address.address_type = IP_ADDR_TYPE;
102     the_inet_addr = inet_addr(argv[2]);
103     if ( the_inet_addr == -1) {
104       fprintf(stderr, "bogus addr %s\n", argv[2]);
105       exit(1);
106     }
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 	fprintf(stderr, "Bad answer returned from server %s\n", server);
114 	exit(1);
115       } else
116 	exit(0);
117      } else {
118        clnt_stat=clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
119 			       BOOTPARAMPROC_WHOAMI,
120 			       xdr_bp_whoami_arg, &whoami_arg,
121 			       xdr_bp_whoami_res, &stat_whoami_res, eachres_whoami);
122        exit(0);
123      }
124 
125   case 4:
126 
127     getfile_arg.client_name = argv[2];
128     getfile_arg.file_id = argv[3];
129 
130     if (! broadcast ) {
131       getfile_res = bootparamproc_getfile_1(&getfile_arg,clnt);
132       printf("getfile returning:\n");
133       if (printgetfile(getfile_res)) {
134 	fprintf(stderr, "Bad answer returned from server %s\n", server);
135 	exit(1);
136       } else
137 	exit(0);
138     } else {
139       clnt_stat=clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
140 			       BOOTPARAMPROC_GETFILE,
141 			       xdr_bp_getfile_arg, &getfile_arg,
142 			       xdr_bp_getfile_res, &stat_getfile_res,eachres_getfile);
143       exit(0);
144     }
145 
146   default:
147 
148     fprintf(stderr,
149 	    "Usage: %s server procnum (IP-addr | host fileid)\n", argv[0]);
150     exit(1);
151   }
152 
153 }
154 
155 
156 
157 int printwhoami(res)
158 bp_whoami_res *res;
159 {
160       if ( res) {
161 	printf("client_name:\t%s\ndomain_name:\t%s\n",
162 	     res->client_name, res->domain_name);
163 	printf("router:\t%d.%d.%d.%d\n",
164 	     255 &  res->router_address.bp_address_u.ip_addr.net,
165 	     255 & res->router_address.bp_address_u.ip_addr.host,
166 	     255 &  res->router_address.bp_address_u.ip_addr.lh,
167 	     255 & res->router_address.bp_address_u.ip_addr.impno);
168 	return(0);
169       } else {
170 	fprintf(stderr,"Null answer!!!\n");
171 	return(1);
172       }
173     }
174 
175 
176 
177 
178 int
179 printgetfile(res)
180 bp_getfile_res *res;
181 {
182       if (res) {
183 	printf("server_name:\t%s\nserver_address:\t%s\npath:\t%s\n",
184 	       res->server_name,
185 	       inet_ntoa(res->server_address.bp_address_u.ip_addr),
186 	       res->server_path);
187 	return(0);
188       } else {
189 	fprintf(stderr,"Null answer!!!\n");
190 	return(1);
191       }
192     }
193