XDPing C# Function

by Jeremy Saunders on June 13, 2023

I wrote this C# (csharp) function back in 2020 when developing the Self-Service Session Reset Tool. I have since enhanced it for improved error checking and logging and am happy to share it here for anyone else who wants to leverage it. I released a PowerShell version back in 2019. I like to integrate these functions into the scripts and tools I create as part of the health checks I do. I believe that by checking that the Broker’s Registrar service is reachable, listening and processing requests on its configured port, we can only then assume it is in a healthy state.

The original code was taken from the VDAAssistant.Backend.dll library which is included with the Citrix Health Assistant Tool. Citrix don’t obfuscate these libraries, so it’s easy to use tools like JetBrains decompiler to take a look at how they wrote this. From there I reverse engineer the code to extract what I need and wrap it up into a function that will work for me.

The original Citrix XDPing tool can be found here. The Citrix Health Assistant tool has since replaced XDPing.

Here is the code. It’s also available as a full project from my GitHub repository. Total number of downloads to date: 3.

using System;
using System.Text;
// Required for sockets
using System.Net.Sockets;

namespace XDPing
{
    class Program
    {
        static int Main(string[] args)
        {
            string deliverycontroller = string.Empty;
            int port = 80;

            for (int i = 0; i < args.Length; i++)
            {
                var arg = args[i].ToLower();
                if (arg == "-deliverycontroller" || arg == "--deliverycontroller" || arg == "/deliverycontroller")
                {
                    if (args.Length >= i + 2)
                    {
                        deliverycontroller = args[i + 1];
                    }
                }

                if (arg == "-port" || arg == "--port" || arg == "/port")
                {
                    if (args.Length >= i + 2)
                    {
                        int.TryParse(args[i + 1], out port);
                    }
                }
            }

            if (string.IsNullOrEmpty(deliverycontroller))
            {
                Console.WriteLine("Valid command line arguments must be supplied:");
                Console.WriteLine("-deliverycontroller, --deliverycontroller or /deliverycontroller is a required flag. This must be followed by the name of a Delivery Controller or Cloud Connector.");
                Console.WriteLine("-port, --port or /port is an optional flag. It will default to 80 if not supplied. This is the port the Broker's Registrar service listens on.");
                return -1;
            }

            XDPing(deliverycontroller, port);

            return 0;
        }

        /// <summary>
        /// Performs an XDPing to make sure the Delivery Controller or Cloud Connector is in a healthy state.
        /// It test whether the Broker service is reachable, listening and processing requests on its configured port.
        /// We do this by issuing a blank HTTP POST requests to the Broker's Registrar service.
        /// Including "Expect: 100-continue" in the body will ensure we receive a respose of "HTTP/1.1 100 Continue",
        /// which is what we use to verify that it's in a healthy state.
        /// </summary>
        /// <param name="deliverycontroller"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        static private bool XDPing(string deliverycontroller, int port)
        {
            // This code has essentially been taken from the Citrix Health Assistant Tool and improved for reliability and troubleshooting purposes.
            // I was able to reverse engineer the process by decompiling the VDAAssistant.Backend.dll, which is a component of the Citrix Health
            // Assistant Tool.
            string service = "http://" + deliverycontroller + ":" + port +"/Citrix/CdsController/IRegistrar";
            string s = string.Format("POST {0} HTTP/1.1\r\nContent-Type: application/soap+xml; charset=utf-8\r\nHost: {1}:{2}\r\nContent-Length: 1\r\nExpect: 100-continue\r\nConnection: Close\r\n\r\n", (object)service, (object)deliverycontroller, (object)port);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("Attempting an XDPing against " + deliverycontroller + " on TCP port number " + port.ToString());
            bool listening = false;
            try
            {
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    socket.Connect(deliverycontroller, port);
                    if (socket.Connected)
                    {
                        stringBuilder.AppendLine("- Socket connected");
                        byte[] bytes = Encoding.ASCII.GetBytes(s);
                        // Send the string as bytes.
                        socket.Send(bytes, bytes.Length, SocketFlags.None);
                        stringBuilder.AppendLine("- Sent the data");
                        byte[] numArray = new byte[21];
                        socket.ReceiveTimeout = 5000;
                        socket.Receive(numArray);
                        stringBuilder.AppendLine("- Received the following 21 byte array: " + BitConverter.ToString(numArray));
                        // ASCII conversion - string from bytes
                        string strASCII = Encoding.ASCII.GetString(numArray, 0, numArray.Length);
                        // UTF conversion - String from bytes  
                        string strUTF8 = Encoding.UTF8.GetString(numArray, 0, numArray.Length);
                        stringBuilder.AppendLine("- Converting the byte array to an ASCII string we get the output between the quotes: \"" + strASCII + "\"");
                        stringBuilder.AppendLine("- Converting the byte array to a UTF8 string we get the output between the quotes: \"" + strUTF8 + "\"");
                        // Send an additional single byte of 32 (space) as 1 byte with no flags.
                        socket.Send(new byte[1] { (byte)32 }, 1, SocketFlags.None);
                        stringBuilder.AppendLine("- Sending the following string as a byte to close the connection: \"" + BitConverter.ToString(new byte[1] { 32 }) + "\"");
                        if (strASCII.Trim().IndexOf("HTTP/1.1 100 Continue", StringComparison.CurrentCultureIgnoreCase) == 0)
                        {
                            listening = true;
                            stringBuilder.AppendLine("- The service is listening and healthy");
                        }
                        else
                        {
                            stringBuilder.AppendLine("- The service is not listening");
                        }
                    }
                    else
                    {
                        stringBuilder.AppendLine("- Socket failed to connect");
                    }
                }
                catch (SocketException se)
                {
                    stringBuilder.AppendLine("- Failed to connect to service");
                    stringBuilder.AppendLine("- ERROR: " + se.Message);
                }
                catch (Exception e)
                {
                    stringBuilder.AppendLine("- Failed with an unexpected error");
                    stringBuilder.AppendLine("- ERROR: " + e.Message);
                }
                if (socket.Connected)
                {
                    try
                    {
                        socket.Close();
                        stringBuilder.AppendLine("- Socket closed");
                    }
                    catch (SocketException se)
                    {
                        stringBuilder.AppendLine("- Failed to close the socket");
                        stringBuilder.AppendLine("- ERROR: " + se.Message);
                    }
                    catch (Exception e)
                    {
                        stringBuilder.AppendLine("- Failed with an unexpected error");
                        stringBuilder.AppendLine("- ERROR: " + e.Message);
                    }
                }
                socket.Dispose();
            }
            catch (Exception e)
            {
                stringBuilder.AppendLine("- Failed to create a socket");
                stringBuilder.AppendLine("- ERROR: " + e.Message);
            }
            Console.WriteLine(stringBuilder.ToString().Substring(0, stringBuilder.ToString().Length - 1));
            return listening;
        }
    }
}

Notes:

  • I have verified that the code conforms to HTTP 1.1 standards. It terminates the headers section correctly with an empty line (line break without any visible characters) before sending the request.
  • I understand that the extra small payload sent as a single byte with a value of 32 (which corresponds to a space character in ASCII encoding) after the initial request is a trigger for the Broker’s Registrar service to close the connection and/or perform any necessary clean-up actions.

The following screen shot shows the intended output of this code.

XDPing csharp output

Enjoy!

Jeremy Saunders

Jeremy Saunders

Technical Architect | DevOps Evangelist | Software Developer | Microsoft, NVIDIA, Citrix and Desktop Virtualisation (VDI) Specialist/Expert | Rapper | Improvisor | Comedian | Property Investor | Kayaking enthusiast at J House Consulting
Jeremy Saunders is the Problem Terminator. He is a highly respected IT Professional with over 35 years’ experience in the industry. Using his exceptional design and problem solving skills with precise methodologies applied at both technical and business levels he is always focused on achieving the best business outcomes. He worked as an independent consultant until September 2017, when he took up a full time role at BHP, one of the largest and most innovative global mining companies. With a diverse skill set, high ethical standards, and attention to detail, coupled with a friendly nature and great sense of humour, Jeremy aligns to industry and vendor best practices, which puts him amongst the leaders of his field. He is intensely passionate about solving technology problems for his organisation, their customers and the tech community, to improve the user experience, reliability and operational support. Views and IP shared on this site belong to Jeremy.
Jeremy Saunders
Jeremy Saunders

Previous post:

Next post: