Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.sbin / rpc.bootparamd / test.c
blob2f6b4f7df5e84644bee412a1c54a7d1c0e09c5be
1 /* $NetBSD: test.c,v 1.2 2004/01/05 23:23:38 jmmv Exp $ */
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
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.
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.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: test.c,v 1.2 2004/01/05 23:23:38 jmmv Exp $");
35 #endif
37 #include <stdio.h>
38 #include <netdb.h>
39 #include <string.h>
40 #include <err.h>
42 #include <rpc/rpc.h>
43 #include <rpcsvc/bootparam_prot.h>
45 /* Default timeout can be changed using clnt_control() */
46 static struct timeval TIMEOUT = {25, 0};
48 int main(int, char *[]);
50 bp_whoami_res *
51 bootparamproc_whoami_1(argp, clnt)
52 bp_whoami_arg *argp;
53 CLIENT *clnt;
55 static bp_whoami_res res;
56 enum clnt_stat st;
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;
65 return &res;
69 int
70 main(argc, argv)
71 int argc;
72 char **argv;
74 CLIENT *cli;
75 bp_whoami_res *out;
76 bp_whoami_arg bp;
77 struct hostent *hp;
78 uint32_t addr;
80 if (argc < 2) {
81 warnx("usage: test <hostname>");
82 errx(1, "Always talks to bootparamd at localhost");
84 if ((hp = gethostbyname(argv[1])) == NULL)
85 errx(1, "Cannot resolve `%s'", argv[1]);
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;
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");
109 return 0;