Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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.2 2003/06/17 04:29:52 dillon 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 __P((void));
35 int printgetfile __P((bp_getfile_res *));
36 int printwhoami __P((bp_whoami_res *));
37
38 int
39 eachres_whoami(resultp, raddr)
40 bp_whoami_res *resultp;
41 struct sockaddr_in *raddr;
42 {
43   struct hostent *he;
44
45   he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
46   printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
47   printwhoami(resultp);
48   printf("\n");
49   return(0);
50 }
51
52 eachres_getfile(resultp, raddr)
53 bp_getfile_res *resultp;
54 struct sockaddr_in *raddr;
55 {
56   struct hostent *he;
57
58   he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
59   printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
60   printgetfile(resultp);
61   printf("\n");
62   return(0);
63 }
64
65
66 int
67 main(argc, argv)
68 int argc;
69 char **argv;
70 {
71   char *server;
72
73   bp_whoami_arg whoami_arg;
74   bp_whoami_res *whoami_res, stat_whoami_res;
75   bp_getfile_arg getfile_arg;
76   bp_getfile_res *getfile_res, stat_getfile_res;
77
78
79   long the_inet_addr;
80   CLIENT *clnt;
81   enum clnt_stat clnt_stat;
82
83   stat_whoami_res.client_name = cln;
84   stat_whoami_res.domain_name = dmn;
85
86   stat_getfile_res.server_name = cln;
87   stat_getfile_res.server_path = path;
88
89   if (argc < 3)
90     usage();
91
92   server = argv[1];
93   if ( ! strcmp(server , "all") ) broadcast = 1;
94
95   if ( ! broadcast ) {
96     clnt = clnt_create(server,BOOTPARAMPROG, BOOTPARAMVERS, "udp");
97   }
98
99   if ( clnt == NULL )
100      errx(1, "could not contact bootparam server on host %s", server);
101
102   switch (argc) {
103   case 3:
104     whoami_arg.client_address.address_type = IP_ADDR_TYPE;
105     the_inet_addr = inet_addr(argv[2]);
106     if ( the_inet_addr == -1)
107       errx(2, "bogus addr %s", argv[2]);
108     bcopy(&the_inet_addr,&whoami_arg.client_address.bp_address_u.ip_addr,4);
109
110     if (! broadcast ) {
111       whoami_res = bootparamproc_whoami_1(&whoami_arg, clnt);
112       printf("Whoami returning:\n");
113       if (printwhoami(whoami_res)) {
114         errx(1, "bad answer returned from server %s", server);
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         errx(1, "bad answer returned from server %s", server);
135       } else
136         exit(0);
137     } else {
138       clnt_stat=clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
139                                BOOTPARAMPROC_GETFILE,
140                                xdr_bp_getfile_arg, &getfile_arg,
141                                xdr_bp_getfile_res, &stat_getfile_res,eachres_getfile);
142       exit(0);
143     }
144
145   default:
146
147     usage();
148   }
149
150 }
151
152
153 static void
154 usage()
155 {
156         fprintf(stderr,
157                 "usage: callbootd server procnum (IP-addr | host fileid)\n");
158     exit(1);
159 }
160
161 int
162 printwhoami(res)
163 bp_whoami_res *res;
164 {
165       if ( res) {
166         printf("client_name:\t%s\ndomain_name:\t%s\n",
167              res->client_name, res->domain_name);
168         printf("router:\t%d.%d.%d.%d\n",
169              255 &  res->router_address.bp_address_u.ip_addr.net,
170              255 & res->router_address.bp_address_u.ip_addr.host,
171              255 &  res->router_address.bp_address_u.ip_addr.lh,
172              255 & res->router_address.bp_address_u.ip_addr.impno);
173         return(0);
174       } else {
175         warnx("null answer!!!");
176         return(1);
177       }
178     }
179
180
181
182
183 int
184 printgetfile(res)
185 bp_getfile_res *res;
186 {
187       if (res) {
188         printf("server_name:\t%s\nserver_address:\t%s\npath:\t%s\n",
189                res->server_name,
190                inet_ntoa(*(struct in_addr *)&res->server_address.bp_address_u.ip_addr),
191                res->server_path);
192         return(0);
193       } else {
194         warnx("null answer!!!");
195         return(1);
196       }
197     }