Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow communicate with tun file dscription opened by 3rd process
- Loading branch information
Showing
9 changed files
with
600 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* vim: set expandtab ts=4 sw=4: */ | ||
/* | ||
* You may redistribute this program and/or modify it under the terms of | ||
* the GNU General Public License as published by the Free Software Foundation, | ||
* either version 3 of the License, or (at your option) any later version. | ||
* | ||
* 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, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#include "interface/Iface.h" | ||
#include "interface/tuntap/AndroidWrapper.h" | ||
#include "util/platform/Sockaddr.h" | ||
#include "memory/Allocator.h" | ||
#include "util/Assert.h" | ||
#include "util/Identity.h" | ||
#include "wire/Ethernet.h" | ||
#include "wire/Headers.h" | ||
#include "wire/Message.h" | ||
#include "wire/Error.h" | ||
|
||
/** | ||
* Android VpnService is expect you to read/write packet from the tun device | ||
* file description opened by system process rather than in the cjd process, | ||
* this InterfaceWrapper handle this case. | ||
*/ | ||
|
||
struct AndroidWrapper_pvt | ||
{ | ||
struct AndroidWrapper pub; | ||
struct Log* logger; | ||
Identity | ||
}; | ||
|
||
static Iface_DEFUN incomingFromWire(struct Message* msg, struct Iface* externalIf) | ||
{ | ||
struct AndroidWrapper_pvt* ctx = | ||
Identity_containerOf(externalIf, struct AndroidWrapper_pvt, pub.externalIf); | ||
|
||
if (!ctx->pub.internalIf.connectedIf) { | ||
Log_debug(ctx->logger, "DROP message for android tun not inited"); | ||
return NULL; | ||
} | ||
|
||
int version = Headers_getIpVersion(msg->bytes); | ||
uint16_t ethertype = 0; | ||
if (version == 4) { | ||
ethertype = Ethernet_TYPE_IP4; | ||
} else if (version == 6) { | ||
ethertype = Ethernet_TYPE_IP6; | ||
} else { | ||
Log_debug(ctx->logger, "Message is not IP/IPv6, dropped."); | ||
return NULL; | ||
} | ||
|
||
Message_shift(msg, 4, NULL); | ||
((uint16_t*) msg->bytes)[0] = 0; | ||
((uint16_t*) msg->bytes)[1] = ethertype; | ||
|
||
return Iface_next(&ctx->pub.internalIf, msg); | ||
} | ||
|
||
static Iface_DEFUN incomingFromUs(struct Message* msg, struct Iface* internalIf) | ||
{ | ||
struct AndroidWrapper_pvt* ctx = | ||
Identity_containerOf(internalIf, struct AndroidWrapper_pvt, pub.internalIf); | ||
|
||
if (!ctx->pub.externalIf.connectedIf) { | ||
Log_debug(ctx->logger, "DROP message for android tun not inited"); | ||
return NULL; | ||
} | ||
|
||
Message_shift(msg, -4, NULL); | ||
|
||
return Iface_next(&ctx->pub.externalIf, msg); | ||
} | ||
|
||
struct AndroidWrapper* AndroidWrapper_new(struct Allocator* alloc, struct Log* log) | ||
{ | ||
struct AndroidWrapper_pvt* context = | ||
Allocator_calloc(alloc, sizeof(struct AndroidWrapper_pvt), 1); | ||
Identity_set(context); | ||
context->pub.externalIf.send = incomingFromWire; | ||
context->pub.internalIf.send = incomingFromUs; | ||
context->logger = log; | ||
|
||
return &context->pub; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* vim: set expandtab ts=4 sw=4: */ | ||
/* | ||
* You may redistribute this program and/or modify it under the terms of | ||
* the GNU General Public License as published by the Free Software Foundation, | ||
* either version 3 of the License, or (at your option) any later version. | ||
* | ||
* 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, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#ifndef AndroidWrapper_H | ||
#define AndroidWrapper_H | ||
|
||
#include "interface/Iface.h" | ||
#include "memory/Allocator.h" | ||
#include "util/log/Log.h" | ||
#include "util/Linker.h" | ||
Linker_require("interface/tuntap/AndroidWrapper.c"); | ||
|
||
struct AndroidWrapper | ||
{ | ||
struct Iface internalIf; | ||
struct Iface externalIf; | ||
}; | ||
|
||
struct AndroidWrapper* AndroidWrapper_new(struct Allocator* alloc, struct Log* log); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* vim: set expandtab ts=4 sw=4: */ | ||
/* | ||
* You may redistribute this program and/or modify it under the terms of | ||
* the GNU General Public License as published by the Free Software Foundation, | ||
* either version 3 of the License, or (at your option) any later version. | ||
* | ||
* 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, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#ifndef FileNo_H | ||
#define FileNo_H | ||
|
||
#include "memory/Allocator.h" | ||
#include "exception/Except.h" | ||
#include "util/events/EventBase.h" | ||
#include "util/Linker.h" | ||
#include "util/log/Log.h" | ||
Linker_require("util/events/libuv/FileNo.c"); | ||
|
||
struct FileNo; | ||
|
||
enum FileNo_Type { | ||
/** Normal tunfd type, no need other wrapper */ | ||
FileNo_Type_NORMAL = 0, | ||
|
||
/** Android tunfd, need AndroidWrapper */ | ||
FileNo_Type_ANDROID | ||
}; | ||
|
||
typedef void (* FileNo_callback)(void* context, enum FileNo_Type type, int fileno); | ||
|
||
struct FileNo | ||
{ | ||
/** The name of the file eg: "/tmp/cjdns_fileno_foo" */ | ||
const char* const pipePath; | ||
|
||
void* userData; | ||
|
||
enum FileNo_Type type; | ||
|
||
struct EventBase* const base; | ||
|
||
FileNo_callback onFileNoReceived; | ||
|
||
struct Log* logger; | ||
}; | ||
|
||
#define FileNo_PADDING_AMOUNT 512 | ||
#define FileNo_BUFFER_CAP 4000 | ||
|
||
struct FileNo* FileNo_new(const char* path, | ||
struct EventBase* eb, | ||
struct Except* eh, | ||
struct Log* logger, | ||
struct Allocator* userAlloc, | ||
FileNo_callback recv_cb); | ||
|
||
#endif |
Oops, something went wrong.