Search code examples
c#socketsudpvoipports

How to convert UDP header of port IAX2 to readable string


I'm trying to make an application in C# that reads the activity of the IAX2 port 4569. I already created the UDP and TCP listeners but when I try to convert the UDP data part to a string I found some strange codes. I don't know if I'm doing it right. Some help with this I need. This class is the UDPHeader from i get the data.

public class UDPHeader
{
    //UDP header fields
    private ushort usSourcePort;            //Sixteen bits for the source port number        
    private ushort usDestinationPort;       //Sixteen bits for the destination port number
    private ushort usLength;                //Length of the UDP header
    private short sChecksum;                //Sixteen bits for the checksum
                                            //(checksum can be negative so taken as short)              
    //End UDP header fields

    private byte[] byUDPData = new byte[4096];  //Data carried by the UDP packet

    public UDPHeader(byte [] byBuffer, int nReceived)
    {
        MemoryStream memoryStream = new MemoryStream(byBuffer, 0, nReceived);
        BinaryReader binaryReader = new BinaryReader(memoryStream);

        //The first sixteen bits contain the source port
        usSourcePort = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16());

        //The next sixteen bits contain the destination port
        usDestinationPort = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16());

        //The next sixteen bits contain the length of the UDP packet
        usLength = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16());

        //The next sixteen bits contain the checksum
        sChecksum = IPAddress.NetworkToHostOrder(binaryReader.ReadInt16());            

        //Copy the data carried by the UDP packet into the data buffer
        Array.Copy(byBuffer, 
                   8,               //The UDP header is of 8 bytes so we start copying after it
                   byUDPData, 
                   0, 
                   nReceived - 8);
    }}

Next i have a class to convert the data from the UDPHeader to normal text. This is the constructor:

public IAXHeader(byte[] byBuffer, int nReceived)
{ 
MemoryStream memoryStream = new MemoryStream(byBuffer, 0, nReceived);
StringReader stringReader = new   StringReader(Encoding.UTF8.GetString(memoryStream.ToArray()));

/** iterate lines of stringReader **/
string aLine = stringReader.ReadLine();
}

The Console.WriteLine of aLine is this: Console log for aLine of stringReader

I need to know what I'm doing wrong to decode the bytes from IAX2 UDP Data.


Solution

  • As this protocol is not a textual readable protocol, just parse it as a UTF8 string will give you unexpected results.

    You should read the protocol description (for example [https://www.rfc-editor.org/rfc/rfc5456]) and parse the data based on this description.

    To start you can maybe print the data byte by byte as hexadecimal codes.