xref: /netbsd/usr.sbin/rpc.bootparamd/test.c (revision df4795aa)
1 /*	$NetBSD: test.c,v 1.4 2011/08/30 20:29:41 joerg Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: test.c,v 1.4 2011/08/30 20:29:41 joerg Exp $");
35 #endif
36 
37 #include <stdio.h>
38 #include <netdb.h>
39 #include <string.h>
40 #include <err.h>
41 
42 #include <rpc/rpc.h>
43 #include <rpcsvc/bootparam_prot.h>
44 
45 /* Default timeout can be changed using clnt_control() */
46 static struct timeval TIMEOUT = {25, 0};
47 
48 bp_whoami_res *
bootparamproc_whoami_1(bp_whoami_arg * argp,CLIENT * clnt)49 bootparamproc_whoami_1(bp_whoami_arg *argp, CLIENT *clnt)
50 {
51 	static bp_whoami_res res;
52 	enum clnt_stat st;
53 
54 
55 	(void)memset(&res, 0, sizeof(res));
56 	if ((st = clnt_call(clnt, BOOTPARAMPROC_WHOAMI, xdr_bp_whoami_arg, argp,
57 	    xdr_bp_whoami_res, &res, TIMEOUT)) != RPC_SUCCESS) {
58 		warnx("clnt_call returned %s", clnt_sperrno(st));
59 		return NULL;
60 	}
61 	return &res;
62 }
63 
64 
65 int
main(int argc,char ** argv)66 main(int argc, char **argv)
67 {
68 	CLIENT *cli;
69 	bp_whoami_res *out;
70 	bp_whoami_arg bp;
71 	struct hostent *hp;
72 	uint32_t addr;
73 
74 	if (argc < 2) {
75 		warnx("usage: test <hostname>");
76 		errx(1, "Always talks to bootparamd at localhost");
77 	}
78 	if ((hp = gethostbyname(argv[1])) == NULL)
79 		errx(1, "Cannot resolve `%s'", argv[1]);
80 
81 	printf("Creating client for localhost\n");
82 	cli = clnt_create("localhost", BOOTPARAMPROG, BOOTPARAMVERS, "udp");
83 	if (!cli)
84 		errx(1, "Failed to create client");
85 	memcpy(&addr, hp->h_addr_list[0], sizeof(addr));
86 	addr = htonl(addr);
87 	bp.client_address.address_type = IP_ADDR_TYPE;
88 	bp.client_address.bp_address_u.ip_addr.net = (addr >> 24) & 0xff;
89 	bp.client_address.bp_address_u.ip_addr.host = (addr >> 16) & 0xff;
90 	bp.client_address.bp_address_u.ip_addr.lh = (addr >> 8) & 0xff;
91 	bp.client_address.bp_address_u.ip_addr.impno = (addr >> 0) & 0xff;
92 
93 	if ((out = bootparamproc_whoami_1(&bp, cli)) != NULL) {
94 		printf("Success [host=%s, domain=%s, gw=%d.%d.%d.%d]\n",
95 		    out->client_name, out->domain_name,
96 		    out->router_address.bp_address_u.ip_addr.net & 0xff,
97 		    out->router_address.bp_address_u.ip_addr.host & 0xff,
98 		    out->router_address.bp_address_u.ip_addr.lh & 0xff,
99 		    out->router_address.bp_address_u.ip_addr.impno & 0xff);
100 	} else
101 		printf("Fail\n");
102 
103 	return 0;
104 }
105