Example #1
0
        private static void SendARP_Reply(byte[] destinationMac, byte[] destinationIp)
        {
            Scratch.Overwrite(0, destinationMac);
            Scratch.Overwrite(6, EthClick._macAddress);
            Scratch.Overwrite(21, Reply);
            Scratch.Overwrite(22, EthClick._macAddress);
            Scratch.Overwrite(28, EthClick._ip ?? new byte[] { 0x00, 0x00, 0x00, 0x00 });
            Scratch.Overwrite(32, destinationMac);
            Scratch.Overwrite(38, destinationIp);

            EthClick.SendFrame(Scratch);
        }
Example #2
0
        internal static void SendARP_Gratuitus()
        {
            Scratch.Overwrite(0, EthClick.BroadcastMac);
            Scratch.Overwrite(6, EthClick._macAddress);
            Scratch.Overwrite(21, Request);
            Scratch.Overwrite(22, EthClick._macAddress);
            Scratch.Overwrite(28, EthClick._ip ?? new byte[] { 0x00, 0x00, 0x00, 0x00 });
            Scratch.Overwrite(32, EthClick.BlankMac);
            Scratch.Overwrite(38, EthClick._ip ?? new byte[] { 0x00, 0x00, 0x00, 0x00 });

            EthClick.SendFrame(Scratch);
        }
Example #3
0
        private static void SendNetbiosReply(byte[] nbName, byte[] destinationMac, byte[] destinationIp, byte[] transactionId)
        {
            Scratch.Overwrite(0, destinationMac);
            Scratch.Overwrite(6, EthClick._macAddress);
            Scratch.Overwrite(26, EthClick._ip);
            Scratch.Overwrite(30, destinationIp);
            Scratch.Overwrite(42, transactionId);
            Scratch.Overwrite(55, nbName);
            Scratch.Overwrite(100, EthClick._ip);
            Scratch.Overwrite(24, new byte[] { 0x00, 0x00 });
            Scratch.Overwrite(24, Scratch.InternetChecksum(20, 14));

            EthClick.SendFrame(Scratch);
        }
Example #4
0
        /// <summary>
        /// Sends a UDP message.
        /// </summary>
        /// <param name="message">The byte encoded message to send.</param>
        /// <param name="destinationIp">The byte encoded Destination IPAddress.</param>
        /// <param name="destinationPort">The destination Port.</param>
        /// <param name="sourcePort">The source Port.</param>
        /// <example>Example usage:
        /// <code language = "C#">
        /// namespace NetworkingExample
        /// {
        ///     public class UdpProgram
        ///     {
        ///         private static EthClick _eth;
        ///
        ///         public static void Main()
        ///         {
        ///             /* Use this for Static IP and No DHCP */
        ///             //_eth.Name = "Quail";
        ///             //_eth.MacAddress = _eth.GenerateUniqueMacAddress("Quail");
        ///             //_eth.IPAddress = "192.168.1.95".ToBytes();
        ///             //_eth.DefaultGateway = "192.168.1.1".ToBytes();
        ///             //_eth.PreferredDomainNameServer = "8.8.8.8".ToBytes(); // Google DNS Servers
        ///             //_eth.AlternateDomainNameServer = "8.8.8.4".ToBytes(); // Google DNS Servers
        ///             //_eth.DHCPDisabled = true;
        ///
        ///             _eth = new EthClick(Hardware.SocketOne);
        ///
        ///             _eth.Start(_eth.GenerateUniqueMacAddress("MikroBusNet"), "MikroBusNet");
        ///
        ///             while (true)
        ///             {
        ///                 if (_eth.ConnectedToInternet)
        ///                 {
        ///                     Debug.Print("Connected to Internet");
        ///                     break;
        ///                 }
        ///                 Debug.Print("Waiting on Internet connection");
        ///             }
        ///
        ///             // Listen for UDP messages sent to activated ports
        ///             EthClick.OnUdpPacketReceived += Adapter_OnUdpPacketReceived;
        ///
        ///             // Activate the NTP (date/time) port 123
        ///             _eth.ListenToPort(123);
        ///
        ///             // Create a NTP (date/time) Request Message
        ///             var msg = new byte[48];
        ///             msg[0] = 0x1B;
        ///
        ///             var ipAddress = "165.193.126.229".ToBytes(); // IPAddress for NIST Time Server in Weehawken, NJ USA see http://tf.nist.gov/tf-cgi/servers.cgi
        ///
        ///             // Let's get the UTC time from a time server using a UDP Message
        ///             UDP.SendUdpMessage(msg, ipAddress, 123, 123);
        ///
        ///             Thread.Sleep(Timeout.Infinite);
        ///         }
        ///
        ///         private static void Adapter_OnUdpPacketReceived(Packet packet)
        ///         {
        ///             if (packet.Socket.RemotePort == 123)
        ///             {
        ///                 var transitTime = Utility.ExtractRangeFromArray(packet.Content, 40, 8);
        ///                 Debug.Print("Current UTC Date/Time is " + transitTime.ToDateTime());
        ///             }
        ///         }
        ///     }
        ///
        ///     public static class Extensions
        ///     {
        ///             /// <summary>
        ///             /// Convert an 8-byte array from NTP format to .NET DateTime.
        ///             /// </summary>
        ///             <![CDATA[///>]]>param name="ntpTime">NTP format 8-byte array containing date and time<![CDATA[<]]>/param>
        ///             /// <returns>A Standard .NET DateTime</returns>
        ///             public static DateTime ToDateTime(this byte[] ntpTime)
        ///             {
        ///                 Microsoft.SPOT.Debug.Assert(ntpTime.Length == 8, "The passed array is too short to be a valid NTP DateTime.");
        ///
        ///                 ulong intpart = 0;
        ///                 ulong fractpart = 0;
        ///
        ///                 for (int i = 0; i <![CDATA[<]]>= 3; i++)
        ///                     intpart = (intpart <![CDATA[<<]]> 8) | ntpTime[i];
        ///
        ///                 for (int i = 4; i <![CDATA[<]]>= 7; i++)
        ///                     fractpart = (fractpart <![CDATA[<<]]> 8) | ntpTime[i];
        ///
        ///                 ulong milliseconds = (intpart*1000 + (fractpart*1000)/0x100000000L);
        ///
        ///                 var timeSince1900 = TimeSpan.FromTicks((long) milliseconds*TimeSpan.TicksPerMillisecond);
        ///                 return new DateTime(1900, 1, 1).Add(timeSince1900);
        ///             }
        ///     }
        /// }
        /// </code>
        /// <code language = "VB">
        /// ' None provided as VS 2012 does not support bit-shifting with VB.Net which is necessary for this example.
        /// </code>
        /// </example>
        ///
        public static void SendUdpMessage(byte[] message, byte[] destinationIp, ushort destinationPort, ushort sourcePort)
        {
            Scratch.Overwrite(0, EthClick._gatewayMac);
            Scratch.Overwrite(6, EthClick._macAddress);
            Scratch.Overwrite(30, destinationIp);
            Scratch.Overwrite(26, EthClick._ip);
            Scratch.Overwrite(34, sourcePort.ToBytes());
            Scratch.Overwrite(36, destinationPort.ToBytes());
            Scratch.Overwrite(38, ((ushort)(message.Length + 8)).ToBytes());
            Scratch.Overwrite(16, ((ushort)((Scratch.Length + message.Length) - 14)).ToBytes());
            Scratch.Overwrite(24, new byte[] { 0x00, 0x00 });
            Scratch.Overwrite(24, Scratch.InternetChecksum(20, 14));
            Scratch.Overwrite(40, new byte[] { 0x00, 0x00 });

            EthClick.SendFrame(Utility.CombineArrays(Scratch, message));
        }
Example #5
0
        /// <summary>
        /// Send a PING Request to the specified IP Address.
        /// </summary>
        /// <param name="ipAddress">The byte encoded IPAddress.</param>
        public static void SendPingRequest(byte[] ipAddress)
        {
            if (EthClick._gatewayMac == null || EthClick._ip == null)
            {
                return;
            }

            Scratch.Overwrite(0, EthClick._gatewayMac);
            Scratch.Overwrite(6, EthClick._macAddress);
            Scratch.Overwrite(26, EthClick._ip);
            Scratch.Overwrite(30, EthClick._ip);
            Scratch.Overwrite(34, Request);
            Scratch.Overwrite(24, new byte[] { 0x00, 0x00 });             // clear header checksum, so that calculation excludes the checksum itself
            Scratch.Overwrite(24, Scratch.InternetChecksum(20, 14));      // header checksum

            EthClick.SendFrame(Scratch);
        }
Example #6
0
        internal static void SendARP_Probe(byte[] ipAddressToQuery)
        {
            if (ipAddressToQuery == null)
            {
                return;
            }

            Scratch.Overwrite(0, EthClick.BroadcastMac);
            Scratch.Overwrite(6, EthClick._macAddress);
            Scratch.Overwrite(21, Request);
            Scratch.Overwrite(22, EthClick._macAddress);
            Scratch.Overwrite(28, EthClick._ip ?? new byte[] { 0x00, 0x00, 0x00, 0x00 });
            Scratch.Overwrite(32, EthClick.BroadcastMac);
            Scratch.Overwrite(38, ipAddressToQuery);

            EthClick.SendFrame(Scratch);
        }
Example #7
0
        private static void SendLLMNRNameReply(string name, byte[] tranId, byte[] destinationMac, byte[] destinationIp, byte[] destinationPort)
        {
            if (EthClick._ip == null || EthClick._name == string.Empty)
            {
                return;
            }

            lock (LockObj)
            {
                Prefix.Overwrite(0, destinationMac);
                Prefix.Overwrite(6, EthClick._macAddress);
                Prefix.Overwrite(26, EthClick._ip);
                Prefix.Overwrite(30, destinationIp);
                Prefix.Overwrite(36, destinationPort);
                Prefix.Overwrite(42, tranId);

                var    suffix   = new byte[name.Length * 2 + 22];
                byte[] byteName = Utility.CombineArrays(DNS.EncodeDnsName(name), new byte[] { 0x00, 0x01, 0x00, 0x01 });
                suffix.Overwrite(0, Utility.CombineArrays(byteName, byteName));
                suffix.Overwrite(suffix.Length - 7, new byte[] { 0x1e });
                suffix.Overwrite(suffix.Length - 5, new byte[] { 0x04 });
                suffix.Overwrite(suffix.Length - 4, EthClick._ip);

                byte[] result = Utility.CombineArrays(Prefix, suffix);

                result.Overwrite(16, ((ushort)(result.Length - 14)).ToBytes());
                result.Overwrite(38, ((ushort)(result.Length - 34)).ToBytes());
                result.Overwrite(24, new byte[] { 0x00, 0x00 });
                result.Overwrite(24, result.InternetChecksum(20, 14));
                result.Overwrite(40, new byte[] { 0x00, 0x00 });

                EthClick.SendFrame(result);

                                #if TINYCLR_TRACE
                if (EthClick._verboseDebugging)
                {
                    System.Diagnostics.Debug.WriteLine("LLMNR Response sent");
                }
                                #endif
            }
        }
Example #8
0
        internal void SendAsync(byte[] buffer, int offset, short size)
        {
            if (EthClick._gatewayMac == null || EthClick._ip == null || size <= 0 || IsClosing)
            {
                return;
            }

            if (!_isOpen)
            {
                Open();
            }

                        #if TINYCLR_TRACE
            if (EthClick._verboseDebugging)
            {
                System.Diagnostics.Debug.WriteLine("Sending TCP Data Segment");
            }
                        #endif

            _scratch.Overwrite(0 + 2, RemoteMac);
            _scratch.Overwrite(6 + 2, EthClick._macAddress);
            _scratch.Overwrite(26 + 2, EthClick._ip);
            _scratch.Overwrite(30 + 2, RemoteIp);
            _scratch.Overwrite(34 + 2, LocalPort.ToBytes());
            _scratch.Overwrite(36 + 2, RemotePort.ToBytes());
            _scratch.Overwrite(38 + 2, AckNumber.ToBytes());
            _scratch.Overwrite(42 + 2, SeqNumber.ToBytes());
            _scratch.Overwrite(48 + 2, new byte[] { 0x04, 0x00 });
            _scratch[47 + 2] = 0x10;
            _scratch.Overwrite(50 + 2, new byte[] { 0x00, 0x00 });
            _scratch.Overwrite(24 + 2, new byte[] { 0x00, 0x00 });

            buffer = Utility.CombineArrays(_scratch, 0, _scratch.Length, buffer, offset, size);
            buffer.Overwrite(16 + 2, ((ushort)((buffer.Length - 2) - 14)).ToBytes());
            buffer.Overwrite(50 + 2, buffer.InternetChecksum(20 + size, 34 + 2, EthClick._ip, RemoteIp));
            buffer.Overwrite(24 + 2, buffer.InternetChecksum(20, 14 + 2));

            AckNumber += (uint)size;
            EthClick.SendFrame(buffer, 2);
        }
Example #9
0
        internal static void SendMessage(byte[] packetType)
        {
            lock (MagicCookie)
            {
                Scratch.Overwrite(0, EthClick.BroadcastMac);
                Scratch.Overwrite(6, EthClick._macAddress);
                Scratch.Overwrite(30, EthClick.BroadcastIpAddress);
                Scratch.Overwrite(54, EthClick.BlankIpAddress);
                Scratch.Overwrite(70, EthClick._macAddress);
                Scratch.Overwrite(284, packetType);

                var options = new byte[13 + (EthClick._name == string.Empty ? 0 : EthClick._name.Length + 2)];
                options.Overwrite(0, EthClick._macAddress);

                _transactionId = _transactionId ?? Extensions.GetRandomBytes(4);

                if (packetType == Discover)
                {
                    _pendingIpAddress = null;
                    options.Overwrite(6, new byte[] { 0x33, 0x04, 0x00, 0x76, 0xa7, 0x00 });
                }
                else if (packetType == Request && _pendingIpAddress != null && EthClick._defaultGateway != null)
                {
                    options.Overwrite(6, new byte[] { 0x32, 0x04 });
                    options.Overwrite(8, _pendingIpAddress);
                    options.Overwrite(12, new byte[] { 0x36, 0x04 });
                    options.Overwrite(14, EthClick._defaultGateway);

                    if (EthClick._gatewayMac != null && EthClick._defaultGateway != null && EthClick._ip != null)
                    {
                        Scratch.Overwrite(54, EthClick._ip);
                    }
                }
                else
                {
                                        #if TINYCLR_TRACE
                    if (EthClick._verboseDebugging)
                    {
                        System.Diagnostics.Debug.WriteLine("Odd DHCP situation... should we be concerned?");
                    }
                                        #endif
                    return;
                }

                if (EthClick._name != string.Empty)
                {
                    options.Overwrite(options.Length - (EthClick._name.Length + 3), new byte[] { 0x0c });
                    options.Overwrite(options.Length - (EthClick._name.Length + 2), DNS.EncodeDnsName(EthClick._name));
                }

                options.Overwrite(options.Length - 1, new byte[] { 0xFF });

                Scratch.Overwrite(46, _transactionId);

                var result = Utility.CombineArrays(Scratch, options);

                result.Overwrite(16, ((ushort)(result.Length - 14)).ToBytes());
                result.Overwrite(38, ((ushort)(result.Length - 34)).ToBytes());
                result.Overwrite(24, new byte[] { 0x00, 0x00 });
                result.Overwrite(24, result.InternetChecksum(20, 14));
                result.Overwrite(40, new byte[] { 0x00, 0x00 });

                EthClick.SendFrame(result);
            }
        }
Example #10
0
        internal void SendAck(bool synchronize = false, bool finish = false, bool ack = true)
        {
            if (EthClick._gatewayMac == null || EthClick._ip == null)
            {
                return;
            }

            AckBase.Overwrite(0, RemoteMac);
            AckBase.Overwrite(6, EthClick._macAddress);
            AckBase.Overwrite(26, EthClick._ip);
            AckBase.Overwrite(30, RemoteIp);
            AckBase.Overwrite(34, LocalPort.ToBytes());
            AckBase.Overwrite(36, RemotePort.ToBytes());
            AckBase.Overwrite(38, AckNumber.ToBytes());
            AckBase.Overwrite(42, SeqNumber.ToBytes());
            AckBase.Overwrite(48, WindowSize.ToBytes());

            if (synchronize)
            {
                AckBase.Overwrite(54, new byte[] { 0x02, 0x04, 0x05, 0xb4 });
                AckBase.Overwrite(16, new byte[] { 0x00, 0x2c });
                AckBase.Overwrite(46, new byte[] { 0x60 });
            }
            else
            {
                AckBase.Overwrite(54, new byte[] { 0x00, 0x00, 0x00, 0x00 });
                AckBase.Overwrite(16, new byte[] { 0x00, 0x28 });
                AckBase.Overwrite(46, new byte[] { 0x50 });
            }

            if (synchronize && ack)
            {
                AckBase[47] = 0x12;
                                #if TINYCLR_TRACE
                if (EthClick._verboseDebugging)
                {
                    System.Diagnostics.Debug.WriteLine("Sending TCP SYN + ACK");
                }
                                #endif
            }
            else if (finish && ack)
            {
                AckBase[47] = 0x11;
                                #if TINYCLR_TRACE
                if (EthClick._verboseDebugging)
                {
                    System.Diagnostics.Debug.WriteLine("Sending TCP FIN + ACK");
                }
                                #endif
            }
            else if (synchronize)
            {
                AckBase[47] = 0x02;
                                #if TINYCLR_TRACE
                if (EthClick._verboseDebugging)
                {
                    System.Diagnostics.Debug.WriteLine("Sending TCP SYN");
                }
                                #endif
            }
            else if (finish)
            {
                AckBase[47] = 0x01;
                                #if TINYCLR_TRACE
                if (EthClick._verboseDebugging)
                {
                    System.Diagnostics.Debug.WriteLine("Sending TCP FIN");
                }
                                #endif
            }
            else
            {
                AckBase[47] = 0x10;
                                #if TINYCLR_TRACE
                if (EthClick._verboseDebugging)
                {
                    System.Diagnostics.Debug.WriteLine("Sending TCP ACK");
                }
                                #endif
            }

            AckBase.Overwrite(50, new byte[] { 0x00, 0x00 });
            AckBase.Overwrite(50, AckBase.InternetChecksum(synchronize ? 24 : 20, 34, EthClick._ip, RemoteIp));

            AckBase.Overwrite(24, new byte[] { 0x00, 0x00 });
            AckBase.Overwrite(24, AckBase.InternetChecksum(20, 14));

            EthClick.SendFrame(AckBase);
        }