Path: blob/main/usr.sbin/bootparamd/callbootd/callbootd.c
101888 views
/*12This code is not copyright, and is placed in the public domain. Feel free to3use and modify. Please send modifications and/or suggestions + bug fixes to45Klas Heggemann <[email protected]>67*/89#include <sys/cdefs.h>10#include "bootparam_prot.h"11#include <rpc/rpc.h>12#include <sys/types.h>13#include <sys/socket.h>14#include <netinet/in.h>15#include <arpa/inet.h>16#include <err.h>17#include <netdb.h>18#include <stdlib.h>192021/* #define bp_address_u bp_address */22#include <stdio.h>23#include <string.h>2425static int broadcast;26static char cln[MAX_MACHINE_NAME+1];27static char dmn[MAX_MACHINE_NAME+1];28static char path[MAX_PATH_LEN+1];2930static void usage(void) __dead2;31int printgetfile(bp_getfile_res *);32int printwhoami(bp_whoami_res *);3334static bool_t35eachres_whoami(bp_whoami_res *resultp, struct sockaddr_in *raddr)36{37struct hostent *he;3839he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);40printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));41printwhoami(resultp);42printf("\n");43return(0);44}4546static bool_t47eachres_getfile(bp_getfile_res *resultp, struct sockaddr_in *raddr)48{49struct hostent *he;5051he = gethostbyaddr((char *)&raddr->sin_addr.s_addr,4,AF_INET);52printf("%s answered:\n", he ? he->h_name : inet_ntoa(raddr->sin_addr));53printgetfile(resultp);54printf("\n");55return(0);56}575859int60main(int argc, char **argv)61{62char *server;6364bp_whoami_arg whoami_arg;65bp_whoami_res *whoami_res, stat_whoami_res;66bp_getfile_arg getfile_arg;67bp_getfile_res *getfile_res, stat_getfile_res;686970in_addr_t the_inet_addr;71CLIENT *clnt = NULL; /* Silence warnings */7273stat_whoami_res.client_name = cln;74stat_whoami_res.domain_name = dmn;7576stat_getfile_res.server_name = cln;77stat_getfile_res.server_path = path;7879if (argc < 3)80usage();8182server = argv[1];83if ( ! strcmp(server , "all") ) broadcast = 1;8485if ( ! broadcast ) {86clnt = clnt_create(server,BOOTPARAMPROG, BOOTPARAMVERS, "udp");87if ( clnt == NULL )88errx(1, "could not contact bootparam server on host %s", server);89}9091switch (argc) {92case 3:93whoami_arg.client_address.address_type = IP_ADDR_TYPE;94the_inet_addr = inet_addr(argv[2]);95if ( the_inet_addr == INADDR_NONE)96errx(2, "bogus addr %s", argv[2]);97bcopy(&the_inet_addr,&whoami_arg.client_address.bp_address_u.ip_addr,4);9899if (! broadcast ) {100whoami_res = bootparamproc_whoami_1(&whoami_arg, clnt);101printf("Whoami returning:\n");102if (printwhoami(whoami_res)) {103errx(1, "bad answer returned from server %s", server);104} else105exit(0);106} else {107(void)clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,108BOOTPARAMPROC_WHOAMI,109(xdrproc_t)xdr_bp_whoami_arg,110(char *)&whoami_arg,111(xdrproc_t)xdr_bp_whoami_res,112(char *)&stat_whoami_res,113(clnt_broadcast_resultproc_t)eachres_whoami);114exit(0);115}116117case 4:118119getfile_arg.client_name = argv[2];120getfile_arg.file_id = argv[3];121122if (! broadcast ) {123getfile_res = bootparamproc_getfile_1(&getfile_arg,clnt);124printf("getfile returning:\n");125if (printgetfile(getfile_res)) {126errx(1, "bad answer returned from server %s", server);127} else128exit(0);129} else {130(void)clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,131BOOTPARAMPROC_GETFILE,132(xdrproc_t)xdr_bp_getfile_arg,133(char *)&getfile_arg,134(xdrproc_t)xdr_bp_getfile_res,135(char *)&stat_getfile_res,136(clnt_broadcast_resultproc_t)eachres_getfile);137exit(0);138}139140default:141142usage();143}144145}146147148static void149usage(void)150{151fprintf(stderr,152"usage: callbootd server procnum (IP-addr | host fileid)\n");153exit(1);154}155156int157printwhoami(bp_whoami_res *res)158{159if ( res) {160printf("client_name:\t%s\ndomain_name:\t%s\n",161res->client_name, res->domain_name);162printf("router:\t%d.%d.%d.%d\n",163255 & res->router_address.bp_address_u.ip_addr.net,164255 & res->router_address.bp_address_u.ip_addr.host,165255 & res->router_address.bp_address_u.ip_addr.lh,166255 & res->router_address.bp_address_u.ip_addr.impno);167return(0);168} else {169warnx("null answer!!!");170return(1);171}172}173174175176177int178printgetfile(bp_getfile_res *res)179{180if (res) {181printf("server_name:\t%s\nserver_address:\t%s\npath:\t%s\n",182res->server_name,183inet_ntoa(*(struct in_addr *)&res->server_address.bp_address_u.ip_addr),184res->server_path);185return(0);186} else {187warnx("null answer!!!");188return(1);189}190}191192193