Hi Eskimo, I am in trouble while writing UDP data packet to tun Interface getting error "Failed to write flow Data", and it is happening for few of the UDP packets while most packet has written successfully. So because of that I am unable to get the complete UDP packet and this result in not to open page. Code that we executing are as follows:
(void)startReceivingData
{
[tcpConnection readMinimumLength:9 maximumLength:9 completionHandler:^(NSData _Nullable header, NSError _Nullable error)
{
if(!error)
{
if(header.length > 0)
{
NSLog(@"///ReadUDPHeader.... : %@",header);
NSData *lengthData = [header subdataWithRange:NSMakeRange(0, 2)];
int dataLength = CFSwapInt16BigToHost(*(int*)([lengthData bytes]));
dataLength = dataLength - 9;
NSLog(@"///ReadDataLength.... : %d",dataLength);
NSData *ipAddressData = [header subdataWithRange:NSMakeRange(3, 7)];
int ip = CFSwapInt32BigToHost(*(int*)([ipAddressData bytes]));
struct in_addr addr = {ip};
char* ipAddress = inet_ntoa(addr);
NSString*hostString = [NSString stringWithFormat:@"%s", ipAddress];
NSLog(@"///ReadHostString.... : %@",hostString);
NSData *portData = [header subdataWithRange:NSMakeRange(7, 9)];
int portValue = CFSwapInt16BigToHost(*(int*)([portData bytes]));
NSString *portString = [NSString stringWithFormat:@"%d", portValue];
NSLog(@"///ReadPortString.... : %@",portString);
NWHostEndpoint * endpoint = [NWHostEndpoint endpointWithHostname:hostString port:portString];
NSLog(@"///Read host and port to create endpoint... : %@",endpoint);
[tcpConnection readMinimumLength:dataLength maximumLength:dataLength completionHandler:^(NSData _Nullable payload, NSError _Nullable error)
{
if(!error)
{
if(payload.length > 0)
{
NSLog(@"///Read payload data.. : %@",payload);
[datagramArray addObject:payload];
[remoteEndPointArray addObject:endpoint];
NSLog(@"///payload data array.. : %@",datagramArray);
NSLog(@"///remote end point array.. : %@",remoteEndPointArray);
[UDPFlow writeDatagrams:datagramArray sentByEndpoints:remoteEndPointArray completionHandler:^(NSError * _Nullable error) {
if(error)
{
NSLog(@"///Error :: %@ occurred while writing data to Tun with flow... : %@", error,UDPFlow);
[UDPFlow closeWriteWithError:nil];
}
else
{
NSLog(@"///Write data to Tun with flow... : %@",UDPFlow);
[datagramArray removeAllObjects];
[remoteEndPointArray removeAllObjects];
[self startReceivingData];
[tcpConnection cancel];
}
}];
}
}
}];
}
else
{
[tcpConnection cancel];
}
}
}];
}
Here we get error:
[UDPFlow writeDatagrams:datagramArray sentByEndpoints:remoteEndPointArray completionHandler:^(NSError * _Nullable error) {
if(error)
{
NSLog(@"///Error :: %@ occurred while writing data to Tun with flow... : %@", error,UDPFlow);
[UDPFlow closeWriteWithError:nil];
}