1 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 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 78 public void setEngineBoots(int engineBoots) { 79 this.localTime.setEngineBoots(engineBoots); 80 } 81 82 94 public int getEngineTime() { 95 return (int)(((System.currentTimeMillis() - lastLocalTimeChange) / 1000) % 96 2147483648L); 97 } 98 99 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 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 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 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 |