コード例 #1
0
        public HttpResponseMessage lease(String scope)
        {
            // Connect to DHCP Server
            var            dhcpServer = DhcpServer.Connect(dhcp_host);
            Boolean        bFound     = false;
            ScopeLeaseList mylist     = new ScopeLeaseList();

            // Browsing through scopes
            foreach (var searchscope in dhcpServer.Scopes)
            {
                if (searchscope.Address.ToString().Equals(scope))
                {
                    // Found the scope given in parameter
                    bFound = true;
                    // MS DHCP return AddressState as full byte (state, Name protection and DNS informations)
                    // We just want the first 2 bits : state so logical and with 3 (0000011)
                    byte allactive = 3;
                    // Get active DHCP client leases
                    var activeClients = searchscope.Clients
                                        .Where(c => (Convert.ToByte(c.AddressState) & allactive) == 1)
                                        .Where(c => c.Type == DhcpServerClientTypes.DHCP);

                    // Browsing through leases
                    foreach (var client in activeClients)
                    {
                        // Retrieving client name
                        String myname = "";
                        if (client.Name == null)
                        {
                            // If no name, send something anyway
                            myname = "EMPTYNAME";
                        }
                        else
                        {
                            myname = client.Name;
                        }
                        // Retrieving client hardware address
                        String myhw = client.HardwareAddress.ToString();

                        // Applying Adrezo formatting
                        if (adrezo_format)
                        {
                            // Keeping only the host part in name
                            int firstpoint = myname.IndexOf('.');
                            if (firstpoint > 0)
                            {
                                myname = myname.Substring(0, firstpoint);
                            }
                            // Limit client name to 20 chars
                            if (myname.Length > 20)
                            {
                                myname = myname.Substring(0, 20);
                            }
                            // Removing : from mac address string
                            myhw = myhw.Replace(":", "");
                            //Limit mac to 12 chars
                            if (myhw.Length > 12)
                            {
                                myhw = myhw.Substring(0, 12);
                            }
                        }

                        // Add this lease with all attributes ip/mac/expiration date/name
                        mylist.leases.Add(new Lease(client.IpAddress.ToString(), myhw, client.LeaseExpires.ToString("yyyy-MM-dd HH:mm:ss"), myname));
                    }
                }
            }

            if (bFound)
            {
                // Returning Json string from mylist omiting null values
                return(new HttpResponseMessage()
                {
                    Content = new StringContent(
                        JsonConvert.SerializeObject(mylist, new JsonSerializerSettings {
                        NullValueHandling = NullValueHandling.Ignore
                    }),
                        Encoding.UTF8,
                        "application/json"
                        )
                });
            }
            else
            {
                // Scope given in parameter was not found
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Scope not found"));
            }
        }
コード例 #2
0
        public HttpResponseMessage reserve(String scope)
        {
            // Connect to DHCP Server
            var            dhcpServer = DhcpServer.Connect(dhcp_host);
            Boolean        bFound     = false;
            ScopeLeaseList mylist     = new ScopeLeaseList();

            // Browsing through scopes
            foreach (var searchscope in dhcpServer.Scopes)
            {
                if (searchscope.Address.ToString().Equals(scope))
                {
                    // Found the scope given in parameter
                    bFound = true;
                    // Browsing through reservations for this scope
                    foreach (var myresa in searchscope.Reservations)
                    {
                        // Retrieving client name
                        String myname = "";
                        if (myresa.Client.Name == null)
                        {
                            // If no name, send something anyway
                            myname = "EMPTYNAME";
                        }
                        else
                        {
                            myname = myresa.Client.Name;
                        }
                        // Retrieving client hardware address
                        String myhw = myresa.HardwareAddress.ToString();

                        // Applying Adrezo formatting
                        if (adrezo_format)
                        {
                            // Keeping only the host part in name
                            int firstpoint = myname.IndexOf('.');
                            if (firstpoint > 0)
                            {
                                myname = myname.Substring(0, firstpoint);
                            }
                            // Limit client name to 20 chars
                            if (myname.Length > 20)
                            {
                                myname = myname.Substring(0, 20);
                            }
                            // Removing : from mac address string
                            myhw = myhw.Replace(":", "");
                            // Limit mac to 12 chars
                            if (myhw.Length > 12)
                            {
                                myhw = myhw.Substring(0, 12);
                            }
                        }

                        // Reserve Mac Address doesn't have to be removed
                        // Add this reservation with no expiration date (null), just ip/mac/name
                        mylist.leases.Add(new Lease(myresa.IpAddress.ToString(), myhw, null, myname));
                    }
                }
            }

            if (bFound)
            {
                // Returning Json string from mylist omiting null values
                return(new HttpResponseMessage()
                {
                    Content = new StringContent(
                        JsonConvert.SerializeObject(mylist, new JsonSerializerSettings {
                        NullValueHandling = NullValueHandling.Ignore
                    }),
                        Encoding.UTF8,
                        "application/json"
                        )
                });
            }
            else
            {
                // Scope given in parameter was not found
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Scope not found"));
            }
        }