public ArpResolver(EthernetInterface ethernetInterface)
        {
            // save a reference to our ethernet interface; we will use this to send ARP fraems
            _ethernetInterface = ethernetInterface;

            // create our ARP cache
            _arpCache = new System.Collections.Hashtable();

            /* write fixed ARP frame parameters (which do not change) to our ARP frame buffer */
            /* Hardware Type: 0x0001 (Ethernet) */
            _arpFrameBuffer[0] = (byte)((HARDWARE_TYPE_ETHERNET >> 8) & 0xFF);
            _arpFrameBuffer[1] = (byte)(HARDWARE_TYPE_ETHERNET & 0xFF);
            /* Protocol Type: 0x0800 (IPv4) */
            _arpFrameBuffer[2] = (byte)((PROTOCOL_TYPE_IPV4 >> 8) & 0xFF);
            _arpFrameBuffer[3] = (byte)(PROTOCOL_TYPE_IPV4 & 0xFF);
            /* Hardware Address Size: 6 bytes */
            _arpFrameBuffer[4] = HARDWARE_ADDRESS_SIZE;
            /* Protocol Address Size: 4 bytes */
            _arpFrameBuffer[5] = PROTOCOL_ADDRESS_SIZE;

            /* fixed values for index and count (passed to EthernetInterface.Send(...) with _arpFrameBuffer */
            _indexArray[0] = 0;
            _countArray[0] = ARP_FRAME_BUFFER_LENGTH;

            // start our "send ARP replies" thread
            _sendArpGenericInBackgroundQueue = new System.Collections.Queue();
            _sendArpGenericInBackgroundThread = new Thread(SendArpGenericThread);
            _sendArpGenericInBackgroundThread.Start();

            // enable our "cleanup ARP cache" timer (fired every 60 seconds)
            _cleanupArpCacheTimer = new Timer(CleanupArpCache, null, 60000, 60000); 

            // wire up the incoming ARP frame handler
            _ethernetInterface.ARPFrameReceived += _ethernetInterface_ARPFrameReceived;
        }
        public ArpResolver(EthernetInterface ethernetInterface)
        {
            // save a reference to our ethernet interface; we will use this to send ARP fraems
            _ethernetInterface = ethernetInterface;

            // create our ARP cache
            _arpCache = new System.Collections.Hashtable();

            /* write fixed ARP frame parameters (which do not change) to our ARP frame buffer */
            /* Hardware Type: 0x0001 (Ethernet) */
            _arpFrameBuffer[0] = (byte)((HARDWARE_TYPE_ETHERNET >> 8) & 0xFF);
            _arpFrameBuffer[1] = (byte)(HARDWARE_TYPE_ETHERNET & 0xFF);
            /* Protocol Type: 0x0800 (IPv4) */
            _arpFrameBuffer[2] = (byte)((PROTOCOL_TYPE_IPV4 >> 8) & 0xFF);
            _arpFrameBuffer[3] = (byte)(PROTOCOL_TYPE_IPV4 & 0xFF);
            /* Hardware Address Size: 6 bytes */
            _arpFrameBuffer[4] = HARDWARE_ADDRESS_SIZE;
            /* Protocol Address Size: 4 bytes */
            _arpFrameBuffer[5] = PROTOCOL_ADDRESS_SIZE;

            /* fixed values for index and count (passed to EthernetInterface.Send(...) with _arpFrameBuffer */
            _indexArray[0] = 0;
            _countArray[0] = ARP_FRAME_BUFFER_LENGTH;

            // start our "send ARP replies" thread
            _sendArpGenericInBackgroundQueue  = new System.Collections.Queue();
            _sendArpGenericInBackgroundThread = new Thread(SendArpGenericThread);
            _sendArpGenericInBackgroundThread.Start();

            // enable our "cleanup ARP cache" timer (fired every 60 seconds)
            _cleanupArpCacheTimer = new Timer(CleanupArpCache, null, 60000, 60000);

            // wire up the incoming ARP frame handler
            _ethernetInterface.ARPFrameReceived += _ethernetInterface_ARPFrameReceived;
        }
        public void Dispose()
        {
            if (_isDisposed) return;

            _isDisposed = true;

            // shut down our loopback thread
            if (_loopbackBufferFilledEvent != null)
            {
                _loopbackBufferFilledEvent.Set();
                _loopbackBufferFilledEvent = null;
            }

            _ethernetInterface = null;
            _ipv4HeaderBuffer = null;
            _ipv4HeaderBufferLockObject = null;

            _dhcpv4Client.Dispose();
            _dhcpv4Client = null;

            _icmpv4Handler.Dispose();
            _icmpv4Handler = null;

            _dnsResolver.Dispose();
            _dnsResolver = null;

            _tcpHandler.Dispose();
            _tcpHandler = null;

            _bufferArray = null;
            _indexArray = null;
            _countArray = null;
        }
Example #4
0
        static internal void Initialize()
        {
            lock (_initializeMethodSyncObject)
            {
                if (_isInitialized)
                {
                    return;
                }

#if CC3100
                Type socketNativeType = Type.GetType("Netduino.IP.LinkLayers.CC3100SocketNative, Netduino.IP.LinkLayers.CC3100");
                System.Reflection.MethodInfo initializeMethod = socketNativeType.GetMethod("Initialize", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
                initializeMethod.Invoke(null, new object[] { });
#else
                // create the appropriate link layer object
                Type linkLayerType = Type.GetType(LINK_LAYER_BASE_TYPENAME);
                System.Reflection.ConstructorInfo linkLayerConstructor = linkLayerType.GetConstructor(new Type[] { typeof(SPI.SPI_module), typeof(Cpu.Pin), typeof(Cpu.Pin), typeof(Cpu.Pin), typeof(Cpu.Pin) });
#if NETDUINOIP_AX88796C
                ILinkLayer linkLayer = (ILinkLayer)linkLayerConstructor.Invoke(new object[] { SPI.SPI_module.SPI2 /* spiBusID */, (Cpu.Pin) 0x28 /* csPinID */, (Cpu.Pin) 0x04 /* intPinID */, (Cpu.Pin) 0x12 /* resetPinID */, (Cpu.Pin) 0x44 /* wakeupPinID */ });
#elif NETDUINOIP_ENC28J60
                ILinkLayer linkLayer = (ILinkLayer)linkLayerConstructor.Invoke(new object[] { SPI.SPI_module.SPI2 /* spiBusID */, (Cpu.Pin) 0x28 /* csPinID */, (Cpu.Pin) 0x04 /* intPinID */, (Cpu.Pin) 0x32 /* resetPinID */, Cpu.Pin.GPIO_NONE /* wakeupPinID */ });
#elif NETDUINOIP_CC3100
#endif
                // retrieve MAC address from the config sector
                object networkInterface = Netduino.IP.Interop.NetworkInterface.GetNetworkInterface(0);
                byte[] physicalAddress  = (byte[])networkInterface.GetType().GetMethod("get_PhysicalAddress").Invoke(networkInterface, new object[] { });
                linkLayer.SetMacAddress(physicalAddress);

                // create EthernetInterface instance
                _ethernetInterface = new Netduino.IP.EthernetInterface(linkLayer);
                // create our IPv4Layer instance
                _ipv4Layer = new IPv4Layer(_ethernetInterface);

                // start up our link layer
                linkLayer.Start();
#endif

                _isInitialized = true;
            }
        }
        public void Dispose()
        {
            if (_isDisposed)
            {
                return;
            }

            _isDisposed = true;

            // shut down our ARP response thread
            if (_sendArpGenericInBackgroundEvent != null)
            {
                _sendArpGenericInBackgroundEvent.Set();
                _sendArpGenericInBackgroundEvent = null;
            }

            // timeout any current ARP requests
            if (_currentArpRequestAnsweredEvent != null)
            {
                _currentArpRequestAnsweredEvent.Set();
                _currentArpRequestAnsweredEvent = null;
            }

            if (_arpCache != null)
            {
                _arpCache.Clear();
            }
            _cleanupArpCacheTimer.Dispose();

            _ethernetInterface  = null;
            _arpFrameBuffer     = null;
            _arpFrameBufferLock = null;

            _bufferArray = null;
            _indexArray  = null;
            _countArray  = null;
        }
        static internal void Initialize()
        {
            lock (_initializeMethodSyncObject)
            {
                if (_isInitialized) return;

#if CC3100
                Type socketNativeType = Type.GetType("Netduino.IP.LinkLayers.CC3100SocketNative, Netduino.IP.LinkLayers.CC3100");
                System.Reflection.MethodInfo initializeMethod = socketNativeType.GetMethod("Initialize", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
                initializeMethod.Invoke(null, new object[] { });
#else
                // create the appropriate link layer object
                Type linkLayerType = Type.GetType(LINK_LAYER_BASE_TYPENAME);
                System.Reflection.ConstructorInfo linkLayerConstructor = linkLayerType.GetConstructor(new Type[] { typeof(SPI.SPI_module), typeof(Cpu.Pin), typeof(Cpu.Pin), typeof(Cpu.Pin), typeof(Cpu.Pin) });
#if NETDUINOIP_AX88796C
                ILinkLayer linkLayer = (ILinkLayer)linkLayerConstructor.Invoke(new object[] { SPI.SPI_module.SPI2  /* spiBusID */, (Cpu.Pin)0x28 /* csPinID */, (Cpu.Pin)0x04 /* intPinID */, (Cpu.Pin)0x12 /* resetPinID */, (Cpu.Pin)0x44 /* wakeupPinID */ });
#elif NETDUINOIP_ENC28J60
                ILinkLayer linkLayer = (ILinkLayer)linkLayerConstructor.Invoke(new object[] { SPI.SPI_module.SPI2  /* spiBusID */, (Cpu.Pin)0x28 /* csPinID */, (Cpu.Pin)0x04 /* intPinID */, (Cpu.Pin)0x32 /* resetPinID */, Cpu.Pin.GPIO_NONE /* wakeupPinID */ });
#elif NETDUINOIP_CC3100
#endif
                // retrieve MAC address from the config sector
                object networkInterface = Netduino.IP.Interop.NetworkInterface.GetNetworkInterface(0);
                byte[] physicalAddress = (byte[])networkInterface.GetType().GetMethod("get_PhysicalAddress").Invoke(networkInterface, new object[] { });
                linkLayer.SetMacAddress(physicalAddress);

                // create EthernetInterface instance
                _ethernetInterface = new Netduino.IP.EthernetInterface(linkLayer);
                // create our IPv4Layer instance
                _ipv4Layer = new IPv4Layer(_ethernetInterface);

                // start up our link layer
                linkLayer.Start();
#endif

                _isInitialized = true;
            }
        }
Example #7
0
        public void Dispose()
        {
            if (_isDisposed) return;

            _isDisposed = true;

            // shut down our loopback thread
            if (_loopbackBufferFilledEvent != null)
            {
                _loopbackBufferFilledEvent.Set();
                _loopbackBufferFilledEvent = null;
            }

            _ethernetInterface = null;
            _ipv4HeaderBuffer = null;
            _ipv4HeaderBufferLockObject = null;

            _dhcpv4Client.Dispose();
            _dhcpv4Client = null;

            _icmpv4Handler.Dispose();
            _icmpv4Handler = null;

            _dnsResolver.Dispose();
            _dnsResolver = null;

            _tcpHandler.Dispose();
            _tcpHandler = null;

            _bufferArray = null;
            _indexArray = null;
            _countArray = null;
        }
Example #8
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());
        }
        public void Dispose()
        {
            if (_isDisposed) return;

            _isDisposed = true;

            // shut down our ARP response thread
            if (_sendArpGenericInBackgroundEvent != null)
            {
                _sendArpGenericInBackgroundEvent.Set();
                _sendArpGenericInBackgroundEvent = null;
            }

            // timeout any current ARP requests
            if (_currentArpRequestAnsweredEvent != null)
            {
                _currentArpRequestAnsweredEvent.Set();
                _currentArpRequestAnsweredEvent = null;
            }

            if (_arpCache != null)
                _arpCache.Clear();
            _cleanupArpCacheTimer.Dispose();

            _ethernetInterface = null;
            _arpFrameBuffer = null;
            _arpFrameBufferLock = null;

            _bufferArray = null;
            _indexArray = null;
            _countArray = null;
        }
Example #10
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());
        }