NWUPDSession setReadHandler not called

Hi, I am implementing VPN using NEPacketTunnelProvider. When I a trying to make handshake with server I created NWUDPSession send UDP packet, server recived this packet and send response but I can't get this response as reader handler not called. It looks like some settings are wrong. I am creating UDP in next way (self.createUDPSession(to: NWHostEndpoint(hostname: serverAddress, port: serverPort), from: nil))
Can you help me with it ?

Replies

Has your reader handler been setup to receive the UDP response from your server before the packet was sent to your server? Could you provide a brief snippet of how your NWUDPSession is setup?



Matt Eaton

DTS Engineering, CoreOS

meaton3 at apple.com

Yes I setup UDPSession before sent packet to the server. I am sending packet to server using this UDPSession
Here is how I setup NWUDPSession inside NEPacketTunnelProvider

self.session = self.createUDPSession(to: NWHostEndpoint(hostname: serverAddress, port: serverPort), from: nil

I send packet using this session

self.session?.writeDatagram(packet, completionHandler: { (error: Error?) in
                    if let error = error {
                        print(error)
                        self.setupUDPSession()
                        return
                    }
                })

How are you reading data off of your NWUDPSession though?


Matt Eaton

DTS Engineering, CoreOS

meaton3 at apple.com

I set reader handler for session


session?.setReadHandler({ (_packets: [Data]?, error: Error?) -> Void in
            if let packets = _packets {
                self.packetFlow.writePackets(packets, withProtocols: [NSNumber](repeating: AF_INET as NSNumber, count: packets.count))
            }
        }, maxDatagrams: 32)

It's hard to say what is happening here. My first recommendation would be to remove the tunnel from the equation and move this connection outside the context of a Network Extension and test the send/receive on a vanilla UDP NWConnection. If your existing setup can send and receive on a NWConnection then I would take a look at the tunnel as the source of the problem. If you are not able to send/receive on the NWConnection then there is an issue on your flow that you need to investigate.



Matt Eaton

DTS Engineering, CoreOS

meaton3 at apple.com