KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > security > UsmTimeTable


1 /*_############################################################################
2   _##
3   _## SNMP4J - UsmTimeTable.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21 package org.snmp4j.security;
22
23 import java.io.*;
24 import java.util.*;
25
26 import org.snmp4j.log.*;
27 import org.snmp4j.mp.*;
28 import org.snmp4j.smi.*;
29
30 /**
31  * The <code>UsmTimeTable</code> class is a singleton that stores USM user
32  * information as part of the Local Configuration Datastore (LCD).
33  *
34  * @author Frank Fock
35  * @version 1.2
36  */

37 public class UsmTimeTable implements Serializable {
38
39   private static final long serialVersionUID = -1538321547688349797L;
40
41   private static final LogAdapter logger = LogFactory.getLogger(UsmTimeTable.class);
42
43   private Hashtable table = new Hashtable(10);
44   private long lastLocalTimeChange = System.currentTimeMillis();
45   private UsmTimeEntry localTime;
46
47   public UsmTimeTable(OctetString localEngineID, int engineBoots) {
48     setLocalTime(new UsmTimeEntry(localEngineID, engineBoots, 0));
49   }
50
51   public void addEntry(final UsmTimeEntry entry) {
52     table.put(entry.getEngineID(), entry);
53   }
54
55   public UsmTimeEntry getEntry(final OctetString engineID) {
56     return (UsmTimeEntry) table.get(engineID);
57   }
58
59   public UsmTimeEntry getLocalTime() {
60     UsmTimeEntry entry = new UsmTimeEntry(localTime.getEngineID(),
61                                           localTime.getEngineBoots(),
62                                           getEngineTime());
63     entry.setTimeDiff(entry.getTimeDiff() * ( -1) + localTime.getTimeDiff());
64     return entry;
65   }
66
67   private void setLocalTime(UsmTimeEntry localTime) {
68     this.localTime = localTime;
69     lastLocalTimeChange = System.currentTimeMillis();
70   }
71
72   /**
73    * Sets the number of engine boots.
74    * @param engineBoots
75    * the number of engine boots.
76    * @since 1.2
77    */

78   public void setEngineBoots(int engineBoots) {
79     this.localTime.setEngineBoots(engineBoots);
80   }
81
82   /**
83    * Returns the number of seconds since the value of
84    * the engineBoots object last changed. When incrementing this object's value
85    * would cause it to exceed its maximum, engineBoots is incremented as if a
86    * re-initialization had occurred, and this
87    * object's value consequently reverts to zero.
88    *
89    * @return
90    * a positive integer value denoting the number of seconds since
91    * the engineBoots value has been changed.
92    * @since 1.2
93    */

94   public int getEngineTime() {
95     return (int)(((System.currentTimeMillis() - lastLocalTimeChange) / 1000) %
96                  2147483648L);
97   }
98
99   /**
100    * The number of times that the SNMP engine has (re-)initialized itself
101    * since snmpEngineID was last configured.
102    * @return
103    * the number of SNMP engine reboots.
104    */

105   public int getEngineBoots() {
106     return localTime.getEngineBoots();
107   }
108
109   public synchronized UsmTimeEntry getTime(OctetString engineID) {
110     if (localTime.getEngineID().equals(engineID)) {
111       return getLocalTime();
112     }
113     UsmTimeEntry found = (UsmTimeEntry) table.get(engineID);
114     if (found == null) {
115       return null;
116     }
117     return new UsmTimeEntry(engineID, found.getEngineBoots(),
118                             found.getTimeDiff() +
119                             (int) (System.currentTimeMillis() / 1000));
120   }
121
122   /**
123    * Removes the specified engine ID from the time cache.
124    * @param engineID
125    * the engine ID of the remote SNMP engine to remove from this time cache.
126    */

127   public void removeEntry(final OctetString engineID) {
128     table.remove(engineID);
129   }
130
131   public synchronized int checkEngineID(OctetString engineID,
132                                         boolean discoveryAllowed) {
133     if (table.get(engineID) != null) {
134       return SnmpConstants.SNMPv3_USM_OK;
135     }
136     else if (discoveryAllowed) {
137       addEntry(new UsmTimeEntry(engineID, 0, 0));
138       return SnmpConstants.SNMPv3_USM_OK;
139     }
140     return SnmpConstants.SNMPv3_USM_UNKNOWN_ENGINEID;
141   }
142
143   public synchronized int checkTime(final UsmTimeEntry entry) {
144     int now = (int) (System.currentTimeMillis() / 1000);
145     if (localTime.getEngineID().equals(entry.getEngineID())) {
146       /* Entry found, we are authoritative */
147       if ((localTime.getEngineBoots() == 2147483647) ||
148           (localTime.getEngineBoots() != entry.getEngineBoots()) ||
149           (Math.abs(now + localTime.getTimeDiff() - entry.getLatestReceivedTime())
150            > 150)) {
151         if (logger.isDebugEnabled()) {
152           logger.debug(
153               "CheckTime: received message outside time window (authorative):"+
154               ((localTime.getEngineBoots() !=
155                 entry.getEngineBoots()) ? "engineBoots differ" :
156                ""+(Math.abs(now + localTime.getTimeDiff() -
157                             entry.getLatestReceivedTime()))+" > 150"));
158         }
159         return SnmpConstants.SNMPv3_USM_NOT_IN_TIME_WINDOW;
160       }
161       else {
162         if (logger.isDebugEnabled()) {
163           logger.debug("CheckTime: time ok (authorative)");
164         }
165         return SnmpConstants.SNMPv3_USM_OK;
166       }
167     }
168     else {
169       UsmTimeEntry time = (UsmTimeEntry) table.get(entry.getEngineID());
170       if (time == null) {
171         return SnmpConstants.SNMPv3_USM_UNKNOWN_ENGINEID;
172       }
173       if ((entry.getEngineBoots() < time.getEngineBoots()) ||
174           ((entry.getEngineBoots() == time.getEngineBoots()) &&
175            (time.getTimeDiff() + now >
176             entry.getLatestReceivedTime() + 150)) ||
177           (time.getEngineBoots() == 2147483647)) {
178         if (logger.isDebugEnabled()) {
179           logger.debug(
180               "CheckTime: received message outside time window (non authorative)");
181         }
182         return SnmpConstants.SNMPv3_USM_NOT_IN_TIME_WINDOW;
183       }
184       else {
185         if ((entry.getEngineBoots() > time.getEngineBoots()) ||
186             ((entry.getEngineBoots() == time.getEngineBoots()) &&
187              (entry.getLatestReceivedTime() > time.getLatestReceivedTime()))) {
188           /* time ok, update values */
189           time.setEngineBoots(entry.getEngineBoots());
190           time.setLatestReceivedTime(entry.getLatestReceivedTime());
191           time.setTimeDiff(entry.getLatestReceivedTime() - now);
192         }
193         if (logger.isDebugEnabled()) {
194           logger.debug("CheckTime: time ok (non authorative)");
195         }
196         return SnmpConstants.SNMPv3_USM_OK;
197       }
198     }
199   }
200 }
201
Popular Tags