Beispiel #1
0
 void Print(byte[] buf, int len)
 {
     try
     {
         csSniffer.DataStructures.IPHeader ipHeader = new csSniffer.DataStructures.IPHeader(buf, len);
         bool UDP = checkBox3.Checked;
         bool TCP = checkBox3.Checked;
         if ((ipHeader.ProtocolType == Protocol.UDP && UDP == true) || (ipHeader.ProtocolType == Protocol.TCP && TCP == true) || (ipHeader.ProtocolType == Protocol.ICMP))
         {
             string temp = string.Empty;
             for (int i = 0; i < len; i++)
             {
                 temp += buf[i].ToString("X2") + "  ";
                 if ((i + 1) % 16 == 0 && i != 0)
                 {
                     //string txt = Encoding.ASCII.GetString(buf, i, 16);
                     temp += "\r\n";
                 }
                 else
                 {
                 }
             }
             richTextBox1.BeginInvoke(new Action(() => richTextBox1.AppendText("\r\n\r\n\r\n" + "[---------------------------Raw data---------------------------]\r\n" + temp + "\r\n" + Parse(buf, len) + " \r\n")));
             SmWr.WriteLine("\r\n\r\n\r\n" + "[---------------------------Raw data---------------------------]\r\n" + temp + "\r\n" + Parse(buf, len) + " \r\n");
             SmWr.Flush();
         }
     }
     catch (Exception ex)
     {
         //CatchFunction(ex);
     }
 }
Beispiel #2
0
        public string Parse(byte[] buf, int len)
        {
            try
            {
                csSniffer.DataStructures.IPHeader ipHeader = new csSniffer.DataStructures.IPHeader(buf, len);
                string IpHeader = "\r\n[---------------------------New packet---------------------------]\r\n" +
                                  "--IP header: " +
                                  "-Version: " + ipHeader.Version +
                                  "\r\n-IP Header length: " + ipHeader.HeaderLength +
                                  "\r\n-TOS: " + ipHeader.DifferentiatedServices +
                                  "\r\n-Total length: " + ipHeader.TotalLength +
                                  "\r\n-Identification: " + ipHeader.Identification +
                                  "\r\n-Flags: " + ipHeader.Flags +
                                  "\r\n-Fragmentation: " + ipHeader.FragmentationOffset +
                                  "\r\n-TTL: " + ipHeader.TTL +
                                  "\r\n-Protocol: " + ipHeader.ProtocolType +
                                  "\r\n-Header checksum: " + ipHeader.Checksum +
                                  "\r\n-Source address: " + ipHeader.SourceAddress +
                                  "\r\n-Destination address: " + ipHeader.DestinationAddress;

                if (ipHeader.ProtocolType == Protocol.TCP)
                {
                    csSniffer.DataStructures.TCPHeader tcpHeader = new csSniffer.DataStructures.TCPHeader(ipHeader.Data, len - Convert.ToInt32(ipHeader.HeaderLength));
                    IpHeader += "\r\n\r\n--TCP header: " +
                                "\r\n-Source port: " + tcpHeader.usSourcePort +
                                "\r\n-Destination port: " + tcpHeader.usDestinationPort +
                                "\r\n-Sequence number: " + tcpHeader.uiSequenceNumber +
                                "\r\n-Acknowledge number: " + tcpHeader.uiAcknowledgeNumber +
                                "\r\n-Offset & space & flag: " + tcpHeader.usOffsetSpaceFlag +
                                "\r\n-Window: " + tcpHeader.usWindow +
                                "\r\n-Checksum: " + tcpHeader.usChecksum.ToString("X2").ToLower() +
                                "\r\n-Urgent pointer: " + tcpHeader.usUrgentPointer;
                }
                if (ipHeader.ProtocolType == Protocol.UDP)
                {
                    csSniffer.DataStructures.UDPHeader udpHeader = new csSniffer.DataStructures.UDPHeader(ipHeader.Data, len - Convert.ToInt32(ipHeader.HeaderLength));
                    IpHeader += "\r\n\r\n--UDP header: " +
                                "\r\n-Source port: " + udpHeader.usSourcePort +
                                "\r\n-Destination port: " + udpHeader.usDestinationPort +
                                "\r\n-Datagram length: " + udpHeader.usLength +
                                "\r\n-Checksum: " + udpHeader.usCheckSum.ToString("X2").ToLower();
                }
                if (ipHeader.ProtocolType == Protocol.ICMP)
                {
                    csSniffer.DataStructures.ICMPHeader udpHeader = new csSniffer.DataStructures.ICMPHeader(ipHeader.Data, len - Convert.ToInt32(ipHeader.HeaderLength));
                    IpHeader += "\r\n\r\n--ICMP header: " +
                                "\r\n-Type: " + udpHeader.Type +
                                "\r\n-Status: " + udpHeader.Serial +
                                "\r\n-Checksum: " + udpHeader.usCheckSum.ToString("X2").ToLower();
                }
                return(IpHeader);
            }
            catch (Exception ex)
            {
                //CatchFunction(ex);
                return("");
            }
        }