Exemple #1
0
        // This service creates a full featured Dhcp server
        public static void Main()
        {
            // Initialize logging
            Logger.Initialize(new DebugLogger(), LoggerLevel.Debug);

            //  Create the Dhcp server
            DhcpService DhcpServer = new DhcpService();

            // set the device server name and Dhcp suffix offered to client
            DhcpServer.ServerName = "example";
            DhcpServer.DnsSuffix  = "iot.local";

            // Set a Dhcp pool for the clients to use
            DhcpServer.PoolRange("172.16.10.100", "172.16.10.250");
            DhcpServer.GatewayAddress = "172.16.10.1";
            DhcpServer.SubnetMask     = "255.255.255.0";

            // Set a Dhcp reservation that assigns as specific ip address to a client MAC address
            DhcpServer.PoolReservation("172.16.10.15", "000C29027338");

            // Add a NTP server option for time-a.nist.gov as client time source
            DhcpServer.AddOption(DhcpOption.NTPServer, IPAddress.Parse("129.6.15.28").GetAddressBytes());

            // Sets interface ip address
            DhcpServer.InterfaceAddress = IPAddress.GetDefaultLocalAddress();

            // Starts Dhcp service
            DhcpServer.Start();
        }
Exemple #2
0
        public void StartAll()
        {
            _service = this;

            // DHCP Service
            if (_dhcpEnabled == true)
            {
                _dhcpService.InterfaceAddress = _interfaceAddress;
                _dhcpService.ServerName       = _serverName;
                _dhcpService.DnsSuffix        = _dnsSuffix;
                _dhcpService.StorageRoot      = _storageRoot;

                if (_sntpEnabled == true)
                {
                    _dhcpService.RemoveOption(DhcpOption.NTPServer);
                    _dhcpService.AddOption(DhcpOption.NTPServer, _interfaceAddress.GetAddressBytes());
                }
                _dhcpService.Start();
            }

            // DNS Service
            if (_dnsEnabled == true)
            {
                _dnsService.InterfaceAddress = _interfaceAddress;
                _dnsService.ServerName       = _serverName;
                _dnsService.DnsSuffix        = _dnsSuffix;

                if (!StringUtility.IsNullOrEmpty(_serverName) || !StringUtility.IsNullOrEmpty(_dnsSuffix))
                {
                    Answer record = new Answer();
                    record.Domain = string.Concat(_serverName, ".", _dnsSuffix);
                    record.Class  = RecordClass.IN;
                    record.Type   = RecordType.A;
                    record.Ttl    = 60;
                    record.Record = new ARecord(_interfaceAddress.GetAddressBytes());

                    _service.DnsService.ZoneFile.Add(record);

                    Logger.WriteInfo(this, "Device registered with dns:  " + record.Domain);
                }

                _dnsService.Start();
            }

            // SNTP Service
            if (_sntpEnabled == true)
            {
                _sntpService.InterfaceAddress = _interfaceAddress;
                _sntpService.Start();
            }

            // HTTP Service
            if (_httpEnabled == true)
            {
                ModuleManager _moduleManager = new ModuleManager();

                // Add the router module as the fist module to pipeline
                _moduleManager.Add(new RouterModule());

                if (StorageRoot != null)
                {
                    // Create disk file service for the root storage
                    DiskFileService fileService = new DiskFileService("/", StorageRoot + @"\" + MicroServer.Net.Http.Constants.HTTP_WEB_ROOT_FOLDER + @"\");

                    // Add the file module to pipeline
                    _moduleManager.Add(new FileModule(fileService)
                    {
                        AllowListing = _allowListing
                    });
                }

                // Add the controller module to pipeline
                _moduleManager.Add(new ControllerModule());

                // Add the error module as the last module to pipeline
                _moduleManager.Add(new ErrorModule());

                //  Create the Http service
                _httpService = new HttpService(_moduleManager);

                _httpService.InterfaceAddress = _interfaceAddress;
                _httpService.Start();
            }

            Logger.WriteInfo(this, "Service Manager started all services");
        }