callbootd.c revision 116598
164562Sgshapiro/* 264562Sgshapiro 364562SgshapiroThis code is not copyright, and is placed in the public domain. Feel free to 464562Sgshapirouse and modify. Please send modifications and/or suggestions + bug fixes to 564562Sgshapiro 664562Sgshapiro Klas Heggemann <klas@nada.kth.se> 764562Sgshapiro 864562Sgshapiro*/ 964562Sgshapiro 1064562Sgshapiro#ifndef lint 1164562Sgshapirostatic const char rcsid[] = 12132943Sgshapiro "$FreeBSD: head/usr.sbin/bootparamd/callbootd/callbootd.c 116598 2003-06-20 04:54:27Z jmg $"; 13132943Sgshapiro#endif /* not lint */ 1464562Sgshapiro 15157001Sgshapiro#include "bootparam_prot.h" 16157001Sgshapiro#include <rpc/rpc.h> 17157001Sgshapiro#include <sys/types.h> 1890792Sgshapiro#include <sys/socket.h> 1990792Sgshapiro#include <netinet/in.h> 2090792Sgshapiro#include <arpa/inet.h> 2164562Sgshapiro#include <err.h> 2290792Sgshapiro#include <netdb.h> 2390792Sgshapiro#include <stdlib.h> 2490792Sgshapiro 2590792Sgshapiro 26132943Sgshapiro/* #define bp_address_u bp_address */ 27132943Sgshapiro#include <stdio.h> 2890792Sgshapiro#include <string.h> 29132943Sgshapiro 30132943Sgshapiroint broadcast; 31132943Sgshapiro 3290792Sgshapirochar cln[MAX_MACHINE_NAME+1]; 33132943Sgshapirochar dmn[MAX_MACHINE_NAME+1]; 34132943Sgshapirochar path[MAX_PATH_LEN+1]; 3590792Sgshapiroextern char *inet_ntoa(); 36132943Sgshapirostatic void usage __P((void)); 37132943Sgshapiroint printgetfile __P((bp_getfile_res *)); 38132943Sgshapiroint printwhoami __P((bp_whoami_res *)); 3964562Sgshapiro 4064562Sgshapirobool_t 4164562Sgshapiroeachres_whoami(resultp, raddr) 4264562Sgshapirobp_whoami_res *resultp; 4364562Sgshapirostruct sockaddr_in *raddr; 4464562Sgshapiro{ 4564562Sgshapiro struct hostent *he; 4664562Sgshapiro 47110560Sgshapiro he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET); 4864562Sgshapiro printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr)); 4964562Sgshapiro printwhoami(resultp); 5064562Sgshapiro printf("\n"); 5164562Sgshapiro return(0); 5264562Sgshapiro} 5390792Sgshapiro 5490792Sgshapirobool_t 5564562Sgshapiroeachres_getfile(resultp, raddr) 5664562Sgshapirobp_getfile_res *resultp; 5764562Sgshapirostruct sockaddr_in *raddr; 5864562Sgshapiro{ 5964562Sgshapiro struct hostent *he; 6064562Sgshapiro 6164562Sgshapiro he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET); 6273188Sgshapiro printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr)); 6373188Sgshapiro printgetfile(resultp); 6473188Sgshapiro printf("\n"); 6573188Sgshapiro return(0); 6664562Sgshapiro} 6773188Sgshapiro 6864562Sgshapiro 6964562Sgshapiroint 7064562Sgshapiromain(argc, argv) 7164562Sgshapiroint argc; 7264562Sgshapirochar **argv; 7364562Sgshapiro{ 7464562Sgshapiro char *server; 7564562Sgshapiro 7664562Sgshapiro bp_whoami_arg whoami_arg; 7780785Sgshapiro bp_whoami_res *whoami_res, stat_whoami_res; 7864562Sgshapiro bp_getfile_arg getfile_arg; 7964562Sgshapiro bp_getfile_res *getfile_res, stat_getfile_res; 8064562Sgshapiro 8164562Sgshapiro 8264562Sgshapiro long the_inet_addr; 8364562Sgshapiro CLIENT *clnt; 8480785Sgshapiro enum clnt_stat clnt_stat; 8564562Sgshapiro 8664562Sgshapiro stat_whoami_res.client_name = cln; 8764562Sgshapiro stat_whoami_res.domain_name = dmn; 8864562Sgshapiro 8964562Sgshapiro stat_getfile_res.server_name = cln; 9064562Sgshapiro stat_getfile_res.server_path = path; 9164562Sgshapiro 9264562Sgshapiro if (argc < 3) 9364562Sgshapiro usage(); 9466494Sgshapiro 9590792Sgshapiro server = argv[1]; 9666494Sgshapiro if ( ! strcmp(server , "all") ) broadcast = 1; 9764562Sgshapiro 9880785Sgshapiro if ( ! broadcast ) { 9964562Sgshapiro clnt = clnt_create(server,BOOTPARAMPROG, BOOTPARAMVERS, "udp"); 10064562Sgshapiro if ( clnt == NULL ) 10190792Sgshapiro errx(1, "could not contact bootparam server on host %s", server); 10280785Sgshapiro } 10380785Sgshapiro 10480785Sgshapiro switch (argc) { 10580785Sgshapiro case 3: 10680785Sgshapiro whoami_arg.client_address.address_type = IP_ADDR_TYPE; 10764562Sgshapiro the_inet_addr = inet_addr(argv[2]); 10864562Sgshapiro if ( the_inet_addr == -1) 10980785Sgshapiro errx(2, "bogus addr %s", argv[2]); 11080785Sgshapiro bcopy(&the_inet_addr,&whoami_arg.client_address.bp_address_u.ip_addr,4); 11164562Sgshapiro 11290792Sgshapiro if (! broadcast ) { 11364562Sgshapiro whoami_res = bootparamproc_whoami_1(&whoami_arg, clnt); 11464562Sgshapiro printf("Whoami returning:\n"); 11564562Sgshapiro if (printwhoami(whoami_res)) { 11666494Sgshapiro errx(1, "bad answer returned from server %s", server); 11777349Sgshapiro } else 11877349Sgshapiro exit(0); 11964562Sgshapiro } else { 12066494Sgshapiro clnt_stat=clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS, 12166494Sgshapiro BOOTPARAMPROC_WHOAMI, 12266494Sgshapiro (xdrproc_t)xdr_bp_whoami_arg, 12366494Sgshapiro (char *)&whoami_arg, 12466494Sgshapiro (xdrproc_t)xdr_bp_whoami_res, 12566494Sgshapiro (char *)&stat_whoami_res, 12666494Sgshapiro (resultproc_t)eachres_whoami); 12766494Sgshapiro exit(0); 12866494Sgshapiro } 12964562Sgshapiro 13064562Sgshapiro case 4: 13164562Sgshapiro 13264562Sgshapiro getfile_arg.client_name = argv[2]; 13364562Sgshapiro getfile_arg.file_id = argv[3]; 13464562Sgshapiro 13564562Sgshapiro if (! broadcast ) { 13664562Sgshapiro getfile_res = bootparamproc_getfile_1(&getfile_arg,clnt); 13764562Sgshapiro printf("getfile returning:\n"); 13864562Sgshapiro if (printgetfile(getfile_res)) { 13964562Sgshapiro errx(1, "bad answer returned from server %s", server); 14064562Sgshapiro } else 14164562Sgshapiro exit(0); 14264562Sgshapiro } else { 14364562Sgshapiro clnt_stat=clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS, 14464562Sgshapiro BOOTPARAMPROC_GETFILE, 14564562Sgshapiro (xdrproc_t)xdr_bp_getfile_arg, 14664562Sgshapiro (char *)&getfile_arg, 14764562Sgshapiro (xdrproc_t)xdr_bp_getfile_res, 14864562Sgshapiro (char *)&stat_getfile_res, 14964562Sgshapiro (resultproc_t)eachres_getfile); 15064562Sgshapiro exit(0); 15164562Sgshapiro } 15264562Sgshapiro 15364562Sgshapiro default: 15464562Sgshapiro 15564562Sgshapiro usage(); 15664562Sgshapiro } 15764562Sgshapiro 15864562Sgshapiro} 15964562Sgshapiro 16064562Sgshapiro 16164562Sgshapirostatic void 16264562Sgshapirousage() 16364562Sgshapiro{ 16464562Sgshapiro fprintf(stderr, 16564562Sgshapiro "usage: callbootd server procnum (IP-addr | host fileid)\n"); 16664562Sgshapiro exit(1); 16766494Sgshapiro} 16864562Sgshapiro 16964562Sgshapiroint 17064562Sgshapiroprintwhoami(res) 17164562Sgshapirobp_whoami_res *res; 17264562Sgshapiro{ 17364562Sgshapiro if ( res) { 17464562Sgshapiro printf("client_name:\t%s\ndomain_name:\t%s\n", 17564562Sgshapiro res->client_name, res->domain_name); 17664562Sgshapiro printf("router:\t%d.%d.%d.%d\n", 17764562Sgshapiro 255 & res->router_address.bp_address_u.ip_addr.net, 17864562Sgshapiro 255 & res->router_address.bp_address_u.ip_addr.host, 17964562Sgshapiro 255 & res->router_address.bp_address_u.ip_addr.lh, 18064562Sgshapiro 255 & res->router_address.bp_address_u.ip_addr.impno); 18164562Sgshapiro return(0); 18264562Sgshapiro } else { 18364562Sgshapiro warnx("null answer!!!"); 18464562Sgshapiro return(1); 18564562Sgshapiro } 18664562Sgshapiro } 18764562Sgshapiro 18864562Sgshapiro 18964562Sgshapiro 19064562Sgshapiro 19164562Sgshapiroint 19264562Sgshapiroprintgetfile(res) 19364562Sgshapirobp_getfile_res *res; 19464562Sgshapiro{ 19564562Sgshapiro if (res) { 19664562Sgshapiro printf("server_name:\t%s\nserver_address:\t%s\npath:\t%s\n", 19764562Sgshapiro res->server_name, 19864562Sgshapiro inet_ntoa(*(struct in_addr *)&res->server_address.bp_address_u.ip_addr), 19964562Sgshapiro res->server_path); 20090792Sgshapiro return(0); 20190792Sgshapiro } else { 20290792Sgshapiro warnx("null answer!!!"); 20390792Sgshapiro return(1); 20490792Sgshapiro } 20590792Sgshapiro } 20690792Sgshapiro