xref: /netbsd/usr.sbin/rpc.bootparamd/test.c (revision 6550d01e)
1 /*	$NetBSD: test.c,v 1.3 2008/04/28 20:24:17 martin 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.3 2008/04/28 20:24:17 martin 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 int main(int, char *[]);
49 
50 bp_whoami_res *
51 bootparamproc_whoami_1(argp, clnt)
52 	bp_whoami_arg *argp;
53 	CLIENT *clnt;
54 {
55 	static bp_whoami_res res;
56 	enum clnt_stat st;
57 
58 
59 	(void)memset(&res, 0, sizeof(res));
60 	if ((st = clnt_call(clnt, BOOTPARAMPROC_WHOAMI, xdr_bp_whoami_arg, argp,
61 	    xdr_bp_whoami_res, &res, TIMEOUT)) != RPC_SUCCESS) {
62 		warnx("clnt_call returned %s", clnt_sperrno(st));
63 		return NULL;
64 	}
65 	return &res;
66 }
67 
68 
69 int
70 main(argc, argv)
71 	int argc;
72 	char **argv;
73 {
74 	CLIENT *cli;
75 	bp_whoami_res *out;
76 	bp_whoami_arg bp;
77 	struct hostent *hp;
78 	uint32_t addr;
79 
80 	if (argc < 2) {
81 		warnx("usage: test <hostname>");
82 		errx(1, "Always talks to bootparamd at localhost");
83 	}
84 	if ((hp = gethostbyname(argv[1])) == NULL)
85 		errx(1, "Cannot resolve `%s'", argv[1]);
86 
87 	printf("Creating client for localhost\n");
88 	cli = clnt_create("localhost", BOOTPARAMPROG, BOOTPARAMVERS, "udp");
89 	if (!cli)
90 		errx(1, "Failed to create client");
91 	memcpy(&addr, hp->h_addr_list[0], sizeof(addr));
92 	addr = htonl(addr);
93 	bp.client_address.address_type = IP_ADDR_TYPE;
94 	bp.client_address.bp_address_u.ip_addr.net = (addr >> 24) & 0xff;
95 	bp.client_address.bp_address_u.ip_addr.host = (addr >> 16) & 0xff;
96 	bp.client_address.bp_address_u.ip_addr.lh = (addr >> 8) & 0xff;
97 	bp.client_address.bp_address_u.ip_addr.impno = (addr >> 0) & 0xff;
98 
99 	if ((out = bootparamproc_whoami_1(&bp, cli)) != NULL) {
100 		printf("Success [host=%s, domain=%s, gw=%d.%d.%d.%d]\n",
101 		    out->client_name, out->domain_name,
102 		    out->router_address.bp_address_u.ip_addr.net & 0xff,
103 		    out->router_address.bp_address_u.ip_addr.host & 0xff,
104 		    out->router_address.bp_address_u.ip_addr.lh & 0xff,
105 		    out->router_address.bp_address_u.ip_addr.impno & 0xff);
106 	} else
107 		printf("Fail\n");
108 
109 	return 0;
110 }
111