callbootd.c revision 22997
1178476Sjb/*
2178476Sjb
3178476SjbThis code is not copyright, and is placed in the public domain. Feel free to
4178476Sjbuse and modify. Please send modifications and/or suggestions + bug fixes to
5178476Sjb
6178476Sjb        Klas Heggemann <klas@nada.kth.se>
7178476Sjb
8178476Sjb	$Id$
9178476Sjb*/
10178476Sjb
11178476Sjb
12178476Sjb#include "bootparam_prot.h"
13178476Sjb#include <rpc/rpc.h>
14178476Sjb#include <sys/types.h>
15178476Sjb#include <sys/socket.h>
16178476Sjb#include <netdb.h>
17178476Sjb
18178476Sjb
19178476Sjb/* #define bp_address_u bp_address */
20178476Sjb#include <stdio.h>
21178476Sjb
22178476Sjbint broadcast;
23178476Sjb
24178476Sjbchar cln[MAX_MACHINE_NAME+1];
25178476Sjbchar dmn[MAX_MACHINE_NAME+1];
26178476Sjbchar path[MAX_PATH_LEN+1];
27178476Sjbextern char *inet_ntoa();
28178476Sjb
29178476Sjbeachres_whoami(resultp, raddr)
30178476Sjbbp_whoami_res *resultp;
31178476Sjbstruct sockaddr_in *raddr;
32178476Sjb{
33178476Sjb  struct hostent *he;
34178476Sjb
35178476Sjb  he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
36178476Sjb  printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
37178476Sjb  printwhoami(resultp);
38178476Sjb  printf("\n");
39178476Sjb  return(0);
40178476Sjb}
41178476Sjb
42178476Sjbeachres_getfile(resultp, raddr)
43178476Sjbbp_getfile_res *resultp;
44178476Sjbstruct sockaddr_in *raddr;
45178476Sjb{
46178476Sjb  struct hostent *he;
47178476Sjb
48178476Sjb  he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);
49178476Sjb  printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));
50178476Sjb  printgetfile(resultp);
51178476Sjb  printf("\n");
52178476Sjb  return(0);
53178476Sjb}
54178476Sjb
55178476Sjb
56178476Sjbmain(argc, argv)
57178476Sjbint argc;
58178476Sjbchar **argv;
59178476Sjb{
60178476Sjb  int stat;
61178476Sjb  char *server;
62178476Sjb
63178476Sjb  bp_whoami_arg whoami_arg;
64178476Sjb  bp_whoami_res *whoami_res, stat_whoami_res;
65178476Sjb  bp_getfile_arg getfile_arg;
66178476Sjb  bp_getfile_res *getfile_res, stat_getfile_res;
67178476Sjb
68178476Sjb
69178476Sjb  long the_inet_addr;
70178476Sjb  CLIENT *clnt;
71178476Sjb  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
157int printwhoami(res)
158bp_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
178int
179printgetfile(res)
180bp_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