1/*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
6 *
7 * A full copy of the text of the CDDL should have accompanied this
8 * source.  A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
10 */
11
12/*
13 * Copyright 2019 Nexenta Systems, Inc.  All rights reserved.
14 */
15
16
17#include <sys/note.h>
18#include <stdarg.h>
19#include <stdio.h>
20#include <addisc.h>
21#include <netinet/in.h>
22#include <arpa/inet.h>
23
24int debug;
25char *domainname = NULL;
26char *sitename = NULL;
27
28void print_ds(ad_disc_ds_t *);
29void mylogger(int pri, const char *format, ...);
30
31int
32main(int argc, char *argv[])
33{
34	ad_disc_t ad_ctx = NULL;
35	boolean_t autodisc;
36	ad_disc_ds_t *dc, *gc;
37	char *s;
38	int c;
39
40	while ((c = getopt(argc, argv, "d")) != -1) {
41		switch (c) {
42		case '?':
43			(void) fprintf(stderr, "bad option: -%c\n", optopt);
44			return (1);
45		case 'd':
46			debug++;
47			break;
48		}
49	}
50
51	if (optind < argc)
52		domainname = argv[optind++];
53	if (optind < argc)
54		sitename = argv[optind++];
55
56	adutils_set_logger(mylogger);
57	adutils_set_debug(AD_DEBUG_ALL, debug);
58
59	ad_ctx = ad_disc_init();
60	ad_disc_set_StatusFP(ad_ctx, stdout);
61
62	if (domainname)
63		(void) ad_disc_set_DomainName(ad_ctx, domainname);
64	if (sitename)
65		(void) ad_disc_set_SiteName(ad_ctx, sitename);
66
67	ad_disc_refresh(ad_ctx);
68
69	dc = ad_disc_get_DomainController(ad_ctx,
70	    AD_DISC_PREFER_SITE, &autodisc);
71	if (dc == NULL) {
72		(void) printf("getdc failed\n");
73		return (1);
74	}
75	(void) printf("Found a DC:\n");
76	print_ds(dc);
77	free(dc);
78
79	s = ad_disc_get_ForestName(ad_ctx, NULL);
80	(void) printf("Forest: %s\n", s);
81	free(s);
82
83	s = ad_disc_get_SiteName(ad_ctx, NULL);
84	(void) printf("Site: %s\n", s);
85	free(s);
86
87	gc = ad_disc_get_GlobalCatalog(ad_ctx,
88	    AD_DISC_PREFER_SITE, &autodisc);
89	if (gc != NULL) {
90		(void) printf("Found a GC:\n");
91		print_ds(gc);
92		free(gc);
93	}
94
95	ad_disc_done(ad_ctx);
96	ad_disc_fini(ad_ctx);
97	ad_ctx = NULL;
98
99	return (0);
100}
101
102void
103print_ds(ad_disc_ds_t *ds)
104{
105	char buf[64];
106
107	for (; ds->host[0] != '\0'; ds++) {
108		const char *p;
109
110		(void) printf("Name: %s\n", ds->host);
111		(void) printf(" flags: 0x%X\n", ds->flags);
112		if (ds->addr.ss_family == AF_INET) {
113			struct sockaddr_in *sin;
114			sin = (struct sockaddr_in *)&ds->addr;
115			p = inet_ntop(AF_INET, &sin->sin_addr,
116			    buf, sizeof (buf));
117			if (p == NULL)
118				p = "?";
119			(void) printf(" A %s %d\n", p, ds->port);
120		}
121		if (ds->addr.ss_family == AF_INET6) {
122			struct sockaddr_in6 *sin6;
123			sin6 = (struct sockaddr_in6 *)&ds->addr;
124			p = inet_ntop(AF_INET6, &sin6->sin6_addr,
125			    buf, sizeof (buf));
126			if (p == NULL)
127				p = "?";
128			(void) printf(" AAAA %s %d\n", p, ds->port);
129		}
130	}
131}
132
133/* printflike */
134void
135mylogger(int pri, const char *format, ...)
136{
137	_NOTE(ARGUNUSED(pri))
138	va_list args;
139
140	va_start(args, format);
141	(void) vfprintf(stderr, format, args);
142	(void) fprintf(stderr, "\n");
143	va_end(args);
144}
145
146/*
147 * This is a unit-test program.  Always enable libumem debugging.
148 */
149const char *
150_umem_debug_init(void)
151{
152	return ("default,verbose"); /* $UMEM_DEBUG setting */
153}
154
155const char *
156_umem_logging_init(void)
157{
158	return ("fail,contents"); /* $UMEM_LOGGING setting */
159}
160