Exemplo n.º 1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void sendTcpData(TcpConnectionState tcpConnectionState, byte[] data) throws java.io.EOFException
        private void sendTcpData(TcpConnectionState tcpConnectionState, sbyte[] data)
        {
            EtherFrame answerFrame = new EtherFrame();

            answerFrame.srcMac = tcpConnectionState.destinationMacAddress;
            answerFrame.dstMac = tcpConnectionState.sourceMacAddress;
            answerFrame.type   = ETHER_TYPE_IPv4;

            IPv4 answerIPv4 = new IPv4();

            answerIPv4.protocol             = IPv4_PROTOCOL_TCP;
            answerIPv4.sourceIPAddress      = tcpConnectionState.destinationIPAddress;
            answerIPv4.destinationIPAddress = tcpConnectionState.sourceIPAddress;

            TCP answerTcp = new TCP();

            answerTcp.sourcePort           = tcpConnectionState.destinationPort;
            answerTcp.destinationPort      = tcpConnectionState.sourcePort;
            answerTcp.sequenceNumber       = tcpConnectionState.destinationSequenceNumber;
            answerTcp.acknowledgmentNumber = tcpConnectionState.sourceSequenceNumber;
            answerTcp.flagACK = true;
            answerTcp.flagPSH = true;
            tcpConnectionState.destinationSequenceNumber += data.Length;
            answerTcp.data = data;

            // Update lengths and checksums
            answerTcp.computeChecksum(answerIPv4);
            answerIPv4.totalLength = answerIPv4.sizeOf() + answerTcp.sizeOf();
            answerIPv4.computeChecksum();

            // Write the different headers in sequence
            NetPacket answerPacket = new NetPacket(BUFFER_SIZE);

            answerFrame.write(answerPacket);
            answerIPv4.write(answerPacket);
            answerTcp.write(answerPacket);

            //if (log.DebugEnabled)
            {
                Console.WriteLine(string.Format("sendTcpData frame={0}", answerFrame));
                Console.WriteLine(string.Format("sendTcpData IPv4={0}", answerIPv4));
                Console.WriteLine(string.Format("sendTcpData TCP={0}", answerTcp));
            }

            sendPacket(answerPacket);
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void sendDHCPReply(pspsharp.network.protocols.EtherFrame frame, pspsharp.network.protocols.IPv4 ipv4, pspsharp.network.protocols.UDP udp, pspsharp.network.protocols.DHCP dhcp, int messageType) throws java.io.EOFException
        private void sendDHCPReply(EtherFrame frame, IPv4 ipv4, UDP udp, DHCP dhcp, int messageType)
        {
            // Send back a DHCP offer message
            EtherFrame answerFrame = new EtherFrame(frame);

            answerFrame.swapSourceAndDestination();
            answerFrame.srcMac = MacAddress;

            IPv4 answerIPv4 = new IPv4(ipv4);

            answerIPv4.sourceIPAddress = IpAddress;
            answerIPv4.timeToLive--;             // When a packet arrives at a router, the router decreases the TTL FieldInfo.

            UDP answerUdp = new UDP(udp);

            answerUdp.swapSourceAndDestination();

            DHCP answerDhcp = new DHCP(dhcp);

            answerDhcp.opcode              = DHCP_BOOT_REPLY;
            answerDhcp.yourIPAddress       = LocalIpAddress;
            answerDhcp.nextServerIPAddress = IpAddress;

            answerDhcp.clearOptions();
            // The DHCP message type
            answerDhcp.addOption(new DHCP.DHCPOption(DHCP.DHCP_OPTION_MESSAGE_TYPE, (sbyte)messageType));
            // The subnet mask
            answerDhcp.addOption(new DHCP.DHCPOption(DHCP.DHCP_OPTION_SUBNET_MASK, getIpAddress(sceNetApctl.SubnetMaskInt)));
            // The only router is myself
            answerDhcp.addOption(new DHCP.DHCPOption(DHCP.DHCP_OPTION_ROUTER, IpAddress));
            // The IP address lease time is forever
            answerDhcp.addOption(new DHCP.DHCPOption(DHCP.DHCP_OPTION_IP_ADDRESS_LEASE_TIME, int.MaxValue));
            // The DHCP server identification is myself
            answerDhcp.addOption(new DHCP.DHCPOption(DHCP.DHCP_OPTION_SERVER_IDENTIFIER, IpAddress));
            // The only DNS server is myself
            answerDhcp.addOption(new DHCP.DHCPOption(DHCP.DHCP_OPTION_DNS, IpAddress));
            // The broadcast address
            answerDhcp.addOption(new DHCP.DHCPOption(DHCP.DHCP_OPTION_BROADCAST_ADDRESS, DHCP.broadcastIPAddress));

            // Update lengths and checksums
            answerUdp.Length = answerUdp.sizeOf() + answerDhcp.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);
            answerDhcp.write(answerPacket);

            //if (log.DebugEnabled)
            {
                Console.WriteLine(string.Format("sendDHCPReply frame={0}", answerFrame));
                Console.WriteLine(string.Format("sendDHCPReply IPv4={0}", answerIPv4));
                Console.WriteLine(string.Format("sendDHCPReply UDP={0}", answerUdp));
                Console.WriteLine(string.Format("sendDHCPReply messageType={0:D}, DHCP={1}", messageType, answerDhcp));
            }

            sendPacket(answerPacket);
        }