From c4378784ae2caec57634f9f04bcb3dcddc673f36 Mon Sep 17 00:00:00 2001 From: Rick Lei Date: Wed, 14 Jan 2015 22:11:43 +0800 Subject: Add Android 4.1+ support. See doc/README-Android.md for build HOWTO. * Update CMake files to work with android-cmake * Use unix domain socket for communicating with Android GUI * May also run standalone but requires rooted Android device --- src/tuntap.c | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) (limited to 'src/tuntap.c') diff --git a/src/tuntap.c b/src/tuntap.c index e35858e..2656486 100644 --- a/src/tuntap.c +++ b/src/tuntap.c @@ -2,6 +2,10 @@ Copyright (c) 2012-2014, Matthias Schiffer All rights reserved. + Android port contributor: + Copyright (c) 2014-2015, Haofeng "Rick" Lei + All rights reserved. + Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -35,7 +39,6 @@ #include #include - #ifdef __linux__ #include @@ -60,16 +63,15 @@ static const bool multiaf_tun = false; static const bool multiaf_tun = true; #endif +#ifdef __linux__ -#if defined(__linux__) +/** Opens the TUN/TAP device helper shared by Android and Linux targets */ +static void tuntap_open_linux(const char * dev_name) { + pr_debug("initializing tun/tap device..."); -/** Opens the TUN/TAP device */ -void fastd_tuntap_open(void) { struct ifreq ifr = {}; - pr_debug("initializing tun/tap device..."); - - if ((ctx.tunfd = open("/dev/net/tun", O_RDWR|O_NONBLOCK)) < 0) + if ((ctx.tunfd = open(dev_name, O_RDWR|O_NONBLOCK)) < 0) exit_errno("could not open tun/tap device file"); if (conf.ifname) @@ -115,6 +117,40 @@ void fastd_tuntap_open(void) { pr_debug("tun/tap device initialized."); } +#endif + +#if defined(__ANDROID__) + +/** Opens the TUN/TAP device */ +void fastd_tuntap_open(void) { + pr_debug("initializing tun device..."); + + if (conf.android_integration) { + if (conf.mode != MODE_TUN) { + exit_error("Non root Android supports only TUN mode"); + } + + pr_debug("using android TUN fd"); + ctx.tunfd = fastd_android_receive_tunfd(); + + fastd_android_send_pid(); + + fastd_poll_set_fd_tuntap(); + + pr_debug("tun device initialized."); + } else { + /* this requires root on Android */ + tuntap_open_linux("/dev/tun"); + } +} + +#elif defined(__linux__) + +/** Opens the TUN/TAP device */ +void fastd_tuntap_open(void) { + tuntap_open_linux("/dev/net/tun"); +} + #elif defined(__FreeBSD__) || defined(__OpenBSD__) /** Sets the MTU of the TUN/TAP device */ -- cgit v1.2.3