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.server.dhcp.DhcpException;
43  import org.apache.directory.server.dhcp.messages.HardwareAddress;
44  import org.apache.directory.server.dhcp.options.OptionsField;
45  import org.apache.directory.server.dhcp.service.Lease;
46  
47  
48  /**
49   * Very simple dummy/proof-of-concept implementation of a DhcpStore.
50   * 
51   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
52   * @version $Rev: 545042 $, $Date: 2007-06-06 22:32:01 -0500 (Mi, 06 Jun 2007) $
53   */
54  public class SimpleDhcpStore extends AbstractDhcpStore
55  {
56      // private static final String DEFAULT_INITIAL_CONTEXT_FACTORY =
57      // "org.apache.directory.server.core.jndi.CoreContextFactory";
58  
59      // a map of current leases
60      private Map leases = new HashMap();
61  
62      private List subnets = new ArrayList();
63  
64  
65      public SimpleDhcpStore()
66      {
67          try
68          {
69              subnets.add( new Subnet( InetAddress.getByName( "192.168.168.0" ),
70                  InetAddress.getByName( "255.255.255.0" ), InetAddress.getByName( "192.168.168.159" ), InetAddress
71                      .getByName( "192.168.168.179" ) ) );
72          }
73          catch ( UnknownHostException e )
74          {
75              throw new RuntimeException( "Can't init", e );
76          }
77      }
78  
79  
80      protected DirContext getContext() throws NamingException
81      {
82          Hashtable env = new Hashtable();
83          env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
84          // env.put( Context.INITIAL_CONTEXT_FACTORY,
85          // DEFAULT_INITIAL_CONTEXT_FACTORY );
86          env.put( Context.PROVIDER_URL, "ldap://localhost:389/dc=tcat,dc=test" );
87  
88          return new InitialDirContext( env );
89      }
90  
91  
92      /**
93       * @param hardwareAddress
94       * @param existingLease
95       * @return Lease
96       */
97      protected Lease findExistingLease( HardwareAddress hardwareAddress, Lease existingLease )
98      {
99          if ( leases.containsKey( hardwareAddress ) )
100             existingLease = ( Lease ) leases.get( hardwareAddress );
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             try
116             {
117                 String filter = "(&(objectclass=ipHost)(objectclass=ieee802Device)(macaddress={0}))";
118                 SearchControls sc = new SearchControls();
119                 sc.setCountLimit( 1 );
120                 sc.setSearchScope( SearchControls.SUBTREE_SCOPE );
121                 NamingEnumeration ne = ctx.search( "", filter, new Object[]
122                     { hardwareAddress.toString() }, sc );
123 
124                 if ( ne.hasMoreElements() )
125                 {
126                     SearchResult sr = ( SearchResult ) ne.next();
127                     Attributes att = sr.getAttributes();
128                     Attribute ipHostNumberAttribute = att.get( "iphostnumber" );
129                     if ( ipHostNumberAttribute != null )
130                     {
131                         InetAddress clientAddress = InetAddress.getByName( ( String ) ipHostNumberAttribute.get() );
132                         Attribute cnAttribute = att.get( "cn" );
133                         return new Host( cnAttribute != null ? ( String ) cnAttribute.get() : "unknown", clientAddress,
134                             hardwareAddress );
135                     }
136                 }
137             }
138             catch ( Exception e )
139             {
140                 throw new DhcpException( "Can't lookup lease", e );
141             }
142             finally
143             {
144                 ctx.close();
145             }
146         }
147         catch ( NamingException e )
148         {
149             throw new DhcpException( "Can't lookup lease", e );
150         }
151 
152         return null;
153     }
154 
155 
156     /**
157      * Find the subnet for the given client address.
158      * 
159      * @param clientAddress
160      * @return Subnet
161      */
162     protected Subnet findSubnet( InetAddress clientAddress )
163     {
164         for ( Iterator i = subnets.iterator(); i.hasNext(); )
165         {
166             Subnet subnet = ( Subnet ) i.next();
167             if ( subnet.contains( clientAddress ) )
168                 return subnet;
169         }
170         return null;
171     }
172 
173 
174     /*
175      * @see org.apache.directory.server.dhcp.store.AbstractDhcpStore#updateLease(org.apache.directory.server.dhcp.service.Lease)
176      */
177     public void updateLease( Lease lease )
178     {
179         leases.put( lease.getHardwareAddress(), lease );
180     }
181 
182 
183     /*
184      * @see org.apache.directory.server.dhcp.store.AbstractDhcpStore#getOptions(org.apache.directory.server.dhcp.store.DhcpConfigElement)
185      */
186     protected OptionsField getOptions( DhcpConfigElement element )
187     {
188         // we don't have groups, classes, etc. yet.
189         return element.getOptions();
190     }
191 
192 
193     /*
194      * @see org.apache.directory.server.dhcp.store.AbstractDhcpStore#getProperties(org.apache.directory.server.dhcp.store.DhcpConfigElement)
195      */
196     protected Map getProperties( DhcpConfigElement element )
197     {
198         // we don't have groups, classes, etc. yet.
199         return element.getProperties();
200     }
201 }