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