0

I want to check if the upd port for OpenVPN is open. For Tcp Port it was really simple, but now I struggle with Udp ports.

This is my TCP Implementation

private static bool TestConnectionInternal(string hostname, int port, int timeOutMs, int maxTries, int count)
{
    using (var tcpClient = new TcpClient())
    {
        try
        {
            Task result = tcpClient.ConnectAsync(hostname, port);
            return result.Wait(timeOutMs);

        }
        catch (Exception e)
        {
            count += 1;
            if (count < maxTries)
            {
                return TestConnectionInternal(hostname, port, timeOutMs, maxTries, count);
            }
            return false;
        }
    }
}
hdev
  • 6,097
  • 1
  • 45
  • 62

2 Answers2

2

There no way to know if a UDP port is open. If you lucky you get a icmp unreachable port closed negative answer. For some protocols like NTP you can try to send a valid query and check for a response. If OpenVPN is configured with --tls-auth or --secret you cannot produce a valid packet to trigger a repsonse if you don know the secret key.

plaisthos
  • 6,255
  • 6
  • 35
  • 63
  • At the moment I check for icmp, but I want to be shure if a OpenVPN Server answers. In the Config are the following switches, client, dev tun, proto udp,resolv-retry infinite, nobind, persist-key, persist-tun – hdev Feb 01 '14 at 21:08
  • Yeah reproduce a valid client query to the server. Reading openvpn source code/using tcpdump is the (stony) way forward. – plaisthos Feb 02 '14 at 18:21
1

Simple check OpenVPN UDP service (except that one uses --tls-auth or --secret)

bool CheckOpenVPNudp(string ip, int port)
        {
            IPEndPoint RemoteEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            byte[] data = { 56, 1, 0, 0, 0, 0, 0, 0, 0 }; //OpenVPN client welcome datagram
            server.SendTo(data, data.Length, SocketFlags.None, RemoteEndPoint);
            server.ReceiveTimeout = 15000; //15 seconds timeout
            EndPoint Remote = (EndPoint)(RemoteEndPoint);
            try
            {
                byte[] answer = new byte[1024];
                int recv = server.ReceiveFrom(answer, ref Remote);
                Console.WriteLine("Message received from {0}:", Remote.ToString());
                Console.WriteLine(System.Text.Encoding.ASCII.GetString(answer, 0, recv));
                return true;

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

        }

undermind
  • 36
  • 4