From 7b5f0cd71497ec5c6451d1f4eda58d185cce4b2e Mon Sep 17 00:00:00 2001 From: Sergey Abramchuk Date: Mon, 6 Mar 2017 20:12:51 +0300 Subject: [PATCH] Read data from OpenVPN and pass it to the TUN interface --- OpenVPN Tunnel Provider/OpenVPNAdapter.mm | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/OpenVPN Tunnel Provider/OpenVPNAdapter.mm b/OpenVPN Tunnel Provider/OpenVPNAdapter.mm index 569fd6c..4113b8a 100644 --- a/OpenVPN Tunnel Provider/OpenVPNAdapter.mm +++ b/OpenVPN Tunnel Provider/OpenVPNAdapter.mm @@ -397,7 +397,24 @@ static void socketCallback(CFSocketRef socket, CFSocketCallBackType type, CFData #pragma mark OpenVPN -> TUN - (void)readVPNData:(NSData *)data { - // TODO: Read data from the VPN and send it to the TUN interface + // Get network protocol from data + NSUInteger prefixSize = sizeof(uint32_t); + + if (data.length < prefixSize) { + NSLog(@"Incorrect OpenVPN packet size"); + return; + } + + 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)]; + if (![self.packetFlow writePackets:@[packet] withProtocols:@[@(protocol)]]) { + NSLog(@"Failed to send OpenVPN packet to the TUN interface"); + } } #pragma mark Deallocation