1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
51
52
53
54 public class SimpleDhcpStore extends AbstractDhcpStore
55 {
56
57 private Map leases = new HashMap();
58
59 private List subnets = new ArrayList();
60
61
62
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
91
92
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
107
108
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
161
162
163
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
183
184 public void updateLease( Lease lease )
185 {
186 leases.put( lease.getHardwareAddress(), lease );
187 }
188
189
190
191
192
193 protected OptionsField getOptions( DhcpConfigElement element )
194 {
195
196 return element.getOptions();
197 }
198
199
200
201
202
203 protected Map getProperties( DhcpConfigElement element )
204 {
205
206 return element.getProperties();
207 }
208 }