From 76413feea75b341909f48d1cb9de10426418849d Mon Sep 17 00:00:00 2001 From: Sergey Abramchuk Date: Thu, 22 Jun 2017 20:50:23 +0300 Subject: [PATCH] Get protocol version from header if a target is macOS --- OpenVPN Adapter/OpenVPNAdapter.mm | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/OpenVPN Adapter/OpenVPNAdapter.mm b/OpenVPN Adapter/OpenVPNAdapter.mm index c5d88e4..def79d1 100644 --- a/OpenVPN Adapter/OpenVPNAdapter.mm +++ b/OpenVPN Adapter/OpenVPNAdapter.mm @@ -14,6 +14,7 @@ #import +#import #import #import "OpenVPNTunnelSettings.h" @@ -583,7 +584,8 @@ static void socketCallback(CFSocketRef socket, CFSocketCallBackType type, CFData #pragma mark OpenVPN -> TUN - (void)readVPNData:(NSData *)data { - // Get network protocol from data +#if TARGET_OS_IPHONE + // Get network protocol from prefix NSUInteger prefixSize = sizeof(uint32_t); if (data.length < prefixSize) { @@ -593,11 +595,20 @@ static void socketCallback(CFSocketRef socket, CFSocketCallBackType type, CFData uint32_t protocol = UINT32_MAX; [data getBytes:&protocol length:prefixSize]; - + protocol = CFSwapInt32BigToHost(protocol); - // Send the packet to the TUN interface 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)]]) { NSLog(@"Failed to send OpenVPN packet to the TUN interface"); }