Initial import from FreeBSD RELENG_4:
[games.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 #ifndef lint
11 static const char rcsid[] =
12   "$FreeBSD: src/usr.sbin/bootparamd/callbootd/callbootd.c,v 1.8 1999/08/28 01:15:40 peter Exp $";
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
29 int broadcast;
30
31 char cln[MAX_MACHINE_NAME+1];
32 char dmn[MAX_MACHINE_NAME+1];
33 char path[MAX_PATH_LEN+1];
34 extern char *inet_ntoa();
35 static void usage __P((void));
36 int printgetfile __P((bp_getfile_res *));
37 int printwhoami __P((bp_whoami_res *));
38
39 int
40 eachres_whoami(resultp, raddr)
41 bp_whoami_res *resultp;
42 struct 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
53 eachres_getfile(resultp, raddr)
54 bp_getfile_res *resultp;
55 struct 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
67 int
68 main(argc, argv)
69 int argc;
70 char **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                                xdr_bp_whoami_arg, &whoami_arg,
122                                xdr_bp_whoami_res, &stat_whoami_res, eachres_whoami);
123        exit(0);
124      }
125
126   case 4:
127
128     getfile_arg.client_name = argv[2];
129     getfile_arg.file_id = argv[3];
130
131     if (! broadcast ) {
132       getfile_res = bootparamproc_getfile_1(&getfile_arg,clnt);
133       printf("getfile returning:\n");
134       if (printgetfile(getfile_res)) {
135         errx(1, "bad answer returned from server %s", server);
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     usage();
149   }
150
151 }
152
153
154 static void
155 usage()
156 {
157         fprintf(stderr,
158                 "usage: callbootd server procnum (IP-addr | host fileid)\n");
159     exit(1);
160 }
161
162 int
163 printwhoami(res)
164 bp_whoami_res *res;
165 {
166       if ( res) {
167         printf("client_name:\t%s\ndomain_name:\t%s\n",
168              res->client_name, res->domain_name);
169         printf("router:\t%d.%d.%d.%d\n",
170              255 &  res->router_address.bp_address_u.ip_addr.net,
171              255 & res->router_address.bp_address_u.ip_addr.host,
172              255 &  res->router_address.bp_address_u.ip_addr.lh,
173              255 & res->router_address.bp_address_u.ip_addr.impno);
174         return(0);
175       } else {
176         warnx("null answer!!!");
177         return(1);
178       }
179     }
180
181
182
183
184 int
185 printgetfile(res)
186 bp_getfile_res *res;
187 {
188       if (res) {
189         printf("server_name:\t%s\nserver_address:\t%s\npath:\t%s\n",
190                res->server_name,
191                inet_ntoa(*(struct in_addr *)&res->server_address.bp_address_u.ip_addr),
192                res->server_path);
193         return(0);
194       } else {
195         warnx("null answer!!!");
196         return(1);
197       }
198     }