Example #1
0
 //source ip
 //dest ip
 public IPPacket(IPPayload pl)
 {
     _pl = pl;
     hLen = 20;
     Length = (UInt16)(pl.Length + hLen);
     Protocol = _pl.Protocol;
 }
Example #2
0
        private void ReadBuffer(byte[] buffer, int offset, int bufferSize)
        {
            int pktOffset = offset;

            //Bits 0-31
            byte v_hl;
            NetLib.ReadByte08(buffer, ref pktOffset, out v_hl);
            hLen = ((v_hl & 0xF) << 2);
            NetLib.ReadByte08(buffer, ref pktOffset, out typeOfService); //TODO, Implement this
            NetLib.ReadUInt16(buffer, ref pktOffset, out _Length);
            if (_Length > bufferSize - offset)
            {
                Log_Error("Unexpected Length");
                _Length = (UInt16)(bufferSize - offset);
            }
            //Error.WriteLine("len=" + Length); //Includes hlen

            //Bits 32-63
            NetLib.ReadUInt16(buffer, ref pktOffset, out id); //Send packets with unique IDs
            NetLib.ReadUInt16(buffer, ref pktOffset, out FragmentFlags);

            if (MoreFragments)
            {
                Log_Error("FragmentedPacket");
            }

            //Bits 64-95
            NetLib.ReadByte08(buffer, ref pktOffset, out ttl);
            NetLib.ReadByte08(buffer, ref pktOffset, out Protocol);
            NetLib.ReadUInt16(buffer, ref pktOffset, out checksum);
            //bool ccsum = verifyCheckSum(Ef.RawPacket.buffer, pktoffset);
            //Error.WriteLine("IP Checksum Good? " + ccsum);//Should ALWAYS be true

            //Bits 96-127
            NetLib.ReadByteArray(buffer, ref pktOffset, 4, out SourceIP);
            //Bits 128-159
            NetLib.ReadByteArray(buffer, ref pktOffset, 4, out DestinationIP);
            //WriteLine("Target IP :" + DestinationIP[0] + "." + DestinationIP[1] + "." + DestinationIP[2] + "." + DestinationIP[3]);

            //Bits 160+
            if (hLen > 20) //IP options (if any)
            {
                Log_Error("hlen=" + hLen + " > 20");
                Log_Error("IP options are not supported");
                throw new NotImplementedException("IP options are not supported");
            }
            switch (Protocol) //(Prase Payload)
            {
                case (byte)IPType.ICMP:
                    _pl = new ICMP(buffer, pktOffset, Length - hLen);
                    //((ICMP)_pl).VerifyCheckSum(SourceIP, DestinationIP);
                    break;
                case (byte)IPType.IGMP:
                    _pl = new IGMP(buffer, pktOffset, Length - hLen);
                    //((ICMP)_pl).VerifyCheckSum(SourceIP, DestinationIP);
                    break;
                case (byte)IPType.TCP:
                    _pl = new TCP(buffer, pktOffset, Length - hLen);
                    //((TCP)_pl).VerifyCheckSum(SourceIP, DestinationIP);
                    break;
                case (byte)IPType.UDP:
                    _pl = new UDP(buffer, pktOffset, Length - hLen);
                    //((UDP)_pl).VerifyCheckSum(SourceIP, DestinationIP);
                    break;
                default:
                    throw new NotImplementedException("Unkown IPv4 Protocol " + Protocol.ToString("X2"));
                    //break;
            }
        }
Example #3
0
 public abstract bool Send(IPPayload payload);
Example #4
0
        public override bool Send(IPPayload payload)
        {
            lock (deathClock)
            {
                deathClock.Restart();
            }
            UDP udp = (UDP)payload;

            if (destPort != 0)
            {
                //client already created
                if (!(udp.DestinationPort == destPort && udp.SourcePort == srcPort))
                {
                    Log_Error("UDP packet invalid for current session (Duplicate key?)");
                    return false;
                }
            }
            else
            {
                destPort = udp.DestinationPort;
                srcPort = udp.SourcePort;

                //Multicast address start with 0b1110
                if (Utils.memcmp(DestIP, 0, broadcastAddr, 0, 4))
                {
                    isBroadcast = true;
                }
                if ((DestIP[0] & 0xF0) == 0xE0)
                {
                    isMulticast = true;
                }

                if (isBroadcast)
                {
                    Log_Info("Is Broadcast");
                    client = new UdpClient(new IPEndPoint(adapterIP, srcPort)); //Assuming broadcast wants a return message
                    client.EnableBroadcast = true;

                    //client.Close();
                    //client = new UdpClient(SrcPort);
                    //client.BeginReceive(ReceiveFromBroadcast, new object());
                    //open = true;
                }
                else if (isMulticast)
                {
                    Log_Info("Is Multicast");
                    multicastAddr = DestIP;
                    client = new UdpClient(new IPEndPoint(adapterIP, 0));
                    IPAddress address = new IPAddress(multicastAddr);
                    //client.JoinMulticastGroup(address);
                }
                else
                {
                    IPAddress address = new IPAddress(DestIP);
                    if (srcPort == destPort)
                    {
                        client = new UdpClient(new IPEndPoint(adapterIP, srcPort)); //Needed for Crash TTR (and probable other games) LAN
                    }
                    else
                    {
                        client = new UdpClient(new IPEndPoint(adapterIP, 0));
                    }

                    client.Connect(address, destPort); //address to send on
                    if (srcPort != 0)
                    {
                        //Error.WriteLine("UDP expects Data");
                        //open = true;
                    }
                }
                open = true;
            }

            if (isBroadcast)
            {
                client.Send(udp.GetPayload(), udp.GetPayload().Length, new IPEndPoint(IPAddress.Broadcast, destPort));
            }
            else if (isMulticast)
            {
                client.Send(udp.GetPayload(), udp.GetPayload().Length, new IPEndPoint(new IPAddress(multicastAddr), destPort));
            }
            else
            {
                client.Send(udp.GetPayload(), udp.GetPayload().Length);
            }

            //if (udp.DestinationPort == 1900 | (DestIP[0] == 192 & DestIP[1] == 168))
            //{
                //Log_Error("Send");
                //string ret;
                //System.Text.Encoding targetEncoding = System.Text.Encoding.ASCII;
                //ret = targetEncoding.GetString(udp.GetPayload(), 0, udp.GetPayload().Length);
                //Log_Error(ret);
            //}

            //Error.WriteLine("UDP Sent");
            return true;
        }
Example #5
0
        private void ReadBuffer(byte[] buffer, int offset, int bufferSize, bool fromICMP)
        {
            int initialOffset = offset;
            int pktOffset     = offset;

            //Bits 0-31
            byte v_hl;

            NetLib.ReadByte08(buffer, ref pktOffset, out v_hl);
            hLen = ((v_hl & 0xF) << 2);
            NetLib.ReadByte08(buffer, ref pktOffset, out typeOfService); //TODO, Implement this

            //Not sure PS2 supports this

            //Log_Error("Class :" + Class.ToString());
            ////(DSCP support)
            //Log_Error("DropValue :" + DropProbability.ToString());
            //Log_Error("Supports ECN :" + IsECNCapable.ToString());
            //Log_Error("Congestion :" + CongestionEvent.ToString());
            ////TOS Support
            //Log_Error("LowDelay :" + TOSDelay.ToString());
            //Log_Error("HighThroughput :" + TOSThroughout.ToString());
            //Log_Error("LowCost :" + TOSCost.ToString());

            NetLib.ReadUInt16(buffer, ref pktOffset, out length);
            if (length > bufferSize - offset)
            {
                if (!fromICMP)
                {
                    Log_Error("Unexpected Length");
                }
                length = (UInt16)(bufferSize - offset);
            }

            //Bits 32-63
            NetLib.ReadUInt16(buffer, ref pktOffset, out id); //Send packets with unique IDs
            NetLib.ReadByte08(buffer, ref pktOffset, out fragmentFlags1);
            NetLib.ReadByte08(buffer, ref pktOffset, out fragmentFlags2);
            if (MoreFragments | FragmentOffset != 0)
            {
                Log_Error("FragmentedPacket");
                throw new NotImplementedException("Fragmented Packets are not supported");
            }

            //Bits 64-95
            NetLib.ReadByte08(buffer, ref pktOffset, out ttl);
            NetLib.ReadByte08(buffer, ref pktOffset, out Protocol);
            NetLib.ReadUInt16(buffer, ref pktOffset, out checksum);

            //Bits 96-127
            NetLib.ReadByteArray(buffer, ref pktOffset, 4, out SourceIP);
            //Bits 128-159
            NetLib.ReadByteArray(buffer, ref pktOffset, 4, out DestinationIP);
            //WriteLine("Target IP :" + DestinationIP[0] + "." + DestinationIP[1] + "." + DestinationIP[2] + "." + DestinationIP[3]);

            //Bits 160+
            if (hLen > 20) //IP options (if any)
            {
                bool opReadFin = false;
                do
                {
                    byte opKind = buffer[pktOffset];
                    byte opLen  = buffer[pktOffset + 1];
                    switch (opKind)
                    {
                    case 0:
                        opReadFin = true;
                        break;

                    case 1:
                        Options.Add(new IPopNOP());
                        pktOffset += 1;
                        continue;

                    case 148:
                        Options.Add(new IPopRouterAlert(buffer, offset));
                        break;

                    default:
                        Log_Error("Got IP Unknown Option " + opKind + " with len " + opLen);
                        throw new Exception("Got IP Unknown Option " + opKind + " with len " + opLen);
                        //break;
                    }
                    pktOffset += opLen;
                    if (pktOffset == initialOffset + hLen)
                    {
                        opReadFin = true;
                    }
                } while (opReadFin == false);
            }
            pktOffset = initialOffset + hLen;

            switch (Protocol) //(Prase Payload)
            {
            case (byte)IPType.ICMP:
                _pl = new ICMP(buffer, pktOffset, Length - hLen);
                //((ICMP)_pl).VerifyCheckSum(SourceIP, DestinationIP);
                break;

            case (byte)IPType.IGMP:
                _pl = new IGMP(buffer, pktOffset, Length - hLen);
                //((ICMP)_pl).VerifyCheckSum(SourceIP, DestinationIP);
                break;

            case (byte)IPType.TCP:
                _pl = new TCP(buffer, pktOffset, Length - hLen);
                //((TCP)_pl).VerifyCheckSum(SourceIP, DestinationIP);
                break;

            case (byte)IPType.UDP:
                _pl = new UDP(buffer, pktOffset, Length - hLen);
                //((UDP)_pl).VerifyCheckSum(SourceIP, DestinationIP);
                break;

            default:
                _pl = new IPUnkown(buffer, pktOffset, Length - hLen);
                Log_Error("Unkown IPv4 Protocol " + Protocol.ToString("X2"));
                break;
            }
        }
Example #6
0
        public override bool Send(IPPayload payload)
        {
            ICMP icmp = (ICMP)payload;

            switch (icmp.Type)
            {
                case 8: //Echo
                    //Code == zero
                    Log_Verb("Send Ping");
                    lock (sentry)
                    {
                        open += 1;
                    }
                    PingData pd;
                    pd.Data = icmp.Data;
                    pd.HeaderData = icmp.HeaderData;
                    Ping nPing = new Ping();
                    nPing.PingCompleted += PingCompleate;
                    lock (sentry)
                    {
                        pings.Add(nPing);
                    }
                    nPing.SendAsync(new IPAddress(DestIP), pd);
                    System.Threading.Thread.Sleep(1); //Hack Fix
                    break;
                case 3: //
                    switch (icmp.Code)
                    {
                        case 3:
                            Log_Error("Recived Packet Rejected, Port Closed");
                            IPPacket retPkt = new IPPacket(icmp);
                            byte[] srvIP = retPkt.SourceIP;
                            byte prot = retPkt.Protocol;
                            UInt16 srvPort = 0;
                            UInt16 ps2Port = 0;
                            switch (prot)
                            {
                                case (byte)IPType.TCP:
                                    TCP tcp = (TCP)retPkt.Payload;
                                    srvPort = tcp.SourcePort;
                                    ps2Port = tcp.DestinationPort;
                                    break;
                                case (byte)IPType.UDP:
                                    UDP udp = (UDP)retPkt.Payload;
                                    srvPort = udp.SourcePort;
                                    ps2Port = udp.DestinationPort;
                                    break;
                            }
                            ConnectionKey Key = new ConnectionKey();
                            Key.IP0 = srvIP[0]; Key.IP1 = srvIP[1]; Key.IP2 = srvIP[2]; Key.IP3 = srvIP[3];
                            Key.Protocol = prot;
                            Key.PS2Port = ps2Port;
                            Key.SRVPort = srvPort;

                            Session s;
                            connections.TryGetValue(Key, out s);

                            if (s != null)
                            {
                                s.Reset();
                                Log_Info("Reset Rejected Connection");
                            }
                            else
                            {
                                Log_Error("Failed To Reset Rejected Connection");
                            }
                            break;
                        default:
                            throw new NotImplementedException("Unsupported ICMP Code For Destination Unreachable" + icmp.Code);
                    }
                    break;
                default:
                    throw new NotImplementedException("Unsupported ICMP Type" + icmp.Type);
            }

            return true;
        }