xref: /netbsd/usr.sbin/rpc.bootparamd/test.c (revision c4a72b64)
1 /*	$NetBSD: test.c,v 1.1 2002/09/11 18:19:42 christos 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __RCSID("$NetBSD: test.c,v 1.1 2002/09/11 18:19:42 christos Exp $");
42 #endif
43 
44 #include <stdio.h>
45 #include <netdb.h>
46 #include <string.h>
47 #include <err.h>
48 
49 #include <rpc/rpc.h>
50 #include <rpcsvc/bootparam_prot.h>
51 
52 /* Default timeout can be changed using clnt_control() */
53 static struct timeval TIMEOUT = {25, 0};
54 
55 int main(int, char *[]);
56 
57 bp_whoami_res *
58 bootparamproc_whoami_1(argp, clnt)
59 	bp_whoami_arg *argp;
60 	CLIENT *clnt;
61 {
62 	static bp_whoami_res res;
63 	enum clnt_stat st;
64 
65 
66 	(void)memset(&res, 0, sizeof(res));
67 	if ((st = clnt_call(clnt, BOOTPARAMPROC_WHOAMI, xdr_bp_whoami_arg, argp,
68 	    xdr_bp_whoami_res, &res, TIMEOUT)) != RPC_SUCCESS) {
69 		warnx("clnt_call returned %s", clnt_sperrno(st));
70 		return NULL;
71 	}
72 	return &res;
73 }
74 
75 
76 int
77 main(argc, argv)
78 	int argc;
79 	char **argv;
80 {
81 	CLIENT *cli;
82 	bp_whoami_res *out;
83 	bp_whoami_arg bp;
84 	struct hostent *hp;
85 	uint32_t addr;
86 
87 	if (argc < 2) {
88 		warnx("Usage: test <hostname>");
89 		errx(1, "Always talks to bootparamd at localhost");
90 	}
91 	if ((hp = gethostbyname(argv[1])) == NULL)
92 		errx(1, "Cannot resolve `%s'", argv[1]);
93 
94 	printf("Creating client for localhost\n");
95 	cli = clnt_create("localhost", BOOTPARAMPROG, BOOTPARAMVERS, "udp");
96 	if (!cli)
97 		errx(1, "Failed to create client");
98 	memcpy(&addr, hp->h_addr_list[0], sizeof(addr));
99 	addr = htonl(addr);
100 	bp.client_address.address_type = IP_ADDR_TYPE;
101 	bp.client_address.bp_address_u.ip_addr.net = (addr >> 24) & 0xff;
102 	bp.client_address.bp_address_u.ip_addr.host = (addr >> 16) & 0xff;
103 	bp.client_address.bp_address_u.ip_addr.lh = (addr >> 8) & 0xff;
104 	bp.client_address.bp_address_u.ip_addr.impno = (addr >> 0) & 0xff;
105 
106 	if ((out = bootparamproc_whoami_1(&bp, cli)) != NULL) {
107 		printf("Success [host=%s, domain=%s, gw=%d.%d.%d.%d]\n",
108 		    out->client_name, out->domain_name,
109 		    out->router_address.bp_address_u.ip_addr.net & 0xff,
110 		    out->router_address.bp_address_u.ip_addr.host & 0xff,
111 		    out->router_address.bp_address_u.ip_addr.lh & 0xff,
112 		    out->router_address.bp_address_u.ip_addr.impno & 0xff);
113 	} else
114 		printf("Fail\n");
115 
116 	return 0;
117 }
118