Exemple #1
0
        void _dhcpv4Client_IpConfigChanged(object sender, uint ipAddress, uint gatewayAddress, uint subnetMask)
        {
            _ipv4configIPAddress      = ipAddress;
            _ipv4configSubnetMask     = subnetMask;
            _ipv4configGatewayAddress = gatewayAddress;

            _arpResolver.SetIpv4Address(_ipv4configIPAddress);

            /*** TODO: we should re-send our gratuitious (or probe) ARP every time that our IP address changes (via DHCP or otherwise), and also every time our network link goes up ***/
            /*** TODO: if our IP address changes, should we update the source IP address on all of our sockets?  Should we close any active connection-based sockets?  ***/

            Type networkChangeListenerType = Type.GetType("Microsoft.SPOT.Net.NetworkInformation.NetworkChange+NetworkChangeListener, Microsoft.SPOT.Net");

            if (networkChangeListenerType != null)
            {
                // create instance of NetworkChangeListener
                System.Reflection.ConstructorInfo networkChangeListenerConstructor = networkChangeListenerType.GetConstructor(new Type[] { });
                object networkChangeListener = networkChangeListenerConstructor.Invoke(new object[] { });

                // now call the ProcessEvent function to create a NetworkEvent class.
                System.Reflection.MethodInfo processEventMethodType = networkChangeListenerType.GetMethod("ProcessEvent");
                object networkEvent = processEventMethodType.Invoke(networkChangeListener, new object[] { (UInt32)(((UInt32)2 /* AddressChanged*/)), (UInt32)0, DateTime.Now }); /* TODO: should this be DateTime.Now or DateTime.UtcNow? */

                // and finally call the static NetworkChange.OnNetworkChangeCallback function to raise the event.
                Type networkChangeType = Type.GetType("Microsoft.SPOT.Net.NetworkInformation.NetworkChange, Microsoft.SPOT.Net");
                if (networkChangeType != null)
                {
                    System.Reflection.MethodInfo onNetworkChangeCallbackMethod = networkChangeType.GetMethod("OnNetworkChangeCallback", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
                    onNetworkChangeCallbackMethod.Invoke(networkChangeType, new object[] { networkEvent });
                }
            }
        }
        public IPv4Layer(EthernetInterface ethernetInterface)
        {
            // save a reference to our Ethernet; we'll use this to push IPv4 frames onto the Ethernet interface
            _ethernetInterface = ethernetInterface;

            // create and configure my ARP resolver; the ARP resolver will automatically wire itself up to receiving incoming ARP frames
            _arpResolver = new ArpResolver(ethernetInterface);

            // retrieve our IP address configuration from the config sector and configure ARP
            object networkInterface = Netduino.IP.Interop.NetworkInterface.GetNetworkInterface(0);
            bool dhcpIpConfigEnabled = (bool)networkInterface.GetType().GetMethod("get_IsDhcpEnabled").Invoke(networkInterface, new object[] { });
            /* NOTE: IsDynamicDnsEnabled is improperly implemented in NETMF; it should implement dynamic DNS--but instead it returns whether or not DNS addresses are assigned through DHCP */
            bool dhcpDnsConfigEnabled = (bool)networkInterface.GetType().GetMethod("get_IsDynamicDnsEnabled").Invoke(networkInterface, new object[] { });

            // randomize our ephemeral port assignment counter (so that we don't use the same port #s repeatedly after reboots)
            _nextEphemeralPort = (UInt16)(FIRST_EPHEMERAL_PORT + ((new Random()).NextDouble() * (UInt16.MaxValue - FIRST_EPHEMERAL_PORT - 1)));

            // configure our ARP resolver's default IP address settings
            if (dhcpIpConfigEnabled)
            {
                // in case of DHCP, temporarily set our IP address to IP_ADDRESS_ANY (0.0.0.0)
                _arpResolver.SetIpv4Address(0);
            }
            else
            {
                _ipv4configIPAddress = ConvertIPAddressStringToUInt32BE((string)networkInterface.GetType().GetMethod("get_IPAddress").Invoke(networkInterface, new object[] { }));
                _ipv4configSubnetMask = ConvertIPAddressStringToUInt32BE((string)networkInterface.GetType().GetMethod("get_SubnetMask").Invoke(networkInterface, new object[] { }));
                _ipv4configGatewayAddress = ConvertIPAddressStringToUInt32BE((string)networkInterface.GetType().GetMethod("get_GatewayAddress").Invoke(networkInterface, new object[] { }));
                _arpResolver.SetIpv4Address(_ipv4configIPAddress);
            }
            // retrieve our DnsServer IP address configuration
            if (!dhcpDnsConfigEnabled)
            {
                string[] dnsAddressesString = (string[])networkInterface.GetType().GetMethod("get_DnsAddresses").Invoke(networkInterface, new object[] { });
                _ipv4configDnsServerAddresses = new UInt32[dnsAddressesString.Length];
                for (int iDnsAddress = 0; iDnsAddress < _ipv4configDnsServerAddresses.Length; iDnsAddress++)
                {
                    _ipv4configDnsServerAddresses[iDnsAddress] = ConvertIPAddressStringToUInt32BE(dnsAddressesString[iDnsAddress]);
                }
            }

            // initialize our buffers
            for (int i = 0; i < _receivedPacketBuffers.Length; i++ )
            {
                _receivedPacketBuffers[i] = new ReceivedPacketBuffer();
                InitializeReceivedPacketBuffer(_receivedPacketBuffers[i]);
            }

            // wire up our IPv4PacketReceived handler
            _ethernetInterface.IPv4PacketReceived += _ethernetInterface_IPv4PacketReceived;
            // wire up our LinkStateChanged event handler
            _ethernetInterface.LinkStateChanged += _ethernetInterface_LinkStateChanged;

            // start our "loopback thread"
            _loopbackThread = new Thread(LoopbackInBackgroundThread);
            _loopbackThread.Start();

            // create our ICMPv4 handler instance
            _icmpv4Handler = new ICMPv4Handler(this);

            // create our DNS resolver instance
            _dnsResolver = new DnsResolver(this);

            // create our DHCP client instance
            _dhcpv4Client = new DHCPv4Client(this);
            _dhcpv4Client.IpConfigChanged += _dhcpv4Client_IpConfigChanged;
            _dhcpv4Client.DnsConfigChanged += _dhcpv4Client_DnsConfigChanged;

            // if we are configured to use DHCP, then create our DHCPv4Client instance now; its state machine will take care of ip configuration from there
            if (dhcpIpConfigEnabled || dhcpDnsConfigEnabled)
            {
                _dhcpv4Client.IsDhcpIpConfigEnabled = dhcpIpConfigEnabled;
                _dhcpv4Client.IsDhcpDnsConfigEnabled = dhcpDnsConfigEnabled;
            }

            // create our TCP handler instance
            _tcpHandler = new TcpHandler(this);

            // manually fire our LinkStateChanged event to set the initial state of our link.
            _ethernetInterface_LinkStateChanged(_ethernetInterface, _ethernetInterface.GetLinkState());
        }
Exemple #3
0
        public IPv4Layer(EthernetInterface ethernetInterface)
        {
            // save a reference to our Ethernet; we'll use this to push IPv4 frames onto the Ethernet interface
            _ethernetInterface = ethernetInterface;

            // create and configure my ARP resolver; the ARP resolver will automatically wire itself up to receiving incoming ARP frames
            _arpResolver = new ArpResolver(ethernetInterface);

            // retrieve our IP address configuration from the config sector and configure ARP
            object networkInterface    = Netduino.IP.Interop.NetworkInterface.GetNetworkInterface(0);
            bool   dhcpIpConfigEnabled = (bool)networkInterface.GetType().GetMethod("get_IsDhcpEnabled").Invoke(networkInterface, new object[] { });
            /* NOTE: IsDynamicDnsEnabled is improperly implemented in NETMF; it should implement dynamic DNS--but instead it returns whether or not DNS addresses are assigned through DHCP */
            bool dhcpDnsConfigEnabled = (bool)networkInterface.GetType().GetMethod("get_IsDynamicDnsEnabled").Invoke(networkInterface, new object[] { });

            // randomize our ephemeral port assignment counter (so that we don't use the same port #s repeatedly after reboots)
            _nextEphemeralPort = (UInt16)(FIRST_EPHEMERAL_PORT + ((new Random()).NextDouble() * (UInt16.MaxValue - FIRST_EPHEMERAL_PORT - 1)));

            // configure our ARP resolver's default IP address settings
            if (dhcpIpConfigEnabled)
            {
                // in case of DHCP, temporarily set our IP address to IP_ADDRESS_ANY (0.0.0.0)
                _arpResolver.SetIpv4Address(0);
            }
            else
            {
                _ipv4configIPAddress      = ConvertIPAddressStringToUInt32BE((string)networkInterface.GetType().GetMethod("get_IPAddress").Invoke(networkInterface, new object[] { }));
                _ipv4configSubnetMask     = ConvertIPAddressStringToUInt32BE((string)networkInterface.GetType().GetMethod("get_SubnetMask").Invoke(networkInterface, new object[] { }));
                _ipv4configGatewayAddress = ConvertIPAddressStringToUInt32BE((string)networkInterface.GetType().GetMethod("get_GatewayAddress").Invoke(networkInterface, new object[] { }));
                _arpResolver.SetIpv4Address(_ipv4configIPAddress);
            }
            // retrieve our DnsServer IP address configuration
            if (!dhcpDnsConfigEnabled)
            {
                string[] dnsAddressesString = (string[])networkInterface.GetType().GetMethod("get_DnsAddresses").Invoke(networkInterface, new object[] { });
                _ipv4configDnsServerAddresses = new UInt32[dnsAddressesString.Length];
                for (int iDnsAddress = 0; iDnsAddress < _ipv4configDnsServerAddresses.Length; iDnsAddress++)
                {
                    _ipv4configDnsServerAddresses[iDnsAddress] = ConvertIPAddressStringToUInt32BE(dnsAddressesString[iDnsAddress]);
                }
            }

            // initialize our buffers
            for (int i = 0; i < _receivedPacketBuffers.Length; i++)
            {
                _receivedPacketBuffers[i] = new ReceivedPacketBuffer();
                InitializeReceivedPacketBuffer(_receivedPacketBuffers[i]);
            }

            // wire up our IPv4PacketReceived handler
            _ethernetInterface.IPv4PacketReceived += _ethernetInterface_IPv4PacketReceived;
            // wire up our LinkStateChanged event handler
            _ethernetInterface.LinkStateChanged += _ethernetInterface_LinkStateChanged;

            // start our "loopback thread"
            _loopbackThread = new Thread(LoopbackInBackgroundThread);
            _loopbackThread.Start();

            // create our ICMPv4 handler instance
            _icmpv4Handler = new ICMPv4Handler(this);

            // create our DNS resolver instance
            _dnsResolver = new DnsResolver(this);

            // create our DHCP client instance
            _dhcpv4Client = new DHCPv4Client(this);
            _dhcpv4Client.IpConfigChanged  += _dhcpv4Client_IpConfigChanged;
            _dhcpv4Client.DnsConfigChanged += _dhcpv4Client_DnsConfigChanged;

            // if we are configured to use DHCP, then create our DHCPv4Client instance now; its state machine will take care of ip configuration from there
            if (dhcpIpConfigEnabled || dhcpDnsConfigEnabled)
            {
                _dhcpv4Client.IsDhcpIpConfigEnabled  = dhcpIpConfigEnabled;
                _dhcpv4Client.IsDhcpDnsConfigEnabled = dhcpDnsConfigEnabled;
            }

            // create our TCP handler instance
            _tcpHandler = new TcpHandler(this);

            // manually fire our LinkStateChanged event to set the initial state of our link.
            _ethernetInterface_LinkStateChanged(_ethernetInterface, _ethernetInterface.GetLinkState());
        }