Пример #1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void sendICMPEchoResponse(pspsharp.network.protocols.NetPacket packet, pspsharp.network.protocols.EtherFrame frame, pspsharp.network.protocols.IPv4 ipv4, pspsharp.network.protocols.ICMP icmp) throws java.io.EOFException
        private void sendICMPEchoResponse(NetPacket packet, EtherFrame frame, IPv4 ipv4, ICMP icmp)
        {
            bool reachable = false;

            try
            {
                InetAddress inetAddress = InetAddress.getByAddress(ipv4.destinationIPAddress);
                // Timeout after 1 second
                reachable = inetAddress.isReachable(null, ipv4.timeToLive, 1000);
            }
            catch (UnknownHostException)
            {
            }
            catch (IOException)
            {
            }

            if (reachable)
            {
                // See https://en.wikipedia.org/wiki/Ping_(networking_utility)
                EtherFrame answerFrame = new EtherFrame(frame);
                answerFrame.swapSourceAndDestination();

                IPv4 answerIPv4 = new IPv4(ipv4);
                answerIPv4.swapSourceAndDestination();
                answerIPv4.timeToLive--;                 // When a packet arrives at a router, the router decreases the TTL FieldInfo.

                ICMP answerIcmp = new ICMP(icmp);
                answerIcmp.type = ICMP.ICMP_CONTROL_ECHO_REPLY;
                answerIcmp.computeChecksum();

                answerIPv4.totalLength = answerIPv4.sizeOf() + answerIcmp.sizeOf();
                answerIPv4.computeChecksum();

                // Write the different headers in sequence
                NetPacket answerPacket = new NetPacket(BUFFER_SIZE);
                answerFrame.write(answerPacket);
                answerIPv4.write(answerPacket);
                answerIcmp.write(answerPacket);

                sendPacket(answerPacket);
            }
        }
Пример #2
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void processMessageDNS(pspsharp.network.protocols.NetPacket packet, pspsharp.network.protocols.EtherFrame frame, pspsharp.network.protocols.IPv4 ipv4, pspsharp.network.protocols.UDP udp) throws java.io.EOFException
        private void processMessageDNS(NetPacket packet, EtherFrame frame, IPv4 ipv4, UDP udp)
        {
            DNS dns = new DNS();

            dns.read(packet);

            //if (log.DebugEnabled)
            {
                Console.WriteLine(string.Format("processMessageDNS {0}", dns));
            }

            if (!dns.isResponseFlag && dns.questionCount == 1)
            {
                DNS.DNSRecord question = dns.questions[0];
                string        hostName = question.recordName;

                DNS answerDns = new DNS(dns);
                try
                {
                    InetAddress inetAddress = InetAddress.getByName(hostName);
                    //if (log.DebugEnabled)
                    {
                        Console.WriteLine(string.Format("DNS response '{0}'={1}", hostName, inetAddress));
                    }

                    DNS.DNSAnswerRecord answer = new DNS.DNSAnswerRecord();
                    answer.recordName           = hostName;
                    answer.recordClass          = question.recordClass;
                    answer.recordType           = question.recordType;
                    answer.data                 = inetAddress.Address;
                    answer.dataLength           = answer.data.Length;
                    answerDns.responseCode      = DNS_RESPONSE_CODE_NO_ERROR;
                    answerDns.answerRecordCount = 1;
                    answerDns.answerRecords     = new DNS.DNSAnswerRecord[] { answer };
                }
                catch (UnknownHostException e)
                {
                    answerDns.responseCode = DNS_RESPONSE_CODE_NAME_ERROR;
                    //if (log.DebugEnabled)
                    {
                        Console.WriteLine(string.Format("processMessageDNS unknown host '{0}'({1})", hostName, e.ToString()));
                    }
                }

                answerDns.isResponseFlag = true;

                EtherFrame answerFrame = new EtherFrame(frame);
                answerFrame.swapSourceAndDestination();

                IPv4 answerIPv4 = new IPv4(ipv4);
                answerIPv4.swapSourceAndDestination();
                answerIPv4.timeToLive--;                 // When a packet arrives at a router, the router decreases the TTL FieldInfo.

                UDP answerUdp = new UDP(udp);
                answerUdp.swapSourceAndDestination();

                // Update lengths and checksums
                answerUdp.Length = answerUdp.sizeOf() + answerDns.sizeOf();
                answerUdp.computeChecksum();
                answerIPv4.totalLength = answerIPv4.sizeOf() + answerUdp.Length;
                answerIPv4.computeChecksum();

                // Write the different headers in sequence
                NetPacket answerPacket = new NetPacket(BUFFER_SIZE);
                answerFrame.write(answerPacket);
                answerIPv4.write(answerPacket);
                answerUdp.write(answerPacket);
                answerDns.write(answerPacket);

                sendPacket(answerPacket);
            }
        }