mirror of
https://github.com/deneraraujo/OpenVPNAdapter.git
synced 2026-04-06 00:00:03 +08:00
Get protocol version from header if a target is macOS
This commit is contained in:
@@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
#import <NetworkExtension/NetworkExtension.h>
|
#import <NetworkExtension/NetworkExtension.h>
|
||||||
|
|
||||||
|
#import <openvpn/ip/ip.hpp>
|
||||||
#import <openvpn/addr/ipv4.hpp>
|
#import <openvpn/addr/ipv4.hpp>
|
||||||
|
|
||||||
#import "OpenVPNTunnelSettings.h"
|
#import "OpenVPNTunnelSettings.h"
|
||||||
@@ -583,7 +584,8 @@ static void socketCallback(CFSocketRef socket, CFSocketCallBackType type, CFData
|
|||||||
#pragma mark OpenVPN -> TUN
|
#pragma mark OpenVPN -> TUN
|
||||||
|
|
||||||
- (void)readVPNData:(NSData *)data {
|
- (void)readVPNData:(NSData *)data {
|
||||||
// Get network protocol from data
|
#if TARGET_OS_IPHONE
|
||||||
|
// Get network protocol from prefix
|
||||||
NSUInteger prefixSize = sizeof(uint32_t);
|
NSUInteger prefixSize = sizeof(uint32_t);
|
||||||
|
|
||||||
if (data.length < prefixSize) {
|
if (data.length < prefixSize) {
|
||||||
@@ -593,11 +595,20 @@ static void socketCallback(CFSocketRef socket, CFSocketCallBackType type, CFData
|
|||||||
|
|
||||||
uint32_t protocol = UINT32_MAX;
|
uint32_t protocol = UINT32_MAX;
|
||||||
[data getBytes:&protocol length:prefixSize];
|
[data getBytes:&protocol length:prefixSize];
|
||||||
|
|
||||||
protocol = CFSwapInt32BigToHost(protocol);
|
protocol = CFSwapInt32BigToHost(protocol);
|
||||||
|
|
||||||
// Send the packet to the TUN interface
|
|
||||||
NSData *packet = [data subdataWithRange:NSMakeRange(prefixSize, data.length - prefixSize)];
|
NSData *packet = [data subdataWithRange:NSMakeRange(prefixSize, data.length - prefixSize)];
|
||||||
|
#elif TARGET_OS_MAC
|
||||||
|
// Get network protocol from header
|
||||||
|
uint8_t header = 0;
|
||||||
|
[data getBytes:&header length:1];
|
||||||
|
|
||||||
|
uint32_t protocol = openvpn::IPHeader::version(header);
|
||||||
|
NSData *packet = data;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Send the packet to the TUN interface
|
||||||
if (![self.packetFlow writePackets:@[packet] withProtocols:@[@(protocol)]]) {
|
if (![self.packetFlow writePackets:@[packet] withProtocols:@[@(protocol)]]) {
|
||||||
NSLog(@"Failed to send OpenVPN packet to the TUN interface");
|
NSLog(@"Failed to send OpenVPN packet to the TUN interface");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user