1/*	$NetBSD: feature-test.c,v 1.12 2024/02/21 22:51:17 christos Exp $	*/
2
3/*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 *
12 * See the COPYRIGHT file distributed with this work for additional
13 * information regarding copyright ownership.
14 */
15
16#include <limits.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
21
22#include <isc/md.h>
23#include <isc/net.h>
24#include <isc/print.h>
25#include <isc/util.h>
26
27#include <dns/edns.h>
28
29static void
30usage(void) {
31	fprintf(stderr, "usage: feature-test <arg>\n");
32	fprintf(stderr, "args:\n");
33	fprintf(stderr, "\t--edns-version\n");
34	fprintf(stderr, "\t--enable-dnsrps\n");
35	fprintf(stderr, "\t--enable-dnstap\n");
36	fprintf(stderr, "\t--enable-querytrace\n");
37	fprintf(stderr, "\t--gethostname\n");
38	fprintf(stderr, "\t--gssapi\n");
39	fprintf(stderr, "\t--have-geoip2\n");
40	fprintf(stderr, "\t--have-json-c\n");
41	fprintf(stderr, "\t--have-libxml2\n");
42	fprintf(stderr, "\t--ipv6only=no\n");
43	fprintf(stderr, "\t--md5\n");
44	fprintf(stderr, "\t--tsan\n");
45	fprintf(stderr, "\t--with-dlz-filesystem\n");
46	fprintf(stderr, "\t--with-libidn2\n");
47	fprintf(stderr, "\t--with-lmdb\n");
48	fprintf(stderr, "\t--with-libnghttp2\n");
49	fprintf(stderr, "\t--with-zlib\n");
50}
51
52int
53main(int argc, char **argv) {
54	if (argc != 2) {
55		usage();
56		return (1);
57	}
58
59	if (strcmp(argv[1], "--edns-version") == 0) {
60#ifdef DNS_EDNS_VERSION
61		printf("%d\n", DNS_EDNS_VERSION);
62#else  /* ifdef DNS_EDNS_VERSION */
63		printf("0\n");
64#endif /* ifdef DNS_EDNS_VERSION */
65		return (0);
66	}
67
68	if (strcmp(argv[1], "--enable-dnsrps") == 0) {
69#ifdef USE_DNSRPS
70		return (0);
71#else  /* ifdef USE_DNSRPS */
72		return (1);
73#endif /* ifdef USE_DNSRPS */
74	}
75
76	if (strcmp(argv[1], "--enable-dnstap") == 0) {
77#ifdef HAVE_DNSTAP
78		return (0);
79#else  /* ifdef HAVE_DNSTAP */
80		return (1);
81#endif /* ifdef HAVE_DNSTAP */
82	}
83
84	if (strcmp(argv[1], "--enable-querytrace") == 0) {
85#ifdef WANT_QUERYTRACE
86		return (0);
87#else  /* ifdef WANT_QUERYTRACE */
88		return (1);
89#endif /* ifdef WANT_QUERYTRACE */
90	}
91
92	if (strcmp(argv[1], "--gethostname") == 0) {
93		char hostname[_POSIX_HOST_NAME_MAX + 1];
94		int n;
95
96		n = gethostname(hostname, sizeof(hostname));
97		if (n == -1) {
98			perror("gethostname");
99			return (1);
100		}
101		fprintf(stdout, "%s\n", hostname);
102		return (0);
103	}
104
105	if (strcmp(argv[1], "--gssapi") == 0) {
106#if HAVE_GSSAPI
107		return (0);
108#else  /* HAVE_GSSAPI */
109		return (1);
110#endif /* HAVE_GSSAPI */
111	}
112
113	if (strcmp(argv[1], "--have-geoip2") == 0) {
114#ifdef HAVE_GEOIP2
115		return (0);
116#else  /* ifdef HAVE_GEOIP2 */
117		return (1);
118#endif /* ifdef HAVE_GEOIP2 */
119	}
120
121	if (strcmp(argv[1], "--have-json-c") == 0) {
122#ifdef HAVE_JSON_C
123		return (0);
124#else  /* ifdef HAVE_JSON_C */
125		return (1);
126#endif /* ifdef HAVE_JSON_C */
127	}
128
129	if (strcmp(argv[1], "--have-libxml2") == 0) {
130#ifdef HAVE_LIBXML2
131		return (0);
132#else  /* ifdef HAVE_LIBXML2 */
133		return (1);
134#endif /* ifdef HAVE_LIBXML2 */
135	}
136
137	if (strcmp(argv[1], "--tsan") == 0) {
138#if defined(__has_feature)
139#if __has_feature(thread_sanitizer)
140		return (0);
141#endif
142#endif
143#if __SANITIZE_THREAD__
144		return (0);
145#else
146		return (1);
147#endif
148	}
149
150	if (strcmp(argv[1], "--md5") == 0) {
151		unsigned char digest[ISC_MAX_MD_SIZE];
152		const unsigned char test[] = "test";
153		unsigned int size = sizeof(digest);
154
155		if (isc_md(ISC_MD_MD5, test, sizeof(test), digest, &size) ==
156		    ISC_R_SUCCESS)
157		{
158			return (0);
159		} else {
160			return (1);
161		}
162	}
163
164	if (strcmp(argv[1], "--ipv6only=no") == 0) {
165#if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY)
166		int s;
167		int n = -1;
168		int v6only = -1;
169		socklen_t len = sizeof(v6only);
170
171		s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
172		if (s >= 0) {
173			n = getsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
174				       (void *)&v6only, &len);
175			close(s);
176		}
177		return ((n == 0 && v6only == 0) ? 0 : 1);
178#else  /* defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY) */
179		return (1);
180#endif /* defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY) */
181	}
182
183	if (strcmp(argv[1], "--with-dlz-filesystem") == 0) {
184#ifdef DLZ_FILESYSTEM
185		return (0);
186#else  /* ifdef DLZ_FILESYSTEM */
187		return (1);
188#endif /* ifdef DLZ_FILESYSTEM */
189	}
190
191	if (strcmp(argv[1], "--with-libidn2") == 0) {
192#ifdef HAVE_LIBIDN2
193		return (0);
194#else  /* ifdef HAVE_LIBIDN2 */
195		return (1);
196#endif /* ifdef HAVE_LIBIDN2 */
197	}
198
199	if (strcmp(argv[1], "--with-lmdb") == 0) {
200#ifdef HAVE_LMDB
201		return (0);
202#else  /* ifdef HAVE_LMDB */
203		return (1);
204#endif /* ifdef HAVE_LMDB */
205	}
206
207	if (strcmp(argv[1], "--with-libnghttp2") == 0) {
208#ifdef HAVE_LIBNGHTTP2
209		return (0);
210#else  /* ifdef HAVE_LIBNGHTTP2 */
211		return (1);
212#endif /* ifdef HAVE_LIBNGHTTP2 */
213	}
214
215	if (strcmp(argv[1], "--with-zlib") == 0) {
216#ifdef HAVE_ZLIB
217		return (0);
218#else  /* ifdef HAVE_ZLIB */
219		return (1);
220#endif /* ifdef HAVE_ZLIB */
221	}
222
223	fprintf(stderr, "unknown arg: %s\n", argv[1]);
224	usage();
225	return (1);
226}
227