📄 snmpgw.java
字号:
eserv.createUSMTable(userName.getBytes(), authProtocol, authPassword, privPassword, privProtocol); eserv.v3_session.addSnmpClient(eserv); eserv.start(); } // end main() public boolean authenticate(SnmpPDU pdu, String community) { boolean rv = false; if(pdu != null) { if(((Snmp3Message)pdu.getMsg()).isAuthenticationFailed()) { System.out.println("In snmpgw : authenticate "+ "failed. Dropping PDU."); } else { rv = true; } } return rv; } public boolean callback(SnmpSession sess, SnmpPDU pdu, int reqID) { if (pdu == null) { System.err.println("Null PDU received"); return false; } enQ(pdu); return true; } public void debugPrint(String debugOutput) { } public void createUSMTable(byte[] name, int authProtocol, String authPassword, String privPassword, int privProtocol) { byte level = 0; USMUserEntry entry = new USMUserEntry(name, engineID); entry.setAuthProtocol(authProtocol); if ((authProtocol != USMUserEntry.NO_AUTH) && (authPassword != null)) { byte[] authKey = USMUtils.password_to_key(authProtocol, authPassword.getBytes(), authPassword.getBytes().length, engineID); entry.setAuthKey(authKey); level = 1; if (privPassword != null) { entry.setPrivProtocol(privProtocol); byte[] tempKey = USMUtils.password_to_key(authProtocol, privPassword.getBytes(), privPassword.getBytes().length, engineID, privProtocol); byte privKey[]=null; if(privProtocol==USMUserEntry.CFB_AES_192) { privKey=new byte[24]; System.arraycopy(tempKey,0,privKey,0,24); } else if(privProtocol==USMUserEntry.CFB_AES_256) { privKey =new byte[32]; System.arraycopy(tempKey,0,privKey,0,32); } else if(privProtocol==USMUserEntry.CBC_3DES) { privKey =new byte[32]; System.arraycopy(tempKey,0,privKey,0,32); } else { privKey=new byte[16]; System.arraycopy(tempKey,0,privKey,0,16); } entry.setPrivKey(privKey); level |= 2; } } entry.setSecurityLevel(level); USMUserTable USMTable = (USMUserTable)api.getSecurityProvider().getTable(3); USMTable.addEntry(entry); byte[] names = entry.getUserName(); byte[] id = entry.getEngineID(); SnmpEngineEntry e = new SnmpEngineEntry("localhost", local_port); e.setEngineID(engineID); entry.setEngineEntry(e); api.setSnmpEngineID(engineID); } /** Print octet data in a more readable form */ String printOctets(byte[] data, int length) { StringBuffer s = new StringBuffer(); int j = 0, line = 20; // we'll allow 20 bytes per line if (data.length < length) { length = data.length; } for (int i=0;i<length;i++) { if (j++ > 19) { j=1; s.append("\n"); } String bs = Integer.toString(byteToInt(data[i]),16); if (bs.length() < 2) { bs = "0" + bs; } s.append(bs+ " "); } return s.toString(); } public synchronized SnmpPDU deQ() { for (Enumeration e = v.elements() ; e.hasMoreElements() ;) { SnmpPDU pdu = (SnmpPDU) e.nextElement(); v.removeElement(pdu); return pdu; } return null; } /** Place in specified queue */ public synchronized void enQ(SnmpPDU pdu) { v.addElement(pdu); notifyAll(); } public void run() { System.out.println("snmpgw: Ready to process requests from SNMPv3 Manager"); while (true) { SnmpPDU pdu = deQ(); if (pdu == null) { wait_for_v3pdus(); } if (pdu == null) { continue; } SnmpPDU ref_pdu = pdu; int version = ref_pdu.getVersion(); ProtocolOptions ref_opt = pdu.getProtocolOptions(); ref_pdu.setVersion(remoteVersion); ref_pdu.setProtocolOptions(null); SnmpPDU rpdu = null; try { // Send PDU SnmpVarBind varb = ref_pdu.getVariableBinding(0); if(varb!=null) { System.out.println("sent V" + (remoteVersion + 1) + " request: OID sent = " + varb.getObjectID()); } rpdu = v1_session.syncSend(ref_pdu); } catch (SnmpException e) { System.err.println("Sending V1 PDU" + e.getMessage()); continue; } if (rpdu == null) // timeout { System.err.println("V1 Request timed out to: " + pdu.getProtocolOptions().getSessionId() ); continue; } SnmpVarBind varb = null; int size = pdu.getVariableBindings().size(); for (int i = 0; (i < size); i++) { pdu.removeVariableBinding(0); } size = rpdu.getVariableBindings().size(); for (int i = 0; i < size; i++) { pdu.addVariableBinding(rpdu.getVariableBinding(i)); } SnmpVarBind varbr = pdu.getVariableBinding(0); if(varbr!=null) { System.out.println("Received V" + (remoteVersion + 1) + " response: OID received = " + varbr.getObjectID()); } pdu.setVersion(version); pdu.setCommand(rpdu.getCommand()); pdu.setErrstat(rpdu.getErrstat()); pdu.setErrindex(rpdu.getErrindex()); pdu.setProtocolOptions(ref_opt); try { v3_session.send(pdu); } catch (SnmpException e) { System.err.println("Session Open "+e.getMessage()); continue; } } } public synchronized void wait_for_v3pdus() { try { if (v.size() > 0) { return; } else { wait(); } } catch (InterruptedException i) { } } static int byteToInt(byte b) { return (int)b & 0xFF; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -