1#! /bin/bash
2# This is not an autoconf generated configure
3#
4INCLUDE=${1:-"$PWD/include"}
5
6# Output file which is input to Makefile
7CONFIG=config.mk
8
9# Make a temp directory in build tree.
10TMPDIR=$(mktemp -d config.XXXXXX)
11trap 'status=$?; rm -rf $TMPDIR; exit $status' EXIT HUP INT QUIT TERM
12
13check_prog()
14{
15    echo -n "$2"
16    command -v $1 >/dev/null 2>&1 && (echo "$3:=y" >> $CONFIG; echo "yes") || (echo "no"; return 1)
17}
18
19check_docs()
20{
21    if check_prog latex " latex: " HAVE_LATEX; then
22        check_prog pdflatex " pdflatex: " HAVE_PDFLATEX || echo " WARNING: no PDF docs can be built from LaTeX files"
23        check_prog sgml2latex " sgml2latex: " HAVE_SGML2LATEX || echo " WARNING: no LaTeX files can be build from SGML files"
24    else
25        echo " WARNING: no docs can be built from LaTeX files"
26    fi
27
28    check_prog sgml2html " sgml2html: " HAVE_SGML2HTML || echo " WARNING: no HTML docs can be built from SGML"
29}
30
31check_toolchain()
32{
33    : ${PKG_CONFIG:=pkg-config}
34    : ${AR=ar}
35    : ${CC=gcc}
36    echo "PKG_CONFIG:=${PKG_CONFIG}" >>$CONFIG
37    echo "AR:=${AR}" >>$CONFIG
38    echo "CC:=${CC}" >>$CONFIG
39}
40
41check_atm()
42{
43    cat >$TMPDIR/atmtest.c <<EOF
44#include <atm.h>
45int main(int argc, char **argv) {
46	struct atm_qos qos;
47	(void) text2qos("aal5,ubr:sdu=9180,rx:none",&qos,0);
48	return 0;
49}
50EOF
51
52    $CC -I$INCLUDE -o $TMPDIR/atmtest $TMPDIR/atmtest.c -latm >/dev/null 2>&1
53    if [ $? -eq 0 ]
54    then
55	echo "TC_CONFIG_ATM:=y" >>$CONFIG
56	echo yes
57    else
58	echo no
59    fi
60    rm -f $TMPDIR/atmtest.c $TMPDIR/atmtest
61}
62
63check_xtables()
64{
65	if ! ${PKG_CONFIG} xtables --exists
66	then
67		echo "TC_CONFIG_NO_XT:=y" >>$CONFIG
68	fi
69}
70
71check_xt()
72{
73    #check if we have xtables from iptables >= 1.4.5.
74    cat >$TMPDIR/ipttest.c <<EOF
75#include <xtables.h>
76#include <linux/netfilter.h>
77static struct xtables_globals test_globals = {
78	.option_offset = 0,
79	.program_name = "tc-ipt",
80	.program_version = XTABLES_VERSION,
81	.orig_opts = NULL,
82	.opts = NULL,
83	.exit_err = NULL,
84};
85
86int main(int argc, char **argv)
87{
88	xtables_init_all(&test_globals, NFPROTO_IPV4);
89	return 0;
90}
91EOF
92
93    if $CC -I$INCLUDE $IPTC -o $TMPDIR/ipttest $TMPDIR/ipttest.c $IPTL \
94	$(${PKG_CONFIG} xtables --cflags --libs) -ldl >/dev/null 2>&1
95    then
96	echo "TC_CONFIG_XT:=y" >>$CONFIG
97	echo "using xtables"
98    fi
99    rm -f $TMPDIR/ipttest.c $TMPDIR/ipttest
100}
101
102check_xt_old()
103{
104    # bail if previous XT checks has already succeded.
105    if grep -q TC_CONFIG_XT $CONFIG
106    then
107	return
108    fi
109
110    #check if we dont need our internal header ..
111    cat >$TMPDIR/ipttest.c <<EOF
112#include <xtables.h>
113char *lib_dir;
114unsigned int global_option_offset = 0;
115const char *program_version = XTABLES_VERSION;
116const char *program_name = "tc-ipt";
117struct afinfo afinfo = {
118	.libprefix      = "libxt_",
119};
120
121void exit_error(enum exittype status, const char *msg, ...)
122{
123}
124
125int main(int argc, char **argv) {
126
127	return 0;
128}
129
130EOF
131
132    $CC -I$INCLUDE $IPTC -o $TMPDIR/ipttest $TMPDIR/ipttest.c $IPTL -ldl >/dev/null 2>&1
133    if [ $? -eq 0 ]
134    then
135	echo "TC_CONFIG_XT_OLD:=y" >>$CONFIG
136	echo "using old xtables (no need for xt-internal.h)"
137    fi
138    rm -f $TMPDIR/ipttest.c $TMPDIR/ipttest
139}
140
141check_xt_old_internal_h()
142{
143    # bail if previous XT checks has already succeded.
144    if grep -q TC_CONFIG_XT $CONFIG
145    then
146	return
147    fi
148
149    #check if we need our own internal.h
150    cat >$TMPDIR/ipttest.c <<EOF
151#include <xtables.h>
152#include "xt-internal.h"
153char *lib_dir;
154unsigned int global_option_offset = 0;
155const char *program_version = XTABLES_VERSION;
156const char *program_name = "tc-ipt";
157struct afinfo afinfo = {
158	.libprefix      = "libxt_",
159};
160
161void exit_error(enum exittype status, const char *msg, ...)
162{
163}
164
165int main(int argc, char **argv) {
166
167	return 0;
168}
169
170EOF
171	$CC -I$INCLUDE $IPTC -o $TMPDIR/ipttest $TMPDIR/ipttest.c $IPTL -ldl >/dev/null 2>&1
172
173	if [ $? -eq 0 ]
174	then
175	    echo "using old xtables with xt-internal.h"
176	    echo "TC_CONFIG_XT_OLD_H:=y" >>$CONFIG
177	fi
178	rm -f $TMPDIR/ipttest.c $TMPDIR/ipttest
179}
180
181check_ipt()
182{
183	if ! grep TC_CONFIG_XT $CONFIG > /dev/null
184	then
185		echo "using iptables"
186	fi
187}
188
189check_ipt_lib_dir()
190{
191	IPT_LIB_DIR=$(${PKG_CONFIG} --variable=xtlibdir xtables)
192	if [ -n "$IPT_LIB_DIR" ]; then
193		echo $IPT_LIB_DIR
194		echo "IPT_LIB_DIR:=$IPT_LIB_DIR" >> $CONFIG
195		return
196	fi
197
198	for dir in /lib /usr/lib /usr/local/lib
199	do
200		for file in $dir/{xtables,iptables}/lib*t_*so ; do
201			if [ -f $file ]; then
202				echo ${file%/*}
203				echo "IPT_LIB_DIR:=${file%/*}" >> $CONFIG
204				return
205			fi
206		done
207	done
208	echo "not found!"
209}
210
211check_setns()
212{
213    cat >$TMPDIR/setnstest.c <<EOF
214#include <sched.h>
215int main(int argc, char **argv)
216{
217	(void)setns(0,0);
218	return 0;
219}
220EOF
221    $CC -I$INCLUDE -o $TMPDIR/setnstest $TMPDIR/setnstest.c >/dev/null 2>&1
222    if [ $? -eq 0 ]
223    then
224	echo "IP_CONFIG_SETNS:=y" >>$CONFIG
225	echo "yes"
226	echo "CFLAGS += -DHAVE_SETNS" >>$CONFIG
227    else
228	echo "no"
229    fi
230    rm -f $TMPDIR/setnstest.c $TMPDIR/setnstest
231}
232
233check_ipset()
234{
235    cat >$TMPDIR/ipsettest.c <<EOF
236#include <linux/netfilter/ipset/ip_set.h>
237#ifndef IP_SET_INVALID
238#define IPSET_DIM_MAX 3
239typedef unsigned short ip_set_id_t;
240#endif
241#include <linux/netfilter/xt_set.h>
242
243struct xt_set_info info;
244#if IPSET_PROTOCOL == 6
245int main(void)
246{
247	return IPSET_MAXNAMELEN;
248}
249#else
250#error unknown ipset version
251#endif
252EOF
253
254    if $CC -I$INCLUDE -o $TMPDIR/ipsettest $TMPDIR/ipsettest.c >/dev/null 2>&1
255    then
256	echo "TC_CONFIG_IPSET:=y" >>$CONFIG
257	echo "yes"
258    else
259	echo "no"
260    fi
261    rm -f $TMPDIR/ipsettest.c $TMPDIR/ipsettest
262}
263
264check_elf()
265{
266    if ${PKG_CONFIG} libelf --exists
267    then
268	echo "HAVE_ELF:=y" >>$CONFIG
269	echo "yes"
270
271	echo 'CFLAGS += -DHAVE_ELF' `${PKG_CONFIG} libelf --cflags` >> $CONFIG
272	echo 'LDLIBS += ' `${PKG_CONFIG} libelf --libs` >>$CONFIG
273    else
274	echo "no"
275    fi
276}
277
278check_selinux()
279# SELinux is a compile time option in the ss utility
280{
281	if ${PKG_CONFIG} libselinux --exists
282	then
283		echo "HAVE_SELINUX:=y" >>$CONFIG
284		echo "yes"
285
286		echo 'LDLIBS +=' `${PKG_CONFIG} --libs libselinux` >>$CONFIG
287		echo 'CFLAGS += -DHAVE_SELINUX' `${PKG_CONFIG} --cflags libselinux` >>$CONFIG
288	else
289		echo "no"
290	fi
291}
292
293check_mnl()
294{
295	if ${PKG_CONFIG} libmnl --exists
296	then
297		echo "HAVE_MNL:=y" >>$CONFIG
298		echo "yes"
299
300		echo 'CFLAGS += -DHAVE_LIBMNL' `${PKG_CONFIG} libmnl --cflags` >>$CONFIG
301		echo 'LDLIBS +=' `${PKG_CONFIG} libmnl --libs` >> $CONFIG
302	else
303		echo "no"
304	fi
305}
306
307check_berkeley_db()
308{
309    cat >$TMPDIR/dbtest.c <<EOF
310#include <fcntl.h>
311#include <stdlib.h>
312#include <db_185.h>
313int main(int argc, char **argv) {
314	dbopen("/tmp/xxx_test_db.db", O_CREAT|O_RDWR, 0644, DB_HASH, NULL);
315	return 0;
316}
317EOF
318    $CC -I$INCLUDE -o $TMPDIR/dbtest $TMPDIR/dbtest.c -ldb >/dev/null 2>&1
319    if [ $? -eq 0 ]
320    then
321	echo "HAVE_BERKELEY_DB:=y" >>$CONFIG
322	echo "yes"
323    else
324	echo "no"
325    fi
326    rm -f $TMPDIR/dbtest.c $TMPDIR/dbtest
327}
328
329check_strlcpy()
330{
331    cat >$TMPDIR/strtest.c <<EOF
332#include <string.h>
333int main(int argc, char **argv) {
334	char dst[10];
335	strlcpy(dst, "test", sizeof(dst));
336	return 0;
337}
338EOF
339    $CC -I$INCLUDE -o $TMPDIR/strtest $TMPDIR/strtest.c >/dev/null 2>&1
340    if [ $? -eq 0 ]
341    then
342	echo "no"
343    else
344	echo 'CFLAGS += -DNEED_STRLCPY' >>$CONFIG
345	echo "yes"
346    fi
347    rm -f $TMPDIR/strtest.c $TMPDIR/strtest
348}
349
350quiet_config()
351{
352	cat <<EOF
353# user can control verbosity similar to kernel builds (e.g., V=1)
354ifeq ("\$(origin V)", "command line")
355  VERBOSE = \$(V)
356endif
357ifndef VERBOSE
358  VERBOSE = 0
359endif
360ifeq (\$(VERBOSE),1)
361  Q =
362else
363  Q = @
364endif
365
366ifeq (\$(VERBOSE), 0)
367    QUIET_CC       = @echo '    CC       '\$@;
368    QUIET_AR       = @echo '    AR       '\$@;
369    QUIET_LINK     = @echo '    LINK     '\$@;
370    QUIET_YACC     = @echo '    YACC     '\$@;
371    QUIET_LEX      = @echo '    LEX      '\$@;
372endif
373EOF
374}
375
376echo "# Generated config based on" $INCLUDE >$CONFIG
377quiet_config >> $CONFIG
378
379check_toolchain
380
381echo "TC schedulers"
382
383echo -n " ATM	"
384check_atm
385
386check_xtables
387if ! grep -q TC_CONFIG_NO_XT $CONFIG
388then
389	echo -n " IPT	"
390	check_xt
391	check_xt_old
392	check_xt_old_internal_h
393	check_ipt
394
395	echo -n " IPSET  "
396	check_ipset
397fi
398
399echo
400if ! grep -q TC_CONFIG_NO_XT $CONFIG
401then
402	echo -n "iptables modules directory: "
403	check_ipt_lib_dir
404fi
405
406echo -n "libc has setns: "
407check_setns
408
409echo -n "SELinux support: "
410check_selinux
411
412echo -n "ELF support: "
413check_elf
414
415echo -n "libmnl support: "
416check_mnl
417
418echo -n "Berkeley DB: "
419check_berkeley_db
420
421echo -n "need for strlcpy: "
422check_strlcpy
423
424echo
425echo -n "docs:"
426check_docs
427echo
428
429echo >> $CONFIG
430echo "%.o: %.c" >> $CONFIG
431echo '	$(QUIET_CC)$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<' >> $CONFIG
432