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