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.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
50
51
52
53
54 public class SimpleDhcpStore extends AbstractDhcpStore
55 {
56
57
58
59
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
85
86 env.put( Context.PROVIDER_URL, "ldap://localhost:389/dc=tcat,dc=test" );
87
88 return new InitialDirContext( env );
89 }
90
91
92
93
94
95
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
107
108
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
158
159
160
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
176
177 public void updateLease( Lease lease )
178 {
179 leases.put( lease.getHardwareAddress(), lease );
180 }
181
182
183
184
185
186 protected OptionsField getOptions( DhcpConfigElement element )
187 {
188
189 return element.getOptions();
190 }
191
192
193
194
195
196 protected Map getProperties( DhcpConfigElement element )
197 {
198
199 return element.getProperties();
200 }
201 }