1 11 package com.sun.jmx.snmp.internal; 12 13 import java.net.InetAddress ; 14 import java.net.UnknownHostException ; 15 import java.util.Hashtable ; 16 import java.io.Serializable ; 17 18 import com.sun.jmx.snmp.SnmpDefinitions; 19 import com.sun.jmx.snmp.SnmpEngineId; 20 import com.sun.jmx.snmp.SnmpEngine; 21 import com.sun.jmx.snmp.SnmpUsmKeyHandler; 22 import com.sun.jmx.snmp.SnmpEngineFactory; 23 import com.sun.jmx.snmp.SnmpUnknownModelException; 24 25 import com.sun.jmx.snmp.internal.SnmpTools; 26 import com.sun.jmx.snmp.SnmpBadSecurityLevelException; 27 import com.sun.jmx.trace.Trace; 28 56 public class SnmpEngineImpl implements SnmpEngine, Serializable { 57 61 public static final int noAuthNoPriv = 0; 62 66 public static final int authNoPriv = 1; 67 71 public static final int authPriv = 3; 72 75 public static final int reportableFlag = 4; 76 77 80 public static final int authMask = 1; 81 84 public static final int privMask = 2; 85 88 public static final int authPrivMask = 3; 89 90 private SnmpEngineId engineid = null; 91 private SnmpEngineFactory factory = null; 92 private long startTime = 0; 93 94 private int boot = 0; 95 private boolean checkOid = false; 96 97 transient private SnmpUsmKeyHandler usmKeyHandler = null; 98 transient private SnmpLcd lcd = null; 99 100 transient private SnmpSecuritySubSystem securitySub = null; 101 102 transient private SnmpMsgProcessingSubSystem messageSub = null; 103 104 transient private SnmpAccessControlSubSystem accessSub = null; 105 106 110 public synchronized int getEngineTime() { 111 long delta = (System.currentTimeMillis() / 1000) - startTime; 113 if(delta > 0x7FFFFFFF) { 114 startTime = System.currentTimeMillis() / 1000; 117 118 if(boot != 0x7FFFFFFF) 120 boot += 1; 121 storeNBBoots(boot); 123 } 124 125 return (int) ((System.currentTimeMillis() / 1000) - startTime); 126 } 127 128 132 public SnmpEngineId getEngineId() { 133 return engineid; 134 } 135 136 140 public SnmpUsmKeyHandler getUsmKeyHandler() { 141 return usmKeyHandler; 142 } 143 144 148 public SnmpLcd getLcd() { 149 return lcd; 150 } 151 155 public int getEngineBoots() { 156 return boot; 157 } 158 159 173 public SnmpEngineImpl(SnmpEngineFactory fact, 174 SnmpLcd lcd, 175 SnmpEngineId engineid) throws UnknownHostException { 176 177 init(lcd, fact); 178 initEngineID(); 179 if(this.engineid == null) { 180 if(engineid != null) 181 this.engineid = engineid; 182 else 183 this.engineid = SnmpEngineId.createEngineId(); 184 } 185 lcd.storeEngineId(this.engineid); 186 if(isTraceOn()) { 187 trace("SnmpEngine", "LOCAL ENGINE ID: " + this.engineid); 188 } 189 } 190 204 public SnmpEngineImpl(SnmpEngineFactory fact, 205 SnmpLcd lcd, 206 InetAddress address, 207 int port) throws UnknownHostException { 208 init(lcd, fact); 209 initEngineID(); 210 if(engineid == null) 211 engineid = SnmpEngineId.createEngineId(address, port); 212 213 lcd.storeEngineId(engineid); 214 215 if(isTraceOn()) { 216 trace("SnmpEngine", "LOCAL ENGINE ID: " + engineid + " / " + 217 "LOCAL ENGINE NB BOOTS: " + boot + " / " + 218 "LOCAL ENGINE START TIME: " + getEngineTime()); 219 } 220 } 221 222 235 public SnmpEngineImpl(SnmpEngineFactory fact, 236 SnmpLcd lcd, 237 int port) throws UnknownHostException { 238 init(lcd, fact); 239 initEngineID(); 240 if(engineid == null) 241 engineid = SnmpEngineId.createEngineId(port); 242 243 lcd.storeEngineId(engineid); 244 245 if(isTraceOn()) { 246 trace("SnmpEngine", "LOCAL ENGINE ID: " + engineid + " / " + 247 "LOCAL ENGINE NB BOOTS: " + boot + " / " + 248 "LOCAL ENGINE START TIME: " + getEngineTime()); 249 } 250 } 251 252 264 public SnmpEngineImpl(SnmpEngineFactory fact, 265 SnmpLcd lcd) throws UnknownHostException { 266 init(lcd, fact); 267 initEngineID(); 268 if(engineid == null) 269 engineid = SnmpEngineId.createEngineId(); 270 271 lcd.storeEngineId(engineid); 272 273 274 if(isTraceOn()) { 275 trace("SnmpEngine", "LOCAL ENGINE ID: " + engineid + " / " + 276 "LOCAL ENGINE NB BOOTS: " + boot + " / " + 277 "LOCAL ENGINE START TIME: " + getEngineTime()); 278 } 279 } 280 281 284 public synchronized void activateCheckOid() { 285 checkOid = true; 286 } 287 288 291 public synchronized void deactivateCheckOid() { 292 checkOid = false; 293 } 294 295 298 public synchronized boolean isCheckOidActivated() { 299 return checkOid; 300 } 301 302 private void storeNBBoots(int boot) { 304 if(boot < 0 || boot == 0x7FFFFFFF) { 305 boot = 0x7FFFFFFF; 306 lcd.storeEngineBoots(boot); 307 } 308 else 309 lcd.storeEngineBoots(boot + 1); 310 } 311 312 private void init(SnmpLcd lcd, SnmpEngineFactory fact) { 314 this.factory = fact; 315 this.lcd = lcd; 316 boot = lcd.getEngineBoots(); 317 318 if(boot == -1 || boot == 0) 319 boot = 1; 320 321 storeNBBoots(boot); 322 323 startTime = System.currentTimeMillis() / 1000; 324 325 } 326 327 void setUsmKeyHandler(SnmpUsmKeyHandler usmKeyHandler) { 328 this.usmKeyHandler = usmKeyHandler; 329 } 330 331 private void initEngineID() throws UnknownHostException { 333 String id = lcd.getEngineId(); 334 if(id != null) { 335 engineid = SnmpEngineId.createEngineId(id); 336 } 337 } 338 339 340 344 public SnmpMsgProcessingSubSystem getMsgProcessingSubSystem() { 345 return messageSub; 346 } 347 348 352 public void setMsgProcessingSubSystem(SnmpMsgProcessingSubSystem sys) { 353 messageSub = sys; 354 } 355 356 360 public SnmpSecuritySubSystem getSecuritySubSystem() { 361 return securitySub; 362 } 363 367 public void setSecuritySubSystem(SnmpSecuritySubSystem sys) { 368 securitySub = sys; 369 } 370 374 public void setAccessControlSubSystem(SnmpAccessControlSubSystem sys) { 375 accessSub = sys; 376 } 377 378 382 public SnmpAccessControlSubSystem getAccessControlSubSystem() { 383 return accessSub; 384 } 385 389 public static void checkSecurityLevel(byte msgFlags) 390 throws SnmpBadSecurityLevelException { 391 int secLevel = msgFlags & SnmpDefinitions.authPriv; 392 if((secLevel & SnmpDefinitions.privMask) != 0) 393 if((secLevel & SnmpDefinitions.authMask) == 0) { 394 throw new SnmpBadSecurityLevelException("Security level:"+ 395 " noAuthPriv!!!"); 396 } 397 } 398 399 402 boolean isTraceOn() { 403 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_SNMP); 404 } 405 406 void trace(String clz, String func, String info) { 407 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_SNMP, clz, func, info); 408 } 409 410 void trace(String func, String info) { 411 trace(dbgTag, func, info); 412 } 413 414 boolean isDebugOn() { 415 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_SNMP); 416 } 417 418 void debug(String clz, String func, String info) { 419 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_SNMP, clz, func, info); 420 } 421 422 void debug(String clz, String func, Throwable exception) { 423 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_SNMP, clz, func, exception); 424 } 425 426 void debug(String func, String info) { 427 debug(dbgTag, func, info); 428 } 429 430 void debug(String func, Throwable exception) { 431 debug(dbgTag, func, exception); 432 } 433 434 String dbgTag = "SnmpEngineImpl"; 435 } 436 | Popular Tags |