mirror of https://github.com/oxen-io/lokinet
revise ./contrib/format.sh
This commit is contained in:
parent
7f27760c97
commit
a02679b87a
|
@ -21,11 +21,11 @@ fi
|
||||||
|
|
||||||
cd "$(dirname $0)/../"
|
cd "$(dirname $0)/../"
|
||||||
if [ "$1" = "verify" ] ; then
|
if [ "$1" = "verify" ] ; then
|
||||||
if [ $($binary --output-replacements-xml $(find jni daemon llarp include pybind | grep -E '\.([hc](pp)?|mm?)$' | grep -v '\#') | grep '</replacement>' | wc -l) -ne 0 ] ; then
|
if [ $($binary --output-replacements-xml $(find jni daemon llarp include pybind | grep -E '\.([hc](pp)?|m(m)?)$' | grep -v '\#') | grep '</replacement>' | wc -l) -ne 0 ] ; then
|
||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
$binary -i $(find jni daemon llarp include pybind | grep -E '\.([hc](pp)?|mm)$' | grep -v '\#') &> /dev/null
|
$binary -i $(find jni daemon llarp include pybind | grep -E '\.([hc](pp)?|m(m)?)$' | grep -v '\#') &> /dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
swift_format=$(command -v swiftformat 2>/dev/null)
|
swift_format=$(command -v swiftformat 2>/dev/null)
|
||||||
|
|
|
@ -3,23 +3,31 @@
|
||||||
|
|
||||||
NSString* error_domain = @"org.lokinet";
|
NSString* error_domain = @"org.lokinet";
|
||||||
|
|
||||||
|
|
||||||
// Receiving an incoming packet, presumably from libunbound. NB: this is called from the libuv
|
// Receiving an incoming packet, presumably from libunbound. NB: this is called from the libuv
|
||||||
// event loop.
|
// event loop.
|
||||||
static void on_request(uv_udp_t* socket, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags) {
|
static void
|
||||||
if (nread < 0) {
|
on_request(
|
||||||
|
uv_udp_t* socket,
|
||||||
|
ssize_t nread,
|
||||||
|
const uv_buf_t* buf,
|
||||||
|
const struct sockaddr* addr,
|
||||||
|
unsigned flags)
|
||||||
|
{
|
||||||
|
if (nread < 0)
|
||||||
|
{
|
||||||
NSLog(@"Read error: %s", uv_strerror(nread));
|
NSLog(@"Read error: %s", uv_strerror(nread));
|
||||||
free(buf->base);
|
free(buf->base);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nread == 0 || !addr) {
|
if (nread == 0 || !addr)
|
||||||
|
{
|
||||||
if (buf)
|
if (buf)
|
||||||
free(buf->base);
|
free(buf->base);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
LLARPDNSTrampoline* t = (__bridge LLARPDNSTrampoline*) socket->data;
|
LLARPDNSTrampoline* t = (__bridge LLARPDNSTrampoline*)socket->data;
|
||||||
|
|
||||||
// We configure libunbound to use just one single port so we'll just send replies to the last port
|
// We configure libunbound to use just one single port so we'll just send replies to the last port
|
||||||
// to talk to us. (And we're only listening on localhost in the first place).
|
// to talk to us. (And we're only listening on localhost in the first place).
|
||||||
|
@ -31,61 +39,67 @@ static void on_request(uv_udp_t* socket, ssize_t nread, const uv_buf_t* buf, con
|
||||||
[t flushWrites];
|
[t flushWrites];
|
||||||
}
|
}
|
||||||
|
|
||||||
static void on_sent(uv_udp_send_t* req, int status) {
|
static void
|
||||||
NSArray<NSData*>* datagrams = (__bridge_transfer NSArray<NSData*>*) req->data;
|
on_sent(uv_udp_send_t* req, int status)
|
||||||
|
{
|
||||||
|
NSArray<NSData*>* datagrams = (__bridge_transfer NSArray<NSData*>*)req->data;
|
||||||
free(req);
|
free(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
// NB: called from the libuv event loop (so we don't have to worry about the above and this one
|
// NB: called from the libuv event loop (so we don't have to worry about the above and this one
|
||||||
// running at once from different threads).
|
// running at once from different threads).
|
||||||
static void write_flusher(uv_async_t* async) {
|
static void
|
||||||
LLARPDNSTrampoline* t = (__bridge LLARPDNSTrampoline*) async->data;
|
write_flusher(uv_async_t* async)
|
||||||
|
{
|
||||||
|
LLARPDNSTrampoline* t = (__bridge LLARPDNSTrampoline*)async->data;
|
||||||
if (t->pending_writes.count == 0)
|
if (t->pending_writes.count == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
NSArray<NSData*>* data = [NSArray<NSData*> arrayWithArray:t->pending_writes];
|
NSArray<NSData*>* data = [NSArray<NSData*> arrayWithArray:t->pending_writes];
|
||||||
[t->pending_writes removeAllObjects];
|
[t->pending_writes removeAllObjects];
|
||||||
__weak LLARPDNSTrampoline* weakSelf = t;
|
__weak LLARPDNSTrampoline* weakSelf = t;
|
||||||
[t->upstream writeMultipleDatagrams:data completionHandler: ^(NSError* error)
|
[t->upstream writeMultipleDatagrams:data
|
||||||
{
|
completionHandler:^(NSError* error) {
|
||||||
if (error)
|
if (error)
|
||||||
NSLog(@"Failed to send request to upstream DNS: %@", error);
|
NSLog(@"Failed to send request to upstream DNS: %@", error);
|
||||||
|
|
||||||
// Trigger another flush in case anything built up while Apple was doing its things. Just
|
// Trigger another flush in case anything built up while Apple was doing its
|
||||||
// call it unconditionally (rather than checking the queue) because this handler is probably
|
// things. Just call it unconditionally (rather than checking the queue)
|
||||||
// running in some other thread.
|
// because this handler is probably running in some other thread.
|
||||||
[weakSelf flushWrites];
|
[weakSelf flushWrites];
|
||||||
}
|
}];
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
static void alloc_buffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
|
alloc_buffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf)
|
||||||
|
{
|
||||||
buf->base = malloc(suggested_size);
|
buf->base = malloc(suggested_size);
|
||||||
buf->len = suggested_size;
|
buf->len = suggested_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@implementation LLARPDNSTrampoline
|
@implementation LLARPDNSTrampoline
|
||||||
|
|
||||||
- (void)startWithUpstreamDns:(NWUDPSession*) dns
|
- (void)startWithUpstreamDns:(NWUDPSession*)dns
|
||||||
listenIp:(NSString*) listenIp
|
listenIp:(NSString*)listenIp
|
||||||
listenPort:(uint16_t) listenPort
|
listenPort:(uint16_t)listenPort
|
||||||
uvLoop:(uv_loop_t*) loop
|
uvLoop:(uv_loop_t*)loop
|
||||||
completionHandler:(void (^)(NSError* error))completionHandler
|
completionHandler:(void (^)(NSError* error))completionHandler
|
||||||
{
|
{
|
||||||
NSLog(@"Setting up trampoline");
|
NSLog(@"Setting up trampoline");
|
||||||
pending_writes = [[NSMutableArray<NSData*> alloc] init];
|
pending_writes = [[NSMutableArray<NSData*> alloc] init];
|
||||||
write_trigger.data = (__bridge void*) self;
|
write_trigger.data = (__bridge void*)self;
|
||||||
uv_async_init(loop, &write_trigger, write_flusher);
|
uv_async_init(loop, &write_trigger, write_flusher);
|
||||||
|
|
||||||
request_socket.data = (__bridge void*) self;
|
request_socket.data = (__bridge void*)self;
|
||||||
uv_udp_init(loop, &request_socket);
|
uv_udp_init(loop, &request_socket);
|
||||||
struct sockaddr_in recv_addr;
|
struct sockaddr_in recv_addr;
|
||||||
uv_ip4_addr(listenIp.UTF8String, listenPort, &recv_addr);
|
uv_ip4_addr(listenIp.UTF8String, listenPort, &recv_addr);
|
||||||
int ret = uv_udp_bind(&request_socket, (const struct sockaddr*) &recv_addr, UV_UDP_REUSEADDR);
|
int ret = uv_udp_bind(&request_socket, (const struct sockaddr*)&recv_addr, UV_UDP_REUSEADDR);
|
||||||
if (ret < 0) {
|
if (ret < 0)
|
||||||
NSString* errstr = [NSString stringWithFormat:@"Failed to start DNS trampoline: %s", uv_strerror(ret)];
|
{
|
||||||
NSError *err = [NSError errorWithDomain:error_domain code:ret userInfo:@{@"Error": errstr}];
|
NSString* errstr =
|
||||||
|
[NSString stringWithFormat:@"Failed to start DNS trampoline: %s", uv_strerror(ret)];
|
||||||
|
NSError* err = [NSError errorWithDomain:error_domain code:ret userInfo:@{@"Error": errstr}];
|
||||||
NSLog(@"%@", err);
|
NSLog(@"%@", err);
|
||||||
return completionHandler(err);
|
return completionHandler(err);
|
||||||
}
|
}
|
||||||
|
@ -95,30 +109,40 @@ static void alloc_buffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* b
|
||||||
|
|
||||||
upstream = dns;
|
upstream = dns;
|
||||||
__weak LLARPDNSTrampoline* weakSelf = self;
|
__weak LLARPDNSTrampoline* weakSelf = self;
|
||||||
[upstream setReadHandler: ^(NSArray<NSData*>* datagrams, NSError* error) {
|
[upstream
|
||||||
// Reading a reply back from the UDP socket used to talk to upstream
|
setReadHandler:^(NSArray<NSData*>* datagrams, NSError* error) {
|
||||||
if (error) {
|
// Reading a reply back from the UDP socket used to talk to upstream
|
||||||
NSLog(@"Reader handler failed: %@", error);
|
if (error)
|
||||||
return;
|
{
|
||||||
}
|
NSLog(@"Reader handler failed: %@", error);
|
||||||
LLARPDNSTrampoline* strongSelf = weakSelf;
|
return;
|
||||||
if (!strongSelf || datagrams.count == 0)
|
}
|
||||||
return;
|
LLARPDNSTrampoline* strongSelf = weakSelf;
|
||||||
|
if (!strongSelf || datagrams.count == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
uv_buf_t* buffers = malloc(datagrams.count * sizeof(uv_buf_t));
|
uv_buf_t* buffers = malloc(datagrams.count * sizeof(uv_buf_t));
|
||||||
size_t buf_count = 0;
|
size_t buf_count = 0;
|
||||||
for (NSData* packet in datagrams) {
|
for (NSData* packet in datagrams)
|
||||||
buffers[buf_count].base = (void*) packet.bytes;
|
{
|
||||||
buffers[buf_count].len = packet.length;
|
buffers[buf_count].base = (void*)packet.bytes;
|
||||||
buf_count++;
|
buffers[buf_count].len = packet.length;
|
||||||
}
|
buf_count++;
|
||||||
uv_udp_send_t* uvsend = malloc(sizeof(uv_udp_send_t));
|
}
|
||||||
uvsend->data = (__bridge_retained void*) datagrams;
|
uv_udp_send_t* uvsend = malloc(sizeof(uv_udp_send_t));
|
||||||
int ret = uv_udp_send(uvsend, &strongSelf->request_socket, buffers, buf_count, &strongSelf->reply_addr, on_sent);
|
uvsend->data = (__bridge_retained void*)datagrams;
|
||||||
free(buffers);
|
int ret = uv_udp_send(
|
||||||
if (ret < 0)
|
uvsend,
|
||||||
NSLog(@"Error returning DNS responses to unbound: %s", uv_strerror(ret));
|
&strongSelf->request_socket,
|
||||||
} maxDatagrams:NSUIntegerMax];
|
buffers,
|
||||||
|
buf_count,
|
||||||
|
&strongSelf->reply_addr,
|
||||||
|
on_sent);
|
||||||
|
free(buffers);
|
||||||
|
if (ret < 0)
|
||||||
|
NSLog(@"Error returning DNS responses to unbound: %s", uv_strerror(ret));
|
||||||
|
}
|
||||||
|
maxDatagrams:NSUIntegerMax];
|
||||||
|
|
||||||
completionHandler(nil);
|
completionHandler(nil);
|
||||||
}
|
}
|
||||||
|
@ -128,11 +152,11 @@ static void alloc_buffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* b
|
||||||
uv_async_send(&write_trigger);
|
uv_async_send(&write_trigger);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void) dealloc
|
- (void)dealloc
|
||||||
{
|
{
|
||||||
NSLog(@"Stopping DNS trampoline");
|
NSLog(@"Stopping DNS trampoline");
|
||||||
uv_close((uv_handle_t*) &request_socket, NULL);
|
uv_close((uv_handle_t*)&request_socket, NULL);
|
||||||
uv_close((uv_handle_t*) &write_trigger, NULL);
|
uv_close((uv_handle_t*)&write_trigger, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -9,9 +9,12 @@
|
||||||
{
|
{
|
||||||
void* lokinet;
|
void* lokinet;
|
||||||
llarp_incoming_packet packet_buf[LLARP_APPLE_PACKET_BUF_SIZE];
|
llarp_incoming_packet packet_buf[LLARP_APPLE_PACKET_BUF_SIZE];
|
||||||
@public NEPacketTunnelNetworkSettings* settings;
|
@public
|
||||||
@public NEIPv4Route* tun_route4;
|
NEPacketTunnelNetworkSettings* settings;
|
||||||
@public NEIPv6Route* tun_route6;
|
@public
|
||||||
|
NEIPv4Route* tun_route4;
|
||||||
|
@public
|
||||||
|
NEIPv6Route* tun_route6;
|
||||||
LLARPDNSTrampoline* dns_tramp;
|
LLARPDNSTrampoline* dns_tramp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,107 +33,133 @@
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
static void nslogger(const char* msg) { NSLog(@"%s", msg); }
|
static void
|
||||||
|
nslogger(const char* msg)
|
||||||
|
{
|
||||||
|
NSLog(@"%s", msg);
|
||||||
|
}
|
||||||
|
|
||||||
static void packet_writer(int af, const void* data, size_t size, void* ctx) {
|
static void
|
||||||
|
packet_writer(int af, const void* data, size_t size, void* ctx)
|
||||||
|
{
|
||||||
if (ctx == nil || data == nil)
|
if (ctx == nil || data == nil)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
NSData* buf = [NSData dataWithBytesNoCopy:(void*)data length:size freeWhenDone:NO];
|
NSData* buf = [NSData dataWithBytesNoCopy:(void*)data length:size freeWhenDone:NO];
|
||||||
LLARPPacketTunnel* t = (__bridge LLARPPacketTunnel*) ctx;
|
LLARPPacketTunnel* t = (__bridge LLARPPacketTunnel*)ctx;
|
||||||
[t.packetFlow writePackets:@[buf]
|
[t.packetFlow writePackets:@[buf] withProtocols:@[[NSNumber numberWithInt:af]]];
|
||||||
withProtocols:@[[NSNumber numberWithInt:af]]];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void start_packet_reader(void* ctx) {
|
static void
|
||||||
|
start_packet_reader(void* ctx)
|
||||||
|
{
|
||||||
if (ctx == nil)
|
if (ctx == nil)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
LLARPPacketTunnel* t = (__bridge LLARPPacketTunnel*) ctx;
|
LLARPPacketTunnel* t = (__bridge LLARPPacketTunnel*)ctx;
|
||||||
[t readPackets];
|
[t readPackets];
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_ipv4_route(const char* addr, const char* netmask, void* ctx) {
|
static void
|
||||||
|
add_ipv4_route(const char* addr, const char* netmask, void* ctx)
|
||||||
|
{
|
||||||
NSLog(@"Adding IPv4 route %s:%s to packet tunnel", addr, netmask);
|
NSLog(@"Adding IPv4 route %s:%s to packet tunnel", addr, netmask);
|
||||||
NEIPv4Route* route = [[NEIPv4Route alloc]
|
NEIPv4Route* route =
|
||||||
initWithDestinationAddress: [NSString stringWithUTF8String:addr]
|
[[NEIPv4Route alloc] initWithDestinationAddress:[NSString stringWithUTF8String:addr]
|
||||||
subnetMask: [NSString stringWithUTF8String:netmask]];
|
subnetMask:[NSString stringWithUTF8String:netmask]];
|
||||||
|
|
||||||
LLARPPacketTunnel* t = (__bridge LLARPPacketTunnel*) ctx;
|
LLARPPacketTunnel* t = (__bridge LLARPPacketTunnel*)ctx;
|
||||||
for (NEIPv4Route* r in t->settings.IPv4Settings.includedRoutes)
|
for (NEIPv4Route* r in t->settings.IPv4Settings.includedRoutes)
|
||||||
if ([r.destinationAddress isEqualToString:route.destinationAddress] &&
|
if ([r.destinationAddress isEqualToString:route.destinationAddress] &&
|
||||||
[r.destinationSubnetMask isEqualToString:route.destinationSubnetMask])
|
[r.destinationSubnetMask isEqualToString:route.destinationSubnetMask])
|
||||||
return; // Already in the settings, nothing to add.
|
return; // Already in the settings, nothing to add.
|
||||||
|
|
||||||
t->settings.IPv4Settings.includedRoutes =
|
t->settings.IPv4Settings.includedRoutes =
|
||||||
[t->settings.IPv4Settings.includedRoutes arrayByAddingObject:route];
|
[t->settings.IPv4Settings.includedRoutes arrayByAddingObject:route];
|
||||||
|
|
||||||
[t updateNetworkSettings];
|
[t updateNetworkSettings];
|
||||||
}
|
}
|
||||||
|
|
||||||
static void del_ipv4_route(const char* addr, const char* netmask, void* ctx) {
|
static void
|
||||||
|
del_ipv4_route(const char* addr, const char* netmask, void* ctx)
|
||||||
|
{
|
||||||
NSLog(@"Removing IPv4 route %s:%s to packet tunnel", addr, netmask);
|
NSLog(@"Removing IPv4 route %s:%s to packet tunnel", addr, netmask);
|
||||||
NEIPv4Route* route = [[NEIPv4Route alloc]
|
NEIPv4Route* route =
|
||||||
initWithDestinationAddress: [NSString stringWithUTF8String:addr]
|
[[NEIPv4Route alloc] initWithDestinationAddress:[NSString stringWithUTF8String:addr]
|
||||||
subnetMask: [NSString stringWithUTF8String:netmask]];
|
subnetMask:[NSString stringWithUTF8String:netmask]];
|
||||||
|
|
||||||
LLARPPacketTunnel* t = (__bridge LLARPPacketTunnel*) ctx;
|
LLARPPacketTunnel* t = (__bridge LLARPPacketTunnel*)ctx;
|
||||||
NSMutableArray<NEIPv4Route*>* routes = [NSMutableArray arrayWithArray:t->settings.IPv4Settings.includedRoutes];
|
NSMutableArray<NEIPv4Route*>* routes =
|
||||||
for (int i = 0; i < routes.count; i++) {
|
[NSMutableArray arrayWithArray:t->settings.IPv4Settings.includedRoutes];
|
||||||
|
for (int i = 0; i < routes.count; i++)
|
||||||
|
{
|
||||||
if ([routes[i].destinationAddress isEqualToString:route.destinationAddress] &&
|
if ([routes[i].destinationAddress isEqualToString:route.destinationAddress] &&
|
||||||
[routes[i].destinationSubnetMask isEqualToString:route.destinationSubnetMask]) {
|
[routes[i].destinationSubnetMask isEqualToString:route.destinationSubnetMask])
|
||||||
|
{
|
||||||
[routes removeObjectAtIndex:i];
|
[routes removeObjectAtIndex:i];
|
||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (routes.count != t->settings.IPv4Settings.includedRoutes.count) {
|
if (routes.count != t->settings.IPv4Settings.includedRoutes.count)
|
||||||
|
{
|
||||||
t->settings.IPv4Settings.includedRoutes = routes;
|
t->settings.IPv4Settings.includedRoutes = routes;
|
||||||
[t updateNetworkSettings];
|
[t updateNetworkSettings];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_ipv6_route(const char* addr, int prefix, void* ctx) {
|
static void
|
||||||
NEIPv6Route* route = [[NEIPv6Route alloc]
|
add_ipv6_route(const char* addr, int prefix, void* ctx)
|
||||||
initWithDestinationAddress: [NSString stringWithUTF8String:addr]
|
{
|
||||||
networkPrefixLength: [NSNumber numberWithInt:prefix]];
|
NEIPv6Route* route =
|
||||||
|
[[NEIPv6Route alloc] initWithDestinationAddress:[NSString stringWithUTF8String:addr]
|
||||||
|
networkPrefixLength:[NSNumber numberWithInt:prefix]];
|
||||||
|
|
||||||
LLARPPacketTunnel* t = (__bridge LLARPPacketTunnel*) ctx;
|
LLARPPacketTunnel* t = (__bridge LLARPPacketTunnel*)ctx;
|
||||||
for (NEIPv6Route* r in t->settings.IPv6Settings.includedRoutes)
|
for (NEIPv6Route* r in t->settings.IPv6Settings.includedRoutes)
|
||||||
if ([r.destinationAddress isEqualToString:route.destinationAddress] &&
|
if ([r.destinationAddress isEqualToString:route.destinationAddress] &&
|
||||||
[r.destinationNetworkPrefixLength isEqualToNumber:route.destinationNetworkPrefixLength])
|
[r.destinationNetworkPrefixLength isEqualToNumber:route.destinationNetworkPrefixLength])
|
||||||
return; // Already in the settings, nothing to add.
|
return; // Already in the settings, nothing to add.
|
||||||
|
|
||||||
t->settings.IPv6Settings.includedRoutes =
|
t->settings.IPv6Settings.includedRoutes =
|
||||||
[t->settings.IPv6Settings.includedRoutes arrayByAddingObject:route];
|
[t->settings.IPv6Settings.includedRoutes arrayByAddingObject:route];
|
||||||
|
|
||||||
[t updateNetworkSettings];
|
[t updateNetworkSettings];
|
||||||
}
|
}
|
||||||
|
|
||||||
static void del_ipv6_route(const char* addr, int prefix, void* ctx) {
|
static void
|
||||||
NEIPv6Route* route = [[NEIPv6Route alloc]
|
del_ipv6_route(const char* addr, int prefix, void* ctx)
|
||||||
initWithDestinationAddress: [NSString stringWithUTF8String:addr]
|
{
|
||||||
networkPrefixLength: [NSNumber numberWithInt:prefix]];
|
NEIPv6Route* route =
|
||||||
|
[[NEIPv6Route alloc] initWithDestinationAddress:[NSString stringWithUTF8String:addr]
|
||||||
|
networkPrefixLength:[NSNumber numberWithInt:prefix]];
|
||||||
|
|
||||||
LLARPPacketTunnel* t = (__bridge LLARPPacketTunnel*) ctx;
|
LLARPPacketTunnel* t = (__bridge LLARPPacketTunnel*)ctx;
|
||||||
NSMutableArray<NEIPv6Route*>* routes = [NSMutableArray arrayWithArray:t->settings.IPv6Settings.includedRoutes];
|
NSMutableArray<NEIPv6Route*>* routes =
|
||||||
for (int i = 0; i < routes.count; i++) {
|
[NSMutableArray arrayWithArray:t->settings.IPv6Settings.includedRoutes];
|
||||||
|
for (int i = 0; i < routes.count; i++)
|
||||||
|
{
|
||||||
if ([routes[i].destinationAddress isEqualToString:route.destinationAddress] &&
|
if ([routes[i].destinationAddress isEqualToString:route.destinationAddress] &&
|
||||||
[routes[i].destinationNetworkPrefixLength isEqualToNumber:route.destinationNetworkPrefixLength]) {
|
[routes[i].destinationNetworkPrefixLength
|
||||||
|
isEqualToNumber:route.destinationNetworkPrefixLength])
|
||||||
|
{
|
||||||
[routes removeObjectAtIndex:i];
|
[routes removeObjectAtIndex:i];
|
||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (routes.count != t->settings.IPv6Settings.includedRoutes.count) {
|
if (routes.count != t->settings.IPv6Settings.includedRoutes.count)
|
||||||
|
{
|
||||||
t->settings.IPv6Settings.includedRoutes = routes;
|
t->settings.IPv6Settings.includedRoutes = routes;
|
||||||
[t updateNetworkSettings];
|
[t updateNetworkSettings];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_default_route(void* ctx) {
|
static void
|
||||||
|
add_default_route(void* ctx)
|
||||||
|
{
|
||||||
NSLog(@"Making the tunnel the default route");
|
NSLog(@"Making the tunnel the default route");
|
||||||
LLARPPacketTunnel* t = (__bridge LLARPPacketTunnel*) ctx;
|
LLARPPacketTunnel* t = (__bridge LLARPPacketTunnel*)ctx;
|
||||||
|
|
||||||
t->settings.IPv4Settings.includedRoutes = @[NEIPv4Route.defaultRoute];
|
t->settings.IPv4Settings.includedRoutes = @[NEIPv4Route.defaultRoute];
|
||||||
t->settings.IPv6Settings.includedRoutes = @[NEIPv6Route.defaultRoute];
|
t->settings.IPv6Settings.includedRoutes = @[NEIPv6Route.defaultRoute];
|
||||||
|
@ -138,9 +167,11 @@ static void add_default_route(void* ctx) {
|
||||||
[t updateNetworkSettings];
|
[t updateNetworkSettings];
|
||||||
}
|
}
|
||||||
|
|
||||||
static void del_default_route(void* ctx) {
|
static void
|
||||||
|
del_default_route(void* ctx)
|
||||||
|
{
|
||||||
NSLog(@"Removing default route from tunnel");
|
NSLog(@"Removing default route from tunnel");
|
||||||
LLARPPacketTunnel* t = (__bridge LLARPPacketTunnel*) ctx;
|
LLARPPacketTunnel* t = (__bridge LLARPPacketTunnel*)ctx;
|
||||||
|
|
||||||
t->settings.IPv4Settings.includedRoutes = @[t->tun_route4];
|
t->settings.IPv4Settings.includedRoutes = @[t->tun_route4];
|
||||||
t->settings.IPv6Settings.includedRoutes = @[t->tun_route6];
|
t->settings.IPv6Settings.includedRoutes = @[t->tun_route6];
|
||||||
|
@ -152,12 +183,13 @@ static void del_default_route(void* ctx) {
|
||||||
|
|
||||||
- (void)readPackets
|
- (void)readPackets
|
||||||
{
|
{
|
||||||
[self.packetFlow readPacketObjectsWithCompletionHandler: ^(NSArray<NEPacket*>* packets) {
|
[self.packetFlow readPacketObjectsWithCompletionHandler:^(NSArray<NEPacket*>* packets) {
|
||||||
if (lokinet == nil)
|
if (lokinet == nil)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
for (NEPacket* p in packets) {
|
for (NEPacket* p in packets)
|
||||||
|
{
|
||||||
packet_buf[size].bytes = p.data.bytes;
|
packet_buf[size].bytes = p.data.bytes;
|
||||||
packet_buf[size].size = p.data.length;
|
packet_buf[size].size = p.data.length;
|
||||||
size++;
|
size++;
|
||||||
|
@ -186,19 +218,21 @@ static void del_default_route(void* ctx) {
|
||||||
.ns_logger = nslogger,
|
.ns_logger = nslogger,
|
||||||
.packet_writer = packet_writer,
|
.packet_writer = packet_writer,
|
||||||
.start_reading = start_packet_reader,
|
.start_reading = start_packet_reader,
|
||||||
.route_callbacks = {
|
.route_callbacks =
|
||||||
.add_ipv4_route = add_ipv4_route,
|
{.add_ipv4_route = add_ipv4_route,
|
||||||
.del_ipv4_route = del_ipv4_route,
|
.del_ipv4_route = del_ipv4_route,
|
||||||
.add_ipv6_route = add_ipv6_route,
|
.add_ipv6_route = add_ipv6_route,
|
||||||
.del_ipv6_route = del_ipv6_route,
|
.del_ipv6_route = del_ipv6_route,
|
||||||
.add_default_route = add_default_route,
|
.add_default_route = add_default_route,
|
||||||
.del_default_route = del_default_route
|
.del_default_route = del_default_route},
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
lokinet = llarp_apple_init(&conf);
|
lokinet = llarp_apple_init(&conf);
|
||||||
if (!lokinet) {
|
if (!lokinet)
|
||||||
NSError *init_failure = [NSError errorWithDomain:error_domain code:500 userInfo:@{@"Error": @"Failed to initialize lokinet"}];
|
{
|
||||||
|
NSError* init_failure = [NSError errorWithDomain:error_domain
|
||||||
|
code:500
|
||||||
|
userInfo:@{@"Error": @"Failed to initialize lokinet"}];
|
||||||
NSLog(@"%@", [init_failure localizedDescription]);
|
NSLog(@"%@", [init_failure localizedDescription]);
|
||||||
return completionHandler(init_failure);
|
return completionHandler(init_failure);
|
||||||
}
|
}
|
||||||
|
@ -237,11 +271,12 @@ static void del_default_route(void* ctx) {
|
||||||
|
|
||||||
NWHostEndpoint* upstreamdns_ep;
|
NWHostEndpoint* upstreamdns_ep;
|
||||||
if (strlen(conf.upstream_dns))
|
if (strlen(conf.upstream_dns))
|
||||||
upstreamdns_ep = [NWHostEndpoint endpointWithHostname:[NSString stringWithUTF8String:conf.upstream_dns] port:@(conf.upstream_dns_port).stringValue];
|
upstreamdns_ep =
|
||||||
|
[NWHostEndpoint endpointWithHostname:[NSString stringWithUTF8String:conf.upstream_dns]
|
||||||
|
port:@(conf.upstream_dns_port).stringValue];
|
||||||
|
|
||||||
NEIPv4Settings* ipv4 = [[NEIPv4Settings alloc] initWithAddresses:@[ip]
|
NEIPv4Settings* ipv4 = [[NEIPv4Settings alloc] initWithAddresses:@[ip] subnetMasks:@[mask]];
|
||||||
subnetMasks:@[mask]];
|
tun_route4 = [[NEIPv4Route alloc] initWithDestinationAddress:ip subnetMask:mask];
|
||||||
tun_route4 = [[NEIPv4Route alloc] initWithDestinationAddress:ip subnetMask: mask];
|
|
||||||
ipv4.includedRoutes = @[tun_route4];
|
ipv4.includedRoutes = @[tun_route4];
|
||||||
settings.IPv4Settings = ipv4;
|
settings.IPv4Settings = ipv4;
|
||||||
|
|
||||||
|
@ -249,50 +284,62 @@ static void del_default_route(void* ctx) {
|
||||||
NSNumber* ip6_prefix = [NSNumber numberWithUnsignedInt:conf.tunnel_ipv6_prefix];
|
NSNumber* ip6_prefix = [NSNumber numberWithUnsignedInt:conf.tunnel_ipv6_prefix];
|
||||||
NEIPv6Settings* ipv6 = [[NEIPv6Settings alloc] initWithAddresses:@[ip6]
|
NEIPv6Settings* ipv6 = [[NEIPv6Settings alloc] initWithAddresses:@[ip6]
|
||||||
networkPrefixLengths:@[ip6_prefix]];
|
networkPrefixLengths:@[ip6_prefix]];
|
||||||
tun_route6 = [[NEIPv6Route alloc] initWithDestinationAddress:ip6
|
tun_route6 = [[NEIPv6Route alloc] initWithDestinationAddress:ip6 networkPrefixLength:ip6_prefix];
|
||||||
networkPrefixLength:ip6_prefix];
|
|
||||||
ipv6.includedRoutes = @[tun_route6];
|
ipv6.includedRoutes = @[tun_route6];
|
||||||
settings.IPv6Settings = ipv6;
|
settings.IPv6Settings = ipv6;
|
||||||
|
|
||||||
__weak LLARPPacketTunnel* weakSelf = self;
|
__weak LLARPPacketTunnel* weakSelf = self;
|
||||||
[self setTunnelNetworkSettings:settings completionHandler:^(NSError* err) {
|
[self setTunnelNetworkSettings:settings
|
||||||
if (err) {
|
completionHandler:^(NSError* err) {
|
||||||
NSLog(@"Failed to configure lokinet tunnel: %@", err);
|
if (err)
|
||||||
return completionHandler(err);
|
{
|
||||||
}
|
NSLog(@"Failed to configure lokinet tunnel: %@", err);
|
||||||
LLARPPacketTunnel* strongSelf = weakSelf;
|
return completionHandler(err);
|
||||||
if (!strongSelf)
|
}
|
||||||
return completionHandler(nil);
|
LLARPPacketTunnel* strongSelf = weakSelf;
|
||||||
|
if (!strongSelf)
|
||||||
|
return completionHandler(nil);
|
||||||
|
|
||||||
int start_ret = llarp_apple_start(strongSelf->lokinet, (__bridge void*) strongSelf);
|
int start_ret = llarp_apple_start(strongSelf->lokinet, (__bridge void*)strongSelf);
|
||||||
if (start_ret != 0) {
|
if (start_ret != 0)
|
||||||
NSError *start_failure = [NSError errorWithDomain:error_domain code:start_ret userInfo:@{@"Error": @"Failed to start lokinet"}];
|
{
|
||||||
NSLog(@"%@", start_failure);
|
NSError* start_failure =
|
||||||
lokinet = nil;
|
[NSError errorWithDomain:error_domain
|
||||||
return completionHandler(start_failure);
|
code:start_ret
|
||||||
}
|
userInfo:@{@"Error": @"Failed to start lokinet"}];
|
||||||
|
NSLog(@"%@", start_failure);
|
||||||
|
lokinet = nil;
|
||||||
|
return completionHandler(start_failure);
|
||||||
|
}
|
||||||
|
|
||||||
NSString* dns_tramp_ip = @"127.0.0.1";
|
NSString* dns_tramp_ip = @"127.0.0.1";
|
||||||
NSLog(@"Starting DNS exit mode trampoline to %@ on %@:%d", upstreamdns_ep, dns_tramp_ip, dns_trampoline_port);
|
NSLog(
|
||||||
NWUDPSession* upstreamdns = [strongSelf createUDPSessionThroughTunnelToEndpoint:upstreamdns_ep fromEndpoint:nil];
|
@"Starting DNS exit mode trampoline to %@ on %@:%d",
|
||||||
strongSelf->dns_tramp = [LLARPDNSTrampoline alloc];
|
upstreamdns_ep,
|
||||||
[strongSelf->dns_tramp
|
dns_tramp_ip,
|
||||||
startWithUpstreamDns:upstreamdns
|
dns_trampoline_port);
|
||||||
listenIp:dns_tramp_ip
|
NWUDPSession* upstreamdns =
|
||||||
listenPort:dns_trampoline_port
|
[strongSelf createUDPSessionThroughTunnelToEndpoint:upstreamdns_ep
|
||||||
uvLoop:llarp_apple_get_uv_loop(strongSelf->lokinet)
|
fromEndpoint:nil];
|
||||||
completionHandler:^(NSError* error) {
|
strongSelf->dns_tramp = [LLARPDNSTrampoline alloc];
|
||||||
if (error)
|
[strongSelf->dns_tramp
|
||||||
NSLog(@"Error starting dns trampoline: %@", error);
|
startWithUpstreamDns:upstreamdns
|
||||||
return completionHandler(error);
|
listenIp:dns_tramp_ip
|
||||||
}];
|
listenPort:dns_trampoline_port
|
||||||
}];
|
uvLoop:llarp_apple_get_uv_loop(strongSelf->lokinet)
|
||||||
|
completionHandler:^(NSError* error) {
|
||||||
|
if (error)
|
||||||
|
NSLog(@"Error starting dns trampoline: %@", error);
|
||||||
|
return completionHandler(error);
|
||||||
|
}];
|
||||||
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)stopTunnelWithReason:(NEProviderStopReason)reason
|
- (void)stopTunnelWithReason:(NEProviderStopReason)reason
|
||||||
completionHandler:(void (^)(void))completionHandler
|
completionHandler:(void (^)(void))completionHandler
|
||||||
{
|
{
|
||||||
if (lokinet) {
|
if (lokinet)
|
||||||
|
{
|
||||||
llarp_apple_shutdown(lokinet);
|
llarp_apple_shutdown(lokinet);
|
||||||
lokinet = nil;
|
lokinet = nil;
|
||||||
}
|
}
|
||||||
|
@ -319,29 +366,35 @@ static void del_default_route(void* ctx) {
|
||||||
//
|
//
|
||||||
// Thanks for the accurate documentation, Apple.
|
// Thanks for the accurate documentation, Apple.
|
||||||
//
|
//
|
||||||
[self setTunnelNetworkSettings:nil completionHandler:^(NSError* err) {
|
[self setTunnelNetworkSettings:nil
|
||||||
if (err)
|
completionHandler:^(NSError* err) {
|
||||||
NSLog(@"Failed to clear lokinet tunnel settings: %@", err);
|
if (err)
|
||||||
LLARPPacketTunnel* strongSelf = weakSelf;
|
NSLog(@"Failed to clear lokinet tunnel settings: %@", err);
|
||||||
if (strongSelf) {
|
LLARPPacketTunnel* strongSelf = weakSelf;
|
||||||
[weakSelf setTunnelNetworkSettings:strongSelf->settings completionHandler:^(NSError* err) {
|
if (strongSelf)
|
||||||
LLARPPacketTunnel* strongSelf = weakSelf;
|
{
|
||||||
if (strongSelf)
|
[weakSelf
|
||||||
strongSelf.reasserting = NO;
|
setTunnelNetworkSettings:strongSelf->settings
|
||||||
if (err)
|
completionHandler:^(NSError* err) {
|
||||||
NSLog(@"Failed to reconfigure lokinet tunnel settings: %@", err);
|
LLARPPacketTunnel* strongSelf = weakSelf;
|
||||||
}];
|
if (strongSelf)
|
||||||
}
|
strongSelf.reasserting = NO;
|
||||||
}];
|
if (err)
|
||||||
|
NSLog(@"Failed to reconfigure lokinet tunnel settings: %@", err);
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
#ifdef MACOS_SYSTEM_EXTENSION
|
#ifdef MACOS_SYSTEM_EXTENSION
|
||||||
|
|
||||||
int main() {
|
int
|
||||||
[NEProvider startSystemExtensionMode];
|
main()
|
||||||
dispatch_main();
|
{
|
||||||
|
[NEProvider startSystemExtensionMode];
|
||||||
|
dispatch_main();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue