Example #1
0
            //sends the packet and deletes it when done (if successful).rv :true success
            public override bool send(netHeader.NetPacket pkt)
            {
                int writen = 0;

                Console.Error.WriteLine("---------------------Sent Packet");
                PacketReader.EthernetFrame ef = new PacketReader.EthernetFrame(pkt);

                htapstream.Write(pkt.buffer, 0, pkt.size);
                htapstream.Flush();
                //return type is void, assume full write
                writen = pkt.size;

                bool result = false;

                result = true;

                if (result)
                {
                    if (writen != pkt.size)
                    {
                        Console.Error.WriteLine("Incompleat Send " + Marshal.GetLastWin32Error());
                        return(false);
                    }

                    return(true);
                }
                else
                {
                    Console.Error.WriteLine("No Send Result" + Marshal.GetLastWin32Error());
                    return(false);
                }
            }
Example #2
0
            //gets a packet.rv :true success
            public override bool recv(ref netHeader.NetPacket pkt)
            {
                int  read_size = 0;
                bool result    = false;

                try {
                    read_size = htapstream.Read(pkt.buffer, 0, pkt.buffer.Length);
                    result    = true;
                }catch (Exception e)
                {
                    Console.WriteLine("Packet Recive Error :" + e.ToString());
                    return(false);
                }

                //Console.Error.WriteLine(read_size);

                if (result)
                {
                    //original memcmp returns 0 on perfect match
                    //the if statment check if !=0
                    byte[] eeprombytes = new byte[6];
                    for (int i = 0; i < 3; i++)
                    {
                        byte[] tmp = BitConverter.GetBytes(DEV9Header.dev9.eeprom[i]);
                        Utils.memcpy(ref eeprombytes, i * 2, tmp, 0, 2);
                    }
                    if ((Utils.memcmp(pkt.buffer, 0, eeprombytes, 0, 6) == false) & (Utils.memcmp(pkt.buffer, 0, broadcast_adddrrrr, 0, 6) == false))
                    {
                        //ignore strange packets
                        Console.Error.WriteLine("Dropping Strange Packet");
                        return(false);
                    }

                    if (Utils.memcmp(pkt.buffer, 6, eeprombytes, 0, 6) == true)
                    {
                        //avoid pcap looping packets
                        Console.Error.WriteLine("Dropping Looping Packet");
                        return(false);
                    }
                    pkt.size = read_size;
                    Console.Error.WriteLine("---------------------Recived Packet");
                    PacketReader.EthernetFrame ef = new PacketReader.EthernetFrame(pkt);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
Example #3
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);
        }
Example #4
0
        public IPPacket(EthernetFrame Ef)
        {
            int pktoffset = Ef.HeaderLength;

            //Bits 0-31
            byte v_hl = Ef.RawPacket.buffer[pktoffset];

            hlen          = ((v_hl & 0xF) << 2);
            typeofservice = Ef.RawPacket.buffer[pktoffset + 1]; //TODO, Implement this
            NO_Length     = (BitConverter.ToInt16(Ef.RawPacket.buffer, pktoffset + 2));
            //Console.Error.WriteLine("len=" + Length); //Includes hlen

            //Bits 32-63
            NO_id = (BitConverter.ToInt16(Ef.RawPacket.buffer, pktoffset + 4)); //Send packets with unique IDs
            NO_fragmented_flags = BitConverter.ToInt16(Ef.RawPacket.buffer, pktoffset + 6);

            if (MoreFragments)
            {
                Console.Error.WriteLine("FragmentedPacket");
            }

            //Bits 64-95
            ttl      = Ef.RawPacket.buffer[pktoffset + 8];
            Protocol = Ef.RawPacket.buffer[pktoffset + 9];
            NO_csum  = (BitConverter.ToInt16(Ef.RawPacket.buffer, pktoffset + 10));
            //bool ccsum = verifyCheckSum(Ef.RawPacket.buffer, pktoffset);
            //Console.Error.WriteLine("IP Checksum Good? " + ccsum);//Should ALWAYS be true

            //Bits 96-127
            Utils.memcpy(ref SourceIP, 0, Ef.RawPacket.buffer, pktoffset + 12, 4);
            //Bits 128-159
            Utils.memcpy(ref DestinationIP, 0, Ef.RawPacket.buffer, pktoffset + 16, 4);
            //Console.WriteLine("Target IP :" + DestinationIP[0] + "." + DestinationIP[1] + "." + DestinationIP[2] + "." + DestinationIP[3]);

            //Bits 160+
            if (hlen > 20) //IP options (if any)
            {
                Console.Error.WriteLine("hlen=" + hlen + " > 20");
                Console.Error.WriteLine("IP options are not supported");
                throw new NotImplementedException("IP options are not supported");
            }
            switch (Protocol) //(Prase Payload)
            {
            case (byte)IPType.ICMP:
                _pl = new ICMP(Ef, pktoffset + hlen, Length - hlen);
                //((ICMP)_pl).VerifyCheckSum(SourceIP, DestinationIP);
                break;

            case (byte)IPType.TCP:
                _pl = new TCP(Ef, pktoffset + hlen, Length - hlen);
                //((TCP)_pl).VerifyCheckSum(SourceIP, DestinationIP);
                break;

            case (byte)IPType.UDP:
                _pl = new UDP(Ef, pktoffset + hlen);
                //((UDP)_pl).VerifyCheckSum(SourceIP, DestinationIP);
                break;

            default:
                throw new NotImplementedException("Unkown IPv4 Protocol " + Protocol.ToString("X2"));
                //break;
            }
        }