Read relevant routing table
This commit is contained in:
parent
d44d08c95e
commit
4295e63c98
|
@ -32,6 +32,12 @@
|
|||
0E07597F20F0060E00F38FD8 /* CryptoAEAD.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E07597C20F0060E00F38FD8 /* CryptoAEAD.h */; };
|
||||
0E07598020F0060E00F38FD8 /* CryptoAEAD.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E07597D20F0060E00F38FD8 /* CryptoAEAD.m */; };
|
||||
0E07598120F0060E00F38FD8 /* CryptoAEAD.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E07597D20F0060E00F38FD8 /* CryptoAEAD.m */; };
|
||||
0E0B203C227886AD007A3CB9 /* RoutingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0B203B227886AD007A3CB9 /* RoutingTests.swift */; };
|
||||
0E0B203D227886AD007A3CB9 /* RoutingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0B203B227886AD007A3CB9 /* RoutingTests.swift */; };
|
||||
0E0B20402278A85C007A3CB9 /* RoutingTableEntry.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E0B203E2278A85B007A3CB9 /* RoutingTableEntry.h */; };
|
||||
0E0B20412278A85C007A3CB9 /* RoutingTableEntry.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E0B203E2278A85B007A3CB9 /* RoutingTableEntry.h */; };
|
||||
0E0B20422278A85C007A3CB9 /* RoutingTableEntry.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E0B203F2278A85B007A3CB9 /* RoutingTableEntry.m */; };
|
||||
0E0B20432278A85C007A3CB9 /* RoutingTableEntry.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E0B203F2278A85B007A3CB9 /* RoutingTableEntry.m */; };
|
||||
0E0C2125212ED29D008AB282 /* SessionError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0C2123212ED29D008AB282 /* SessionError.swift */; };
|
||||
0E0C2126212ED29D008AB282 /* SessionError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0C2123212ED29D008AB282 /* SessionError.swift */; };
|
||||
0E0C2127212ED29D008AB282 /* SessionProxy+Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0C2124212ED29D008AB282 /* SessionProxy+Configuration.swift */; };
|
||||
|
@ -177,6 +183,10 @@
|
|||
0EF5CF262141E142004FF1BD /* PacketMacros.h in Headers */ = {isa = PBXBuildFile; fileRef = 0EE7A79420F61EDC00B42E6A /* PacketMacros.h */; };
|
||||
0EF5CF272141E15B004FF1BD /* PacketMacros.h in Headers */ = {isa = PBXBuildFile; fileRef = 0EE7A79420F61EDC00B42E6A /* PacketMacros.h */; };
|
||||
0EF5CF282141E183004FF1BD /* CompressionFramingNative.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E245D6B2137F73600B012A2 /* CompressionFramingNative.h */; };
|
||||
0EFB902922788511006405E4 /* RoutingTable.h in Headers */ = {isa = PBXBuildFile; fileRef = 0EFB902722788511006405E4 /* RoutingTable.h */; };
|
||||
0EFB902A22788511006405E4 /* RoutingTable.h in Headers */ = {isa = PBXBuildFile; fileRef = 0EFB902722788511006405E4 /* RoutingTable.h */; };
|
||||
0EFB902B22788512006405E4 /* RoutingTable.m in Sources */ = {isa = PBXBuildFile; fileRef = 0EFB902822788511006405E4 /* RoutingTable.m */; };
|
||||
0EFB902C22788512006405E4 /* RoutingTable.m in Sources */ = {isa = PBXBuildFile; fileRef = 0EFB902822788511006405E4 /* RoutingTable.m */; };
|
||||
0EFEB4552006D3C800F81029 /* SessionProxy+EncryptionBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EFEB42A2006D3C800F81029 /* SessionProxy+EncryptionBridge.swift */; };
|
||||
0EFEB4562006D3C800F81029 /* SessionProxy+SessionKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EFEB42B2006D3C800F81029 /* SessionProxy+SessionKey.swift */; };
|
||||
0EFEB4582006D3C800F81029 /* MSS.h in Headers */ = {isa = PBXBuildFile; fileRef = 0EFEB42D2006D3C800F81029 /* MSS.h */; };
|
||||
|
@ -290,6 +300,9 @@
|
|||
0E07596D20EF79B400F38FD8 /* CryptoCBC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CryptoCBC.h; sourceTree = "<group>"; };
|
||||
0E07597C20F0060E00F38FD8 /* CryptoAEAD.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CryptoAEAD.h; sourceTree = "<group>"; };
|
||||
0E07597D20F0060E00F38FD8 /* CryptoAEAD.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CryptoAEAD.m; sourceTree = "<group>"; };
|
||||
0E0B203B227886AD007A3CB9 /* RoutingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoutingTests.swift; sourceTree = "<group>"; };
|
||||
0E0B203E2278A85B007A3CB9 /* RoutingTableEntry.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RoutingTableEntry.h; sourceTree = "<group>"; };
|
||||
0E0B203F2278A85B007A3CB9 /* RoutingTableEntry.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RoutingTableEntry.m; sourceTree = "<group>"; };
|
||||
0E0C2123212ED29D008AB282 /* SessionError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SessionError.swift; sourceTree = "<group>"; };
|
||||
0E0C2124212ED29D008AB282 /* SessionProxy+Configuration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "SessionProxy+Configuration.swift"; sourceTree = "<group>"; };
|
||||
0E11089A1F77B9E800A92462 /* TunnelKitTests-iOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "TunnelKitTests-iOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
|
@ -372,6 +385,8 @@
|
|||
0EE7A79D20F6488400B42E6A /* DataPathCrypto.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DataPathCrypto.h; sourceTree = "<group>"; };
|
||||
0EE7A7A020F664AB00B42E6A /* DataPathEncryptionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataPathEncryptionTests.swift; sourceTree = "<group>"; };
|
||||
0EEC49DB20B5E732008FEB91 /* Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
|
||||
0EFB902722788511006405E4 /* RoutingTable.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RoutingTable.h; sourceTree = "<group>"; };
|
||||
0EFB902822788511006405E4 /* RoutingTable.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RoutingTable.m; sourceTree = "<group>"; };
|
||||
0EFEB42A2006D3C800F81029 /* SessionProxy+EncryptionBridge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "SessionProxy+EncryptionBridge.swift"; sourceTree = "<group>"; };
|
||||
0EFEB42B2006D3C800F81029 /* SessionProxy+SessionKey.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "SessionProxy+SessionKey.swift"; sourceTree = "<group>"; };
|
||||
0EFEB42D2006D3C800F81029 /* MSS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MSS.h; sourceTree = "<group>"; };
|
||||
|
@ -489,6 +504,7 @@
|
|||
0E245D682135972800B012A2 /* PushTests.swift */,
|
||||
0EB2B45620F0BD16004233D7 /* RandomTests.swift */,
|
||||
0EB2B45C20F0BF41004233D7 /* RawPerformanceTests.swift */,
|
||||
0E0B203B227886AD007A3CB9 /* RoutingTests.swift */,
|
||||
0E041D0B2152E80A0025FE3C /* StaticKeyTests.swift */,
|
||||
0EB2B45A20F0BE4C004233D7 /* TestUtils.swift */,
|
||||
0E749F612178911C00BB2701 /* pia-2048.pem */,
|
||||
|
@ -678,6 +694,10 @@
|
|||
0EFEB4382006D3C800F81029 /* ProtocolMacros.swift */,
|
||||
0EFEB4392006D3C800F81029 /* ReplayProtector.h */,
|
||||
0EFEB4482006D3C800F81029 /* ReplayProtector.m */,
|
||||
0EFB902722788511006405E4 /* RoutingTable.h */,
|
||||
0EFB902822788511006405E4 /* RoutingTable.m */,
|
||||
0E0B203E2278A85B007A3CB9 /* RoutingTableEntry.h */,
|
||||
0E0B203F2278A85B007A3CB9 /* RoutingTableEntry.m */,
|
||||
0EFEB4372006D3C800F81029 /* SecureRandom.swift */,
|
||||
0E0C2123212ED29D008AB282 /* SessionError.swift */,
|
||||
0EFEB43C2006D3C800F81029 /* SessionProxy.swift */,
|
||||
|
@ -765,12 +785,14 @@
|
|||
0E07596E20EF79B400F38FD8 /* CryptoCBC.h in Headers */,
|
||||
0E58BF3522405410006FB157 /* lzodefs.h in Headers */,
|
||||
0E58BF502240F98F006FB157 /* CompressionAlgorithmNative.h in Headers */,
|
||||
0E0B20402278A85C007A3CB9 /* RoutingTableEntry.h in Headers */,
|
||||
0E07596320EF733F00F38FD8 /* CryptoMacros.h in Headers */,
|
||||
0EFEB46E2006D3C800F81029 /* TLSBox.h in Headers */,
|
||||
0E411B9B2271F90700E0852C /* DNS.h in Headers */,
|
||||
0E07596B20EF79AB00F38FD8 /* Crypto.h in Headers */,
|
||||
0EFEB46B2006D3C800F81029 /* CryptoBox.h in Headers */,
|
||||
0EFEB4592006D3C800F81029 /* Allocation.h in Headers */,
|
||||
0EFB902922788511006405E4 /* RoutingTable.h in Headers */,
|
||||
0EFEB4582006D3C800F81029 /* MSS.h in Headers */,
|
||||
0E58BF3322405410006FB157 /* lzoconf.h in Headers */,
|
||||
0E245D6C2137F73600B012A2 /* CompressionFramingNative.h in Headers */,
|
||||
|
@ -795,12 +817,14 @@
|
|||
0E07596F20EF79B400F38FD8 /* CryptoCBC.h in Headers */,
|
||||
0E58BF3622405410006FB157 /* lzodefs.h in Headers */,
|
||||
0E58BF512240F98F006FB157 /* CompressionAlgorithmNative.h in Headers */,
|
||||
0E0B20412278A85C007A3CB9 /* RoutingTableEntry.h in Headers */,
|
||||
0E07596420EF733F00F38FD8 /* CryptoMacros.h in Headers */,
|
||||
0EEC49EA20B5F7F6008FEB91 /* ZeroingData.h in Headers */,
|
||||
0E411B9C2271F90700E0852C /* DNS.h in Headers */,
|
||||
0E07596C20EF79AB00F38FD8 /* Crypto.h in Headers */,
|
||||
0EEC49E120B5F7EA008FEB91 /* Allocation.h in Headers */,
|
||||
0EEC49E320B5F7F6008FEB91 /* DataPath.h in Headers */,
|
||||
0EFB902A22788511006405E4 /* RoutingTable.h in Headers */,
|
||||
0EF5CF282141E183004FF1BD /* CompressionFramingNative.h in Headers */,
|
||||
0E58BF3422405410006FB157 /* lzoconf.h in Headers */,
|
||||
0EEC49E820B5F7F6008FEB91 /* ReplayProtector.h in Headers */,
|
||||
|
@ -1185,6 +1209,7 @@
|
|||
0E041D0C2152E80A0025FE3C /* StaticKeyTests.swift in Sources */,
|
||||
0E245D692135972800B012A2 /* PushTests.swift in Sources */,
|
||||
0EB2B46120F0C0A4004233D7 /* DataPathPerformanceTests.swift in Sources */,
|
||||
0E0B203C227886AD007A3CB9 /* RoutingTests.swift in Sources */,
|
||||
0EB2B45F20F0C098004233D7 /* EncryptionPerformanceTests.swift in Sources */,
|
||||
0EE7A7A120F664AC00B42E6A /* DataPathEncryptionTests.swift in Sources */,
|
||||
0EB2B45D20F0BF41004233D7 /* RawPerformanceTests.swift in Sources */,
|
||||
|
@ -1215,6 +1240,7 @@
|
|||
0EEC49DC20B5E732008FEB91 /* Utils.swift in Sources */,
|
||||
0EFEB4562006D3C800F81029 /* SessionProxy+SessionKey.swift in Sources */,
|
||||
0EC1BBA520D71190007C4C7B /* DNSResolver.swift in Sources */,
|
||||
0E0B20422278A85C007A3CB9 /* RoutingTableEntry.m in Sources */,
|
||||
0E58BF5922411FEF006FB157 /* LZO.m in Sources */,
|
||||
0EFEB4AB200760EC00F81029 /* MemoryDestination.swift in Sources */,
|
||||
0E12B29E21449ADB00B4BAE9 /* NSRegularExpression+Shortcuts.swift in Sources */,
|
||||
|
@ -1233,6 +1259,7 @@
|
|||
0EFEB45C2006D3C800F81029 /* ZeroingData.m in Sources */,
|
||||
0EFEB4632006D3C800F81029 /* ProtocolMacros.swift in Sources */,
|
||||
0EFEB4AC200760EC00F81029 /* InterfaceObserver.swift in Sources */,
|
||||
0EFB902B22788512006405E4 /* RoutingTable.m in Sources */,
|
||||
0EFEB46D2006D3C800F81029 /* Data+Manipulation.swift in Sources */,
|
||||
0EFEB47B2006D3C800F81029 /* TunnelKitProvider.swift in Sources */,
|
||||
0ECE3528212EB7770040F253 /* CryptoContainer.swift in Sources */,
|
||||
|
@ -1285,6 +1312,7 @@
|
|||
0EE7A79920F6296F00B42E6A /* PacketMacros.m in Sources */,
|
||||
0EEC49DD20B5E732008FEB91 /* Utils.swift in Sources */,
|
||||
0EFEB4B12007627700F81029 /* MemoryDestination.swift in Sources */,
|
||||
0E0B20432278A85C007A3CB9 /* RoutingTableEntry.m in Sources */,
|
||||
0E58BF5A22411FEF006FB157 /* LZO.m in Sources */,
|
||||
0EC1BBA620D712DE007C4C7B /* DNSResolver.swift in Sources */,
|
||||
0E12B29F21449ADB00B4BAE9 /* NSRegularExpression+Shortcuts.swift in Sources */,
|
||||
|
@ -1303,6 +1331,7 @@
|
|||
0EFEB4982006D7F300F81029 /* ZeroingData.swift in Sources */,
|
||||
0EFEB4A32006D7F300F81029 /* Errors.m in Sources */,
|
||||
0EFEB4A22006D7F300F81029 /* CoreConfiguration.swift in Sources */,
|
||||
0EFB902C22788512006405E4 /* RoutingTable.m in Sources */,
|
||||
0EFEB4952006D7F300F81029 /* SecureRandom.swift in Sources */,
|
||||
0EFEB49A2006D7F300F81029 /* MSS.m in Sources */,
|
||||
0ECE352A212EB88E0040F253 /* CryptoContainer.swift in Sources */,
|
||||
|
@ -1358,6 +1387,7 @@
|
|||
0EA82A302190B2B9007960EB /* ControlChannelTests.swift in Sources */,
|
||||
0EA82A312190B2B9007960EB /* DataManipulationTests.swift in Sources */,
|
||||
0EA82A382190B2B9007960EB /* PacketTests.swift in Sources */,
|
||||
0E0B203D227886AD007A3CB9 /* RoutingTests.swift in Sources */,
|
||||
0EA82A3B2190B2B9007960EB /* RawPerformanceTests.swift in Sources */,
|
||||
0EA82A362190B2B9007960EB /* EncryptionTests.swift in Sources */,
|
||||
0EA82A3D2190B2B9007960EB /* TestUtils.swift in Sources */,
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
//
|
||||
// RoutingTable.h
|
||||
// TunnelKit
|
||||
//
|
||||
// Created by Davide De Rosa on 4/30/19.
|
||||
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
|
||||
//
|
||||
// https://github.com/keeshux
|
||||
//
|
||||
// This file is part of TunnelKit.
|
||||
//
|
||||
// TunnelKit is free software: you can redistribute it 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.
|
||||
//
|
||||
// TunnelKit 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 TunnelKit. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#import "RoutingTableEntry.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface RoutingTable : NSObject
|
||||
|
||||
- (NSArray<RoutingTableEntry *> *)ipv4;
|
||||
- (NSArray<RoutingTableEntry *> *)ipv6;
|
||||
- (nullable RoutingTableEntry *)defaultGateway4;
|
||||
- (nullable RoutingTableEntry *)defaultGateway6;
|
||||
- (nullable RoutingTableEntry *)broadestRoute4MatchingDestination:(NSString *)destination;
|
||||
- (nullable RoutingTableEntry *)broadestRoute6MatchingDestination:(NSString *)destination;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
|
@ -0,0 +1,162 @@
|
|||
//
|
||||
// RoutingTable.m
|
||||
// TunnelKit
|
||||
//
|
||||
// Created by Davide De Rosa on 4/30/19.
|
||||
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
|
||||
//
|
||||
// https://github.com/keeshux
|
||||
//
|
||||
// This file is part of TunnelKit.
|
||||
//
|
||||
// TunnelKit is free software: you can redistribute it 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.
|
||||
//
|
||||
// TunnelKit 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 TunnelKit. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
#import <sys/sysctl.h>
|
||||
#import <net/route.h>
|
||||
|
||||
#import "RoutingTable.h"
|
||||
#import "Allocation.h"
|
||||
|
||||
// adapted from: https://github.com/jianpx/ios-cabin
|
||||
|
||||
@interface RoutingTableEntry ()
|
||||
|
||||
- (instancetype)initWithRTM:(const struct rt_msghdr2 *)rtm;
|
||||
|
||||
@end
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@interface RoutingTable ()
|
||||
|
||||
@property (nonatomic, strong) NSArray<RoutingTableEntry *> *ipv4;
|
||||
@property (nonatomic, strong) NSArray<RoutingTableEntry *> *ipv6;
|
||||
|
||||
@end
|
||||
|
||||
@implementation RoutingTable
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
if (!(self = [super init])) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
int mib[] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_DUMP2, 0 };
|
||||
const int mibLen = sizeof(mib) / sizeof(int);
|
||||
size_t len;
|
||||
if (sysctl(mib, mibLen, NULL, &len, NULL, 0) || (len <= 0)) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
char *buf = allocate_safely(len);
|
||||
if (!buf) {
|
||||
return nil;
|
||||
}
|
||||
if (sysctl(mib, mibLen, buf, &len, NULL, 0)) {
|
||||
free(buf);
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSMutableArray<RoutingTableEntry *> *entries4 = [[NSMutableArray alloc] init];
|
||||
NSMutableArray<RoutingTableEntry *> *entries6 = [[NSMutableArray alloc] init];
|
||||
|
||||
for (const char *ptr = buf; ptr < buf + len;) {
|
||||
const struct rt_msghdr2 *rtm = (struct rt_msghdr2 *)ptr;
|
||||
|
||||
if (rtm->rtm_addrs & RTA_DST) {
|
||||
struct sockaddr *dst_sa = (struct sockaddr *)(rtm + 1); // XXX: why +1 ?!?
|
||||
|
||||
if (((dst_sa->sa_family == AF_INET) || (dst_sa->sa_family == AF_INET6)) && !((rtm->rtm_flags & RTF_WASCLONED) && (rtm->rtm_parentflags & RTF_PRCLONING))) {
|
||||
RoutingTableEntry *entry = [[RoutingTableEntry alloc] initWithRTM:rtm];
|
||||
if (!entry) {
|
||||
continue;
|
||||
}
|
||||
if (dst_sa->sa_family == AF_INET) {
|
||||
[entries4 addObject:entry];
|
||||
} else if (dst_sa->sa_family == AF_INET6) {
|
||||
[entries6 addObject:entry];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ptr += rtm->rtm_msglen;
|
||||
}
|
||||
|
||||
free(buf);
|
||||
|
||||
self.ipv4 = entries4;
|
||||
self.ipv6 = entries6;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (RoutingTableEntry *)defaultGateway4
|
||||
{
|
||||
for (RoutingTableEntry *entry in self.ipv4) {
|
||||
if ([entry isDefault]) {
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (RoutingTableEntry *)defaultGateway6
|
||||
{
|
||||
for (RoutingTableEntry *entry in self.ipv6) {
|
||||
if ([entry isDefault]) {
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (RoutingTableEntry *)broadestRoute4MatchingDestination:(NSString *)destination
|
||||
{
|
||||
RoutingTableEntry *defaultRoute;
|
||||
RoutingTableEntry *minRoute;
|
||||
NSInteger minPrefix = 32 + 1;
|
||||
for (RoutingTableEntry *route in self.ipv4) {
|
||||
if ([route isDefault]) { // leave last
|
||||
defaultRoute = route;
|
||||
continue;
|
||||
}
|
||||
if ([route matchesDestination:destination] && route.prefix < minPrefix) {
|
||||
minRoute = route;
|
||||
minPrefix = route.prefix;
|
||||
}
|
||||
}
|
||||
return minRoute ?: defaultRoute;
|
||||
}
|
||||
|
||||
- (RoutingTableEntry *)broadestRoute6MatchingDestination:(NSString *)destination
|
||||
{
|
||||
RoutingTableEntry *defaultRoute;
|
||||
RoutingTableEntry *minRoute;
|
||||
NSInteger minPrefix = 128 + 1;
|
||||
for (RoutingTableEntry *route in self.ipv6) {
|
||||
if ([route isDefault]) { // leave last
|
||||
defaultRoute = route;
|
||||
continue;
|
||||
}
|
||||
if ([route matchesDestination:destination] && route.prefix < minPrefix) {
|
||||
minRoute = route;
|
||||
minPrefix = route.prefix;
|
||||
}
|
||||
}
|
||||
return minRoute ?: defaultRoute;
|
||||
}
|
||||
|
||||
@end
|
|
@ -0,0 +1,46 @@
|
|||
//
|
||||
// RoutingTableEntry.h
|
||||
// TunnelKit
|
||||
//
|
||||
// Created by Davide De Rosa on 4/30/19.
|
||||
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
|
||||
//
|
||||
// https://github.com/keeshux
|
||||
//
|
||||
// This file is part of TunnelKit.
|
||||
//
|
||||
// TunnelKit is free software: you can redistribute it 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.
|
||||
//
|
||||
// TunnelKit 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 TunnelKit. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface RoutingTableEntry : NSObject
|
||||
|
||||
- (instancetype)initWithIPv4Network:(NSString *)network gateway:(nullable NSString *)gateway networkInterface:(NSString *)networkInterface;
|
||||
- (instancetype)initWithIPv6Network:(NSString *)network gateway:(nullable NSString *)gateway networkInterface:(NSString *)networkInterface;
|
||||
|
||||
- (BOOL)isIPv6;
|
||||
- (NSString *)network;
|
||||
- (NSInteger)prefix;
|
||||
- (nullable NSString *)gateway;
|
||||
- (NSString *)networkInterface;
|
||||
|
||||
- (BOOL)isDefault;
|
||||
- (BOOL)matchesDestination:(NSString *)destination;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
|
@ -0,0 +1,537 @@
|
|||
//
|
||||
// RoutingTableEntry.m
|
||||
// TunnelKit
|
||||
//
|
||||
// Created by Davide De Rosa on 4/30/19.
|
||||
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
|
||||
//
|
||||
// https://github.com/keeshux
|
||||
//
|
||||
// This file is part of TunnelKit.
|
||||
//
|
||||
// TunnelKit is free software: you can redistribute it 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.
|
||||
//
|
||||
// TunnelKit 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 TunnelKit. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
#import <arpa/inet.h>
|
||||
#import <netdb.h>
|
||||
#import <net/if.h>
|
||||
#import <net/route.h>
|
||||
|
||||
#import "RoutingTableEntry.h"
|
||||
|
||||
// adapted from: https://github.com/jianpx/ios-cabin
|
||||
|
||||
#define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint32_t) - 1))) : sizeof(uint32_t))
|
||||
|
||||
typedef union {
|
||||
uint32_t dummy;
|
||||
struct sockaddr u_sa;
|
||||
u_short u_data[128];
|
||||
} sa_u;
|
||||
|
||||
static inline uint32_t RoutingTableEntryAddress4(NSString *string);
|
||||
static inline NSData *RoutingTableEntryAddress6(NSString *string);
|
||||
static NSString *RoutingTableEntryName(struct sockaddr *sa, struct sockaddr *mask, int flags);
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@interface RoutingTableEntry ()
|
||||
|
||||
@property (nonatomic, assign) BOOL isIPv6;
|
||||
@property (nonatomic, copy) NSString *network;
|
||||
@property (nonatomic, assign) NSInteger prefix;
|
||||
@property (nonatomic, copy) NSString *gateway;
|
||||
@property (nonatomic, copy) NSString *networkInterface;
|
||||
|
||||
@end
|
||||
|
||||
@implementation RoutingTableEntry
|
||||
|
||||
- (instancetype)initWithNetwork:(NSString *)network prefix:(NSInteger)prefix gateway:(NSString *)gateway networkInterface:(NSString *)networkInterface
|
||||
{
|
||||
if (!(self = [super init])) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
self.network = network;
|
||||
self.prefix = prefix;
|
||||
self.gateway = gateway;
|
||||
self.networkInterface = networkInterface;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithIPv4Network:(NSString *)network gateway:(NSString *)gateway networkInterface:(NSString *)networkInterface
|
||||
{
|
||||
NSInteger prefix = 0;
|
||||
|
||||
NSArray<NSString *> *networkComps = [network componentsSeparatedByString:@"/"];
|
||||
network = networkComps.firstObject;
|
||||
if (networkComps.count == 2) {
|
||||
prefix = [networkComps.lastObject integerValue];
|
||||
} else {
|
||||
prefix = 32;
|
||||
}
|
||||
|
||||
NSMutableArray<NSString *> *groups = [[network componentsSeparatedByString:@"."] mutableCopy];
|
||||
if (![network isEqualToString:@"default"]) {
|
||||
if (prefix == 32) {
|
||||
prefix = 8 * groups.count;
|
||||
}
|
||||
for (NSInteger i = groups.count; i < 4; ++i) {
|
||||
[groups addObject:@"0"];
|
||||
}
|
||||
network = [groups componentsJoinedByString:@"."];
|
||||
}
|
||||
|
||||
if (!(self = [self initWithNetwork:network prefix:prefix gateway:gateway networkInterface:networkInterface])) {
|
||||
return nil;
|
||||
}
|
||||
self.isIPv6 = NO;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithIPv6Network:(NSString *)network gateway:(NSString *)gateway networkInterface:(NSString *)networkInterface
|
||||
{
|
||||
NSInteger prefix = 0;
|
||||
|
||||
NSArray<NSString *> *networkComps = [network componentsSeparatedByString:@"/"];
|
||||
network = networkComps.firstObject;
|
||||
if (networkComps.count == 2) {
|
||||
prefix = [networkComps.lastObject integerValue];
|
||||
} else {
|
||||
prefix = 128;
|
||||
}
|
||||
network = [[network componentsSeparatedByString:@"%"] firstObject];
|
||||
gateway = [[gateway componentsSeparatedByString:@"%"] firstObject];
|
||||
|
||||
if (!(self = [self initWithNetwork:network prefix:prefix gateway:gateway networkInterface:networkInterface])) {
|
||||
return nil;
|
||||
}
|
||||
self.isIPv6 = YES;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithRTM:(const struct rt_msghdr2 *)rtm
|
||||
{
|
||||
NSParameterAssert(rtm);
|
||||
|
||||
NSString *network;
|
||||
NSString *gateway;
|
||||
NSString *networkInterface;
|
||||
|
||||
struct sockaddr *rti_info[RTAX_MAX];
|
||||
struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
|
||||
for (int i = 0; i < RTAX_MAX; ++i) {
|
||||
if (rtm->rtm_addrs & (1 << i)) {
|
||||
rti_info[i] = sa;
|
||||
sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
|
||||
} else {
|
||||
rti_info[i] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// network
|
||||
sa_u destinationStruct, destinationNetmask;
|
||||
bzero(&destinationStruct, sizeof(destinationStruct));
|
||||
if (rtm->rtm_addrs & RTA_DST) {
|
||||
bcopy(rti_info[RTAX_DST], &destinationStruct, rti_info[RTAX_DST]->sa_len);
|
||||
}
|
||||
bzero(&destinationNetmask, sizeof(destinationNetmask));
|
||||
if (rtm->rtm_addrs & RTA_NETMASK) {
|
||||
bcopy(rti_info[RTAX_NETMASK], &destinationNetmask, rti_info[RTAX_NETMASK]->sa_len);
|
||||
}
|
||||
network = RoutingTableEntryName(&destinationStruct.u_sa, &destinationNetmask.u_sa, rtm->rtm_flags);
|
||||
|
||||
// gateway
|
||||
sa_u gatewayStruct;
|
||||
bzero(&gatewayStruct, sizeof(gatewayStruct));
|
||||
if (rtm->rtm_addrs & RTA_GATEWAY) {
|
||||
bcopy(rti_info[RTAX_GATEWAY], &gatewayStruct, rti_info[RTAX_GATEWAY]->sa_len);
|
||||
}
|
||||
gateway = RoutingTableEntryName(rti_info[RTAX_GATEWAY], NULL, RTF_HOST);
|
||||
|
||||
// network interface
|
||||
char networkInterfaceStr[IF_NAMESIZE];
|
||||
const char *networkInterfaceName = if_indextoname(rtm->rtm_index, networkInterfaceStr);
|
||||
if (networkInterfaceName) {
|
||||
networkInterface = [NSString stringWithCString:networkInterfaceName encoding:NSASCIIStringEncoding];
|
||||
}
|
||||
|
||||
if (rti_info[RTAX_DST]->sa_family == AF_INET6) {
|
||||
return [self initWithIPv6Network:network gateway:gateway networkInterface:networkInterface];
|
||||
} else {
|
||||
return [self initWithIPv4Network:network gateway:gateway networkInterface:networkInterface];
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)isDefault
|
||||
{
|
||||
return [self.network isEqualToString:@"default"];
|
||||
}
|
||||
|
||||
- (BOOL)matchesDestination:(NSString *)destination
|
||||
{
|
||||
NSParameterAssert(destination);
|
||||
|
||||
if ([self isDefault]) {
|
||||
return YES;
|
||||
}
|
||||
|
||||
if (self.isIPv6) {
|
||||
NSData *networkAddress = RoutingTableEntryAddress6(self.network);
|
||||
NSData *destinationAddress = RoutingTableEntryAddress6(destination);
|
||||
|
||||
// NSLog(@"network: %@ = %@", networkAddress, self.network);
|
||||
// NSLog(@"destination: %@ = %@", destinationAddress, destination);
|
||||
|
||||
const uint8_t *networkPtr = networkAddress.bytes;
|
||||
const uint8_t *destinationPtr = destinationAddress.bytes;
|
||||
|
||||
NSInteger leftBits = self.prefix;
|
||||
// NSLog(@"\tprefix = %u", (int)self.prefix);
|
||||
for (NSInteger i = 0; leftBits > 0; ++i) {
|
||||
uint8_t networkMask;
|
||||
if (leftBits >= 8) {
|
||||
networkMask = 0xff;
|
||||
} else {
|
||||
networkMask = ~((1 << (8 - leftBits)) - 1);
|
||||
}
|
||||
// NSLog(@"\tnetworkMask[%u] = %x", (int)i, networkMask);
|
||||
if (((networkPtr[i] ^ destinationPtr[i]) & networkMask) != 0) {
|
||||
return NO;
|
||||
}
|
||||
leftBits -= 8;
|
||||
}
|
||||
// NSLog(@"\tMATCH");
|
||||
return YES;
|
||||
}
|
||||
else {
|
||||
const uint32_t networkAddress = RoutingTableEntryAddress4(self.network);
|
||||
const uint32_t destinationAddress = RoutingTableEntryAddress4(destination);
|
||||
const uint32_t networkMask = ~((1 << (32 - self.prefix)) - 1);
|
||||
|
||||
// NSLog(@"network: %x = %@", networkAddress, self.network);
|
||||
// NSLog(@"destination: %x = %@", destinationAddress, destination);
|
||||
// NSLog(@"mask: %x", networkMask);
|
||||
|
||||
return ((networkAddress ^ destinationAddress) & networkMask) == 0;
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString *)description
|
||||
{
|
||||
return [NSString stringWithFormat:@"{%@/%ld -> %@ via %@}", self.network, self.prefix, self.gateway ?: @"nil", self.networkInterface];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
#pragma mark -
|
||||
|
||||
static char *netname(uint32_t in, uint32_t mask);
|
||||
static char *netname6(struct sockaddr_in6 *sa6, struct sockaddr *sam);
|
||||
static char *routename(uint32_t in);
|
||||
static char *routename6(struct sockaddr_in6 *sa6);
|
||||
static uint32_t forgemask(uint32_t a);
|
||||
static void domask(char *dst, uint32_t addr, uint32_t mask);
|
||||
static void trimdomain(char *cp);
|
||||
|
||||
static inline uint32_t RoutingTableEntryAddress4(NSString *string)
|
||||
{
|
||||
struct in_addr addr;
|
||||
if (inet_pton(AF_INET, [string cStringUsingEncoding:NSASCIIStringEncoding], &addr) <= 0) {
|
||||
return UINT32_MAX;
|
||||
}
|
||||
return CFSwapInt32BigToHost(addr.s_addr);
|
||||
}
|
||||
|
||||
static inline NSData *RoutingTableEntryAddress6(NSString *string)
|
||||
{
|
||||
struct in6_addr addr;
|
||||
if (inet_pton(AF_INET6, [string cStringUsingEncoding:NSASCIIStringEncoding], &addr) <= 0) {
|
||||
return nil;
|
||||
}
|
||||
NSMutableData *data = [[NSMutableData alloc] initWithLength:16];
|
||||
memcpy(data.mutableBytes, (void *)&addr, data.length);
|
||||
return data;
|
||||
}
|
||||
|
||||
static NSString *RoutingTableEntryName(struct sockaddr *sa, struct sockaddr *mask, int flags)
|
||||
{
|
||||
char *cp = NULL;
|
||||
switch (sa->sa_family) {
|
||||
case AF_INET: {
|
||||
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
|
||||
|
||||
if ((sin->sin_addr.s_addr == INADDR_ANY) && mask && (ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr) == 0L || mask->sa_len == 0)) {
|
||||
cp = "default";
|
||||
} else if (flags & RTF_HOST) {
|
||||
cp = routename(sin->sin_addr.s_addr);
|
||||
} else if (mask) {
|
||||
cp = netname(sin->sin_addr.s_addr, ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr));
|
||||
} else {
|
||||
cp = netname(sin->sin_addr.s_addr, 0L);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case AF_INET6: {
|
||||
struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
|
||||
struct in6_addr *in6 = &sa6->sin6_addr;
|
||||
|
||||
/*
|
||||
* XXX: This is a special workaround for KAME kernels.
|
||||
* sin6_scope_id field of SA should be set in the future.
|
||||
*/
|
||||
if (IN6_IS_ADDR_LINKLOCAL(in6) ||
|
||||
IN6_IS_ADDR_MC_NODELOCAL(in6) ||
|
||||
IN6_IS_ADDR_MC_LINKLOCAL(in6)) {
|
||||
|
||||
/* XXX: override is ok? */
|
||||
sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)&in6->s6_addr[2]);
|
||||
*(u_short *)&in6->s6_addr[2] = 0;
|
||||
}
|
||||
|
||||
if (flags & RTF_HOST) {
|
||||
cp = routename6(sa6);
|
||||
} else if (mask) {
|
||||
cp = netname6(sa6, mask);
|
||||
} else {
|
||||
cp = netname6(sa6, NULL);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (!cp) {
|
||||
return nil;
|
||||
}
|
||||
return [NSString stringWithCString:cp encoding:NSASCIIStringEncoding];
|
||||
}
|
||||
|
||||
char *routename(uint32_t in)
|
||||
{
|
||||
static char line[MAXHOSTNAMELEN];
|
||||
|
||||
#define C(x) ((x) & 0xff)
|
||||
in = ntohl(in);
|
||||
snprintf(line, sizeof(line), "%u.%u.%u.%u", C(in >> 24), C(in >> 16), C(in >> 8), C(in));
|
||||
|
||||
return (line);
|
||||
}
|
||||
|
||||
char *routename6(struct sockaddr_in6 *sa6)
|
||||
{
|
||||
static char line[MAXHOSTNAMELEN];
|
||||
int flag = NI_NUMERICHOST;
|
||||
/* use local variable for safety */
|
||||
struct sockaddr_in6 sa6_local = {sizeof(sa6_local), AF_INET6, };
|
||||
|
||||
sa6_local.sin6_addr = sa6->sin6_addr;
|
||||
sa6_local.sin6_scope_id = sa6->sin6_scope_id;
|
||||
|
||||
getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len, line, sizeof(line), NULL, 0, flag);
|
||||
|
||||
return line;
|
||||
}
|
||||
/*
|
||||
* Return the name of the network whose address is given.
|
||||
* The address is assumed to be that of a net or subnet, not a host.
|
||||
*/
|
||||
char *netname(uint32_t in, uint32_t mask)
|
||||
{
|
||||
char *cp = 0;
|
||||
static char line[MAXHOSTNAMELEN];
|
||||
struct netent *np = 0;
|
||||
uint32_t net, omask, dmask;
|
||||
uint32_t i;
|
||||
|
||||
i = ntohl(in);
|
||||
dmask = forgemask(i);
|
||||
omask = mask;
|
||||
// if (!nflag && i) {
|
||||
if (i) {
|
||||
net = i & dmask;
|
||||
if (!(np = getnetbyaddr(i, AF_INET)) && net != i)
|
||||
np = getnetbyaddr(net, AF_INET);
|
||||
if (np) {
|
||||
cp = np->n_name;
|
||||
trimdomain(cp);
|
||||
}
|
||||
}
|
||||
if (cp) {
|
||||
strncpy(line, cp, sizeof(line) - 1);
|
||||
} else {
|
||||
switch (dmask) {
|
||||
case IN_CLASSA_NET:
|
||||
if ((i & IN_CLASSA_HOST) == 0) {
|
||||
snprintf(line, sizeof(line), "%u", C(i >> 24));
|
||||
break;
|
||||
}
|
||||
/* FALLTHROUGH */
|
||||
case IN_CLASSB_NET:
|
||||
if ((i & IN_CLASSB_HOST) == 0) {
|
||||
snprintf(line, sizeof(line), "%u.%u",
|
||||
C(i >> 24), C(i >> 16));
|
||||
break;
|
||||
}
|
||||
/* FALLTHROUGH */
|
||||
case IN_CLASSC_NET:
|
||||
if ((i & IN_CLASSC_HOST) == 0) {
|
||||
snprintf(line, sizeof(line), "%u.%u.%u",
|
||||
C(i >> 24), C(i >> 16), C(i >> 8));
|
||||
break;
|
||||
}
|
||||
/* FALLTHROUGH */
|
||||
default:
|
||||
snprintf(line, sizeof(line), "%u.%u.%u.%u",
|
||||
C(i >> 24), C(i >> 16), C(i >> 8), C(i));
|
||||
break;
|
||||
}
|
||||
}
|
||||
domask(line+strlen(line), i, omask);
|
||||
return (line);
|
||||
}
|
||||
|
||||
|
||||
char *netname6(struct sockaddr_in6 *sa6, struct sockaddr *sam)
|
||||
{
|
||||
static char line[MAXHOSTNAMELEN + 10];
|
||||
u_char *lim;
|
||||
int masklen, illegal = 0, flag = NI_NUMERICHOST;
|
||||
struct in6_addr *mask = sam ? &((struct sockaddr_in6 *)sam)->sin6_addr : 0;
|
||||
|
||||
if (sam && sam->sa_len == 0) {
|
||||
masklen = 0;
|
||||
} else if (mask) {
|
||||
u_char *p = (u_char *)mask;
|
||||
for (masklen = 0, lim = p + 16; p < lim; p++) {
|
||||
switch (*p) {
|
||||
case 0xff:
|
||||
masklen += 8;
|
||||
break;
|
||||
case 0xfe:
|
||||
masklen += 7;
|
||||
break;
|
||||
case 0xfc:
|
||||
masklen += 6;
|
||||
break;
|
||||
case 0xf8:
|
||||
masklen += 5;
|
||||
break;
|
||||
case 0xf0:
|
||||
masklen += 4;
|
||||
break;
|
||||
case 0xe0:
|
||||
masklen += 3;
|
||||
break;
|
||||
case 0xc0:
|
||||
masklen += 2;
|
||||
break;
|
||||
case 0x80:
|
||||
masklen += 1;
|
||||
break;
|
||||
case 0x00:
|
||||
break;
|
||||
default:
|
||||
illegal ++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (illegal)
|
||||
fprintf(stderr, "illegal prefixlen\n");
|
||||
} else {
|
||||
masklen = 128;
|
||||
}
|
||||
if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr)) {
|
||||
return("default");
|
||||
}
|
||||
|
||||
getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line), NULL, 0, flag);
|
||||
|
||||
if (masklen > 0) {
|
||||
sprintf(line, "%s/%u", line, masklen);
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
uint32_t forgemask(uint32_t a)
|
||||
{
|
||||
uint32_t m;
|
||||
|
||||
if (IN_CLASSA(a))
|
||||
m = IN_CLASSA_NET;
|
||||
else if (IN_CLASSB(a))
|
||||
m = IN_CLASSB_NET;
|
||||
else
|
||||
m = IN_CLASSC_NET;
|
||||
return (m);
|
||||
}
|
||||
|
||||
void domask(char *dst, uint32_t addr, uint32_t mask)
|
||||
{
|
||||
int b, i;
|
||||
|
||||
if (!mask || (forgemask(addr) == mask)) {
|
||||
*dst = '\0';
|
||||
return;
|
||||
}
|
||||
i = 0;
|
||||
for (b = 0; b < 32; b++) {
|
||||
if (mask & (1 << b)) {
|
||||
int bb;
|
||||
|
||||
i = b;
|
||||
for (bb = b+1; bb < 32; bb++)
|
||||
if (!(mask & (1 << bb))) {
|
||||
i = -1; /* noncontig */
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == -1) {
|
||||
snprintf(dst, sizeof(dst), "&0x%x", mask);
|
||||
} else {
|
||||
snprintf(dst, sizeof(dst), "/%d", 32-i);
|
||||
}
|
||||
}
|
||||
|
||||
void trimdomain(char *cp)
|
||||
{
|
||||
static char domain[MAXHOSTNAMELEN + 1];
|
||||
static int first = 1;
|
||||
char *s;
|
||||
|
||||
if (first) {
|
||||
first = 0;
|
||||
if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
|
||||
(s = strchr(domain, '.')))
|
||||
(void) strcpy(domain, s + 1);
|
||||
else
|
||||
domain[0] = 0;
|
||||
}
|
||||
|
||||
if (domain[0]) {
|
||||
while ((cp = strchr(cp, '.'))) {
|
||||
if (!strcasecmp(cp + 1, domain)) {
|
||||
*cp = 0; /* hit it */
|
||||
break;
|
||||
} else {
|
||||
cp++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -51,5 +51,6 @@ module __TunnelKitNative {
|
|||
header "DataPathCrypto.h"
|
||||
header "DNS.h"
|
||||
header "LZO.h"
|
||||
header "RoutingTable.h"
|
||||
export *
|
||||
}
|
||||
|
|
|
@ -0,0 +1,96 @@
|
|||
//
|
||||
// RoutingTests.swift
|
||||
// TunnelKitTests
|
||||
//
|
||||
// Created by Davide De Rosa on 4/30/19.
|
||||
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
|
||||
//
|
||||
// https://github.com/keeshux
|
||||
//
|
||||
// This file is part of TunnelKit.
|
||||
//
|
||||
// TunnelKit is free software: you can redistribute it 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.
|
||||
//
|
||||
// TunnelKit 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 TunnelKit. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
import __TunnelKitNative
|
||||
|
||||
class RoutingTests: XCTestCase {
|
||||
|
||||
override func setUp() {
|
||||
// Put setup code here. This method is called before the invocation of each test method in the class.
|
||||
}
|
||||
|
||||
override func tearDown() {
|
||||
// Put teardown code here. This method is called after the invocation of each test method in the class.
|
||||
}
|
||||
|
||||
func testEntryMatch4() {
|
||||
let entry24 = RoutingTableEntry(iPv4Network: "192.168.1.0/24", gateway: nil, networkInterface: "en0")
|
||||
for i in 0x0...0xff {
|
||||
XCTAssertTrue(entry24.matchesDestination("192.168.1.\(i)"))
|
||||
}
|
||||
for i in 0x0...0xff {
|
||||
XCTAssertFalse(entry24.matchesDestination("192.168.2.\(i)"))
|
||||
}
|
||||
|
||||
let entry28 = RoutingTableEntry(iPv4Network: "192.168.1.0/28", gateway: nil, networkInterface: "en0")
|
||||
for i in 0x0...0xf {
|
||||
XCTAssertTrue(entry28.matchesDestination("192.168.1.\(i)"))
|
||||
}
|
||||
for i in 0x10...0x1f {
|
||||
XCTAssertFalse(entry28.matchesDestination("192.168.1.\(i)"))
|
||||
}
|
||||
}
|
||||
|
||||
func testEntryMatch6() {
|
||||
let entry24 = RoutingTableEntry(iPv6Network: "abcd:efef:1234::/46", gateway: nil, networkInterface: "en0")
|
||||
for i in 0x0...0xf {
|
||||
XCTAssertTrue(entry24.matchesDestination("abcd:efef:1234::\(i)"))
|
||||
}
|
||||
for i in 0x0...0xf {
|
||||
XCTAssertFalse(entry24.matchesDestination("abcd:efef:1233::\(i)"))
|
||||
}
|
||||
}
|
||||
|
||||
func testFindGatewayLAN4() {
|
||||
let table = RoutingTable()
|
||||
|
||||
for entry in table.ipv4() {
|
||||
print(entry)
|
||||
}
|
||||
|
||||
if let defaultGateway = table.defaultGateway4()?.gateway() {
|
||||
print("Default gateway: \(defaultGateway)")
|
||||
if let lan = table.broadestRoute4(matchingDestination: defaultGateway) {
|
||||
print("Gateway LAN: \(lan.network())/\(lan.prefix())")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testFindGatewayLAN6() {
|
||||
let table = RoutingTable()
|
||||
|
||||
for entry in table.ipv6() {
|
||||
print(entry)
|
||||
}
|
||||
|
||||
if let defaultGateway = table.defaultGateway6()?.gateway() {
|
||||
print("Default gateway: \(defaultGateway)")
|
||||
if let lan = table.broadestRoute6(matchingDestination: defaultGateway) {
|
||||
print("Gateway LAN: \(lan.network())/\(lan.prefix())")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue