diff options
Diffstat (limited to 'src/tapctl')
-rw-r--r-- | src/tapctl/Makefile.am | 51 | ||||
-rw-r--r-- | src/tapctl/Makefile.in | 831 | ||||
-rw-r--r-- | src/tapctl/basic.h | 66 | ||||
-rw-r--r-- | src/tapctl/error.c | 36 | ||||
-rw-r--r-- | src/tapctl/error.h | 97 | ||||
-rw-r--r-- | src/tapctl/main.c | 444 | ||||
-rw-r--r-- | src/tapctl/tap.c | 1367 | ||||
-rw-r--r-- | src/tapctl/tap.h | 181 | ||||
-rw-r--r-- | src/tapctl/tapctl.exe.manifest | 10 | ||||
-rw-r--r-- | src/tapctl/tapctl.props | 18 | ||||
-rw-r--r-- | src/tapctl/tapctl.vcxproj | 163 | ||||
-rw-r--r-- | src/tapctl/tapctl.vcxproj.filters | 49 | ||||
-rw-r--r-- | src/tapctl/tapctl_resources.rc | 64 |
13 files changed, 3377 insertions, 0 deletions
diff --git a/src/tapctl/Makefile.am b/src/tapctl/Makefile.am new file mode 100644 index 0000000..7ebbbf2 --- /dev/null +++ b/src/tapctl/Makefile.am @@ -0,0 +1,51 @@ +# +# tapctl -- Utility to manipulate TUN/TAP interfaces on Windows +# +# Copyright (C) 2002-2021 OpenVPN Inc <sales@openvpn.net> +# Copyright (C) 2018-2021 Simon Rozman <simon@rozman.si> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 +# as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# + +include $(top_srcdir)/build/ltrc.inc + +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in + +EXTRA_DIST = \ + tapctl.vcxproj \ + tapctl.vcxproj.filters \ + tapctl.props \ + tapctl.exe.manifest + +AM_CPPFLAGS = \ + -I$(top_srcdir)/include -I$(top_srcdir)/src/compat + +AM_CFLAGS = \ + $(TAP_CFLAGS) + +if WIN32 +sbin_PROGRAMS = tapctl +tapctl_CFLAGS = \ + -municode -D_UNICODE \ + -UNTDDI_VERSION -U_WIN32_WINNT \ + -D_WIN32_WINNT=_WIN32_WINNT_VISTA +tapctl_LDADD = -ladvapi32 -lole32 -lsetupapi +endif + +tapctl_SOURCES = \ + basic.h \ + error.c error.h \ + main.c \ + tap.c tap.h \ + tapctl_resources.rc diff --git a/src/tapctl/Makefile.in b/src/tapctl/Makefile.in new file mode 100644 index 0000000..00e3f08 --- /dev/null +++ b/src/tapctl/Makefile.in @@ -0,0 +1,831 @@ +# Makefile.in generated by automake 1.16.2 from Makefile.am. +# @configure_input@ + +# Copyright (C) 1994-2020 Free Software Foundation, Inc. + +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +@SET_MAKE@ + +# +# tapctl -- Utility to manipulate TUN/TAP interfaces on Windows +# +# Copyright (C) 2002-2021 OpenVPN Inc <sales@openvpn.net> +# Copyright (C) 2018-2021 Simon Rozman <simon@rozman.si> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 +# as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# + +# +# OpenVPN -- An application to securely tunnel IP networks +# over a single UDP port, with support for SSL/TLS-based +# session authentication and key exchange, +# packet encryption, packet authentication, and +# packet compression. +# +# Copyright (C) 2008-2012 Alon Bar-Lev <alon.barlev@gmail.com> +# +# Required to build Windows resource file + +VPATH = @srcdir@ +am__is_gnu_make = { \ + if test -z '$(MAKELEVEL)'; then \ + false; \ + elif test -n '$(MAKE_HOST)'; then \ + true; \ + elif test -n '$(MAKE_VERSION)' && test -n '$(CURDIR)'; then \ + true; \ + else \ + false; \ + fi; \ +} +am__make_running_with_option = \ + case $${target_option-} in \ + ?) ;; \ + *) echo "am__make_running_with_option: internal error: invalid" \ + "target option '$${target_option-}' specified" >&2; \ + exit 1;; \ + esac; \ + has_opt=no; \ + sane_makeflags=$$MAKEFLAGS; \ + if $(am__is_gnu_make); then \ + sane_makeflags=$$MFLAGS; \ + else \ + case $$MAKEFLAGS in \ + *\\[\ \ ]*) \ + bs=\\; \ + sane_makeflags=`printf '%s\n' "$$MAKEFLAGS" \ + | sed "s/$$bs$$bs[$$bs $$bs ]*//g"`;; \ + esac; \ + fi; \ + skip_next=no; \ + strip_trailopt () \ + { \ + flg=`printf '%s\n' "$$flg" | sed "s/$$1.*$$//"`; \ + }; \ + for flg in $$sane_makeflags; do \ + test $$skip_next = yes && { skip_next=no; continue; }; \ + case $$flg in \ + *=*|--*) continue;; \ + -*I) strip_trailopt 'I'; skip_next=yes;; \ + -*I?*) strip_trailopt 'I';; \ + -*O) strip_trailopt 'O'; skip_next=yes;; \ + -*O?*) strip_trailopt 'O';; \ + -*l) strip_trailopt 'l'; skip_next=yes;; \ + -*l?*) strip_trailopt 'l';; \ + -[dEDm]) skip_next=yes;; \ + -[JT]) skip_next=yes;; \ + esac; \ + case $$flg in \ + *$$target_option*) has_opt=yes; break;; \ + esac; \ + done; \ + test $$has_opt = yes +am__make_dryrun = (target_option=n; $(am__make_running_with_option)) +am__make_keepgoing = (target_option=k; $(am__make_running_with_option)) +pkgdatadir = $(datadir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkglibexecdir = $(libexecdir)/@PACKAGE@ +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_triplet = @build@ +host_triplet = @host@ +@WIN32_TRUE@sbin_PROGRAMS = tapctl$(EXEEXT) +subdir = src/tapctl +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/m4/ax_emptyarray.m4 \ + $(top_srcdir)/m4/ax_socklen_t.m4 \ + $(top_srcdir)/m4/ax_varargs.m4 $(top_srcdir)/m4/libtool.m4 \ + $(top_srcdir)/m4/ltoptions.m4 $(top_srcdir)/m4/ltsugar.m4 \ + $(top_srcdir)/m4/ltversion.m4 $(top_srcdir)/m4/lt~obsolete.m4 \ + $(top_srcdir)/m4/pkg.m4 $(top_srcdir)/version.m4 \ + $(top_srcdir)/compat.m4 $(top_srcdir)/configure.ac +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +DIST_COMMON = $(srcdir)/Makefile.am $(am__DIST_COMMON) +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = $(top_builddir)/config.h \ + $(top_builddir)/include/openvpn-plugin.h +CONFIG_CLEAN_FILES = +CONFIG_CLEAN_VPATH_FILES = +am__installdirs = "$(DESTDIR)$(sbindir)" +PROGRAMS = $(sbin_PROGRAMS) +am_tapctl_OBJECTS = tapctl-error.$(OBJEXT) tapctl-main.$(OBJEXT) \ + tapctl-tap.$(OBJEXT) tapctl_resources.$(OBJEXT) +tapctl_OBJECTS = $(am_tapctl_OBJECTS) +tapctl_DEPENDENCIES = +AM_V_lt = $(am__v_lt_@AM_V@) +am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) +am__v_lt_0 = --silent +am__v_lt_1 = +tapctl_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CCLD) $(tapctl_CFLAGS) $(CFLAGS) \ + $(AM_LDFLAGS) $(LDFLAGS) -o $@ +AM_V_P = $(am__v_P_@AM_V@) +am__v_P_ = $(am__v_P_@AM_DEFAULT_V@) +am__v_P_0 = false +am__v_P_1 = : +AM_V_GEN = $(am__v_GEN_@AM_V@) +am__v_GEN_ = $(am__v_GEN_@AM_DEFAULT_V@) +am__v_GEN_0 = @echo " GEN " $@; +am__v_GEN_1 = +AM_V_at = $(am__v_at_@AM_V@) +am__v_at_ = $(am__v_at_@AM_DEFAULT_V@) +am__v_at_0 = @ +am__v_at_1 = +DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) -I$(top_builddir)/include +depcomp = $(SHELL) $(top_srcdir)/depcomp +am__maybe_remake_depfiles = depfiles +am__depfiles_remade = ./$(DEPDIR)/tapctl-error.Po \ + ./$(DEPDIR)/tapctl-main.Po ./$(DEPDIR)/tapctl-tap.Po +am__mv = mv -f +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +LTCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) \ + $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \ + $(AM_CFLAGS) $(CFLAGS) +AM_V_CC = $(am__v_CC_@AM_V@) +am__v_CC_ = $(am__v_CC_@AM_DEFAULT_V@) +am__v_CC_0 = @echo " CC " $@; +am__v_CC_1 = +CCLD = $(CC) +LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ + $(AM_LDFLAGS) $(LDFLAGS) -o $@ +AM_V_CCLD = $(am__v_CCLD_@AM_V@) +am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@) +am__v_CCLD_0 = @echo " CCLD " $@; +am__v_CCLD_1 = +SOURCES = $(tapctl_SOURCES) +DIST_SOURCES = $(tapctl_SOURCES) +am__can_run_installinfo = \ + case $$AM_UPDATE_INFO_DIR in \ + n|no|NO) false;; \ + *) (install-info --version) >/dev/null 2>&1;; \ + esac +am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) +# Read a list of newline-separated strings from the standard input, +# and print each of them once, without duplicates. Input order is +# *not* preserved. +am__uniquify_input = $(AWK) '\ + BEGIN { nonempty = 0; } \ + { items[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in items) print i; }; } \ +' +# Make sure the list of sources is unique. This is necessary because, +# e.g., the same source file might be shared among _SOURCES variables +# for different programs/libraries. +am__define_uniq_tagged_files = \ + list='$(am__tagged_files)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | $(am__uniquify_input)` +ETAGS = etags +CTAGS = ctags +am__DIST_COMMON = $(srcdir)/Makefile.in $(top_srcdir)/build/ltrc.inc \ + $(top_srcdir)/depcomp +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = @ACLOCAL@ +AMTAR = @AMTAR@ +AM_DEFAULT_VERBOSITY = @AM_DEFAULT_VERBOSITY@ +AR = @AR@ +AS = @AS@ +AUTOCONF = @AUTOCONF@ +AUTOHEADER = @AUTOHEADER@ +AUTOMAKE = @AUTOMAKE@ +AWK = @AWK@ +CC = @CC@ +CCDEPMODE = @CCDEPMODE@ +CFLAGS = @CFLAGS@ +CMOCKA_CFLAGS = @CMOCKA_CFLAGS@ +CMOCKA_LIBS = @CMOCKA_LIBS@ +CPP = @CPP@ +CPPFLAGS = @CPPFLAGS@ +CYGPATH_W = @CYGPATH_W@ +DEFS = @DEFS@ +DEPDIR = @DEPDIR@ +DLLTOOL = @DLLTOOL@ +DL_LIBS = @DL_LIBS@ +DSYMUTIL = @DSYMUTIL@ +DUMPBIN = @DUMPBIN@ +ECHO_C = @ECHO_C@ +ECHO_N = @ECHO_N@ +ECHO_T = @ECHO_T@ +EGREP = @EGREP@ +ENABLE_UNITTESTS = @ENABLE_UNITTESTS@ +EXEEXT = @EXEEXT@ +FGREP = @FGREP@ +GIT = @GIT@ +GREP = @GREP@ +IFCONFIG = @IFCONFIG@ +INSTALL = @INSTALL@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +IPROUTE = @IPROUTE@ +LD = @LD@ +LDFLAGS = @LDFLAGS@ +LIBOBJS = @LIBOBJS@ +LIBPAM_CFLAGS = @LIBPAM_CFLAGS@ +LIBPAM_LIBS = @LIBPAM_LIBS@ +LIBS = @LIBS@ +LIBTOOL = @LIBTOOL@ +LIPO = @LIPO@ +LN_S = @LN_S@ +LTLIBOBJS = @LTLIBOBJS@ +LT_SYS_LIBRARY_PATH = @LT_SYS_LIBRARY_PATH@ +LZ4_CFLAGS = @LZ4_CFLAGS@ +LZ4_LIBS = @LZ4_LIBS@ +LZO_CFLAGS = @LZO_CFLAGS@ +LZO_LIBS = @LZO_LIBS@ +MAKEINFO = @MAKEINFO@ +MANIFEST_TOOL = @MANIFEST_TOOL@ +MBEDTLS_CFLAGS = @MBEDTLS_CFLAGS@ +MBEDTLS_LIBS = @MBEDTLS_LIBS@ +MKDIR_P = @MKDIR_P@ +NETSTAT = @NETSTAT@ +NM = @NM@ +NMEDIT = @NMEDIT@ +OBJDUMP = @OBJDUMP@ +OBJEXT = @OBJEXT@ +OPENSSL_CFLAGS = @OPENSSL_CFLAGS@ +OPENSSL_LIBS = @OPENSSL_LIBS@ +OPENVPN_VERSION_MAJOR = @OPENVPN_VERSION_MAJOR@ +OPENVPN_VERSION_MINOR = @OPENVPN_VERSION_MINOR@ +OPENVPN_VERSION_PATCH = @OPENVPN_VERSION_PATCH@ +OPTIONAL_CRYPTO_CFLAGS = @OPTIONAL_CRYPTO_CFLAGS@ +OPTIONAL_CRYPTO_LIBS = @OPTIONAL_CRYPTO_LIBS@ +OPTIONAL_DL_LIBS = @OPTIONAL_DL_LIBS@ +OPTIONAL_INOTIFY_CFLAGS = @OPTIONAL_INOTIFY_CFLAGS@ +OPTIONAL_INOTIFY_LIBS = @OPTIONAL_INOTIFY_LIBS@ +OPTIONAL_LZ4_CFLAGS = @OPTIONAL_LZ4_CFLAGS@ +OPTIONAL_LZ4_LIBS = @OPTIONAL_LZ4_LIBS@ +OPTIONAL_LZO_CFLAGS = @OPTIONAL_LZO_CFLAGS@ +OPTIONAL_LZO_LIBS = @OPTIONAL_LZO_LIBS@ +OPTIONAL_PKCS11_HELPER_CFLAGS = @OPTIONAL_PKCS11_HELPER_CFLAGS@ +OPTIONAL_PKCS11_HELPER_LIBS = @OPTIONAL_PKCS11_HELPER_LIBS@ +OPTIONAL_SELINUX_LIBS = @OPTIONAL_SELINUX_LIBS@ +OPTIONAL_SYSTEMD_LIBS = @OPTIONAL_SYSTEMD_LIBS@ +OTOOL = @OTOOL@ +OTOOL64 = @OTOOL64@ +P11KIT_CFLAGS = @P11KIT_CFLAGS@ +P11KIT_LIBS = @P11KIT_LIBS@ +PACKAGE = @PACKAGE@ +PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ +PACKAGE_NAME = @PACKAGE_NAME@ +PACKAGE_STRING = @PACKAGE_STRING@ +PACKAGE_TARNAME = @PACKAGE_TARNAME@ +PACKAGE_URL = @PACKAGE_URL@ +PACKAGE_VERSION = @PACKAGE_VERSION@ +PATH_SEPARATOR = @PATH_SEPARATOR@ +PKCS11_HELPER_CFLAGS = @PKCS11_HELPER_CFLAGS@ +PKCS11_HELPER_LIBS = @PKCS11_HELPER_LIBS@ +PKG_CONFIG = @PKG_CONFIG@ +PKG_CONFIG_LIBDIR = @PKG_CONFIG_LIBDIR@ +PKG_CONFIG_PATH = @PKG_CONFIG_PATH@ +PLUGINDIR = @PLUGINDIR@ +PLUGIN_AUTH_PAM_CFLAGS = @PLUGIN_AUTH_PAM_CFLAGS@ +PLUGIN_AUTH_PAM_LIBS = @PLUGIN_AUTH_PAM_LIBS@ +RANLIB = @RANLIB@ +RC = @RC@ +ROUTE = @ROUTE@ +RST2HTML = @RST2HTML@ +RST2MAN = @RST2MAN@ +SED = @SED@ +SELINUX_LIBS = @SELINUX_LIBS@ +SET_MAKE = @SET_MAKE@ +SHELL = @SHELL@ +SOCKETS_LIBS = @SOCKETS_LIBS@ +STRIP = @STRIP@ +SYSTEMD_ASK_PASSWORD = @SYSTEMD_ASK_PASSWORD@ +SYSTEMD_UNIT_DIR = @SYSTEMD_UNIT_DIR@ +TAP_CFLAGS = @TAP_CFLAGS@ +TAP_WIN_COMPONENT_ID = @TAP_WIN_COMPONENT_ID@ +TAP_WIN_MIN_MAJOR = @TAP_WIN_MIN_MAJOR@ +TAP_WIN_MIN_MINOR = @TAP_WIN_MIN_MINOR@ +TEST_CFLAGS = @TEST_CFLAGS@ +TEST_LDFLAGS = @TEST_LDFLAGS@ +TMPFILES_DIR = @TMPFILES_DIR@ +VERSION = @VERSION@ +abs_builddir = @abs_builddir@ +abs_srcdir = @abs_srcdir@ +abs_top_builddir = @abs_top_builddir@ +abs_top_srcdir = @abs_top_srcdir@ +ac_ct_AR = @ac_ct_AR@ +ac_ct_CC = @ac_ct_CC@ +ac_ct_DUMPBIN = @ac_ct_DUMPBIN@ +am__include = @am__include@ +am__leading_dot = @am__leading_dot@ +am__quote = @am__quote@ +am__tar = @am__tar@ +am__untar = @am__untar@ +bindir = @bindir@ +build = @build@ +build_alias = @build_alias@ +build_cpu = @build_cpu@ +build_os = @build_os@ +build_vendor = @build_vendor@ +builddir = @builddir@ +datadir = @datadir@ +datarootdir = @datarootdir@ +docdir = @docdir@ +dvidir = @dvidir@ +exec_prefix = @exec_prefix@ +host = @host@ +host_alias = @host_alias@ +host_cpu = @host_cpu@ +host_os = @host_os@ +host_vendor = @host_vendor@ +htmldir = @htmldir@ +includedir = @includedir@ +infodir = @infodir@ +install_sh = @install_sh@ +libdir = @libdir@ +libexecdir = @libexecdir@ +libsystemd_CFLAGS = @libsystemd_CFLAGS@ +libsystemd_LIBS = @libsystemd_LIBS@ +localedir = @localedir@ +localstatedir = @localstatedir@ +mandir = @mandir@ +mkdir_p = @mkdir_p@ +oldincludedir = @oldincludedir@ +pdfdir = @pdfdir@ +plugindir = @plugindir@ +prefix = @prefix@ +program_transform_name = @program_transform_name@ +psdir = @psdir@ +runstatedir = @runstatedir@ +sampledir = @sampledir@ +sbindir = @sbindir@ +sharedstatedir = @sharedstatedir@ +srcdir = @srcdir@ +sysconfdir = @sysconfdir@ +systemdunitdir = @systemdunitdir@ +target_alias = @target_alias@ +tmpfilesdir = @tmpfilesdir@ +top_build_prefix = @top_build_prefix@ +top_builddir = @top_builddir@ +top_srcdir = @top_srcdir@ +RCCOMPILE = $(RC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ + $(AM_CPPFLAGS) $(CPPFLAGS) + +LTRCCOMPILE = $(LIBTOOL) --mode=compile --tag=RC $(RCCOMPILE) +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in +EXTRA_DIST = \ + tapctl.vcxproj \ + tapctl.vcxproj.filters \ + tapctl.props \ + tapctl.exe.manifest + +AM_CPPFLAGS = \ + -I$(top_srcdir)/include -I$(top_srcdir)/src/compat + +AM_CFLAGS = \ + $(TAP_CFLAGS) + +@WIN32_TRUE@tapctl_CFLAGS = \ +@WIN32_TRUE@ -municode -D_UNICODE \ +@WIN32_TRUE@ -UNTDDI_VERSION -U_WIN32_WINNT \ +@WIN32_TRUE@ -D_WIN32_WINNT=_WIN32_WINNT_VISTA + +@WIN32_TRUE@tapctl_LDADD = -ladvapi32 -lole32 -lsetupapi +tapctl_SOURCES = \ + basic.h \ + error.c error.h \ + main.c \ + tap.c tap.h \ + tapctl_resources.rc + +all: all-am + +.SUFFIXES: +.SUFFIXES: .c .lo .mc .o .obj .rc +$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(top_srcdir)/build/ltrc.inc $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \ + && { if test -f $@; then exit 0; else break; fi; }; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign src/tapctl/Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --foreign src/tapctl/Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__maybe_remake_depfiles)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__maybe_remake_depfiles);; \ + esac; +$(top_srcdir)/build/ltrc.inc $(am__empty): + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +$(top_srcdir)/configure: $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(am__aclocal_m4_deps): +install-sbinPROGRAMS: $(sbin_PROGRAMS) + @$(NORMAL_INSTALL) + @list='$(sbin_PROGRAMS)'; test -n "$(sbindir)" || list=; \ + if test -n "$$list"; then \ + echo " $(MKDIR_P) '$(DESTDIR)$(sbindir)'"; \ + $(MKDIR_P) "$(DESTDIR)$(sbindir)" || exit 1; \ + fi; \ + for p in $$list; do echo "$$p $$p"; done | \ + sed 's/$(EXEEXT)$$//' | \ + while read p p1; do if test -f $$p \ + || test -f $$p1 \ + ; then echo "$$p"; echo "$$p"; else :; fi; \ + done | \ + sed -e 'p;s,.*/,,;n;h' \ + -e 's|.*|.|' \ + -e 'p;x;s,.*/,,;s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/' | \ + sed 'N;N;N;s,\n, ,g' | \ + $(AWK) 'BEGIN { files["."] = ""; dirs["."] = 1 } \ + { d=$$3; if (dirs[d] != 1) { print "d", d; dirs[d] = 1 } \ + if ($$2 == $$4) files[d] = files[d] " " $$1; \ + else { print "f", $$3 "/" $$4, $$1; } } \ + END { for (d in files) print "f", d, files[d] }' | \ + while read type dir files; do \ + if test "$$dir" = .; then dir=; else dir=/$$dir; fi; \ + test -z "$$files" || { \ + echo " $(INSTALL_PROGRAM_ENV) $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=install $(INSTALL_PROGRAM) $$files '$(DESTDIR)$(sbindir)$$dir'"; \ + $(INSTALL_PROGRAM_ENV) $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=install $(INSTALL_PROGRAM) $$files "$(DESTDIR)$(sbindir)$$dir" || exit $$?; \ + } \ + ; done + +uninstall-sbinPROGRAMS: + @$(NORMAL_UNINSTALL) + @list='$(sbin_PROGRAMS)'; test -n "$(sbindir)" || list=; \ + files=`for p in $$list; do echo "$$p"; done | \ + sed -e 'h;s,^.*/,,;s/$(EXEEXT)$$//;$(transform)' \ + -e 's/$$/$(EXEEXT)/' \ + `; \ + test -n "$$list" || exit 0; \ + echo " ( cd '$(DESTDIR)$(sbindir)' && rm -f" $$files ")"; \ + cd "$(DESTDIR)$(sbindir)" && rm -f $$files + +clean-sbinPROGRAMS: + @list='$(sbin_PROGRAMS)'; test -n "$$list" || exit 0; \ + echo " rm -f" $$list; \ + rm -f $$list || exit $$?; \ + test -n "$(EXEEXT)" || exit 0; \ + list=`for p in $$list; do echo "$$p"; done | sed 's/$(EXEEXT)$$//'`; \ + echo " rm -f" $$list; \ + rm -f $$list + +tapctl$(EXEEXT): $(tapctl_OBJECTS) $(tapctl_DEPENDENCIES) $(EXTRA_tapctl_DEPENDENCIES) + @rm -f tapctl$(EXEEXT) + $(AM_V_CCLD)$(tapctl_LINK) $(tapctl_OBJECTS) $(tapctl_LDADD) $(LIBS) + +mostlyclean-compile: + -rm -f *.$(OBJEXT) + +distclean-compile: + -rm -f *.tab.c + +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tapctl-error.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tapctl-main.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tapctl-tap.Po@am__quote@ # am--include-marker + +$(am__depfiles_remade): + @$(MKDIR_P) $(@D) + @echo '# dummy' >$@-t && $(am__mv) $@-t $@ + +am--depfiles: $(am__depfiles_remade) + +.c.o: +@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ $< + +.c.obj: +@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ `$(CYGPATH_W) '$<'` + +.c.lo: +@am__fastdepCC_TRUE@ $(AM_V_CC)$(LTCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LTCOMPILE) -c -o $@ $< + +tapctl-error.o: error.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(tapctl_CFLAGS) $(CFLAGS) -MT tapctl-error.o -MD -MP -MF $(DEPDIR)/tapctl-error.Tpo -c -o tapctl-error.o `test -f 'error.c' || echo '$(srcdir)/'`error.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/tapctl-error.Tpo $(DEPDIR)/tapctl-error.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='error.c' object='tapctl-error.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(tapctl_CFLAGS) $(CFLAGS) -c -o tapctl-error.o `test -f 'error.c' || echo '$(srcdir)/'`error.c + +tapctl-error.obj: error.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(tapctl_CFLAGS) $(CFLAGS) -MT tapctl-error.obj -MD -MP -MF $(DEPDIR)/tapctl-error.Tpo -c -o tapctl-error.obj `if test -f 'error.c'; then $(CYGPATH_W) 'error.c'; else $(CYGPATH_W) '$(srcdir)/error.c'; fi` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/tapctl-error.Tpo $(DEPDIR)/tapctl-error.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='error.c' object='tapctl-error.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(tapctl_CFLAGS) $(CFLAGS) -c -o tapctl-error.obj `if test -f 'error.c'; then $(CYGPATH_W) 'error.c'; else $(CYGPATH_W) '$(srcdir)/error.c'; fi` + +tapctl-main.o: main.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(tapctl_CFLAGS) $(CFLAGS) -MT tapctl-main.o -MD -MP -MF $(DEPDIR)/tapctl-main.Tpo -c -o tapctl-main.o `test -f 'main.c' || echo '$(srcdir)/'`main.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/tapctl-main.Tpo $(DEPDIR)/tapctl-main.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='main.c' object='tapctl-main.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(tapctl_CFLAGS) $(CFLAGS) -c -o tapctl-main.o `test -f 'main.c' || echo '$(srcdir)/'`main.c + +tapctl-main.obj: main.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(tapctl_CFLAGS) $(CFLAGS) -MT tapctl-main.obj -MD -MP -MF $(DEPDIR)/tapctl-main.Tpo -c -o tapctl-main.obj `if test -f 'main.c'; then $(CYGPATH_W) 'main.c'; else $(CYGPATH_W) '$(srcdir)/main.c'; fi` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/tapctl-main.Tpo $(DEPDIR)/tapctl-main.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='main.c' object='tapctl-main.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(tapctl_CFLAGS) $(CFLAGS) -c -o tapctl-main.obj `if test -f 'main.c'; then $(CYGPATH_W) 'main.c'; else $(CYGPATH_W) '$(srcdir)/main.c'; fi` + +tapctl-tap.o: tap.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(tapctl_CFLAGS) $(CFLAGS) -MT tapctl-tap.o -MD -MP -MF $(DEPDIR)/tapctl-tap.Tpo -c -o tapctl-tap.o `test -f 'tap.c' || echo '$(srcdir)/'`tap.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/tapctl-tap.Tpo $(DEPDIR)/tapctl-tap.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='tap.c' object='tapctl-tap.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(tapctl_CFLAGS) $(CFLAGS) -c -o tapctl-tap.o `test -f 'tap.c' || echo '$(srcdir)/'`tap.c + +tapctl-tap.obj: tap.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(tapctl_CFLAGS) $(CFLAGS) -MT tapctl-tap.obj -MD -MP -MF $(DEPDIR)/tapctl-tap.Tpo -c -o tapctl-tap.obj `if test -f 'tap.c'; then $(CYGPATH_W) 'tap.c'; else $(CYGPATH_W) '$(srcdir)/tap.c'; fi` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/tapctl-tap.Tpo $(DEPDIR)/tapctl-tap.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='tap.c' object='tapctl-tap.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(tapctl_CFLAGS) $(CFLAGS) -c -o tapctl-tap.obj `if test -f 'tap.c'; then $(CYGPATH_W) 'tap.c'; else $(CYGPATH_W) '$(srcdir)/tap.c'; fi` + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs + +ID: $(am__tagged_files) + $(am__define_uniq_tagged_files); mkid -fID $$unique +tags: tags-am +TAGS: tags + +tags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + set x; \ + here=`pwd`; \ + $(am__define_uniq_tagged_files); \ + shift; \ + if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \ + test -n "$$unique" || unique=$$empty_fix; \ + if test $$# -gt 0; then \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + "$$@" $$unique; \ + else \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + $$unique; \ + fi; \ + fi +ctags: ctags-am + +CTAGS: ctags +ctags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + $(am__define_uniq_tagged_files); \ + test -z "$(CTAGS_ARGS)$$unique" \ + || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ + $$unique + +GTAGS: + here=`$(am__cd) $(top_builddir) && pwd` \ + && $(am__cd) $(top_srcdir) \ + && gtags -i $(GTAGS_ARGS) "$$here" +cscopelist: cscopelist-am + +cscopelist-am: $(am__tagged_files) + list='$(am__tagged_files)'; \ + case "$(srcdir)" in \ + [\\/]* | ?:[\\/]*) sdir="$(srcdir)" ;; \ + *) sdir=$(subdir)/$(srcdir) ;; \ + esac; \ + for i in $$list; do \ + if test -f "$$i"; then \ + echo "$(subdir)/$$i"; \ + else \ + echo "$$sdir/$$i"; \ + fi; \ + done >> $(top_builddir)/cscope.files + +distclean-tags: + -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags + +distdir: $(BUILT_SOURCES) + $(MAKE) $(AM_MAKEFLAGS) distdir-am + +distdir-am: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d "$(distdir)/$$file"; then \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \ + else \ + test -f "$(distdir)/$$file" \ + || cp -p $$d/$$file "$(distdir)/$$file" \ + || exit 1; \ + fi; \ + done +check-am: all-am +check: check-am +all-am: Makefile $(PROGRAMS) +installdirs: + for dir in "$(DESTDIR)$(sbindir)"; do \ + test -z "$$dir" || $(MKDIR_P) "$$dir"; \ + done +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + if test -z '$(STRIP)'; then \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + install; \ + else \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'" install; \ + fi +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." + -test -z "$(MAINTAINERCLEANFILES)" || rm -f $(MAINTAINERCLEANFILES) +clean: clean-am + +clean-am: clean-generic clean-libtool clean-sbinPROGRAMS \ + mostlyclean-am + +distclean: distclean-am + -rm -f ./$(DEPDIR)/tapctl-error.Po + -rm -f ./$(DEPDIR)/tapctl-main.Po + -rm -f ./$(DEPDIR)/tapctl-tap.Po + -rm -f Makefile +distclean-am: clean-am distclean-compile distclean-generic \ + distclean-tags + +dvi: dvi-am + +dvi-am: + +html: html-am + +html-am: + +info: info-am + +info-am: + +install-data-am: + +install-dvi: install-dvi-am + +install-dvi-am: + +install-exec-am: install-sbinPROGRAMS + +install-html: install-html-am + +install-html-am: + +install-info: install-info-am + +install-info-am: + +install-man: + +install-pdf: install-pdf-am + +install-pdf-am: + +install-ps: install-ps-am + +install-ps-am: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -f ./$(DEPDIR)/tapctl-error.Po + -rm -f ./$(DEPDIR)/tapctl-main.Po + -rm -f ./$(DEPDIR)/tapctl-tap.Po + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-compile mostlyclean-generic \ + mostlyclean-libtool + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: uninstall-sbinPROGRAMS + +.MAKE: install-am install-strip + +.PHONY: CTAGS GTAGS TAGS all all-am am--depfiles check check-am clean \ + clean-generic clean-libtool clean-sbinPROGRAMS cscopelist-am \ + ctags ctags-am distclean distclean-compile distclean-generic \ + distclean-libtool distclean-tags distdir dvi dvi-am html \ + html-am info info-am install install-am install-data \ + install-data-am install-dvi install-dvi-am install-exec \ + install-exec-am install-html install-html-am install-info \ + install-info-am install-man install-pdf install-pdf-am \ + install-ps install-ps-am install-sbinPROGRAMS install-strip \ + installcheck installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-compile \ + mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \ + tags tags-am uninstall uninstall-am uninstall-sbinPROGRAMS + +.PRECIOUS: Makefile + + +.rc.lo: + $(LTRCCOMPILE) -i "$<" -o "$@" + +.rc.o: + $(RCCOMPILE) -i "$<" -o "$@" + +.mc.rc: + $(WINDMC) "$<" + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/src/tapctl/basic.h b/src/tapctl/basic.h new file mode 100644 index 0000000..3de237d --- /dev/null +++ b/src/tapctl/basic.h @@ -0,0 +1,66 @@ +/* + * basic -- Basic macros + * https://community.openvpn.net/openvpn/wiki/Tapctl + * + * Copyright (C) 2002-2021 OpenVPN Inc <sales@openvpn.net> + * Copyright (C) 2018-2021 Simon Rozman <simon@rozman.si> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef BASIC_H +#define BASIC_H + +#ifdef _UNICODE +#define PRIsLPTSTR "ls" +#define PRIsLPOLESTR "ls" +#else +#define PRIsLPTSTR "s" +#define PRIsLPOLESTR "ls" +#endif +#define PRIXGUID "{%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX}" +#define PRIGUID_PARAM(g) \ + (g).Data1, (g).Data2, (g).Data3, (g).Data4[0], (g).Data4[1], (g).Data4[2], (g).Data4[3], (g).Data4[4], (g).Data4[5], (g).Data4[6], (g).Data4[7] +#define PRIGUID_PARAM_REF(g) \ + &(g).Data1, &(g).Data2, &(g).Data3, &(g).Data4[0], &(g).Data4[1], &(g).Data4[2], &(g).Data4[3], &(g).Data4[4], &(g).Data4[5], &(g).Data4[6], &(g).Data4[7] + +#define __L(q) L ## q +#define _L(q) __L(q) + +#ifndef _In_ +#define _In_ +#endif +#ifndef _In_opt_ +#define _In_opt_ +#endif +#ifndef _In_z_ +#define _In_z_ +#endif +#ifndef _Inout_ +#define _Inout_ +#endif +#ifndef _Inout_opt_ +#define _Inout_opt_ +#endif +#ifndef _Out_ +#define _Out_ +#endif +#ifndef _Out_opt_ +#define _Out_opt_ +#endif +#ifndef _Out_z_cap_ +#define _Out_z_cap_(n) +#endif + +#endif /* ifndef BASIC_H */ diff --git a/src/tapctl/error.c b/src/tapctl/error.c new file mode 100644 index 0000000..16662ec --- /dev/null +++ b/src/tapctl/error.c @@ -0,0 +1,36 @@ +/* + * error -- OpenVPN compatible error reporting API + * https://community.openvpn.net/openvpn/wiki/Tapctl + * + * Copyright (C) 2002-2021 OpenVPN Inc <sales@openvpn.net> + * Copyright (C) 2018-2021 Simon Rozman <simon@rozman.si> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "error.h" + + +/* Globals */ +unsigned int x_debug_level; /* GLOBAL */ + + +void +x_msg(const unsigned int flags, const char *format, ...) +{ + va_list arglist; + va_start(arglist, format); + x_msg_va(flags, format, arglist); + va_end(arglist); +} diff --git a/src/tapctl/error.h b/src/tapctl/error.h new file mode 100644 index 0000000..fa6e3ff --- /dev/null +++ b/src/tapctl/error.h @@ -0,0 +1,97 @@ +/* + * error -- OpenVPN compatible error reporting API + * https://community.openvpn.net/openvpn/wiki/Tapctl + * + * Copyright (C) 2002-2021 OpenVPN Inc <sales@openvpn.net> + * Copyright (C) 2018-2021 Simon Rozman <simon@rozman.si> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef ERROR_H +#define ERROR_H + +#include <stdarg.h> +#include <stdbool.h> +#include <stdlib.h> + +/* + * These globals should not be accessed directly, + * but rather through macros or inline functions defined below. + */ +extern unsigned int x_debug_level; +extern int x_msg_line_num; + +/* msg() flags */ + +#define M_DEBUG_LEVEL (0x0F) /* debug level mask */ + +#define M_FATAL (1<<4) /* exit program */ +#define M_NONFATAL (1<<5) /* non-fatal error */ +#define M_WARN (1<<6) /* call syslog with LOG_WARNING */ +#define M_DEBUG (1<<7) + +#define M_ERRNO (1<<8) /* show errno description */ + +#define M_NOMUTE (1<<11) /* don't do mute processing */ +#define M_NOPREFIX (1<<12) /* don't show date/time prefix */ +#define M_USAGE_SMALL (1<<13) /* fatal options error, call usage_small */ +#define M_MSG_VIRT_OUT (1<<14) /* output message through msg_status_output callback */ +#define M_OPTERR (1<<15) /* print "Options error:" prefix */ +#define M_NOLF (1<<16) /* don't print new line */ +#define M_NOIPREFIX (1<<17) /* don't print instance prefix */ + +/* flag combinations which are frequently used */ +#define M_ERR (M_FATAL | M_ERRNO) +#define M_USAGE (M_USAGE_SMALL | M_NOPREFIX | M_OPTERR) +#define M_CLIENT (M_MSG_VIRT_OUT | M_NOMUTE | M_NOIPREFIX) + + +/** Check muting filter */ +bool dont_mute(unsigned int flags); + +/* Macro to ensure (and teach static analysis tools) we exit on fatal errors */ +#ifdef _MSC_VER +#pragma warning(disable: 4127) /* EXIT_FATAL(flags) macro raises "warning C4127: conditional expression is constant" on each non M_FATAL invocation. */ +#endif +#define EXIT_FATAL(flags) do { if ((flags) & M_FATAL) {_exit(1);}} while (false) + +#define HAVE_VARARG_MACROS +#define msg(flags, ...) do { if (msg_test(flags)) {x_msg((flags), __VA_ARGS__);} EXIT_FATAL(flags); } while (false) +#ifdef ENABLE_DEBUG +#define dmsg(flags, ...) do { if (msg_test(flags)) {x_msg((flags), __VA_ARGS__);} EXIT_FATAL(flags); } while (false) +#else +#define dmsg(flags, ...) +#endif + +void x_msg(const unsigned int flags, const char *format, ...); /* should be called via msg above */ + +void x_msg_va(const unsigned int flags, const char *format, va_list arglist); + +/* Inline functions */ + +static inline bool +check_debug_level(unsigned int level) +{ + return (level & M_DEBUG_LEVEL) <= x_debug_level; +} + +/** Return true if flags represent and enabled, not muted log level */ +static inline bool +msg_test(unsigned int flags) +{ + return check_debug_level(flags) && dont_mute(flags); +} + +#endif /* ifndef ERROR_H */ diff --git a/src/tapctl/main.c b/src/tapctl/main.c new file mode 100644 index 0000000..73ec40b --- /dev/null +++ b/src/tapctl/main.c @@ -0,0 +1,444 @@ +/* + * tapctl -- Utility to manipulate TUN/TAP adapters on Windows + * https://community.openvpn.net/openvpn/wiki/Tapctl + * + * Copyright (C) 2002-2021 OpenVPN Inc <sales@openvpn.net> + * Copyright (C) 2018-2021 Simon Rozman <simon@rozman.si> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#elif defined(_MSC_VER) +#include <config-msvc.h> +#endif +#ifdef HAVE_CONFIG_VERSION_H +#include <config-version.h> +#endif + +#include "tap.h" +#include "error.h" + +#include <objbase.h> +#include <setupapi.h> +#include <stdio.h> +#include <tchar.h> + +#ifdef _MSC_VER +#pragma comment(lib, "ole32.lib") +#pragma comment(lib, "setupapi.lib") +#endif + + +const TCHAR title_string[] = + TEXT(PACKAGE_NAME) TEXT(" ") TEXT(PACKAGE_VERSION) + TEXT(" built on ") TEXT(__DATE__) +; + +static const TCHAR usage_message[] = + TEXT("%s\n") + TEXT("\n") + TEXT("Usage:\n") + TEXT("\n") + TEXT("tapctl <command> [<command specific options>]\n") + TEXT("\n") + TEXT("Commands:\n") + TEXT("\n") + TEXT("create Create a new TUN/TAP adapter\n") + TEXT("list List TUN/TAP adapters\n") + TEXT("delete Delete specified network adapter\n") + TEXT("help Display this text\n") + TEXT("\n") + TEXT("Hint: Use \"tapctl help <command>\" to display help for particular command.\n") +; + +static const TCHAR usage_message_create[] = + TEXT("%s\n") + TEXT("\n") + TEXT("Creates a new TUN/TAP adapter\n") + TEXT("\n") + TEXT("Usage:\n") + TEXT("\n") + TEXT("tapctl create [<options>]\n") + TEXT("\n") + TEXT("Options:\n") + TEXT("\n") + TEXT("--name <name> Set TUN/TAP adapter name. Should the adapter with given name \n") + TEXT(" already exist, an error is returned. If this option is not \n") + TEXT(" specified, a default adapter name is chosen by Windows. \n") + TEXT(" Note: This name can also be specified as OpenVPN's --dev-node \n") + TEXT(" option. \n") + TEXT("--hwid <hwid> Adapter hardware ID. Default value is root\\tap0901, which \n") + TEXT(" describes tap-windows6 driver. To work with wintun driver, \n") + TEXT(" specify 'wintun'. \n") + TEXT("\n") + TEXT("Output:\n") + TEXT("\n") + TEXT("This command prints newly created TUN/TAP adapter's GUID to stdout. \n") +; + +static const TCHAR usage_message_list[] = + TEXT("%s\n") + TEXT("\n") + TEXT("Lists TUN/TAP adapters\n") + TEXT("\n") + TEXT("Usage:\n") + TEXT("\n") + TEXT("tapctl list\n") + TEXT("\n") + TEXT("Options:\n") + TEXT("\n") + TEXT("--hwid <hwid> Adapter hardware ID. By default, root\\tap0901, tap0901 and \n") + TEXT(" wintun adapters are listed. Use this switch to limit the list. \n") + TEXT("\n") + TEXT("Output:\n") + TEXT("\n") + TEXT("This command prints all TUN/TAP adapters to stdout. \n") +; + +static const TCHAR usage_message_delete[] = + TEXT("%s\n") + TEXT("\n") + TEXT("Deletes the specified network adapter\n") + TEXT("\n") + TEXT("Usage:\n") + TEXT("\n") + TEXT("tapctl delete <adapter GUID | adapter name>\n") +; + + +/** + * Print the help message. + */ +static void +usage(void) +{ + _ftprintf(stderr, + usage_message, + title_string); +} + + +/** + * Program entry point + */ +int __cdecl +_tmain(int argc, LPCTSTR argv[]) +{ + int iResult; + BOOL bRebootRequired = FALSE; + + /* Ask SetupAPI to keep quiet. */ + SetupSetNonInteractiveMode(TRUE); + + if (argc < 2) + { + usage(); + return 1; + } + else if (_tcsicmp(argv[1], TEXT("help")) == 0) + { + /* Output help. */ + if (argc < 3) + { + usage(); + } + else if (_tcsicmp(argv[2], TEXT("create")) == 0) + { + _ftprintf(stderr, usage_message_create, title_string); + } + else if (_tcsicmp(argv[2], TEXT("list")) == 0) + { + _ftprintf(stderr, usage_message_list, title_string); + } + else if (_tcsicmp(argv[2], TEXT("delete")) == 0) + { + _ftprintf(stderr, usage_message_delete, title_string); + } + else + { + _ftprintf(stderr, TEXT("Unknown command \"%s\". Please, use \"tapctl help\" to list supported commands.\n"), argv[2]); + } + + return 1; + } + else if (_tcsicmp(argv[1], TEXT("create")) == 0) + { + LPCTSTR szName = NULL; + LPCTSTR szHwId = TEXT("root\\") TEXT(TAP_WIN_COMPONENT_ID); + + /* Parse options. */ + for (int i = 2; i < argc; i++) + { + if (_tcsicmp(argv[i], TEXT("--name")) == 0) + { + szName = argv[++i]; + } + else + if (_tcsicmp(argv[i], TEXT("--hwid")) == 0) + { + szHwId = argv[++i]; + } + else + { + _ftprintf(stderr, TEXT("Unknown option \"%s\". Please, use \"tapctl help create\" to list supported options. Ignored.\n"), argv[i]); + } + } + + /* Create TUN/TAP adapter. */ + GUID guidAdapter; + LPOLESTR szAdapterId = NULL; + DWORD dwResult = tap_create_adapter( + NULL, + TEXT("Virtual Ethernet"), + szHwId, + &bRebootRequired, + &guidAdapter); + if (dwResult != ERROR_SUCCESS) + { + _ftprintf(stderr, TEXT("Creating TUN/TAP adapter failed (error 0x%x).\n"), dwResult); + iResult = 1; goto quit; + } + + if (szName) + { + /* Get existing network adapters. */ + struct tap_adapter_node *pAdapterList = NULL; + dwResult = tap_list_adapters(NULL, NULL, &pAdapterList); + if (dwResult != ERROR_SUCCESS) + { + _ftprintf(stderr, TEXT("Enumerating adapters failed (error 0x%x).\n"), dwResult); + iResult = 1; goto create_delete_adapter; + } + + /* Check for duplicates. */ + for (struct tap_adapter_node *pAdapter = pAdapterList; pAdapter; pAdapter = pAdapter->pNext) + { + if (_tcsicmp(szName, pAdapter->szName) == 0) + { + StringFromIID((REFIID)&pAdapter->guid, &szAdapterId); + _ftprintf(stderr, TEXT("Adapter \"%s\" already exists (GUID %") TEXT(PRIsLPOLESTR) TEXT(").\n"), pAdapter->szName, szAdapterId); + CoTaskMemFree(szAdapterId); + iResult = 1; goto create_cleanup_pAdapterList; + } + } + + /* Rename the adapter. */ + dwResult = tap_set_adapter_name(&guidAdapter, szName, FALSE); + if (dwResult != ERROR_SUCCESS) + { + StringFromIID((REFIID)&guidAdapter, &szAdapterId); + _ftprintf(stderr, TEXT("Renaming TUN/TAP adapter %") TEXT(PRIsLPOLESTR) TEXT(" to \"%s\" failed (error 0x%x).\n"), szAdapterId, szName, dwResult); + CoTaskMemFree(szAdapterId); + iResult = 1; goto quit; + } + + iResult = 0; + +create_cleanup_pAdapterList: + tap_free_adapter_list(pAdapterList); + if (iResult) + { + goto create_delete_adapter; + } + } + + /* Output adapter GUID. */ + StringFromIID((REFIID)&guidAdapter, &szAdapterId); + _ftprintf(stdout, TEXT("%") TEXT(PRIsLPOLESTR) TEXT("\n"), szAdapterId); + CoTaskMemFree(szAdapterId); + + iResult = 0; goto quit; + +create_delete_adapter: + tap_delete_adapter( + NULL, + &guidAdapter, + &bRebootRequired); + iResult = 1; goto quit; + } + else if (_tcsicmp(argv[1], TEXT("list")) == 0) + { + TCHAR szzHwId[0x100] = + TEXT("root\\") TEXT(TAP_WIN_COMPONENT_ID) TEXT("\0") + TEXT(TAP_WIN_COMPONENT_ID) TEXT("\0") + TEXT("Wintun\0"); + + /* Parse options. */ + for (int i = 2; i < argc; i++) + { + if (_tcsicmp(argv[i], TEXT("--hwid")) == 0) + { + memset(szzHwId, 0, sizeof(szzHwId)); + ++i; + memcpy_s(szzHwId, sizeof(szzHwId) - 2*sizeof(TCHAR) /*requires double zero termination*/, argv[i], _tcslen(argv[i])*sizeof(TCHAR)); + } + else + { + _ftprintf(stderr, TEXT("Unknown option \"%s\". Please, use \"tapctl help list\" to list supported options. Ignored.\n"), argv[i]); + } + } + + /* Output list of adapters with given hardware ID. */ + struct tap_adapter_node *pAdapterList = NULL; + DWORD dwResult = tap_list_adapters(NULL, szzHwId, &pAdapterList); + if (dwResult != ERROR_SUCCESS) + { + _ftprintf(stderr, TEXT("Enumerating TUN/TAP adapters failed (error 0x%x).\n"), dwResult); + iResult = 1; goto quit; + } + + for (struct tap_adapter_node *pAdapter = pAdapterList; pAdapter; pAdapter = pAdapter->pNext) + { + LPOLESTR szAdapterId = NULL; + StringFromIID((REFIID)&pAdapter->guid, &szAdapterId); + _ftprintf(stdout, TEXT("%") TEXT(PRIsLPOLESTR) TEXT("\t%") TEXT(PRIsLPTSTR) TEXT("\n"), szAdapterId, pAdapter->szName); + CoTaskMemFree(szAdapterId); + } + + iResult = 0; + tap_free_adapter_list(pAdapterList); + } + else if (_tcsicmp(argv[1], TEXT("delete")) == 0) + { + if (argc < 3) + { + _ftprintf(stderr, TEXT("Missing adapter GUID or name. Please, use \"tapctl help delete\" for usage info.\n")); + return 1; + } + + GUID guidAdapter; + if (FAILED(IIDFromString(argv[2], (LPIID)&guidAdapter))) + { + /* The argument failed to covert to GUID. Treat it as the adapter name. */ + struct tap_adapter_node *pAdapterList = NULL; + DWORD dwResult = tap_list_adapters(NULL, NULL, &pAdapterList); + if (dwResult != ERROR_SUCCESS) + { + _ftprintf(stderr, TEXT("Enumerating TUN/TAP adapters failed (error 0x%x).\n"), dwResult); + iResult = 1; goto quit; + } + + for (struct tap_adapter_node *pAdapter = pAdapterList;; pAdapter = pAdapter->pNext) + { + if (pAdapter == NULL) + { + _ftprintf(stderr, TEXT("\"%s\" adapter not found.\n"), argv[2]); + iResult = 1; goto delete_cleanup_pAdapterList; + } + else if (_tcsicmp(argv[2], pAdapter->szName) == 0) + { + memcpy(&guidAdapter, &pAdapter->guid, sizeof(GUID)); + break; + } + } + + iResult = 0; + +delete_cleanup_pAdapterList: + tap_free_adapter_list(pAdapterList); + if (iResult) + { + goto quit; + } + } + + /* Delete the network adapter. */ + DWORD dwResult = tap_delete_adapter( + NULL, + &guidAdapter, + &bRebootRequired); + if (dwResult != ERROR_SUCCESS) + { + _ftprintf(stderr, TEXT("Deleting adapter \"%s\" failed (error 0x%x).\n"), argv[2], dwResult); + iResult = 1; goto quit; + } + + iResult = 0; goto quit; + } + else + { + _ftprintf(stderr, TEXT("Unknown command \"%s\". Please, use \"tapctl help\" to list supported commands.\n"), argv[1]); + return 1; + } + +quit: + if (bRebootRequired) + { + _ftprintf(stderr, TEXT("A system reboot is required.\n")); + } + + return iResult; +} + + +bool +dont_mute(unsigned int flags) +{ + UNREFERENCED_PARAMETER(flags); + + return true; +} + + +void +x_msg_va(const unsigned int flags, const char *format, va_list arglist) +{ + /* Output message string. Note: Message strings don't contain line terminators. */ + vfprintf(stderr, format, arglist); + _ftprintf(stderr, TEXT("\n")); + + if ((flags & M_ERRNO) != 0) + { + /* Output system error message (if possible). */ + DWORD dwResult = GetLastError(); + LPTSTR szErrMessage = NULL; + if (FormatMessage( + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, + 0, + dwResult, + 0, + (LPTSTR)&szErrMessage, + 0, + NULL) && szErrMessage) + { + /* Trim trailing whitespace. Set terminator after the last non-whitespace character. This prevents excessive trailing line breaks. */ + for (size_t i = 0, i_last = 0;; i++) + { + if (szErrMessage[i]) + { + if (!_istspace(szErrMessage[i])) + { + i_last = i + 1; + } + } + else + { + szErrMessage[i_last] = 0; + break; + } + } + + /* Output error message. */ + _ftprintf(stderr, TEXT("Error 0x%x: %s\n"), dwResult, szErrMessage); + + LocalFree(szErrMessage); + } + else + { + _ftprintf(stderr, TEXT("Error 0x%x\n"), dwResult); + } + } +} diff --git a/src/tapctl/tap.c b/src/tapctl/tap.c new file mode 100644 index 0000000..dd4a10a --- /dev/null +++ b/src/tapctl/tap.c @@ -0,0 +1,1367 @@ +/* + * tapctl -- Utility to manipulate TUN/TAP adapters on Windows + * https://community.openvpn.net/openvpn/wiki/Tapctl + * + * Copyright (C) 2018-2020 Simon Rozman <simon@rozman.si> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#elif defined(_MSC_VER) +#include <config-msvc.h> +#endif + +#include "tap.h" +#include "error.h" + +#include <windows.h> +#include <cfgmgr32.h> +#include <objbase.h> +#include <setupapi.h> +#include <stdio.h> +#include <tchar.h> +#include <newdev.h> + +#ifdef _MSC_VER +#pragma comment(lib, "advapi32.lib") +#pragma comment(lib, "ole32.lib") +#pragma comment(lib, "setupapi.lib") +#pragma comment(lib, "newdev.lib") +#endif + + +const static GUID GUID_DEVCLASS_NET = { 0x4d36e972L, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } }; + +const static TCHAR szAdapterRegKeyPathTemplate[] = TEXT("SYSTEM\\CurrentControlSet\\Control\\Network\\%") TEXT(PRIsLPOLESTR) TEXT("\\%") TEXT(PRIsLPOLESTR) TEXT("\\Connection"); +#define ADAPTER_REGKEY_PATH_MAX (_countof(TEXT("SYSTEM\\CurrentControlSet\\Control\\Network\\")) - 1 + 38 + _countof(TEXT("\\")) - 1 + 38 + _countof(TEXT("\\Connection"))) + +/** + * Dynamically load a library and find a function in it + * + * @param libname Name of the library to load + * @param funcname Name of the function to find + * @param m Pointer to a module. On return this is set to the + * the handle to the loaded library. The caller must + * free it by calling FreeLibrary() if not NULL. + * + * @return Pointer to the function + * NULL on error -- use GetLastError() to find the error code. + * + **/ +static void * +find_function(const WCHAR *libname, const char *funcname, HMODULE *m) +{ + WCHAR libpath[MAX_PATH]; + void *fptr = NULL; + + /* Make sure the dll is loaded from the system32 folder */ + if (!GetSystemDirectoryW(libpath, _countof(libpath))) + { + return NULL; + } + + size_t len = _countof(libpath) - wcslen(libpath) - 1; + if (len < wcslen(libname) + 1) + { + SetLastError(ERROR_INSUFFICIENT_BUFFER); + return NULL; + } + wcsncat(libpath, L"\\", len); + wcsncat(libpath, libname, len-1); + + *m = LoadLibraryW(libpath); + if (*m == NULL) + { + return NULL; + } + fptr = GetProcAddress(*m, funcname); + if (!fptr) + { + FreeLibrary(*m); + *m = NULL; + return NULL; + } + return fptr; +} + +/** + * Returns length of string of strings + * + * @param szz Pointer to a string of strings (terminated by an empty string) + * + * @return Number of characters not counting the final zero terminator + **/ +static inline size_t +_tcszlen(_In_z_ LPCTSTR szz) +{ + LPCTSTR s; + for (s = szz; s[0]; s += _tcslen(s) + 1) + { + } + return s - szz; +} + + +/** + * Checks if string is contained in the string of strings. Comparison is made case-insensitive. + * + * @param szzHay Pointer to a string of strings (terminated by an empty string) we are + * looking in + * + * @param szNeedle The string we are searching for + * + * @return Pointer to the string in szzHay that matches szNeedle is found; NULL otherwise + */ +static LPCTSTR +_tcszistr(_In_z_ LPCTSTR szzHay, _In_z_ LPCTSTR szNeedle) +{ + for (LPCTSTR s = szzHay; s[0]; s += _tcslen(s) + 1) + { + if (_tcsicmp(s, szNeedle) == 0) + { + return s; + } + } + + return NULL; +} + + +/** + * Function that performs a specific task on a device + * + * @param hDeviceInfoSet A handle to a device information set that contains a device + * information element that represents the device. + * + * @param pDeviceInfoData A pointer to an SP_DEVINFO_DATA structure that specifies the + * device information element in hDeviceInfoSet. + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +typedef DWORD (*devop_func_t)( + _In_ HDEVINFO hDeviceInfoSet, + _In_ PSP_DEVINFO_DATA pDeviceInfoData, + _Inout_ LPBOOL pbRebootRequired); + + +/** + * Checks device install parameters if a system reboot is required. + * + * @param hDeviceInfoSet A handle to a device information set that contains a device + * information element that represents the device. + * + * @param pDeviceInfoData A pointer to an SP_DEVINFO_DATA structure that specifies the + * device information element in hDeviceInfoSet. + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +static DWORD +check_reboot( + _In_ HDEVINFO hDeviceInfoSet, + _In_ PSP_DEVINFO_DATA pDeviceInfoData, + _Inout_ LPBOOL pbRebootRequired) +{ + if (pbRebootRequired == NULL) + { + return ERROR_BAD_ARGUMENTS; + } + + SP_DEVINSTALL_PARAMS devinstall_params = { .cbSize = sizeof(SP_DEVINSTALL_PARAMS) }; + if (!SetupDiGetDeviceInstallParams( + hDeviceInfoSet, + pDeviceInfoData, + &devinstall_params)) + { + DWORD dwResult = GetLastError(); + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiGetDeviceInstallParams failed", __FUNCTION__); + return dwResult; + } + + if ((devinstall_params.Flags & (DI_NEEDREBOOT | DI_NEEDRESTART)) != 0) + { + *pbRebootRequired = TRUE; + } + + return ERROR_SUCCESS; +} + + +/** + * Deletes the device. + * + * @param hDeviceInfoSet A handle to a device information set that contains a device + * information element that represents the device. + * + * @param pDeviceInfoData A pointer to an SP_DEVINFO_DATA structure that specifies the + * device information element in hDeviceInfoSet. + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +static DWORD +delete_device( + _In_ HDEVINFO hDeviceInfoSet, + _In_ PSP_DEVINFO_DATA pDeviceInfoData, + _Inout_ LPBOOL pbRebootRequired) +{ + SP_REMOVEDEVICE_PARAMS params = + { + .ClassInstallHeader = + { + .cbSize = sizeof(SP_CLASSINSTALL_HEADER), + .InstallFunction = DIF_REMOVE, + }, + .Scope = DI_REMOVEDEVICE_GLOBAL, + .HwProfile = 0, + }; + + /* Set class installer parameters for DIF_REMOVE. */ + if (!SetupDiSetClassInstallParams( + hDeviceInfoSet, + pDeviceInfoData, + ¶ms.ClassInstallHeader, + sizeof(SP_REMOVEDEVICE_PARAMS))) + { + DWORD dwResult = GetLastError(); + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiSetClassInstallParams failed", __FUNCTION__); + return dwResult; + } + + /* Call appropriate class installer. */ + if (!SetupDiCallClassInstaller( + DIF_REMOVE, + hDeviceInfoSet, + pDeviceInfoData)) + { + DWORD dwResult = GetLastError(); + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiCallClassInstaller(DIF_REMOVE) failed", __FUNCTION__); + return dwResult; + } + + /* Check if a system reboot is required. */ + check_reboot(hDeviceInfoSet, pDeviceInfoData, pbRebootRequired); + return ERROR_SUCCESS; +} + + +/** + * Changes the device state. + * + * @param hDeviceInfoSet A handle to a device information set that contains a device + * information element that represents the device. + * + * @param pDeviceInfoData A pointer to an SP_DEVINFO_DATA structure that specifies the + * device information element in hDeviceInfoSet. + * + * @param bEnable TRUE to enable the device; FALSE to disable. + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +static DWORD +change_device_state( + _In_ HDEVINFO hDeviceInfoSet, + _In_ PSP_DEVINFO_DATA pDeviceInfoData, + _In_ BOOL bEnable, + _Inout_ LPBOOL pbRebootRequired) +{ + SP_PROPCHANGE_PARAMS params = + { + .ClassInstallHeader = + { + .cbSize = sizeof(SP_CLASSINSTALL_HEADER), + .InstallFunction = DIF_PROPERTYCHANGE, + }, + .StateChange = bEnable ? DICS_ENABLE : DICS_DISABLE, + .Scope = DICS_FLAG_GLOBAL, + .HwProfile = 0, + }; + + /* Set class installer parameters for DIF_PROPERTYCHANGE. */ + if (!SetupDiSetClassInstallParams( + hDeviceInfoSet, + pDeviceInfoData, + ¶ms.ClassInstallHeader, + sizeof(SP_PROPCHANGE_PARAMS))) + { + DWORD dwResult = GetLastError(); + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiSetClassInstallParams failed", __FUNCTION__); + return dwResult; + } + + /* Call appropriate class installer. */ + if (!SetupDiCallClassInstaller( + DIF_PROPERTYCHANGE, + hDeviceInfoSet, + pDeviceInfoData)) + { + DWORD dwResult = GetLastError(); + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiCallClassInstaller(DIF_PROPERTYCHANGE) failed", __FUNCTION__); + return dwResult; + } + + /* Check if a system reboot is required. */ + check_reboot(hDeviceInfoSet, pDeviceInfoData, pbRebootRequired); + return ERROR_SUCCESS; +} + + +/** + * Enables the device. + * + * @param hDeviceInfoSet A handle to a device information set that contains a device + * information element that represents the device. + * + * @param pDeviceInfoData A pointer to an SP_DEVINFO_DATA structure that specifies the + * device information element in hDeviceInfoSet. + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +static DWORD +enable_device( + _In_ HDEVINFO hDeviceInfoSet, + _In_ PSP_DEVINFO_DATA pDeviceInfoData, + _Inout_ LPBOOL pbRebootRequired) +{ + return change_device_state(hDeviceInfoSet, pDeviceInfoData, TRUE, pbRebootRequired); +} + + +/** + * Disables the device. + * + * @param hDeviceInfoSet A handle to a device information set that contains a device + * information element that represents the device. + * + * @param pDeviceInfoData A pointer to an SP_DEVINFO_DATA structure that specifies the + * device information element in hDeviceInfoSet. + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +static DWORD +disable_device( + _In_ HDEVINFO hDeviceInfoSet, + _In_ PSP_DEVINFO_DATA pDeviceInfoData, + _Inout_ LPBOOL pbRebootRequired) +{ + return change_device_state(hDeviceInfoSet, pDeviceInfoData, FALSE, pbRebootRequired); +} + + +/** + * Reads string value from registry key. + * + * @param hKey Handle of the registry key to read from. Must be opened with read + * access. + * + * @param szName Name of the value to read. + * + * @param pszValue Pointer to string to retrieve registry value. If the value type is + * REG_EXPAND_SZ the value is expanded using ExpandEnvironmentStrings(). + * The string must be released with free() after use. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + */ +static DWORD +get_reg_string( + _In_ HKEY hKey, + _In_ LPCTSTR szName, + _Out_ LPTSTR *pszValue) +{ + if (pszValue == NULL) + { + return ERROR_BAD_ARGUMENTS; + } + + DWORD dwValueType = REG_NONE, dwSize = 0; + DWORD dwResult = RegQueryValueEx( + hKey, + szName, + NULL, + &dwValueType, + NULL, + &dwSize); + if (dwResult != ERROR_SUCCESS) + { + SetLastError(dwResult); /* MSDN does not mention RegQueryValueEx() to set GetLastError(). But we do have an error code. Set last error manually. */ + msg(M_NONFATAL | M_ERRNO, "%s: enumerating \"%" PRIsLPTSTR "\" registry value failed", __FUNCTION__, szName); + return dwResult; + } + + switch (dwValueType) + { + case REG_SZ: + case REG_EXPAND_SZ: + { + /* Read value. */ + LPTSTR szValue = (LPTSTR)malloc(dwSize); + if (szValue == NULL) + { + msg(M_FATAL, "%s: malloc(%u) failed", __FUNCTION__, dwSize); + return ERROR_OUTOFMEMORY; + } + + dwResult = RegQueryValueEx( + hKey, + szName, + NULL, + NULL, + (LPBYTE)szValue, + &dwSize); + if (dwResult != ERROR_SUCCESS) + { + SetLastError(dwResult); /* MSDN does not mention RegQueryValueEx() to set GetLastError(). But we do have an error code. Set last error manually. */ + msg(M_NONFATAL | M_ERRNO, "%s: reading \"%" PRIsLPTSTR "\" registry value failed", __FUNCTION__, szName); + free(szValue); + return dwResult; + } + + if (dwValueType == REG_EXPAND_SZ) + { + /* Expand the environment strings. */ + DWORD + dwSizeExp = dwSize * 2, + dwCountExp = +#ifdef UNICODE + dwSizeExp / sizeof(TCHAR); +#else + dwSizeExp / sizeof(TCHAR) - 1; /* Note: ANSI version requires one extra char. */ +#endif + LPTSTR szValueExp = (LPTSTR)malloc(dwSizeExp); + if (szValueExp == NULL) + { + free(szValue); + msg(M_FATAL, "%s: malloc(%u) failed", __FUNCTION__, dwSizeExp); + return ERROR_OUTOFMEMORY; + } + + DWORD dwCountExpResult = ExpandEnvironmentStrings( + szValue, + szValueExp, dwCountExp + ); + if (dwCountExpResult == 0) + { + msg(M_NONFATAL | M_ERRNO, "%s: expanding \"%" PRIsLPTSTR "\" registry value failed", __FUNCTION__, szName); + free(szValueExp); + free(szValue); + return dwResult; + } + else if (dwCountExpResult <= dwCountExp) + { + /* The buffer was big enough. */ + free(szValue); + *pszValue = szValueExp; + return ERROR_SUCCESS; + } + else + { + /* Retry with a bigger buffer. */ + free(szValueExp); +#ifdef UNICODE + dwSizeExp = dwCountExpResult * sizeof(TCHAR); +#else + /* Note: ANSI version requires one extra char. */ + dwSizeExp = (dwCountExpResult + 1) * sizeof(TCHAR); +#endif + dwCountExp = dwCountExpResult; + szValueExp = (LPTSTR)malloc(dwSizeExp); + if (szValueExp == NULL) + { + free(szValue); + msg(M_FATAL, "%s: malloc(%u) failed", __FUNCTION__, dwSizeExp); + return ERROR_OUTOFMEMORY; + } + + dwCountExpResult = ExpandEnvironmentStrings( + szValue, + szValueExp, dwCountExp); + free(szValue); + *pszValue = szValueExp; + return ERROR_SUCCESS; + } + } + else + { + *pszValue = szValue; + return ERROR_SUCCESS; + } + } + + default: + msg(M_NONFATAL, "%s: \"%" PRIsLPTSTR "\" registry value is not string (type %u)", __FUNCTION__, dwValueType); + return ERROR_UNSUPPORTED_TYPE; + } +} + + +/** + * Returns network adapter ID. + * + * @param hDeviceInfoSet A handle to a device information set that contains a device + * information element that represents the device. + * + * @param pDeviceInfoData A pointer to an SP_DEVINFO_DATA structure that specifies the + * device information element in hDeviceInfoSet. + * + * @param iNumAttempts After the device is created, it might take some time before the + * registry key is populated. This parameter specifies the number of + * attempts to read NetCfgInstanceId value from registry. A 1sec sleep + * is inserted between retry attempts. + * + * @param pguidAdapter A pointer to GUID that receives network adapter ID. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +static DWORD +get_net_adapter_guid( + _In_ HDEVINFO hDeviceInfoSet, + _In_ PSP_DEVINFO_DATA pDeviceInfoData, + _In_ int iNumAttempts, + _Out_ LPGUID pguidAdapter) +{ + DWORD dwResult = ERROR_BAD_ARGUMENTS; + + if (pguidAdapter == NULL || iNumAttempts < 1) + { + return ERROR_BAD_ARGUMENTS; + } + + /* Open HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\<class>\<id> registry key. */ + HKEY hKey = SetupDiOpenDevRegKey( + hDeviceInfoSet, + pDeviceInfoData, + DICS_FLAG_GLOBAL, + 0, + DIREG_DRV, + KEY_READ); + if (hKey == INVALID_HANDLE_VALUE) + { + dwResult = GetLastError(); + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiOpenDevRegKey failed", __FUNCTION__); + return dwResult; + } + + while (iNumAttempts > 0) + { + /* Query the NetCfgInstanceId value. Using get_reg_string() right on might clutter the output with error messages while the registry is still being populated. */ + LPTSTR szCfgGuidString = NULL; + dwResult = RegQueryValueEx(hKey, TEXT("NetCfgInstanceId"), NULL, NULL, NULL, NULL); + if (dwResult != ERROR_SUCCESS) + { + if (dwResult == ERROR_FILE_NOT_FOUND && --iNumAttempts > 0) + { + /* Wait and retry. */ + Sleep(1000); + continue; + } + + SetLastError(dwResult); /* MSDN does not mention RegQueryValueEx() to set GetLastError(). But we do have an error code. Set last error manually. */ + msg(M_NONFATAL | M_ERRNO, "%s: querying \"NetCfgInstanceId\" registry value failed", __FUNCTION__); + break; + } + + /* Read the NetCfgInstanceId value now. */ + dwResult = get_reg_string( + hKey, + TEXT("NetCfgInstanceId"), + &szCfgGuidString); + if (dwResult != ERROR_SUCCESS) + { + break; + } + + dwResult = SUCCEEDED(CLSIDFromString(szCfgGuidString, (LPCLSID)pguidAdapter)) ? ERROR_SUCCESS : ERROR_INVALID_DATA; + free(szCfgGuidString); + break; + } + + RegCloseKey(hKey); + return dwResult; +} + + +/** + * Returns a specified Plug and Play device property. + * + * @param hDeviceInfoSet A handle to a device information set that contains a device + * information element that represents the device. + * + * @param pDeviceInfoData A pointer to an SP_DEVINFO_DATA structure that specifies the + * device information element in hDeviceInfoSet. + * + * @param dwProperty Specifies the property to be retrieved. See + * https://msdn.microsoft.com/en-us/library/windows/hardware/ff551967.aspx + * + * @pdwPropertyRegDataType A pointer to a variable that receives the data type of the + * property that is being retrieved. This is one of the standard + * registry data types. This parameter is optional and can be NULL. + * + * @param ppData A pointer to pointer to data that receives the device property. The + * data must be released with free() after use. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +static DWORD +get_device_reg_property( + _In_ HDEVINFO hDeviceInfoSet, + _In_ PSP_DEVINFO_DATA pDeviceInfoData, + _In_ DWORD dwProperty, + _Out_opt_ LPDWORD pdwPropertyRegDataType, + _Out_ LPVOID *ppData) +{ + DWORD dwResult = ERROR_BAD_ARGUMENTS; + + if (ppData == NULL) + { + return ERROR_BAD_ARGUMENTS; + } + + /* Try with stack buffer first. */ + BYTE bBufStack[128]; + DWORD dwRequiredSize = 0; + if (SetupDiGetDeviceRegistryProperty( + hDeviceInfoSet, + pDeviceInfoData, + dwProperty, + pdwPropertyRegDataType, + bBufStack, + sizeof(bBufStack), + &dwRequiredSize)) + { + /* Copy from stack. */ + *ppData = malloc(dwRequiredSize); + if (*ppData == NULL) + { + msg(M_FATAL, "%s: malloc(%u) failed", __FUNCTION__, dwRequiredSize); + return ERROR_OUTOFMEMORY; + } + + memcpy(*ppData, bBufStack, dwRequiredSize); + return ERROR_SUCCESS; + } + else + { + dwResult = GetLastError(); + if (dwResult == ERROR_INSUFFICIENT_BUFFER) + { + /* Allocate on heap and retry. */ + *ppData = malloc(dwRequiredSize); + if (*ppData == NULL) + { + msg(M_FATAL, "%s: malloc(%u) failed", __FUNCTION__, dwRequiredSize); + return ERROR_OUTOFMEMORY; + } + + if (SetupDiGetDeviceRegistryProperty( + hDeviceInfoSet, + pDeviceInfoData, + dwProperty, + pdwPropertyRegDataType, + *ppData, + dwRequiredSize, + &dwRequiredSize)) + { + return ERROR_SUCCESS; + } + else + { + dwResult = GetLastError(); + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiGetDeviceRegistryProperty(%u) failed", __FUNCTION__, dwProperty); + return dwResult; + } + } + else + { + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiGetDeviceRegistryProperty(%u) failed", __FUNCTION__, dwProperty); + return dwResult; + } + } +} + + +DWORD +tap_create_adapter( + _In_opt_ HWND hwndParent, + _In_opt_ LPCTSTR szDeviceDescription, + _In_ LPCTSTR szHwId, + _Inout_ LPBOOL pbRebootRequired, + _Out_ LPGUID pguidAdapter) +{ + DWORD dwResult; + HMODULE libnewdev = NULL; + + if (szHwId == NULL + || pbRebootRequired == NULL + || pguidAdapter == NULL) + { + return ERROR_BAD_ARGUMENTS; + } + + /* Create an empty device info set for network adapter device class. */ + HDEVINFO hDevInfoList = SetupDiCreateDeviceInfoList(&GUID_DEVCLASS_NET, hwndParent); + if (hDevInfoList == INVALID_HANDLE_VALUE) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiCreateDeviceInfoList failed", __FUNCTION__); + return dwResult; + } + + /* Get the device class name from GUID. */ + TCHAR szClassName[MAX_CLASS_NAME_LEN]; + if (!SetupDiClassNameFromGuid( + &GUID_DEVCLASS_NET, + szClassName, + _countof(szClassName), + NULL)) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiClassNameFromGuid failed", __FUNCTION__); + goto cleanup_hDevInfoList; + } + + /* Create a new device info element and add it to the device info set. */ + SP_DEVINFO_DATA devinfo_data = { .cbSize = sizeof(SP_DEVINFO_DATA) }; + if (!SetupDiCreateDeviceInfo( + hDevInfoList, + szClassName, + &GUID_DEVCLASS_NET, + szDeviceDescription, + hwndParent, + DICD_GENERATE_ID, + &devinfo_data)) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiCreateDeviceInfo failed", __FUNCTION__); + goto cleanup_hDevInfoList; + } + + /* Set a device information element as the selected member of a device information set. */ + if (!SetupDiSetSelectedDevice( + hDevInfoList, + &devinfo_data)) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiSetSelectedDevice failed", __FUNCTION__); + goto cleanup_hDevInfoList; + } + + /* Set Plug&Play device hardware ID property. */ + if (!SetupDiSetDeviceRegistryProperty( + hDevInfoList, + &devinfo_data, + SPDRP_HARDWAREID, + (const BYTE *)szHwId, (DWORD)((_tcslen(szHwId) + 1) * sizeof(TCHAR)))) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiSetDeviceRegistryProperty failed", __FUNCTION__); + goto cleanup_hDevInfoList; + } + + /* Register the device instance with the PnP Manager */ + if (!SetupDiCallClassInstaller( + DIF_REGISTERDEVICE, + hDevInfoList, + &devinfo_data)) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiCallClassInstaller(DIF_REGISTERDEVICE) failed", __FUNCTION__); + goto cleanup_hDevInfoList; + } + + /* Install the device using DiInstallDevice() + * We instruct the system to use the best driver in the driver store + * by setting the drvinfo argument of DiInstallDevice as NULL. This + * assumes a driver is already installed in the driver store. + */ +#ifdef HAVE_DIINSTALLDEVICE + if (!DiInstallDevice(hwndParent, hDevInfoList, &devinfo_data, NULL, 0, pbRebootRequired)) +#else + /* mingw does not resolve DiInstallDevice, so load it at run time. */ + typedef BOOL (WINAPI *DiInstallDeviceFn) (HWND, HDEVINFO, SP_DEVINFO_DATA *, + SP_DRVINFO_DATA *, DWORD, BOOL *); + DiInstallDeviceFn installfn + = find_function (L"newdev.dll", "DiInstallDevice", &libnewdev); + + if (!installfn) + { + dwResult = GetLastError(); + msg(M_NONFATAL | M_ERRNO, "%s: Failed to locate DiInstallDevice()", __FUNCTION__); + goto cleanup_hDevInfoList; + } + + if (!installfn(hwndParent, hDevInfoList, &devinfo_data, NULL, 0, pbRebootRequired)) +#endif + { + dwResult = GetLastError(); + msg(M_NONFATAL | M_ERRNO, "%s: DiInstallDevice failed", __FUNCTION__); + goto cleanup_remove_device; + } + + /* Get network adapter ID from registry. Retry for max 30sec. */ + dwResult = get_net_adapter_guid(hDevInfoList, &devinfo_data, 30, pguidAdapter); + +cleanup_remove_device: + if (dwResult != ERROR_SUCCESS) + { + /* The adapter was installed. But, the adapter ID was unobtainable. Clean-up. */ + SP_REMOVEDEVICE_PARAMS removedevice_params = + { + .ClassInstallHeader = + { + .cbSize = sizeof(SP_CLASSINSTALL_HEADER), + .InstallFunction = DIF_REMOVE, + }, + .Scope = DI_REMOVEDEVICE_GLOBAL, + .HwProfile = 0, + }; + + /* Set class installer parameters for DIF_REMOVE. */ + if (SetupDiSetClassInstallParams( + hDevInfoList, + &devinfo_data, + &removedevice_params.ClassInstallHeader, + sizeof(SP_REMOVEDEVICE_PARAMS))) + { + /* Call appropriate class installer. */ + if (SetupDiCallClassInstaller( + DIF_REMOVE, + hDevInfoList, + &devinfo_data)) + { + /* Check if a system reboot is required. */ + check_reboot(hDevInfoList, &devinfo_data, pbRebootRequired); + } + else + { + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiCallClassInstaller(DIF_REMOVE) failed", __FUNCTION__); + } + } + else + { + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiSetClassInstallParams failed", __FUNCTION__); + } + } + +cleanup_hDevInfoList: + if (libnewdev) + { + FreeLibrary(libnewdev); + } + SetupDiDestroyDeviceInfoList(hDevInfoList); + return dwResult; +} + + +/** + * Performs a given task on an adapter. + * + * @param hwndParent A handle to the top-level window to use for any user adapter that is + * related to non-device-specific actions (such as a select-device dialog + * box that uses the global class driver list). This handle is optional + * and can be NULL. If a specific top-level window is not required, set + * hwndParent to NULL. + * + * @param pguidAdapter A pointer to GUID that contains network adapter ID. + * + * @param funcOperation A pointer for the function to perform specific task on the adapter. + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +static DWORD +execute_on_first_adapter( + _In_opt_ HWND hwndParent, + _In_ LPCGUID pguidAdapter, + _In_ devop_func_t funcOperation, + _Inout_ LPBOOL pbRebootRequired) +{ + DWORD dwResult; + + if (pguidAdapter == NULL) + { + return ERROR_BAD_ARGUMENTS; + } + + /* Create a list of network devices. */ + HDEVINFO hDevInfoList = SetupDiGetClassDevsEx( + &GUID_DEVCLASS_NET, + NULL, + hwndParent, + DIGCF_PRESENT, + NULL, + NULL, + NULL); + if (hDevInfoList == INVALID_HANDLE_VALUE) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiGetClassDevsEx failed", __FUNCTION__); + return dwResult; + } + + /* Retrieve information associated with a device information set. */ + SP_DEVINFO_LIST_DETAIL_DATA devinfo_list_detail_data = { .cbSize = sizeof(SP_DEVINFO_LIST_DETAIL_DATA) }; + if (!SetupDiGetDeviceInfoListDetail(hDevInfoList, &devinfo_list_detail_data)) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiGetDeviceInfoListDetail failed", __FUNCTION__); + goto cleanup_hDevInfoList; + } + + /* Iterate. */ + for (DWORD dwIndex = 0;; dwIndex++) + { + /* Get the device from the list. */ + SP_DEVINFO_DATA devinfo_data = { .cbSize = sizeof(SP_DEVINFO_DATA) }; + if (!SetupDiEnumDeviceInfo( + hDevInfoList, + dwIndex, + &devinfo_data)) + { + if (GetLastError() == ERROR_NO_MORE_ITEMS) + { + LPOLESTR szAdapterId = NULL; + StringFromIID((REFIID)pguidAdapter, &szAdapterId); + msg(M_NONFATAL, "%s: Adapter %" PRIsLPOLESTR " not found", __FUNCTION__, szAdapterId); + CoTaskMemFree(szAdapterId); + dwResult = ERROR_FILE_NOT_FOUND; + goto cleanup_hDevInfoList; + } + else + { + /* Something is wrong with this device. Skip it. */ + msg(M_WARN | M_ERRNO, "%s: SetupDiEnumDeviceInfo(%u) failed", __FUNCTION__, dwIndex); + continue; + } + } + + /* Get adapter GUID. */ + GUID guidAdapter; + dwResult = get_net_adapter_guid(hDevInfoList, &devinfo_data, 1, &guidAdapter); + if (dwResult != ERROR_SUCCESS) + { + /* Something is wrong with this device. Skip it. */ + continue; + } + + /* Compare GUIDs. */ + if (memcmp(pguidAdapter, &guidAdapter, sizeof(GUID)) == 0) + { + dwResult = funcOperation(hDevInfoList, &devinfo_data, pbRebootRequired); + break; + } + } + +cleanup_hDevInfoList: + SetupDiDestroyDeviceInfoList(hDevInfoList); + return dwResult; +} + + +DWORD +tap_delete_adapter( + _In_opt_ HWND hwndParent, + _In_ LPCGUID pguidAdapter, + _Inout_ LPBOOL pbRebootRequired) +{ + return execute_on_first_adapter(hwndParent, pguidAdapter, delete_device, pbRebootRequired); +} + + +DWORD +tap_enable_adapter( + _In_opt_ HWND hwndParent, + _In_ LPCGUID pguidAdapter, + _In_ BOOL bEnable, + _Inout_ LPBOOL pbRebootRequired) +{ + return execute_on_first_adapter(hwndParent, pguidAdapter, bEnable ? enable_device : disable_device, pbRebootRequired); +} + +/* stripped version of ExecCommand in interactive.c */ +static DWORD +ExecCommand(const WCHAR* cmdline) +{ + DWORD exit_code; + STARTUPINFOW si; + PROCESS_INFORMATION pi; + DWORD proc_flags = CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT; + WCHAR* cmdline_dup = NULL; + + ZeroMemory(&si, sizeof(si)); + ZeroMemory(&pi, sizeof(pi)); + + si.cb = sizeof(si); + + /* CreateProcess needs a modifiable cmdline: make a copy */ + cmdline_dup = _wcsdup(cmdline); + if (cmdline_dup && CreateProcessW(NULL, cmdline_dup, NULL, NULL, FALSE, + proc_flags, NULL, NULL, &si, &pi)) + { + WaitForSingleObject(pi.hProcess, INFINITE); + if (!GetExitCodeProcess(pi.hProcess, &exit_code)) + { + exit_code = GetLastError(); + } + + CloseHandle(pi.hProcess); + CloseHandle(pi.hThread); + } + else + { + exit_code = GetLastError(); + } + + free(cmdline_dup); + return exit_code; +} + +DWORD +tap_set_adapter_name( + _In_ LPCGUID pguidAdapter, + _In_ LPCTSTR szName, + _In_ BOOL bSilent) +{ + DWORD dwResult; + int msg_flag = bSilent ? M_WARN : M_NONFATAL; + msg_flag |= M_ERRNO; + + if (pguidAdapter == NULL || szName == NULL) + { + return ERROR_BAD_ARGUMENTS; + } + + /* Get the device class GUID as string. */ + LPOLESTR szDevClassNetId = NULL; + StringFromIID((REFIID)&GUID_DEVCLASS_NET, &szDevClassNetId); + + /* Get the adapter GUID as string. */ + LPOLESTR szAdapterId = NULL; + StringFromIID((REFIID)pguidAdapter, &szAdapterId); + + /* Render registry key path. */ + TCHAR szRegKey[ADAPTER_REGKEY_PATH_MAX]; + _stprintf_s( + szRegKey, _countof(szRegKey), + szAdapterRegKeyPathTemplate, + szDevClassNetId, + szAdapterId); + + /* Open network adapter registry key. */ + HKEY hKey = NULL; + dwResult = RegOpenKeyEx( + HKEY_LOCAL_MACHINE, + szRegKey, + 0, + KEY_QUERY_VALUE, + &hKey); + if (dwResult != ERROR_SUCCESS) + { + SetLastError(dwResult); /* MSDN does not mention RegOpenKeyEx() to set GetLastError(). But we do have an error code. Set last error manually. */ + msg(msg_flag, "%s: RegOpenKeyEx(HKLM, \"%" PRIsLPTSTR "\") failed", __FUNCTION__, szRegKey); + goto cleanup_szAdapterId; + } + + LPTSTR szOldName = NULL; + dwResult = get_reg_string(hKey, TEXT("Name"), &szOldName); + if (dwResult != ERROR_SUCCESS) + { + SetLastError(dwResult); + msg(msg_flag, "%s: Error reading adapter name", __FUNCTION__); + goto cleanup_hKey; + } + + /* rename adapter via netsh call */ + const TCHAR* szFmt = _T("netsh interface set interface name=\"%s\" newname=\"%s\""); + size_t ncmdline = _tcslen(szFmt) + _tcslen(szOldName) + _tcslen(szName) + 1; + WCHAR* szCmdLine = malloc(ncmdline * sizeof(TCHAR)); + _stprintf_s(szCmdLine, ncmdline, szFmt, szOldName, szName); + + free(szOldName); + + dwResult = ExecCommand(szCmdLine); + free(szCmdLine); + + if (dwResult != ERROR_SUCCESS) + { + SetLastError(dwResult); + msg(msg_flag, "%s: Error renaming adapter", __FUNCTION__); + goto cleanup_hKey; + } + +cleanup_hKey: + RegCloseKey(hKey); +cleanup_szAdapterId: + CoTaskMemFree(szAdapterId); + CoTaskMemFree(szDevClassNetId); + return dwResult; +} + + +DWORD +tap_list_adapters( + _In_opt_ HWND hwndParent, + _In_opt_ LPCTSTR szzHwIDs, + _Out_ struct tap_adapter_node **ppAdapter) +{ + DWORD dwResult; + + if (ppAdapter == NULL) + { + return ERROR_BAD_ARGUMENTS; + } + + /* Create a list of network devices. */ + HDEVINFO hDevInfoList = SetupDiGetClassDevsEx( + &GUID_DEVCLASS_NET, + NULL, + hwndParent, + DIGCF_PRESENT, + NULL, + NULL, + NULL); + if (hDevInfoList == INVALID_HANDLE_VALUE) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiGetClassDevsEx failed", __FUNCTION__); + return dwResult; + } + + /* Retrieve information associated with a device information set. */ + SP_DEVINFO_LIST_DETAIL_DATA devinfo_list_detail_data = { .cbSize = sizeof(SP_DEVINFO_LIST_DETAIL_DATA) }; + if (!SetupDiGetDeviceInfoListDetail(hDevInfoList, &devinfo_list_detail_data)) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiGetDeviceInfoListDetail failed", __FUNCTION__); + goto cleanup_hDevInfoList; + } + + /* Get the device class GUID as string. */ + LPOLESTR szDevClassNetId = NULL; + StringFromIID((REFIID)&GUID_DEVCLASS_NET, &szDevClassNetId); + + /* Iterate. */ + *ppAdapter = NULL; + struct tap_adapter_node *pAdapterTail = NULL; + for (DWORD dwIndex = 0;; dwIndex++) + { + /* Get the device from the list. */ + SP_DEVINFO_DATA devinfo_data = { .cbSize = sizeof(SP_DEVINFO_DATA) }; + if (!SetupDiEnumDeviceInfo( + hDevInfoList, + dwIndex, + &devinfo_data)) + { + if (GetLastError() == ERROR_NO_MORE_ITEMS) + { + break; + } + else + { + /* Something is wrong with this device. Skip it. */ + msg(M_WARN | M_ERRNO, "%s: SetupDiEnumDeviceInfo(%u) failed", __FUNCTION__, dwIndex); + continue; + } + } + + /* Get device hardware ID(s). */ + DWORD dwDataType = REG_NONE; + LPTSTR szzDeviceHardwareIDs = NULL; + dwResult = get_device_reg_property( + hDevInfoList, + &devinfo_data, + SPDRP_HARDWAREID, + &dwDataType, + (LPVOID)&szzDeviceHardwareIDs); + if (dwResult != ERROR_SUCCESS) + { + /* Something is wrong with this device. Skip it. */ + continue; + } + + /* Check that hardware ID is REG_SZ/REG_MULTI_SZ, and optionally if it matches ours. */ + if (dwDataType == REG_SZ) + { + if (szzHwIDs && !_tcszistr(szzHwIDs, szzDeviceHardwareIDs)) + { + /* This is not our device. Skip it. */ + goto cleanup_szzDeviceHardwareIDs; + } + } + else if (dwDataType == REG_MULTI_SZ) + { + if (szzHwIDs) + { + for (LPTSTR s = szzDeviceHardwareIDs;; s += _tcslen(s) + 1) + { + if (s[0] == 0) + { + /* This is not our device. Skip it. */ + goto cleanup_szzDeviceHardwareIDs; + } + else if (_tcszistr(szzHwIDs, s)) + { + /* This is our device. */ + break; + } + } + } + } + else + { + /* Unexpected hardware ID format. Skip device. */ + goto cleanup_szzDeviceHardwareIDs; + } + + /* Get adapter GUID. */ + GUID guidAdapter; + dwResult = get_net_adapter_guid(hDevInfoList, &devinfo_data, 1, &guidAdapter); + if (dwResult != ERROR_SUCCESS) + { + /* Something is wrong with this device. Skip it. */ + goto cleanup_szzDeviceHardwareIDs; + } + + /* Get the adapter GUID as string. */ + LPOLESTR szAdapterId = NULL; + StringFromIID((REFIID)&guidAdapter, &szAdapterId); + + /* Render registry key path. */ + TCHAR szRegKey[ADAPTER_REGKEY_PATH_MAX]; + _stprintf_s( + szRegKey, _countof(szRegKey), + szAdapterRegKeyPathTemplate, + szDevClassNetId, + szAdapterId); + + /* Open network adapter registry key. */ + HKEY hKey = NULL; + dwResult = RegOpenKeyEx( + HKEY_LOCAL_MACHINE, + szRegKey, + 0, + KEY_READ, + &hKey); + if (dwResult != ERROR_SUCCESS) + { + SetLastError(dwResult); /* MSDN does not mention RegOpenKeyEx() to set GetLastError(). But we do have an error code. Set last error manually. */ + msg(M_WARN | M_ERRNO, "%s: RegOpenKeyEx(HKLM, \"%" PRIsLPTSTR "\") failed", __FUNCTION__, szRegKey); + goto cleanup_szAdapterId; + } + + /* Read adapter name. */ + LPTSTR szName = NULL; + dwResult = get_reg_string( + hKey, + TEXT("Name"), + &szName); + if (dwResult != ERROR_SUCCESS) + { + SetLastError(dwResult); + msg(M_WARN | M_ERRNO, "%s: Cannot determine %" PRIsLPOLESTR " adapter name", __FUNCTION__, szAdapterId); + goto cleanup_hKey; + } + + /* Append to the list. */ + size_t hwid_size = (_tcszlen(szzDeviceHardwareIDs) + 1) * sizeof(TCHAR); + size_t name_size = (_tcslen(szName) + 1) * sizeof(TCHAR); + struct tap_adapter_node *node = (struct tap_adapter_node *)malloc(sizeof(struct tap_adapter_node) + hwid_size + name_size); + if (node == NULL) + { + msg(M_FATAL, "%s: malloc(%u) failed", __FUNCTION__, sizeof(struct tap_adapter_node) + hwid_size + name_size); + dwResult = ERROR_OUTOFMEMORY; goto cleanup_szName; + } + + memcpy(&node->guid, &guidAdapter, sizeof(GUID)); + node->szzHardwareIDs = (LPTSTR)(node + 1); + memcpy(node->szzHardwareIDs, szzDeviceHardwareIDs, hwid_size); + node->szName = (LPTSTR)((LPBYTE)node->szzHardwareIDs + hwid_size); + memcpy(node->szName, szName, name_size); + node->pNext = NULL; + if (pAdapterTail) + { + pAdapterTail->pNext = node; + pAdapterTail = node; + } + else + { + *ppAdapter = pAdapterTail = node; + } + +cleanup_szName: + free(szName); +cleanup_hKey: + RegCloseKey(hKey); +cleanup_szAdapterId: + CoTaskMemFree(szAdapterId); +cleanup_szzDeviceHardwareIDs: + free(szzDeviceHardwareIDs); + } + + dwResult = ERROR_SUCCESS; + + CoTaskMemFree(szDevClassNetId); +cleanup_hDevInfoList: + SetupDiDestroyDeviceInfoList(hDevInfoList); + return dwResult; +} + + +void +tap_free_adapter_list( + _In_ struct tap_adapter_node *pAdapterList) +{ + /* Iterate over all nodes of the list. */ + while (pAdapterList) + { + struct tap_adapter_node *node = pAdapterList; + pAdapterList = pAdapterList->pNext; + + /* Free the adapter node. */ + free(node); + } +} diff --git a/src/tapctl/tap.h b/src/tapctl/tap.h new file mode 100644 index 0000000..847040c --- /dev/null +++ b/src/tapctl/tap.h @@ -0,0 +1,181 @@ +/* + * tapctl -- Utility to manipulate TUN/TAP adapters on Windows + * https://community.openvpn.net/openvpn/wiki/Tapctl + * + * Copyright (C) 2018-2021 Simon Rozman <simon@rozman.si> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef TAP_H +#define TAP_H + +#include <windows.h> +#include "basic.h" + + +/** + * Creates a TUN/TAP adapter. + * + * @param hwndParent A handle to the top-level window to use for any user adapter that is + * related to non-device-specific actions (such as a select-device dialog + * box that uses the global class driver list). This handle is optional + * and can be NULL. If a specific top-level window is not required, set + * hwndParent to NULL. + * + * @param szDeviceDescription A pointer to a NULL-terminated string that supplies the text + * description of the device. This pointer is optional and can be NULL. + * + * @param szHwId A pointer to a NULL-terminated string that supplies the hardware id + * of the device (e.g. "root\\tap0901", "Wintun"). + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @param pguidAdapter A pointer to GUID that receives network adapter ID. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +DWORD +tap_create_adapter( + _In_opt_ HWND hwndParent, + _In_opt_ LPCTSTR szDeviceDescription, + _In_ LPCTSTR szHwId, + _Inout_ LPBOOL pbRebootRequired, + _Out_ LPGUID pguidAdapter); + + +/** + * Deletes an adapter. + * + * @param hwndParent A handle to the top-level window to use for any user adapter that is + * related to non-device-specific actions (such as a select-device dialog + * box that uses the global class driver list). This handle is optional + * and can be NULL. If a specific top-level window is not required, set + * hwndParent to NULL. + * + * @param pguidAdapter A pointer to GUID that contains network adapter ID. + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +DWORD +tap_delete_adapter( + _In_opt_ HWND hwndParent, + _In_ LPCGUID pguidAdapter, + _Inout_ LPBOOL pbRebootRequired); + + +/** + * Enables or disables an adapter. + * + * @param hwndParent A handle to the top-level window to use for any user adapter that is + * related to non-device-specific actions (such as a select-device dialog + * box that uses the global class driver list). This handle is optional + * and can be NULL. If a specific top-level window is not required, set + * hwndParent to NULL. + * + * @param pguidAdapter A pointer to GUID that contains network adapter ID. + * + * @param bEnable TRUE to enable; FALSE to disable + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +DWORD +tap_enable_adapter( + _In_opt_ HWND hwndParent, + _In_ LPCGUID pguidAdapter, + _In_ BOOL bEnable, + _Inout_ LPBOOL pbRebootRequired); + + +/** + * Sets adapter name. + * + * @param pguidAdapter A pointer to GUID that contains network adapter ID. + * + * @param szName New adapter name - must be unique + * + * @param bSilent If true, MSI installer won't display message box and + * only print error to log. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +DWORD +tap_set_adapter_name( + _In_ LPCGUID pguidAdapter, + _In_ LPCTSTR szName, + _In_ BOOL bSilent); + + +/** + * Network adapter list node + */ +struct tap_adapter_node +{ + GUID guid; /** Adapter GUID */ + LPTSTR szzHardwareIDs; /** Device hardware ID(s) */ + LPTSTR szName; /** Adapter name */ + + struct tap_adapter_node *pNext; /** Pointer to next adapter */ +}; + + +/** + * Creates a list of existing network adapters. + * + * @param hwndParent A handle to the top-level window to use for any user adapter that is + * related to non-device-specific actions (such as a select-device dialog + * box that uses the global class driver list). This handle is optional + * and can be NULL. If a specific top-level window is not required, set + * hwndParent to NULL. + * + * @param szzHwIDs A string of strings that supplies the list of hardware IDs of the device. + * This pointer is optional and can be NULL. When NULL, all network adapters + * found are added to the list. + * + * @param ppAdapterList A pointer to the list to receive pointer to the first adapter in + * the list. After the list is no longer required, free it using + * tap_free_adapter_list(). + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + */ +DWORD +tap_list_adapters( + _In_opt_ HWND hwndParent, + _In_opt_ LPCTSTR szzHwIDs, + _Out_ struct tap_adapter_node **ppAdapterList); + + +/** + * Frees a list of network adapters. + * + * @param pAdapterList A pointer to the first adapter in the list to free. + */ +void +tap_free_adapter_list( + _In_ struct tap_adapter_node *pAdapterList); + +#endif /* ifndef TAP_H */ diff --git a/src/tapctl/tapctl.exe.manifest b/src/tapctl/tapctl.exe.manifest new file mode 100644 index 0000000..1eb5ea8 --- /dev/null +++ b/src/tapctl/tapctl.exe.manifest @@ -0,0 +1,10 @@ +<?xml version='1.0' encoding='UTF-8' standalone='yes'?> +<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'> + <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> + <security> + <requestedPrivileges> + <requestedExecutionLevel level='requireAdministrator' uiAccess='false' /> + </requestedPrivileges> + </security> + </trustInfo> +</assembly> diff --git a/src/tapctl/tapctl.props b/src/tapctl/tapctl.props new file mode 100644 index 0000000..0257b9f --- /dev/null +++ b/src/tapctl/tapctl.props @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ImportGroup Label="PropertySheets" /> + <PropertyGroup Label="UserMacros" /> + <PropertyGroup> + <GenerateManifest>false</GenerateManifest> + </PropertyGroup> + <ItemDefinitionGroup> + <ClCompile> + <PreprocessorDefinitions>_CONSOLE;_WIN32_WINNT=_WIN32_WINNT_VISTA;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <AdditionalIncludeDirectories>..\compat;$(TAP_WINDOWS_HOME)/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + </ClCompile> + <Link> + <SubSystem>Console</SubSystem> + </Link> + </ItemDefinitionGroup> + <ItemGroup /> +</Project>
\ No newline at end of file diff --git a/src/tapctl/tapctl.vcxproj b/src/tapctl/tapctl.vcxproj new file mode 100644 index 0000000..ad96f02 --- /dev/null +++ b/src/tapctl/tapctl.vcxproj @@ -0,0 +1,163 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup Label="ProjectConfigurations"> + <ProjectConfiguration Include="Debug|ARM64"> + <Configuration>Debug</Configuration> + <Platform>ARM64</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Debug|Win32"> + <Configuration>Debug</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Debug|x64"> + <Configuration>Debug</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|ARM64"> + <Configuration>Release</Configuration> + <Platform>ARM64</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|Win32"> + <Configuration>Release</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|x64"> + <Configuration>Release</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + </ItemGroup> + <PropertyGroup Label="Globals"> + <VCProjectVersion>15.0</VCProjectVersion> + <ProjectGuid>{A06436E7-D576-490D-8BA0-0751D920334A}</ProjectGuid> + <Keyword>Win32Proj</Keyword> + <RootNamespace>tapctl</RootNamespace> + <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>true</UseDebugLibraries> + <PlatformToolset>v142</PlatformToolset> + <CharacterSet>Unicode</CharacterSet> + <WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>true</UseDebugLibraries> + <PlatformToolset>v142</PlatformToolset> + <CharacterSet>Unicode</CharacterSet> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>true</UseDebugLibraries> + <PlatformToolset>v142</PlatformToolset> + <CharacterSet>Unicode</CharacterSet> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>false</UseDebugLibraries> + <PlatformToolset>v142</PlatformToolset> + <WholeProgramOptimization>true</WholeProgramOptimization> + <CharacterSet>Unicode</CharacterSet> + <WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>false</UseDebugLibraries> + <PlatformToolset>v142</PlatformToolset> + <WholeProgramOptimization>true</WholeProgramOptimization> + <CharacterSet>Unicode</CharacterSet> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>false</UseDebugLibraries> + <PlatformToolset>v142</PlatformToolset> + <WholeProgramOptimization>true</WholeProgramOptimization> + <CharacterSet>Unicode</CharacterSet> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> + <ImportGroup Label="ExtensionSettings"> + </ImportGroup> + <ImportGroup Label="Shared"> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + <Import Project="..\compat\Debug.props" /> + <Import Project="tapctl.props" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + <Import Project="..\compat\Debug.props" /> + <Import Project="tapctl.props" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + <Import Project="..\compat\Debug.props" /> + <Import Project="tapctl.props" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + <Import Project="..\compat\Release.props" /> + <Import Project="tapctl.props" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + <Import Project="..\compat\Release.props" /> + <Import Project="tapctl.props" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + <Import Project="..\compat\Release.props" /> + <Import Project="tapctl.props" /> + </ImportGroup> + <PropertyGroup Label="UserMacros" /> + <PropertyGroup Label="Vcpkg" Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'"> + <VcpkgEnabled>true</VcpkgEnabled> + </PropertyGroup> + <PropertyGroup Label="Vcpkg" Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'"> + <VcpkgEnabled>true</VcpkgEnabled> + </PropertyGroup> + <PropertyGroup Label="Vcpkg" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <VcpkgEnabled>true</VcpkgEnabled> + </PropertyGroup> + <PropertyGroup Label="Vcpkg" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <VcpkgEnabled>true</VcpkgEnabled> + </PropertyGroup> + <PropertyGroup Label="Vcpkg" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <VcpkgEnabled>true</VcpkgEnabled> + </PropertyGroup> + <PropertyGroup Label="Vcpkg" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <VcpkgEnabled>true</VcpkgEnabled> + </PropertyGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'" /> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'" /> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" /> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" /> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" /> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" /> + <ItemGroup> + <ClCompile Include="error.c" /> + <ClCompile Include="tap.c" /> + <ClCompile Include="main.c" /> + </ItemGroup> + <ItemGroup> + <ClInclude Include="basic.h" /> + <ClInclude Include="error.h" /> + <ClInclude Include="tap.h" /> + </ItemGroup> + <ItemGroup> + <ResourceCompile Include="tapctl_resources.rc" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\..\build\msvc\msvc-generate\msvc-generate.vcxproj"> + <Project>{8598c2c8-34c4-47a1-99b0-7c295a890615}</Project> + <ReferenceOutputAssembly>false</ReferenceOutputAssembly> + </ProjectReference> + </ItemGroup> + <ItemGroup> + <Manifest Include="tapctl.exe.manifest" /> + </ItemGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> + <ImportGroup Label="ExtensionTargets"> + </ImportGroup> +</Project>
\ No newline at end of file diff --git a/src/tapctl/tapctl.vcxproj.filters b/src/tapctl/tapctl.vcxproj.filters new file mode 100644 index 0000000..c7f71e9 --- /dev/null +++ b/src/tapctl/tapctl.vcxproj.filters @@ -0,0 +1,49 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup> + <Filter Include="Source Files"> + <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> + <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions> + </Filter> + <Filter Include="Header Files"> + <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> + <Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions> + </Filter> + <Filter Include="Resource Files"> + <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> + <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> + </Filter> + </ItemGroup> + <ItemGroup> + <ClCompile Include="tap.c"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="main.c"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="error.c"> + <Filter>Source Files</Filter> + </ClCompile> + </ItemGroup> + <ItemGroup> + <ClInclude Include="tap.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="error.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="basic.h"> + <Filter>Header Files</Filter> + </ClInclude> + </ItemGroup> + <ItemGroup> + <ResourceCompile Include="tapctl_resources.rc"> + <Filter>Resource Files</Filter> + </ResourceCompile> + </ItemGroup> + <ItemGroup> + <Manifest Include="tapctl.exe.manifest"> + <Filter>Resource Files</Filter> + </Manifest> + </ItemGroup> +</Project>
\ No newline at end of file diff --git a/src/tapctl/tapctl_resources.rc b/src/tapctl/tapctl_resources.rc new file mode 100644 index 0000000..a195396 --- /dev/null +++ b/src/tapctl/tapctl_resources.rc @@ -0,0 +1,64 @@ +/* + * tapctl -- Utility to manipulate TUN/TAP adapters on Windows + * + * Copyright (C) 2018-2021 Simon Rozman <simon@rozman.si> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#else +#include <config-msvc-version.h> +#endif +#include <winresrc.h> + +#pragma code_page(65001) /* UTF8 */ + +LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL + +VS_VERSION_INFO VERSIONINFO + FILEVERSION OPENVPN_VERSION_RESOURCE + PRODUCTVERSION OPENVPN_VERSION_RESOURCE + FILEFLAGSMASK VS_FF_DEBUG | VS_FF_PRERELEASE | VS_FF_PATCHED | VS_FF_PRIVATEBUILD | VS_FF_SPECIALBUILD +#ifdef _DEBUG + FILEFLAGS VS_FF_DEBUG +#else + FILEFLAGS 0x0L +#endif + FILEOS VOS_NT_WINDOWS32 + FILETYPE VFT_APP + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904b0" + BEGIN + VALUE "CompanyName", "The OpenVPN Project" + VALUE "FileDescription", "Utility to manipulate TUN/TAP adapters on Windows" + VALUE "FileVersion", PACKAGE_VERSION ".0" + VALUE "InternalName", "OpenVPN" + VALUE "LegalCopyright", "Copyright © The OpenVPN Project" + VALUE "OriginalFilename", "tapctl.exe" + VALUE "ProductName", "OpenVPN" + VALUE "ProductVersion", PACKAGE_VERSION ".0" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x409, 1200 + END +END + +1 RT_MANIFEST "tapctl.exe.manifest" |