예제 #1
0
        public bool SendICMP(ConnectionKey Key, IPPacket ipPkt)
        {
            Log_Verb("ICMP");
            //lock (sentry)
            //{
            int res = SendFromConnection(Key, ipPkt);

            if (res == 1)
            {
                return(true);
            }
            else if (res == 0)
            {
                return(false);
            }
            else
            {
                Log_Verb("Creating New Connection with key " + Key);
                ICMPSession s = new ICMPSession(Key, connections);
                s.ConnectionClosedEvent += HandleConnectionClosed;
                s.DestIP   = ipPkt.DestinationIP;
                s.SourceIP = dhcpServer.PS2IP;
                if (!connections.TryAdd(Key, s))
                {
                    throw new Exception("Connection Add Failed");
                }
                return(s.Send(ipPkt.Payload));
            }
            //}
        }
예제 #2
0
        //sends the packet and deletes it when done (if successful).rv :true success
        public override bool send(netHeader.NetPacket pkt)
        {
            bool result = false;

            PacketReader.EthernetFrame ef = new PacketReader.EthernetFrame(pkt);

            switch (ef.Protocol)
            {
            case (int)EtherFrameType.NULL:
                //Adapter Reset
                //TODO close all open connections
                break;

            case (int)EtherFrameType.IPv4:
                //Console.Error.WriteLine("IPv4");
                IPPacket ippkt = ((IPPacket)ef.Payload);
                if (ippkt.VerifyCheckSum() == false)
                {
                    Console.Error.WriteLine("IP packet with bad CSUM");
                }
                if (ippkt.Payload.VerifyCheckSum(ippkt.SourceIP, ippkt.DestinationIP) == false)
                {
                    Console.Error.WriteLine("IP packet with bad Payload CSUM");
                }

                string Key = (ippkt.DestinationIP[0]) + "." + (ippkt.DestinationIP[1]) + "." + (ippkt.DestinationIP[2]) + "." + ((UInt64)ippkt.DestinationIP[3])
                             + "-" + (ippkt.Protocol);

                switch (ippkt.Protocol)     //(Prase Payload)
                {
                case (byte)IPType.ICMP:
                    Console.Error.WriteLine("ICMP");
                    lock (sentry)
                    {
                        if (Connections.ContainsKey(Key))
                        {
                            if (Connections[Key].isOpen() == false)
                            {
                                throw new Exception("Attempt to send on Closed Connection");
                            }
                            Console.Error.WriteLine("Found Open Connection");
                            result = Connections[Key].send(ippkt.Payload);
                        }
                        else
                        {
                            Console.Error.WriteLine("Creating New Connection with key " + Key);
                            ICMPSession s = new ICMPSession();
                            s.DestIP   = ippkt.DestinationIP;
                            s.SourceIP = UDP_DHCPsession.PS2_IP;
                            result     = s.send(ippkt.Payload);
                            Connections.Add(Key, s);
                        }
                    }
                    break;

                case (byte)IPType.TCP:
                    //Console.Error.WriteLine("TCP");
                    TCP tcp = (TCP)ippkt.Payload;

                    Key += "-" + ((UInt64)tcp.SourcePort) + ":" + ((UInt64)tcp.DestinationPort);
                    lock (sentry)
                    {
                        if (Connections.ContainsKey(Key))
                        {
                            if (Connections[Key].isOpen() == false)
                            {
                                throw new Exception("Attempt to send on Closed TCP Connection of Key : " + Key + "");
                            }
                            //Console.Error.WriteLine("Found Open Connection");
                            result = Connections[Key].send(ippkt.Payload);
                        }
                        else
                        {
                            //Console.Error.WriteLine("Creating New Connection with key " + Key);
                            Console.Error.WriteLine("Creating New TCP Connection with Dest Port " + tcp.DestinationPort);
                            TCPSession s = new TCPSession();
                            s.DestIP   = ippkt.DestinationIP;
                            s.SourceIP = UDP_DHCPsession.PS2_IP;
                            result     = s.send(ippkt.Payload);
                            Connections.Add(Key, s);
                        }
                    }
                    break;

                case (byte)IPType.UDP:
                    //Console.Error.WriteLine("UDP");
                    UDP udp = (UDP)ippkt.Payload;

                    Key += "-" + ((UInt64)udp.SourcePort) + ":" + ((UInt64)udp.DestinationPort);
                    if (udp.DestinationPort == 67)
                    {         //DHCP
                        result = DCHP_server.send(ippkt.Payload);
                        break;
                    }
                    lock (sentry)
                    {
                        if (Connections.ContainsKey(Key))
                        {
                            if (Connections[Key].isOpen() == false)
                            {
                                throw new Exception("Attempt to send on Closed Connection");
                            }
                            //Console.Error.WriteLine("Found Open Connection");
                            result = Connections[Key].send(ippkt.Payload);
                        }
                        else
                        {
                            //Console.Error.WriteLine("Creating New Connection with key " + Key);
                            Console.Error.WriteLine("Creating New UDP Connection with Dest Port " + udp.DestinationPort);
                            UDPSession s = new UDPSession();
                            s.DestIP   = ippkt.DestinationIP;
                            s.SourceIP = UDP_DHCPsession.PS2_IP;
                            result     = s.send(ippkt.Payload);
                            Connections.Add(Key, s);
                        }
                    }
                    break;

                default:
                    Console.Error.WriteLine("Unkown Protocol");
                    //throw new NotImplementedException();
                    break;
                }
                //Console.Error.WriteLine("Key = " + Key);
                break;

                #region "ARP"
            case (int)EtherFrameType.ARP:
                Console.Error.WriteLine("ARP (Ignoring)");
                ARPPacket arppkt = ((ARPPacket)ef.Payload);

                ////Detect ARP Packet Types
                //if (Utils.memcmp(arppkt.SenderProtocolAddress, 0, new byte[] { 0, 0, 0, 0 }, 0, 4))
                //{
                //    Console.WriteLine("ARP Probe"); //(Who has my IP?)
                //    break;
                //}
                //if (Utils.memcmp(arppkt.SenderProtocolAddress, 0, arppkt.TargetProtocolAddress, 0, 4))
                //{
                //    if (Utils.memcmp(arppkt.TargetHardwareAddress, 0, new byte[] { 0, 0, 0, 0, 0, 0 }, 0, 6) & arppkt.OP == 1)
                //    {
                //        Console.WriteLine("ARP Announcement Type 1");
                //        break;
                //    }
                //    if (Utils.memcmp(arppkt.SenderHardwareAddress, 0, arppkt.TargetHardwareAddress, 0, 6) & arppkt.OP == 2)
                //    {
                //        Console.WriteLine("ARP Announcement Type 2");
                //        break;
                //    }
                //}

                ////if (arppkt.OP == 1) //ARP request
                ////{
                ////    //This didn't work for whatever reason.
                ////    if (Utils.memcmp(arppkt.TargetProtocolAddress,0,UDP_DHCPsession.GATEWAY_IP,0,4))
                ////    //it's trying to resolve the virtual gateway's mac addr
                ////    {
                ////        Console.Error.WriteLine("ARP Attempt to Resolve Gateway Mac");
                ////        arppkt.TargetHardwareAddress = arppkt.SenderHardwareAddress;
                ////        arppkt.SenderHardwareAddress = gateway_mac;
                ////        arppkt.TargetProtocolAddress = arppkt.SenderProtocolAddress;
                ////        arppkt.SenderProtocolAddress = UDP_DHCPsession.GATEWAY_IP;
                ////        arppkt.OP = 2;

                ////        EthernetFrame retARP = new EthernetFrame(arppkt);
                ////        retARP.DestinationMAC = ps2_mac;
                ////        retARP.SourceMAC = gateway_mac;
                ////        retARP.Protocol = (Int16)EtherFrameType.ARP;
                ////        vRecBuffer.Add(retARP.CreatePacket());
                ////        break;
                ////    }
                ////}
                //Console.Error.WriteLine("Unhandled ARP packet");

                result = true;
                break;

                #endregion
            case (int)0x0081:
                Console.Error.WriteLine("VLAN-tagged frame (IEEE 802.1Q)");
                throw new NotImplementedException();

            //break;
            default:
                Console.Error.WriteLine("Unkown EtherframeType " + ef.Protocol.ToString("X4"));
                break;
            }

            return(result);
        }