1/*	$NetBSD: getifaddrs-test.c,v 1.2 2017/01/28 21:31:50 christos Exp $	*/
2
3/*
4 * Copyright (c) 2009 Kungliga Tekniska H��gskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
7 *
8 * Portions Copyright (c) 2009, Secure Endpoints Inc. All rights reserved.
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 *
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 *
21 * 3. Neither the name of the Institute nor the names of its contributors
22 *    may be used to endorse or promote products derived from this software
23 *    without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38#include <config.h>
39#include <err.h>
40#include <krb5/getarg.h>
41
42#include <krb5/roken.h>
43
44#include <ifaddrs.h>
45
46static int verbose_counter;
47static int version_flag;
48static int help_flag;
49
50static struct getargs args[] = {
51    {"verbose",	0,	arg_counter,	&verbose_counter,"verbose",	NULL},
52    {"version",	0,	arg_flag,	&version_flag,	"print version",NULL},
53    {"help",	0,	arg_flag,	&help_flag,	NULL,		NULL}
54};
55
56static void
57usage(int ret)
58{
59    arg_printusage (args,
60		    sizeof(args) / sizeof(args[0]),
61		    NULL, "");
62    exit (ret);
63}
64
65
66static void
67print_addr(const char *s, struct sockaddr *sa)
68{
69    int i;
70    printf("  %s=%d/", s, sa->sa_family);
71#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
72    for(i = 0; i < sa->sa_len - ((long)sa->sa_data - (long)&sa->sa_family); i++)
73	printf("%02x", ((unsigned char*)sa->sa_data)[i]);
74#else
75    for(i = 0; i < sizeof(sa->sa_data); i++)
76	printf("%02x", ((unsigned char*)sa->sa_data)[i]);
77#endif
78    printf("\n");
79}
80
81static void
82print_ifaddrs(struct ifaddrs *x)
83{
84    struct ifaddrs *p;
85
86    for(p = x; p; p = p->ifa_next) {
87	if (verbose_counter) {
88	    printf("%s\n", p->ifa_name);
89	    printf("  flags=%x\n", p->ifa_flags);
90	    if(p->ifa_addr)
91		print_addr("addr", p->ifa_addr);
92	    if(p->ifa_dstaddr)
93		print_addr("dstaddr", p->ifa_dstaddr);
94	    if(p->ifa_netmask)
95		print_addr("netmask", p->ifa_netmask);
96	    printf("  %p\n", p->ifa_data);
97	}
98    }
99}
100
101int
102main(int argc, char **argv)
103{
104    struct ifaddrs *addrs = NULL;
105    int ret, optidx = 0;
106
107    setprogname (argv[0]);
108
109    if (getarg (args, sizeof(args) / sizeof(args[0]), argc, argv,
110		&optidx))
111	usage (1);
112
113    if (help_flag)
114	usage (0);
115
116    if (version_flag) {
117	fprintf (stderr, "%s from %s-%s\n", getprogname(), PACKAGE, VERSION);
118	return 0;
119    }
120
121    if (rk_SOCK_INIT())
122	errx(1, "Couldn't initialize sockets. Err=%d\n", rk_SOCK_ERRNO);
123
124    ret = getifaddrs(&addrs);
125    if (ret != 0)
126	err(1, "getifaddrs");
127
128    if (addrs == NULL)
129	errx(1, "address == NULL");
130
131    print_ifaddrs(addrs);
132
133    /* Check that freeifaddrs doesn't crash */
134    freeifaddrs(addrs);
135
136    rk_SOCK_EXIT();
137
138    return 0;
139}
140