Esempio n. 1
0
    public static void Run(string c)
    {
        var p = c.Split(".");

        try
        {
            UdpClient uClient = new UdpClient();

            var ip = new Cosmos.System.Network.IPv4.Address((byte)int.Parse(p[0]), (byte)int.Parse(p[1]),
                                                            (byte)int.Parse(p[2]), (byte)int.Parse(p[3]));
            var    host    = new Cosmos.System.Network.IPv4.EndPoint(ip, 80);
            byte[] payload = Encoding.ASCII.GetBytes("abcdefghijklmnopqrstuvwabcdefghi");

            try
            {
                uClient.Connect(ip, 80);
                uClient.Send(payload, ip, 80);
                uClient.Receive(ref host);
                Console.WriteLine("Connected!");
                uClient = null;
            }
            catch (NullReferenceException e)
            {
                Console.WriteLine("Connection Failed");
                Console.WriteLine(e.Message);
            }
        }
        catch
        {
            Console.WriteLine("Connection Failed");
        }
    }
Esempio n. 2
0
        public void EnsureEncodedCorrectly()
        {
            var xMacSender = new MACAddress(new byte[]
                                            {
                                                00,
                                                0x50,
                                                0x56,
                                                0xc0,
                                                0x00,
                                                0x01
                                            });
            var xIPSender = new Address(192, 168, 17, 1);
            var xMacTarget = MACAddress.Broadcast;
            var xIPTarget = new Address(192, 168, 17, 102);
            var xArpTargetMac = new MACAddress(new byte[6]);

            var xPacket = new ARPRequest_Ethernet(xMacSender, xIPSender, xMacTarget, xIPTarget, xArpTargetMac);

            var xExpectedPacket =
                "FF FF FF FF FF FF 00 50 " +
                "56 C0 00 01 08 06 00 01 " +
                "08 00 06 04 00 01 00 50 " +
                "56 C0 00 01 C0 A8 11 01 " +
                "00 00 00 00 00 00 C0 A8 " +
                "11 66";
            Approvals.Verify(Utilities.PrettyPrint(xPacket.RawData));
            //Approvals.VerifyBinaryFile(xPacket.RawData, "bin");
            //Assert.AreEqual(xExpectedPacket, Utilities.PrettyPrint(xPacket.RawData));
        }
Esempio n. 3
0
        public void Send(byte[] data, IPv4.Address dest, Int32 destPort)
        {
            IPv4.Address   source = IPv4.Config.FindNetwork(dest);
            IPv4.UDPPacket packet = new IPv4.UDPPacket(source, dest, (UInt16)this.localPort, (UInt16)destPort, data);

            Sys.Console.WriteLine("Sending " + packet.ToString());
            IPv4.OutgoingBuffer.AddPacket(packet);
        }
Esempio n. 4
0
 protected override void initFields()
 {
     //Sys.Console.WriteLine("ARPPacket_Ethernet.initFields() called");
     base.initFields();
     mSenderMAC = new MACAddress(mRawData, 22);
     mSenderIP = new Address(mRawData, 28);
     mTargetMAC = new MACAddress(mRawData, 32);
     mTargetIP = new Address(mRawData, 38);
 }
Esempio n. 5
0
        internal static Address FindRoute(Address destIP)
        {
            for (int c = 0; c < ipConfigs.Count; c++)
            {
                if (ipConfigs[c].DefaultGateway.CompareTo(Address.Zero) != 0)
                {
                    return ipConfigs[c].DefaultGateway;
                }
            }

            return null;
        }
Esempio n. 6
0
        internal static bool IsLocalAddress(Address destIP)
        {
            for (int c = 0; c < ipConfigs.Count; c++)
            {
                if ((ipConfigs[c].IPAddress.Hash & ipConfigs[c].SubnetMask.Hash) ==
                    (destIP.Hash & ipConfigs[c].SubnetMask.Hash))
                {
                    return true;
                }
            }

            return false;
        }
Esempio n. 7
0
 protected override void initFields()
 {
     Sys.Console.WriteLine("ARPPacket_Ethernet.initFields() called");
     base.initFields();
     mSenderMAC = new MACAddress(mRawData, 22);
     mSenderIP = new Address(mRawData, 28);
     global::System.Console.WriteLine("SenderIP created");
     if (SenderIP == null)
     {
         global::System.Console.WriteLine("But its already null again");
     }
     mTargetMAC = new MACAddress(mRawData, 32);
     mTargetIP = new Address(mRawData, 38);
 }
Esempio n. 8
0
        internal ICMPPacket(Address source, Address dest, byte type, byte code, UInt16 id, UInt16 seq, UInt16 icmpDataSize)
            : base(icmpDataSize, 1, source, dest)
        {
            mRawData[this.dataOffset] = type;
            mRawData[this.dataOffset + 1] = code;
            mRawData[this.dataOffset + 2] = 0x00;
            mRawData[this.dataOffset + 3] = 0x00;
            mRawData[this.dataOffset + 4] = (byte)((id >> 8) & 0xFF);
            mRawData[this.dataOffset + 5] = (byte)((id >> 0) & 0xFF);
            mRawData[this.dataOffset + 6] = (byte)((seq >> 8) & 0xFF);
            mRawData[this.dataOffset + 7] = (byte)((seq >> 0) & 0xFF);

            icmpCRC = CalcICMPCRC((UInt16)(icmpDataSize + 8));
            mRawData[this.dataOffset + 2] = (byte)((icmpCRC >> 8) & 0xFF);
            mRawData[this.dataOffset + 3] = (byte)((icmpCRC >> 0) & 0xFF);
            initFields();
        }
Esempio n. 9
0
        protected ARPPacket_Ethernet(UInt16 operation, MACAddress senderMAC, Address senderIP,
            MACAddress targetMAC, Address targetIP, int packet_size, MACAddress arpTargetMAC)
            : base(targetMAC, senderMAC, 1, 0x0800, 6, 4, operation, packet_size)
        {
            for (int i = 0; i < 6; i++)
            {
                mRawData[22 + i] = senderMAC.bytes[i];
                mRawData[32 + i] = arpTargetMAC.bytes[i];
            }
            for (int i = 0; i < 4; i++)
            {
                mRawData[28 + i] = senderIP.address[i];
                mRawData[38 + i] = targetIP.address[i];
            }

            initFields();
        }
Esempio n. 10
0
 protected override void initFields()
 {
     base.initFields();
     ipVersion = (byte)((mRawData[14] & 0xF0) >> 4);
     ipHeaderLength = (byte)(mRawData[14] & 0x0F);
     tos = mRawData[15];
     ipLength = (UInt16)((mRawData[16] << 8) | mRawData[17]);
     fragmentID = (UInt16)((mRawData[18] << 8) | mRawData[19]);
     flags = (byte)((mRawData[20] & 0xE0) >> 5);
     fragmentOffset = (UInt16)(((mRawData[20] & 0x1F) << 8) | mRawData[21]);
     ttl = mRawData[22];
     proto = mRawData[23];
     ipCRC = (UInt16)((mRawData[24] << 8) | mRawData[25]);
     sourceIP = new Address(mRawData, 26);
     destIP = new Address(mRawData, 30);
     dataOffset = (UInt16)(14 + HeaderLength);
 }
Esempio n. 11
0
        public UDPPacket(Address source, Address dest, UInt16 srcPort, UInt16 destPort, byte[] data)
            : base((UInt16)(data.Length + 8), 17, source, dest)
        {
            mRawData[this.dataOffset + 0] = (byte)((srcPort >> 8) & 0xFF);
            mRawData[this.dataOffset + 1] = (byte)((srcPort >> 0) & 0xFF);
            mRawData[this.dataOffset + 2] = (byte)((destPort >> 8) & 0xFF);
            mRawData[this.dataOffset + 3] = (byte)((destPort >> 0) & 0xFF);
            udpLen = (UInt16)(data.Length + 8);
            mRawData[this.dataOffset + 4] = (byte)((udpLen >> 8) & 0xFF);
            mRawData[this.dataOffset + 5] = (byte)((udpLen >> 0) & 0xFF);
            mRawData[this.dataOffset + 6] = 0;
            mRawData[this.dataOffset + 7] = 0;
            for (int b = 0; b < data.Length; b++)
            {
                mRawData[this.dataOffset + 8 + b] = data[b];
            }

            initFields();
        }
Esempio n. 12
0
        internal static Address FindNetwork(Address destIP)
        {
            Address default_gw = null;

            for (int c = 0; c < ipConfigs.Count; c++)
            {
                if ((ipConfigs[c].IPAddress.Hash & ipConfigs[c].SubnetMask.Hash) ==
                    (destIP.Hash & ipConfigs[c].SubnetMask.Hash))
                {
                    return ipConfigs[c].IPAddress;
                }
                if ((default_gw == null) && (ipConfigs[c].DefaultGateway.CompareTo(Address.Zero) != 0))
                {
                    default_gw = ipConfigs[c].IPAddress;
                }
            }

            return default_gw;
        }
Esempio n. 13
0
        //public class DictIntComparer : IEqualityComparer<int>
        //{
        //    public bool Equals(int x, int y)
        //    {
        //        return x == y;
        //    }

        //    public int GetHashCode(int obj)
        //    {
        //        return obj;
        //    }
        //}

        //private Dictionary<int, string> testDict;
        //private DictIntComparer testDictComparer;

        //public class IntMap<TValue> : Map<int, TValue>
        //{
        //    protected override bool keysEqual(int key1, int key2)
        //    {
        //        return (key1 == key2);
        //    }
        //}
        //public class StringMap<TValue> : Map<string, TValue>
        //{
        //    protected override bool keysEqual(string key1, string key2)
        //    {
        //        return (key1 == key2);
        //    }
        //}

        //public float Pow(float x, float y)
        //{
        //    if (y == 0)
        //    {
        //        return 1;
        //    }
        //    else if (y == 1)
        //    {
        //        return x;
        //    }
        //    else
        //    {
        //        float xResult = x;

        //        for (int i = 2; i <= y; i++)
        //        {
        //            xResult = xResult * x;
        //        }

        //        return xResult;
        //    }
        //}
        //private float Sqrt(float d)
        //{
        //    if ((d < 0) || (float.IsNaN(d) == true) || (float.IsNegativeInfinity(d) == true))
        //    {
        //        return float.NaN;
        //    }
        //    if (float.IsPositiveInfinity(d) == true)
        //    {
        //        return float.PositiveInfinity;
        //    }
        //    if (d == 0)
        //    {
        //        return 0;
        //    }
        //    if (d == 1)
        //    {
        //        return 1;
        //    }

        //    string strD = d.ToString();
        //    int D = strD.Length;
        //    int n = 0;
        //    float x;
        //    if ((D % 2) == 0)
        //    {
        //        n = (D - 2) / 2;
        //        x = 6.0f * Pow(10f, n);
        //    }
        //    else
        //    {
        //        n = (D - 1) / 2;
        //        x = 2.0f * Pow(10f, n);
        //    }
        //    int precision = (strD.Length > 6 ? strD.Length : 6);

        //    return x;
        //}

        //internal class BaseClass
        //{
        //    public int baseField;

        //    internal BaseClass()
        //    {
        //        initFields();
        //    }

        //    protected virtual void initFields()
        //    {
        //        baseField = 1;
        //    }
        //}
        //internal class SubClass : BaseClass
        //{
        //    public int subField;

        //    internal SubClass()
        //        : base()
        //    {
        //    }

        //    protected override void initFields()
        //    {
        //        base.initFields();
        //        subField = 2;
        //    }
        //}
        //internal class SubSubClass : SubClass
        //{
        //    public int subsubField;

        //    internal SubSubClass()
        //        :base()
        //    {
        //    }

        //    protected override void initFields()
        //    {
        //        base.initFields();
        //        subsubField = 3;
        //    }
        //}

        protected override void BeforeRun()
        {
            Console.WriteLine("SSchocke Test Playground");

            Console.WriteLine("Finding network devices...");
            AMDPCNetII.FindAll();

            NetworkDevice nic = NetworkDevice.Devices[0];

            IPv4.Address myIP      = new IPv4.Address(192, 168, 1, 51);
            IPv4.Address mySubnet  = new IPv4.Address(255, 255, 255, 0);
            IPv4.Address myGateway = new IPv4.Address(192, 168, 1, 1);
            IPv4.Config  myConfig  = new IPv4.Config(myIP, mySubnet, myGateway);

            NetworkStack.ConfigIP(nic, myConfig);
            nic.Enable();

            Console.WriteLine("Init Done... Starting main loop");
        }
Esempio n. 14
0
 public EndPoint(Address addr, UInt16 port)
 {
     this.address = addr;
     this.port = port;
 }
Esempio n. 15
0
 internal static NetworkDevice FindInterface(Address sourceIP)
 {
     return NetworkStack.AddressMap[sourceIP.Hash];
 }
Esempio n. 16
0
        private IPPacket(MACAddress srcMAC, MACAddress destMAC, UInt16 dataLength, byte protocol,
            Address source, Address dest)
            : base(destMAC, srcMAC, 0x0800, dataLength + 14 + 20)
        {
            mRawData[14] = 0x45;
            mRawData[15] = 0;
            ipLength = (UInt16)(dataLength + 20);
            ipHeaderLength = 5;
            mRawData[16] = (byte)((ipLength >> 8) & 0xFF);
            mRawData[17] = (byte)((ipLength >> 0) & 0xFF);
            fragmentID = NextIPFragmentID;
            mRawData[18] = (byte)((fragmentID >> 8) & 0xFF);
            mRawData[19] = (byte)((fragmentID >> 0) & 0xFF);
            mRawData[20] = 0x00;
            mRawData[21] = 0x00;
            mRawData[22] = 0x80;
            mRawData[23] = protocol;
            mRawData[24] = 0x00;
            mRawData[25] = 0x00;
            for (int b = 0; b < 4; b++)
            {
                mRawData[26 + b] = source.address[b];
                mRawData[30 + b] = dest.address[b];
            }
            ipCRC = CalcIPCRC(20);
            mRawData[24] = (byte)((ipCRC >> 8) & 0xFF);
            mRawData[25] = (byte)((ipCRC >> 0) & 0xFF);

            initFields();
        }
Esempio n. 17
0
 protected IPPacket(UInt16 dataLength, byte protocol, Address source, Address dest)
     : this(MACAddress.None, MACAddress.None, dataLength, protocol, source, dest)
 { }
Esempio n. 18
0
 /// <summary>
 /// Create a IPv4 Configuration with no default gateway
 /// </summary>
 /// <param name="ip">IP Address</param>
 /// <param name="subnet">Subnet Mask</param>
 public Config(Address ip, Address subnet)
     : this(ip, subnet, Address.Zero)
 { }
Esempio n. 19
0
 /// <summary>
 /// Create a IPv4 Configuration
 /// </summary>
 /// <param name="ip">IP Address</param>
 /// <param name="subnet">Subnet Mask</param>
 /// <param name="gw">Default gateway</param>
 public Config(Address ip, Address subnet, Address gw)
 {
     this.address = ip;
     this.subnetMask = subnet;
     this.defaultGateway = gw;
 }
Esempio n. 20
0
 public void Connect(IPv4.Address dest, Int32 destPort)
 {
     this.destination     = dest;
     this.destinationPort = destPort;
 }
Esempio n. 21
0
 public UdpClient(IPv4.Address dest, Int32 destPort)
     : this(0)
 {
     this.destination     = dest;
     this.destinationPort = destPort;
 }
Esempio n. 22
0
 internal ARPReply_Ethernet(MACAddress ourMAC, Address ourIP, MACAddress targetMAC, Address targetIP)
     : base(2, ourMAC, ourIP, targetMAC, targetIP, 42, MACAddress.None)
 { }
Esempio n. 23
0
        internal ICMPEchoRequest(Address source, Address dest, UInt16 id, UInt16 sequence)
            : base(source, dest, 8, 0, id, sequence, 40)
        {
            for (byte b = 8; b < this.ICMP_DataLength; b++)
            {
                mRawData[this.dataOffset + b] = b;
            }

            mRawData[this.dataOffset + 2] = 0x00;
            mRawData[this.dataOffset + 3] = 0x00;
            icmpCRC = CalcICMPCRC((UInt16)(this.ICMP_DataLength + 8));
            mRawData[this.dataOffset + 2] = (byte)((icmpCRC >> 8) & 0xFF);
            mRawData[this.dataOffset + 3] = (byte)((icmpCRC >> 0) & 0xFF);
        }
Esempio n. 24
0
        //public class DictIntComparer : IEqualityComparer<int>
        //{
        //    public bool Equals(int x, int y)
        //    {
        //        return x == y;
        //    }

        //    public int GetHashCode(int obj)
        //    {
        //        return obj;
        //    }
        //}

        //private Dictionary<int, string> testDict;
        //private DictIntComparer testDictComparer;

        //public class IntMap<TValue> : Map<int, TValue>
        //{
        //    protected override bool keysEqual(int key1, int key2)
        //    {
        //        return (key1 == key2);
        //    }
        //}
        //public class StringMap<TValue> : Map<string, TValue>
        //{
        //    protected override bool keysEqual(string key1, string key2)
        //    {
        //        return (key1 == key2);
        //    }
        //}

        //public float Pow(float x, float y)
        //{
        //    if (y == 0)
        //    {
        //        return 1;
        //    }
        //    else if (y == 1)
        //    {
        //        return x;
        //    }
        //    else
        //    {
        //        float xResult = x;

        //        for (int i = 2; i <= y; i++)
        //        {
        //            xResult = xResult * x;
        //        }

        //        return xResult;
        //    }
        //}
        //private float Sqrt(float d)
        //{
        //    if ((d < 0) || (float.IsNaN(d) == true) || (float.IsNegativeInfinity(d) == true))
        //    {
        //        return float.NaN;
        //    }
        //    if (float.IsPositiveInfinity(d) == true)
        //    {
        //        return float.PositiveInfinity;
        //    }
        //    if (d == 0)
        //    {
        //        return 0;
        //    }
        //    if (d == 1)
        //    {
        //        return 1;
        //    }

        //    string strD = d.ToString();
        //    int D = strD.Length;
        //    int n = 0;
        //    float x;
        //    if ((D % 2) == 0)
        //    {
        //        n = (D - 2) / 2;
        //        x = 6.0f * Pow(10f, n);
        //    }
        //    else
        //    {
        //        n = (D - 1) / 2;
        //        x = 2.0f * Pow(10f, n);
        //    }
        //    int precision = (strD.Length > 6 ? strD.Length : 6);

        //    return x;
        //}

        //internal class BaseClass
        //{
        //    public int baseField;

        //    internal BaseClass()
        //    {
        //        initFields();
        //    }

        //    protected virtual void initFields()
        //    {
        //        baseField = 1;
        //    }
        //}
        //internal class SubClass : BaseClass
        //{
        //    public int subField;

        //    internal SubClass()
        //        : base()
        //    {
        //    }

        //    protected override void initFields()
        //    {
        //        base.initFields();
        //        subField = 2;
        //    }
        //}
        //internal class SubSubClass : SubClass
        //{
        //    public int subsubField;

        //    internal SubSubClass()
        //        :base()
        //    {
        //    }

        //    protected override void initFields()
        //    {
        //        base.initFields();
        //        subsubField = 3;
        //    }
        //}

        protected override void BeforeRun()
        {
            Console.WriteLine("SSchocke Test Playground");

            Console.WriteLine("Finding network devices...");
            AMDPCNetII.FindAll();

            NetworkDevice nic = NetworkDevice.Devices[0];

            IPv4.Address myIP = new IPv4.Address(192, 168, 1, 51);
            IPv4.Address mySubnet = new IPv4.Address(255, 255, 255, 0);
            IPv4.Address myGateway = new IPv4.Address(192, 168, 1, 1);
            IPv4.Config myConfig = new IPv4.Config(myIP, mySubnet, myGateway);

            NetworkStack.ConfigIP(nic, myConfig);
            nic.Enable();

            Console.WriteLine("Init Done... Starting main loop");
        }
Esempio n. 25
0
 internal ARPRequest_Ethernet(MACAddress ourMAC, Address ourIP, MACAddress targetMAC, Address targetIP, MACAddress arpTargetMAC)
     : base(1, ourMAC, ourIP, targetMAC, targetIP, 42, arpTargetMAC)
 { }
Esempio n. 26
0
 public UdpClient(IPv4.Address dest, Int32 destPort)
     : this(0)
 {
     this.destination = dest;
     this.destinationPort = destPort;
 }
Esempio n. 27
0
 public void Connect(IPv4.Address dest, Int32 destPort)
 {
     this.destination = dest;
     this.destinationPort = destPort;
 }