Beispiel #1
0
        public static string UDPToDebugString(IPAddress sourceAddr, IPAddress destAddr, UDPFrame frame)
        {
            var sb = new StringBuilder();

            var srcAddrWords = string.Join(" ",
                                           ChecksumFns.DataBytesToShorts(sourceAddr.GetAddressBytes())
                                           .Select(s => $"{s:x4}"));

            sb.AppendLine($"{srcAddrWords} source addr");

            var dstAddrWords = string.Join(" ",
                                           ChecksumFns.DataBytesToShorts(destAddr.GetAddressBytes())
                                           .Select(s => $"{s:x4}"));

            sb.AppendLine($"{dstAddrWords} dst addr");
            sb.AppendLine($"{17:x} protocol");
            sb.AppendLine($"{frame.Header.HeaderAndDataLength:x4} UDP length");
            sb.AppendLine($"{frame.Header.SourcePort:x4} source port");
            sb.AppendLine($"{frame.Header.DestinationPort:x4} dest port");
            sb.AppendLine($"{frame.Header.HeaderAndDataLength:x4} Length");
            sb.AppendLine($"{frame.Header.Checksum:x4} Checksum");
            sb.AppendLine($"DATA ({frame.Data.Length}B):");

            var byteStep = 8;

            for (int i = 0; i < frame.Data.Length; i += byteStep)
            {
                var words    = ChecksumFns.DataBytesToShorts(frame.Data.Skip(i).Take(byteStep).ToArray());
                var wordLine = string.Join(" ", words.Select(s => $"{s:x4}"));
                sb.AppendLine(wordLine);
            }

            return(sb.ToString());
        }
Beispiel #2
0
        public static ushort UDPChecksum(IPAddress sourceAddress, IPAddress destinationAddress, UDPFrame packet)
        {
            uint checksum = 0;

            checksum += ChecksumFns.SumAsUShorts(sourceAddress.GetAddressBytes());
            checksum += ChecksumFns.SumAsUShorts(destinationAddress.GetAddressBytes());
            checksum += 17;   // protocol udp = 17
            checksum += packet.Header.HeaderAndDataLength;
            checksum += packet.Header.SourcePort;
            checksum += packet.Header.DestinationPort;
            checksum += packet.Header.HeaderAndDataLength;
            checksum += ChecksumFns.SumAsUShorts(packet.Data);
            return(ChecksumFns.FinishChecksum(checksum));
        }