View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.dhcp.store;
21  
22  
23  import java.net.InetAddress;
24  import java.net.UnknownHostException;
25  import java.util.ArrayList;
26  import java.util.HashMap;
27  import java.util.Hashtable;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Map;
31  
32  import javax.naming.Context;
33  import javax.naming.NamingEnumeration;
34  import javax.naming.NamingException;
35  import javax.naming.directory.Attribute;
36  import javax.naming.directory.Attributes;
37  import javax.naming.directory.DirContext;
38  import javax.naming.directory.InitialDirContext;
39  import javax.naming.directory.SearchControls;
40  import javax.naming.directory.SearchResult;
41  
42  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
43  import org.apache.directory.server.dhcp.DhcpException;
44  import org.apache.directory.server.dhcp.messages.HardwareAddress;
45  import org.apache.directory.server.dhcp.options.OptionsField;
46  import org.apache.directory.server.dhcp.service.Lease;
47  
48  
49  /**
50   * Very simple dummy/proof-of-concept implementation of a DhcpStore.
51   * 
52   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
53   */
54  public class SimpleDhcpStore extends AbstractDhcpStore
55  {
56      // a map of current leases
57      private Map leases = new HashMap();
58  
59      private List subnets = new ArrayList();
60  
61  
62      //This will suppress PMD.AvoidUsingHardCodedIP warnings in this class
63      @SuppressWarnings("PMD.AvoidUsingHardCodedIP")
64      public SimpleDhcpStore()
65      {
66          try
67          {
68              subnets.add( new Subnet( InetAddress.getByName( "192.168.168.0" ),
69                  InetAddress.getByName( "255.255.255.0" ), InetAddress.getByName( "192.168.168.159" ), InetAddress
70                      .getByName( "192.168.168.179" ) ) );
71          }
72          catch ( UnknownHostException e )
73          {
74              throw new RuntimeException( "Can't init", e );
75          }
76      }
77  
78  
79      protected DirContext getContext() throws NamingException
80      {
81          Hashtable env = new Hashtable();
82          env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
83          env.put( Context.PROVIDER_URL, "ldap://localhost:389/dc=tcat,dc=test" );
84  
85          return new InitialDirContext( env );
86      }
87  
88  
89      /**
90       * @param hardwareAddress
91       * @param existingLease
92       * @return Lease
93       */
94      protected Leaserver/dhcp/service/Lease.html#Lease">Lease findExistingLease( HardwareAddress hardwareAddress, Lease existingLease )
95      {
96          if ( leases.containsKey( hardwareAddress ) )
97          {
98              existingLease = ( Lease ) leases.get( hardwareAddress );
99          }
100 
101         return existingLease;
102     }
103 
104 
105     /**
106      * @param hardwareAddress
107      * @return Host
108      * @throws DhcpException
109      */
110     protected Host findDesignatedHost( HardwareAddress hardwareAddress ) throws DhcpException
111     {
112         try
113         {
114             DirContext ctx = getContext();
115 
116             try
117             {
118                 String filter = "(&(objectclass=ipHost)(objectclass=ieee802Device)(macaddress={0}))";
119                 SearchControls sc = new SearchControls();
120                 sc.setCountLimit( 1 );
121                 sc.setSearchScope( SearchControls.SUBTREE_SCOPE );
122                 NamingEnumeration ne = ctx.search( "", filter, new Object[]
123                     { hardwareAddress.toString() }, sc );
124 
125                 if ( ne.hasMoreElements() )
126                 {
127                     SearchResult sr = ( SearchResult ) ne.next();
128                     Attributes att = sr.getAttributes();
129                     Attribute ipHostNumberAttribute = att.get( "iphostnumber" );
130 
131                     if ( ipHostNumberAttribute != null )
132                     {
133                         InetAddress clientAddress = InetAddress.getByName( ( String ) ipHostNumberAttribute.get() );
134                         Attribute cnAttribute = att.get( SchemaConstants.CN_AT );
135 
136                         return new Host( cnAttribute != null ? ( String ) cnAttribute.get() : "unknown", clientAddress,
137                             hardwareAddress );
138                     }
139                 }
140             }
141             catch ( Exception e )
142             {
143                 throw new DhcpException( "Can't lookup lease", e );
144             }
145             finally
146             {
147                 ctx.close();
148             }
149         }
150         catch ( NamingException e )
151         {
152             throw new DhcpException( "Can't lookup lease", e );
153         }
154 
155         return null;
156     }
157 
158 
159     /**
160      * Find the subnet for the given client address.
161      * 
162      * @param clientAddress
163      * @return Subnet
164      */
165     protected Subnet findSubnet( InetAddress clientAddress )
166     {
167         for ( Iterator i = subnets.iterator(); i.hasNext(); )
168         {
169             Subnet./../../../../../org/apache/directory/server/dhcp/store/Subnet.html#Subnet">Subnet subnet = ( Subnet ) i.next();
170 
171             if ( subnet.contains( clientAddress ) )
172             {
173                 return subnet;
174             }
175         }
176 
177         return null;
178     }
179 
180 
181     /*
182      * @see org.apache.directory.server.dhcp.store.AbstractDhcpStore#updateLease(org.apache.directory.server.dhcp.service.Lease)
183      */
184     public void updateLease( Lease lease )
185     {
186         leases.put( lease.getHardwareAddress(), lease );
187     }
188 
189 
190     /*
191      * @see org.apache.directory.server.dhcp.store.AbstractDhcpStore#getOptions(org.apache.directory.server.dhcp.store.DhcpConfigElement)
192      */
193     protected OptionsField getOptions( DhcpConfigElement element )
194     {
195         // we don't have groups, classes, etc. yet.
196         return element.getOptions();
197     }
198 
199 
200     /*
201      * @see org.apache.directory.server.dhcp.store.AbstractDhcpStore#getProperties(org.apache.directory.server.dhcp.store.DhcpConfigElement)
202      */
203     protected Map getProperties( DhcpConfigElement element )
204     {
205         // we don't have groups, classes, etc. yet.
206         return element.getProperties();
207     }
208 }