1 11 12 13 package com.sun.jmx.snmp.daemon; 14 15 16 import java.util.Vector ; 19 import java.util.Enumeration ; 20 import java.net.DatagramSocket ; 21 import java.net.DatagramPacket ; 22 import java.net.InetAddress ; 23 import java.net.SocketException ; 24 import java.net.UnknownHostException ; 25 import java.io.ObjectInputStream ; 26 import java.io.IOException ; 27 import java.io.InterruptedIOException ; 28 29 30 import javax.management.MBeanServer ; 33 import javax.management.MBeanRegistration ; 34 import javax.management.ObjectName ; 35 import javax.management.InstanceAlreadyExistsException ; 36 import com.sun.jmx.snmp.SnmpIpAddress; 37 import com.sun.jmx.snmp.SnmpMessage; 38 import com.sun.jmx.snmp.SnmpOid; 39 import com.sun.jmx.snmp.SnmpPduFactory; 40 import com.sun.jmx.snmp.SnmpPduPacket; 41 import com.sun.jmx.snmp.SnmpPduRequest; 42 import com.sun.jmx.snmp.SnmpPduTrap; 43 import com.sun.jmx.snmp.SnmpTimeticks; 44 import com.sun.jmx.snmp.SnmpVarBind; 45 import com.sun.jmx.snmp.SnmpVarBindList; 46 import com.sun.jmx.snmp.SnmpDefinitions; 47 import com.sun.jmx.snmp.SnmpStatusException; 48 import com.sun.jmx.snmp.SnmpTooBigException; 49 import com.sun.jmx.snmp.InetAddressAcl; 50 import com.sun.jmx.snmp.SnmpPeer; 51 import com.sun.jmx.snmp.SnmpParameters; 52 import com.sun.jmx.snmp.SnmpPduFactoryBER; 55 import com.sun.jmx.snmp.agent.SnmpMibAgent; 56 import com.sun.jmx.snmp.agent.SnmpMibHandler; 57 import com.sun.jmx.snmp.agent.SnmpUserDataFactory; 58 import com.sun.jmx.snmp.agent.SnmpErrorHandlerAgent; 59 60 import com.sun.jmx.snmp.IPAcl.SnmpAcl; 61 62 import com.sun.jmx.snmp.tasks.ThreadService; 63 64 114 115 public class SnmpAdaptorServer extends CommunicatorServer 116 implements SnmpAdaptorServerMBean, MBeanRegistration , SnmpDefinitions, 117 SnmpMibHandler { 118 119 122 126 private int trapPort = 162; 127 128 132 private int informPort = 162; 133 134 139 InetAddress address = null; 140 141 144 private Object ipacl = null; 145 146 149 private SnmpPduFactory pduFactory = null; 150 151 154 private SnmpUserDataFactory userDataFactory = null; 155 156 160 private boolean authRespEnabled = true; 161 162 165 private boolean authTrapEnabled = true; 166 167 171 private SnmpOid enterpriseOid = new SnmpOid("1.3.6.1.4.1.42"); 172 173 179 int bufferSize = 1024; 180 181 private transient long startUpTime = 0; 182 private transient DatagramSocket socket = null; 183 transient DatagramSocket trapSocket = null; 184 private transient SnmpSession informSession = null; 185 private transient DatagramPacket packet = null; 186 transient Vector mibs = new Vector (); 187 private transient SnmpMibTree root; 188 189 192 private transient boolean useAcl = true; 193 194 195 198 202 private int maxTries = 3 ; 203 204 208 private int timeout = 3 * 1000 ; 209 210 213 216 int snmpOutTraps=0; 217 218 221 private int snmpOutGetResponses=0; 222 223 226 private int snmpOutGenErrs=0; 227 228 231 private int snmpOutBadValues=0; 232 233 236 private int snmpOutNoSuchNames=0; 237 238 241 private int snmpOutTooBigs=0; 242 243 246 int snmpOutPkts=0; 247 248 251 private int snmpInASNParseErrs=0; 252 253 256 private int snmpInBadCommunityUses=0; 257 258 261 private int snmpInBadCommunityNames=0; 262 263 266 private int snmpInBadVersions=0; 267 268 271 private int snmpInGetRequests=0; 272 273 276 private int snmpInGetNexts=0; 277 278 281 private int snmpInSetRequests=0; 282 283 286 private int snmpInPkts=0; 287 288 291 private int snmpInTotalReqVars=0; 292 293 296 private int snmpInTotalSetVars=0; 297 298 301 private int snmpSilentDrops=0; 302 303 private static final String InterruptSysCallMsg = 304 "Interrupted system call"; 305 static final SnmpOid sysUpTimeOid = new SnmpOid("1.3.6.1.2.1.1.3.0") ; 306 static final SnmpOid snmpTrapOidOid = new SnmpOid("1.3.6.1.6.3.1.1.4.1.0"); 307 308 private ThreadService threadService; 309 310 private static int threadNumber = 6; 311 312 static { 313 String s = System.getProperty("com.sun.jmx.snmp.threadnumber"); 314 315 if (s != null) { 316 try { 317 threadNumber = Integer.parseInt(System.getProperty(s)); 318 } catch (Exception e) { 319 System.err.println("Got wrong value for " + 321 "com.sun.jmx.snmp.threadnumber: "+s); 322 System.err.println("Use the default value: "+threadNumber); 323 } 324 } 325 } 326 327 330 335 public SnmpAdaptorServer() { 336 this(true, null, com.sun.jmx.snmp.ServiceName.SNMP_ADAPTOR_PORT, 337 null) ; 338 } 339 340 347 public SnmpAdaptorServer(int port) { 348 this(true, null, port, null) ; 349 } 350 351 360 public SnmpAdaptorServer(InetAddressAcl acl) { 361 this(false, acl, com.sun.jmx.snmp.ServiceName.SNMP_ADAPTOR_PORT, 362 null) ; 363 } 364 365 374 public SnmpAdaptorServer(InetAddress addr) { 375 this(true, null, com.sun.jmx.snmp.ServiceName.SNMP_ADAPTOR_PORT, 376 addr) ; 377 } 378 379 389 public SnmpAdaptorServer(InetAddressAcl acl, int port) { 390 this(false, acl, port, null) ; 391 } 392 393 402 public SnmpAdaptorServer(int port, InetAddress addr) { 403 this(true, null, port, addr) ; 404 } 405 406 416 public SnmpAdaptorServer(InetAddressAcl acl, InetAddress addr) { 417 this(false, acl, com.sun.jmx.snmp.ServiceName.SNMP_ADAPTOR_PORT, 418 addr) ; 419 } 420 421 432 public SnmpAdaptorServer(InetAddressAcl acl, int port, InetAddress addr) { 433 this(false, acl, port, addr); 434 } 435 436 452 public SnmpAdaptorServer(boolean useAcl, int port, InetAddress addr) { 453 this(useAcl,null,port,addr); 454 } 455 456 private SnmpAdaptorServer(boolean forceAcl, InetAddressAcl acl, 460 int port, InetAddress addr) { 461 super(CommunicatorServer.SNMP_TYPE) ; 462 463 464 if (acl == null && forceAcl) { 467 try { 468 acl = (InetAddressAcl) 469 new SnmpAcl("SNMP protocol adaptor IP ACL"); 470 } catch (UnknownHostException e) { 471 if (isDebugOn()) { 472 debug("constructor", 473 "UnknowHostException when creating ACL"); 474 debug("constructor", e); 475 } 476 } 477 } else { 478 this.useAcl = (acl!=null) || forceAcl; 479 } 480 481 init(acl, port, addr) ; 482 } 483 484 487 495 public int getServedClientCount() { 496 return super.getServedClientCount(); 497 } 498 499 506 public int getActiveClientCount() { 507 return super.getActiveClientCount(); 508 } 509 510 517 public int getMaxActiveClientCount() { 518 return super.getMaxActiveClientCount(); 519 } 520 521 530 public void setMaxActiveClientCount(int c) 531 throws java.lang.IllegalStateException { 532 super.setMaxActiveClientCount(c); 533 } 534 535 541 public InetAddressAcl getInetAddressAcl() { 542 return (InetAddressAcl)ipacl; 543 } 544 545 551 public Integer getTrapPort() { 552 return new Integer (trapPort) ; 553 } 554 555 560 public void setTrapPort(Integer port) { 561 setTrapPort(port.intValue()); 562 } 563 564 569 public void setTrapPort(int port) { 570 int val= port ; 571 if (val < 0) throw new 572 IllegalArgumentException ("Trap port cannot be a negative value"); 573 trapPort= val ; 574 } 575 576 582 public int getInformPort() { 583 return informPort; 584 } 585 586 592 public void setInformPort(int port) { 593 if (port < 0) 594 throw new IllegalArgumentException ("Inform request port "+ 595 "cannot be a negative value"); 596 informPort= port ; 597 } 598 599 604 public String getProtocol() { 605 return "snmp"; 606 } 607 608 616 public Integer getBufferSize() { 617 return new Integer (bufferSize) ; 618 } 619 620 630 public void setBufferSize(Integer s) 631 throws java.lang.IllegalStateException { 632 if ((state == ONLINE) || (state == STARTING)) { 633 throw new IllegalStateException ("Stop server before carrying out"+ 634 " this operation"); 635 } 636 bufferSize = s.intValue() ; 637 } 638 639 645 final public int getMaxTries() { 646 return maxTries; 647 } 648 649 654 final public synchronized void setMaxTries(int newMaxTries) { 655 if (newMaxTries < 0) 656 throw new IllegalArgumentException (); 657 maxTries = newMaxTries; 658 } 659 660 665 final public int getTimeout() { 666 return timeout; 667 } 668 669 673 final public synchronized void setTimeout(int newTimeout) { 674 if (newTimeout < 0) 675 throw new IllegalArgumentException (); 676 timeout= newTimeout; 677 } 678 679 684 public SnmpPduFactory getPduFactory() { 685 return pduFactory ; 686 } 687 688 693 public void setPduFactory(SnmpPduFactory factory) { 694 if (factory == null) 695 pduFactory = new SnmpPduFactoryBER() ; 696 else 697 pduFactory = factory ; 698 } 699 700 706 public void setUserDataFactory(SnmpUserDataFactory factory) { 707 userDataFactory = factory ; 708 } 709 710 716 public SnmpUserDataFactory getUserDataFactory() { 717 return userDataFactory; 718 } 719 720 732 public boolean getAuthTrapEnabled() { 733 return authTrapEnabled ; 734 } 735 736 742 public void setAuthTrapEnabled(boolean enabled) { 743 authTrapEnabled = enabled ; 744 } 745 746 759 public boolean getAuthRespEnabled() { 760 return authRespEnabled ; 761 } 762 763 769 public void setAuthRespEnabled(boolean enabled) { 770 authRespEnabled = enabled ; 771 } 772 773 780 public String getEnterpriseOid() { 781 return enterpriseOid.toString() ; 782 } 783 784 791 public void setEnterpriseOid(String oid) throws IllegalArgumentException { 792 enterpriseOid = new SnmpOid(oid) ; 793 } 794 795 800 public String [] getMibs() { 801 String [] result = new String [mibs.size()] ; 802 int i = 0 ; 803 for (Enumeration e = mibs.elements() ; e.hasMoreElements() ;) { 804 SnmpMibAgent mib = (SnmpMibAgent)e.nextElement() ; 805 result[i++] = mib.getMibName(); 806 } 807 return result ; 808 } 809 810 813 818 public Long getSnmpOutTraps() { 819 return new Long (snmpOutTraps); 820 } 821 822 827 public Long getSnmpOutGetResponses() { 828 return new Long (snmpOutGetResponses); 829 } 830 831 836 public Long getSnmpOutGenErrs() { 837 return new Long (snmpOutGenErrs); 838 } 839 840 845 public Long getSnmpOutBadValues() { 846 return new Long (snmpOutBadValues); 847 } 848 849 854 public Long getSnmpOutNoSuchNames() { 855 return new Long (snmpOutNoSuchNames); 856 } 857 858 863 public Long getSnmpOutTooBigs() { 864 return new Long (snmpOutTooBigs); 865 } 866 867 872 public Long getSnmpInASNParseErrs() { 873 return new Long (snmpInASNParseErrs); 874 } 875 876 881 public Long getSnmpInBadCommunityUses() { 882 return new Long (snmpInBadCommunityUses); 883 } 884 885 891 public Long getSnmpInBadCommunityNames() { 892 return new Long (snmpInBadCommunityNames); 893 } 894 895 900 public Long getSnmpInBadVersions() { 901 return new Long (snmpInBadVersions); 902 } 903 904 909 public Long getSnmpOutPkts() { 910 return new Long (snmpOutPkts); 911 } 912 913 918 public Long getSnmpInPkts() { 919 return new Long (snmpInPkts); 920 } 921 922 927 public Long getSnmpInGetRequests() { 928 return new Long (snmpInGetRequests); 929 } 930 931 936 public Long getSnmpInGetNexts() { 937 return new Long (snmpInGetNexts); 938 } 939 940 945 public Long getSnmpInSetRequests() { 946 return new Long (snmpInSetRequests); 947 } 948 949 954 public Long getSnmpInTotalSetVars() { 955 return new Long (snmpInTotalSetVars); 956 } 957 958 963 public Long getSnmpInTotalReqVars() { 964 return new Long (snmpInTotalReqVars); 965 } 966 967 975 public Long getSnmpSilentDrops() { 976 return new Long (snmpSilentDrops); 977 } 978 979 987 public Long getSnmpProxyDrops() { 988 return new Long (0); 989 } 990 991 992 995 1014 public ObjectName preRegister(MBeanServer server, ObjectName name) 1015 throws java.lang.Exception { 1016 1017 if (name == null) { 1018 name = new ObjectName (server.getDefaultDomain() + ":" + 1019 com.sun.jmx.snmp.ServiceName.SNMP_ADAPTOR_SERVER); 1020 } 1021 return (super.preRegister(server, name)); 1022 } 1023 1024 1027 public void postRegister (Boolean registrationDone) { 1028 super.postRegister(registrationDone); 1029 } 1030 1031 1034 public void preDeregister() throws java.lang.Exception { 1035 super.preDeregister(); 1036 } 1037 1038 1041 public void postDeregister() { 1042 super.postDeregister(); 1043 } 1044 1045 1054 public SnmpMibHandler addMib(SnmpMibAgent mib) 1055 throws IllegalArgumentException { 1056 if (mib == null) { 1057 throw new IllegalArgumentException () ; 1058 } 1059 1060 if(!mibs.contains(mib)) 1061 mibs.addElement(mib); 1062 1063 root.register(mib); 1064 1065 return this; 1066 } 1067 1068 1084 public SnmpMibHandler addMib(SnmpMibAgent mib, SnmpOid[] oids) 1085 throws IllegalArgumentException { 1086 if (mib == null) { 1087 throw new IllegalArgumentException () ; 1088 } 1089 1090 if(oids == null) 1092 return addMib(mib); 1093 1094 if(!mibs.contains(mib)) 1095 mibs.addElement(mib); 1096 1097 for (int i = 0; i < oids.length; i++) { 1098 root.register(mib, oids[i].longValue()); 1099 } 1100 return this; 1101 } 1102 1103 1116 public SnmpMibHandler addMib(SnmpMibAgent mib, String contextName) 1117 throws IllegalArgumentException { 1118 return addMib(mib); 1119 } 1120 1121 1137 public SnmpMibHandler addMib(SnmpMibAgent mib, 1138 String contextName, 1139 SnmpOid[] oids) 1140 throws IllegalArgumentException { 1141 return addMib(mib, oids); 1142 } 1143 1144 1158 public boolean removeMib(SnmpMibAgent mib, String contextName) { 1159 return removeMib(mib); 1160 } 1161 1162 1170 public boolean removeMib(SnmpMibAgent mib) { 1171 root.unregister(mib); 1172 return (mibs.removeElement(mib)) ; 1173 } 1174 1175 1186 public boolean removeMib(SnmpMibAgent mib, SnmpOid[] oids) { 1187 root.unregister(mib, oids); 1188 return (mibs.removeElement(mib)) ; 1189 } 1190 1191 1203 public boolean removeMib(SnmpMibAgent mib, 1204 String contextName, 1205 SnmpOid[] oids) { 1206 return removeMib(mib, oids); 1207 } 1208 1209 1212 1215 protected void doBind() 1216 throws CommunicationException, InterruptedException { 1217 1218 try { 1219 synchronized (this) { 1220 socket = new DatagramSocket (port, address) ; 1221 } 1222 dbgTag = makeDebugTag(); 1223 } catch (SocketException e) { 1224 if (e.getMessage().equals(InterruptSysCallMsg)) 1225 throw new InterruptedException (e.toString()) ; 1226 else { 1227 if (isDebugOn()) { 1228 debug("doBind", "cannot bind on port " + port); 1229 } 1230 throw new CommunicationException(e) ; 1231 } 1232 } 1233 } 1234 1235 1241 public int getPort() { 1242 synchronized (this) { 1243 if (socket != null) return socket.getLocalPort(); 1244 } 1245 return super.getPort(); 1246 } 1247 1248 1251 protected void doUnbind() 1252 throws CommunicationException, InterruptedException { 1253 if (isTraceOn()) { 1254 trace("doUnbind","Finally close the socket"); 1255 } 1256 synchronized (this) { 1257 if (socket != null) { 1258 socket.close() ; 1259 socket = null ; 1260 } 1262 } 1263 closeTrapSocketIfNeeded() ; 1264 closeInformSocketIfNeeded() ; 1265 } 1266 1267 void createSnmpRequestHandler(SnmpAdaptorServer server, int id, 1268 DatagramSocket s, DatagramPacket p, 1269 SnmpMibTree tree, Vector m, Object a, 1270 SnmpPduFactory factory, 1271 SnmpUserDataFactory dataFactory, 1272 MBeanServer f, ObjectName n) { 1273 final SnmpRequestHandler handler = 1274 new SnmpRequestHandler(this, id, s, p, tree, m, a, factory, 1275 dataFactory, f, n); 1276 threadService.submitTask(handler); 1277 } 1278 1279 1283 protected void doReceive() 1284 throws CommunicationException, InterruptedException { 1285 1286 try { 1289 packet = new DatagramPacket (new byte[bufferSize], bufferSize) ; 1290 socket.receive(packet); 1291 int state = getState(); 1292 1293 if(state != ONLINE) { 1294 if (isTraceOn()) { 1295 trace("doReceive", 1296 "received a message but state not online, returning."); 1297 } 1298 return; 1299 } 1300 1301 createSnmpRequestHandler(this, servedClientCount, socket, 1302 packet, root, mibs, ipacl, pduFactory, 1303 userDataFactory, topMBS, objectName); 1304 } catch (SocketException e) { 1305 if (e.getMessage().equals(InterruptSysCallMsg)) 1308 throw new InterruptedException (e.toString()) ; 1309 else 1310 throw new CommunicationException(e) ; 1311 } catch (InterruptedIOException e) { 1312 throw new InterruptedException (e.toString()) ; 1313 } catch (CommunicationException e) { 1314 throw e ; 1315 } catch (Exception e) { 1316 throw new CommunicationException(e) ; 1317 } 1318 if (isTraceOn()) { 1319 trace("doReceive", "received a message"); 1320 } 1321 } 1322 1323 protected void doError(Exception e) throws CommunicationException { 1324 return; 1325 } 1326 1327 1330 protected void doProcess() 1331 throws CommunicationException, InterruptedException { 1332 } 1333 1334 1335 1341 protected int getBindTries() { 1342 return 1; 1343 } 1344 1345 1352 public void stop(){ 1353 1354 final int port = getPort(); 1355 if (isTraceOn()) { 1356 trace("stop", "Stopping: using port " + port); 1357 } 1358 if ((state == ONLINE) || (state == STARTING)){ 1359 super.stop(); 1360 try { 1361 DatagramSocket sn = new DatagramSocket (0); 1362 try { 1363 byte[] ob = new byte[1]; 1364 1365 DatagramPacket pk; 1366 if (address != null) 1367 pk = new DatagramPacket (ob , 1, address, port); 1368 else 1369 pk = new DatagramPacket (ob , 1, 1370 java.net.InetAddress.getLocalHost(), port); 1371 1372 if (isTraceOn()) { 1373 trace("stop", "Sending: using port " + port); 1374 } 1375 sn.send(pk); 1376 } finally { 1377 sn.close(); 1378 } 1379 } catch (Throwable e){ 1380 if (isDebugOn()) { 1381 debug("stop", e); 1382 } 1383 } 1384 } 1385 } 1386 1387 1390 1405 public void snmpV1Trap(int generic, int specific, 1406 SnmpVarBindList varBindList) 1407 throws IOException , SnmpStatusException { 1408 1409 if (isTraceOn()) { 1410 trace("snmpV1Trap", "generic=" + generic + 1411 ", specific=" + specific); 1412 } 1413 1414 SnmpPduTrap pdu = new SnmpPduTrap() ; 1417 pdu.address = null ; 1418 pdu.port = trapPort ; 1419 pdu.type = pduV1TrapPdu ; 1420 pdu.version = snmpVersionOne ; 1421 pdu.community = null ; 1422 pdu.enterprise = enterpriseOid ; 1423 pdu.genericTrap = generic ; 1424 pdu.specificTrap = specific ; 1425 pdu.timeStamp = getSysUpTime(); 1426 1427 if (varBindList != null) { 1428 pdu.varBindList = new SnmpVarBind[varBindList.size()] ; 1429 varBindList.copyInto(pdu.varBindList); 1430 } 1431 else 1432 pdu.varBindList = null ; 1433 1434 try { 1436 if (address != null) 1437 pdu.agentAddr = handleMultipleIpVersion(address.getAddress()); 1438 else pdu.agentAddr = 1439 handleMultipleIpVersion(InetAddress.getLocalHost().getAddress()); 1440 } catch (UnknownHostException e) { 1441 byte[] zeroedAddr = new byte[4]; 1442 pdu.agentAddr = handleMultipleIpVersion(zeroedAddr) ; 1443 } 1444 1445 sendTrapPdu(pdu) ; 1448 } 1449 1450 private SnmpIpAddress handleMultipleIpVersion(byte[] address) { 1451 if(address.length == 4) 1452 return new SnmpIpAddress(address); 1453 else { 1454 if(isDebugOn()) 1455 debug("handleMultipleIPVersion", 1456 "Not an IPv4 address, return null"); 1457 return null; 1458 } 1459 } 1460 1461 1477 public void snmpV1Trap(InetAddress addr, String cs, int generic, 1478 int specific, SnmpVarBindList varBindList) 1479 throws IOException , SnmpStatusException { 1480 1481 if (isTraceOn()) { 1482 trace("snmpV1Trap", "generic=" + generic + ", specific=" + 1483 specific); 1484 } 1485 1486 SnmpPduTrap pdu = new SnmpPduTrap() ; 1489 pdu.address = null ; 1490 pdu.port = trapPort ; 1491 pdu.type = pduV1TrapPdu ; 1492 pdu.version = snmpVersionOne ; 1493 1494 if(cs != null) 1495 pdu.community = cs.getBytes(); 1496 else 1497 pdu.community = null ; 1498 1499 pdu.enterprise = enterpriseOid ; 1500 pdu.genericTrap = generic ; 1501 pdu.specificTrap = specific ; 1502 pdu.timeStamp = getSysUpTime(); 1503 1504 if (varBindList != null) { 1505 pdu.varBindList = new SnmpVarBind[varBindList.size()] ; 1506 varBindList.copyInto(pdu.varBindList); 1507 } 1508 else 1509 pdu.varBindList = null ; 1510 1511 try { 1513 if (address != null) 1514 pdu.agentAddr = handleMultipleIpVersion(address.getAddress()); 1515 else pdu.agentAddr = 1516 handleMultipleIpVersion(InetAddress.getLocalHost().getAddress()); 1517 } catch (UnknownHostException e) { 1518 byte[] zeroedAddr = new byte[4]; 1519 pdu.agentAddr = handleMultipleIpVersion(zeroedAddr) ; 1520 } 1521 1522 if(addr != null) 1525 sendTrapPdu(addr, pdu) ; 1526 else 1527 sendTrapPdu(pdu); 1528 } 1529 1530 1553 public void snmpV1Trap(InetAddress addr, 1554 SnmpIpAddress agentAddr, 1555 String cs, 1556 SnmpOid enterpOid, 1557 int generic, 1558 int specific, 1559 SnmpVarBindList varBindList, 1560 SnmpTimeticks time) 1561 throws IOException , SnmpStatusException { 1562 snmpV1Trap(addr, 1563 trapPort, 1564 agentAddr, 1565 cs, 1566 enterpOid, 1567 generic, 1568 specific, 1569 varBindList, 1570 time); 1571 } 1572 1573 1594 public void snmpV1Trap(SnmpPeer peer, 1595 SnmpIpAddress agentAddr, 1596 SnmpOid enterpOid, 1597 int generic, 1598 int specific, 1599 SnmpVarBindList varBindList, 1600 SnmpTimeticks time) 1601 throws IOException , SnmpStatusException { 1602 SnmpParameters p = (SnmpParameters) peer.getParams(); 1603 snmpV1Trap(peer.getDestAddr(), 1604 peer.getDestPort(), 1605 agentAddr, 1606 p.getRdCommunity(), 1607 enterpOid, 1608 generic, 1609 specific, 1610 varBindList, 1611 time); 1612 } 1613 1614 private void snmpV1Trap(InetAddress addr, 1615 int port, 1616 SnmpIpAddress agentAddr, 1617 String cs, 1618 SnmpOid enterpOid, 1619 int generic, 1620 int specific, 1621 SnmpVarBindList varBindList, 1622 SnmpTimeticks time) 1623 throws IOException , SnmpStatusException { 1624 1625 if (isTraceOn()) { 1626 trace("snmpV1Trap", "generic=" + generic + ", specific=" + 1627 specific); 1628 } 1629 1630 SnmpPduTrap pdu = new SnmpPduTrap() ; 1633 pdu.address = null ; 1634 pdu.port = port ; 1635 pdu.type = pduV1TrapPdu ; 1636 pdu.version = snmpVersionOne ; 1637 1638 if(cs != null) 1640 pdu.community = cs.getBytes(); 1641 else 1642 pdu.community = null ; 1643 1645 if(enterpOid != null) 1647 pdu.enterprise = enterpOid; 1648 else 1649 pdu.enterprise = enterpriseOid ; 1650 pdu.genericTrap = generic ; 1652 pdu.specificTrap = specific ; 1653 if(time != null) 1655 pdu.timeStamp = time.longValue(); 1656 else 1657 pdu.timeStamp = getSysUpTime(); 1658 1660 if (varBindList != null) { 1661 pdu.varBindList = new SnmpVarBind[varBindList.size()] ; 1662 varBindList.copyInto(pdu.varBindList); 1663 } 1664 else 1665 pdu.varBindList = null ; 1666 1667 if (agentAddr == null) { 1668 try { 1671 final InetAddress inetAddr = 1672 (address!=null)?address:InetAddress.getLocalHost(); 1673 agentAddr = handleMultipleIpVersion(inetAddr.getAddress()); 1674 } catch (UnknownHostException e) { 1675 byte[] zeroedAddr = new byte[4]; 1676 agentAddr = handleMultipleIpVersion(zeroedAddr); 1677 } 1678 } 1679 1680 pdu.agentAddr = agentAddr; 1681 1682 if(addr != null) 1686 sendTrapPdu(addr, pdu) ; 1687 else 1688 sendTrapPdu(pdu); 1689 1690 } 1692 1693 1721 public void snmpV2Trap(SnmpPeer peer, 1722 SnmpOid trapOid, 1723 SnmpVarBindList varBindList, 1724 SnmpTimeticks time) 1725 throws IOException , SnmpStatusException { 1726 SnmpParameters p = (SnmpParameters) peer.getParams(); 1727 snmpV2Trap(peer.getDestAddr(), 1728 peer.getDestPort(), 1729 p.getRdCommunity(), 1730 trapOid, 1731 varBindList, 1732 time); 1733 } 1734 1735 1757 public void snmpV2Trap(SnmpOid trapOid, SnmpVarBindList varBindList) 1758 throws IOException , SnmpStatusException { 1759 1760 if (isTraceOn()) { 1761 trace("snmpV2Trap", "trapOid=" + trapOid); 1762 } 1763 1764 SnmpPduRequest pdu = new SnmpPduRequest() ; 1768 pdu.address = null ; 1769 pdu.port = trapPort ; 1770 pdu.type = pduV2TrapPdu ; 1771 pdu.version = snmpVersionTwo ; 1772 pdu.community = null ; 1773 1774 SnmpVarBindList fullVbl ; 1775 if (varBindList != null) 1776 fullVbl = (SnmpVarBindList)varBindList.clone() ; 1777 else 1778 fullVbl = new SnmpVarBindList(2) ; 1779 SnmpTimeticks sysUpTimeValue = new SnmpTimeticks(getSysUpTime()) ; 1780 fullVbl.insertElementAt(new SnmpVarBind(snmpTrapOidOid, trapOid), 0) ; 1781 fullVbl.insertElementAt(new SnmpVarBind(sysUpTimeOid, sysUpTimeValue), 1782 0); 1783 pdu.varBindList = new SnmpVarBind[fullVbl.size()] ; 1784 fullVbl.copyInto(pdu.varBindList) ; 1785 1786 sendTrapPdu(pdu) ; 1789 } 1790 1791 1815 public void snmpV2Trap(InetAddress addr, String cs, SnmpOid trapOid, 1816 SnmpVarBindList varBindList) 1817 throws IOException , SnmpStatusException { 1818 1819 if (isTraceOn()) { 1820 trace("snmpV2Trap", "trapOid=" + trapOid); 1821 } 1822 1823 SnmpPduRequest pdu = new SnmpPduRequest() ; 1827 pdu.address = null ; 1828 pdu.port = trapPort ; 1829 pdu.type = pduV2TrapPdu ; 1830 pdu.version = snmpVersionTwo ; 1831 1832 if(cs != null) 1833 pdu.community = cs.getBytes(); 1834 else 1835 pdu.community = null; 1836 1837 SnmpVarBindList fullVbl ; 1838 if (varBindList != null) 1839 fullVbl = (SnmpVarBindList)varBindList.clone() ; 1840 else 1841 fullVbl = new SnmpVarBindList(2) ; 1842 SnmpTimeticks sysUpTimeValue = new SnmpTimeticks(getSysUpTime()) ; 1843 fullVbl.insertElementAt(new SnmpVarBind(snmpTrapOidOid, trapOid), 0) ; 1844 fullVbl.insertElementAt(new SnmpVarBind(sysUpTimeOid, sysUpTimeValue), 1845 0); 1846 pdu.varBindList = new SnmpVarBind[fullVbl.size()] ; 1847 fullVbl.copyInto(pdu.varBindList) ; 1848 1849 if(addr != null) 1852 sendTrapPdu(addr, pdu); 1853 else 1854 sendTrapPdu(pdu); 1855 } 1856 1857 1887 public void snmpV2Trap(InetAddress addr, 1888 String cs, 1889 SnmpOid trapOid, 1890 SnmpVarBindList varBindList, 1891 SnmpTimeticks time) 1892 throws IOException , SnmpStatusException { 1893 1894 snmpV2Trap(addr, 1895 trapPort, 1896 cs, 1897 trapOid, 1898 varBindList, 1899 time); 1900 } 1901 1902 private void snmpV2Trap(InetAddress addr, 1903 int port, 1904 String cs, 1905 SnmpOid trapOid, 1906 SnmpVarBindList varBindList, 1907 SnmpTimeticks time) 1908 throws IOException , SnmpStatusException { 1909 1910 if (isTraceOn()) { 1911 trace("snmpV2Trap", "trapOid=" + trapOid + 1912 "\ncommunity=" + cs + "\naddr=" + addr + 1913 "\nvarBindList=" + varBindList + "\ntime=" + time + 1914 "\ntrapPort=" + port); 1915 } 1916 1917 SnmpPduRequest pdu = new SnmpPduRequest() ; 1921 pdu.address = null ; 1922 pdu.port = port ; 1923 pdu.type = pduV2TrapPdu ; 1924 pdu.version = snmpVersionTwo ; 1925 1926 if(cs != null) 1927 pdu.community = cs.getBytes(); 1928 else 1929 pdu.community = null; 1930 1931 SnmpVarBindList fullVbl ; 1932 if (varBindList != null) 1933 fullVbl = (SnmpVarBindList)varBindList.clone() ; 1934 else 1935 fullVbl = new SnmpVarBindList(2) ; 1936 1937 SnmpTimeticks sysUpTimeValue = null; 1939 if(time != null) 1940 sysUpTimeValue = time; 1941 else 1942 sysUpTimeValue = new SnmpTimeticks(getSysUpTime()) ; 1943 1945 fullVbl.insertElementAt(new SnmpVarBind(snmpTrapOidOid, trapOid), 0) ; 1946 fullVbl.insertElementAt(new SnmpVarBind(sysUpTimeOid, sysUpTimeValue), 1947 0); 1948 pdu.varBindList = new SnmpVarBind[fullVbl.size()] ; 1949 fullVbl.copyInto(pdu.varBindList) ; 1950 1951 if(addr != null) 1955 sendTrapPdu(addr, pdu) ; 1956 else 1957 sendTrapPdu(pdu); 1958 } 1960 1961 1971 public void snmpPduTrap(InetAddress address, SnmpPduPacket pdu) 1972 throws IOException , SnmpStatusException { 1973 1974 if(address != null) 1975 sendTrapPdu(address, pdu); 1976 else 1977 sendTrapPdu(pdu); 1978 } 1979 1980 1990 public void snmpPduTrap(SnmpPeer peer, 1991 SnmpPduPacket pdu) 1992 throws IOException , SnmpStatusException { 1993 if(peer != null) { 1994 pdu.port = peer.getDestPort(); 1995 sendTrapPdu(peer.getDestAddr(), pdu); 1996 } 1997 else { 1998 pdu.port = getTrapPort().intValue(); 1999 sendTrapPdu(pdu); 2000 } 2001 } 2002 2003 2006 private void sendTrapPdu(SnmpPduPacket pdu) 2007 throws SnmpStatusException, IOException { 2008 2009 SnmpMessage msg = null ; 2012 try { 2013 msg = (SnmpMessage)pduFactory.encodeSnmpPdu(pdu, bufferSize) ; 2014 if (msg == null) { 2015 throw new SnmpStatusException( 2016 SnmpDefinitions.snmpRspAuthorizationError) ; 2017 } 2018 } 2019 catch (SnmpTooBigException x) { 2020 if (isDebugOn()) { 2021 debug("sendTrapPdu", "trap pdu is too big"); 2022 debug("sendTrapPdu", "trap hasn't been sent to anyone"); 2023 } 2024 throw new SnmpStatusException(SnmpDefinitions.snmpRspTooBig) ; 2025 } 2028 2029 int sendingCount = 0 ; 2032 openTrapSocketIfNeeded() ; 2033 if (ipacl != null) { 2034 Enumeration ed = ((InetAddressAcl)ipacl).getTrapDestinations() ; 2035 while (ed.hasMoreElements()) { 2036 msg.address = (InetAddress )ed.nextElement() ; 2037 Enumeration ec = ((InetAddressAcl)ipacl). 2038 getTrapCommunities(msg.address) ; 2039 while (ec.hasMoreElements()) { 2040 msg.community = ((String )ec.nextElement()).getBytes() ; 2041 try { 2042 sendTrapMessage(msg) ; 2043 sendingCount++ ; 2044 } 2045 catch (SnmpTooBigException x) { 2046 if (isDebugOn()) { 2047 debug("sendTrapPdu", "trap pdu is too big"); 2048 debug("sendTrapPdu", "trap hasn't been sent to "+ 2049 msg.address); 2050 } 2051 } 2052 } 2053 } 2054 } 2055 2056 if (sendingCount == 0) { 2061 try { 2062 msg.address = InetAddress.getLocalHost() ; 2063 sendTrapMessage(msg) ; 2064 } catch (SnmpTooBigException x) { 2065 if (isDebugOn()) { 2066 debug("sendTrapPdu", "trap pdu is too big"); 2067 debug("sendTrapPdu", "trap hasn't been sent"); 2068 } 2069 } catch (UnknownHostException e) { 2070 if (isDebugOn()) { 2071 debug("sendTrapPdu", "cannot get the local host"); 2072 debug("sendTrapPdu", "trap hasn't been sent"); 2073 } 2074 } 2075 } 2076 2077 closeTrapSocketIfNeeded() ; 2078 } 2079 2080 2083 private void sendTrapPdu(InetAddress addr, SnmpPduPacket pdu) 2084 throws SnmpStatusException, IOException { 2085 2086 SnmpMessage msg = null ; 2089 try { 2090 msg = (SnmpMessage)pduFactory.encodeSnmpPdu(pdu, bufferSize) ; 2091 if (msg == null) { 2092 throw new SnmpStatusException( 2093 SnmpDefinitions.snmpRspAuthorizationError) ; 2094 } 2095 } catch (SnmpTooBigException x) { 2096 if (isDebugOn()) { 2097 debug("sendTrapPdu", "trap pdu is too big"); 2098 debug("sendTrapPdu", 2099 "trap hasn't been sent to the specified host"); 2100 } 2101 throw new SnmpStatusException(SnmpDefinitions.snmpRspTooBig) ; 2102 } 2105 2106 openTrapSocketIfNeeded() ; 2109 if (addr != null) { 2110 msg.address = addr; 2111 try { 2112 sendTrapMessage(msg) ; 2113 } catch (SnmpTooBigException x) { 2114 if (isDebugOn()) { 2115 debug("sendTrapPdu", "trap pdu is too big"); 2116 debug("sendTrapPdu", "trap hasn't been sent to " + 2117 msg.address); 2118 } 2119 } 2120 } 2121 2122 closeTrapSocketIfNeeded() ; 2123 } 2124 2125 2128 private void sendTrapMessage(SnmpMessage msg) 2129 throws IOException , SnmpTooBigException { 2130 byte[] buffer = new byte[bufferSize] ; 2131 DatagramPacket packet = new DatagramPacket (buffer, buffer.length) ; 2132 int encodingLength = msg.encodeMessage(buffer) ; 2133 packet.setLength(encodingLength) ; 2134 packet.setAddress(msg.address) ; 2135 packet.setPort(msg.port) ; 2136 if (isTraceOn()) { 2137 trace("sendTrapMessage", "sending trap to " + msg.address + ":" + 2138 msg.port); 2139 } 2140 trapSocket.send(packet) ; 2141 if (isTraceOn()) { 2142 trace("sendTrapMessage", "sent to " + msg.address + ":" + 2143 msg.port); 2144 } 2145 snmpOutTraps++; 2146 snmpOutPkts++; 2147 } 2148 2149 2152 synchronized void openTrapSocketIfNeeded() throws SocketException { 2153 if (trapSocket == null) { 2154 trapSocket = new DatagramSocket (0, address) ; 2155 if (isTraceOn()) { 2156 trace("openTrapSocketIfNeeded", "using port " + 2157 trapSocket.getLocalPort() + " to send traps"); 2158 } 2159 } 2160 } 2161 2162 2165 synchronized void closeTrapSocketIfNeeded() { 2166 if ((trapSocket != null) && (state != ONLINE)) { 2167 trapSocket.close() ; 2168 trapSocket = null ; 2169 } 2170 } 2171 2172 2175 2208 public Vector snmpInformRequest(SnmpInformHandler cb, SnmpOid trapOid, 2209 SnmpVarBindList varBindList) 2210 throws IllegalStateException , IOException , SnmpStatusException { 2211 2212 if (!isActive()) { 2213 throw new IllegalStateException ( 2214 "Start SNMP adaptor server before carrying out this operation"); 2215 } 2216 if (isTraceOn()) { 2217 trace("snmpInformRequest", "trapOid=" + trapOid); 2218 } 2219 2220 SnmpVarBindList fullVbl ; 2224 if (varBindList != null) 2225 fullVbl = (SnmpVarBindList)varBindList.clone() ; 2226 else 2227 fullVbl = new SnmpVarBindList(2) ; 2228 SnmpTimeticks sysUpTimeValue = new SnmpTimeticks(getSysUpTime()) ; 2229 fullVbl.insertElementAt(new SnmpVarBind(snmpTrapOidOid, trapOid), 0) ; 2230 fullVbl.insertElementAt(new SnmpVarBind(sysUpTimeOid, sysUpTimeValue), 2231 0); 2232 2233 openInformSocketIfNeeded() ; 2236 2237 Vector informReqList = new Vector (); 2240 InetAddress addr = null; 2241 String cs = null; 2242 if (ipacl != null) { 2243 Enumeration ed = ((InetAddressAcl)ipacl).getInformDestinations() ; 2244 while (ed.hasMoreElements()) { 2245 addr = (InetAddress )ed.nextElement() ; 2246 Enumeration ec = ((InetAddressAcl)ipacl). 2247 getInformCommunities(addr) ; 2248 while (ec.hasMoreElements()) { 2249 cs = (String )ec.nextElement() ; 2250 informReqList.addElement( 2251 informSession.makeAsyncRequest(addr, cs, cb, 2252 fullVbl,getInformPort())) ; 2253 } 2254 } 2255 } 2256 2257 return informReqList ; 2258 } 2259 2260 2292 public SnmpInformRequest snmpInformRequest(InetAddress addr, 2293 String cs, 2294 SnmpInformHandler cb, 2295 SnmpOid trapOid, 2296 SnmpVarBindList varBindList) 2297 throws IllegalStateException , IOException , SnmpStatusException { 2298 2299 return snmpInformRequest(addr, 2300 getInformPort(), 2301 cs, 2302 cb, 2303 trapOid, 2304 varBindList); 2305 } 2306 2307 2342 public SnmpInformRequest snmpInformRequest(SnmpPeer peer, 2343 SnmpInformHandler cb, 2344 SnmpOid trapOid, 2345 SnmpVarBindList varBindList) 2346 throws IllegalStateException , IOException , SnmpStatusException { 2347 SnmpParameters p = (SnmpParameters) peer.getParams(); 2348 return snmpInformRequest(peer.getDestAddr(), 2349 peer.getDestPort(), 2350 p.getInformCommunity(), 2351 cb, 2352 trapOid, 2353 varBindList); 2354 } 2355 2356 2363 public static final int mapErrorStatus(int errorStatus, 2364 int protocolVersion, 2365 int reqPduType) { 2366 return SnmpSubRequestHandler.mapErrorStatus(errorStatus, 2367 protocolVersion, 2368 reqPduType); 2369 } 2370 2371 private SnmpInformRequest snmpInformRequest(InetAddress addr, 2372 int port, 2373 String cs, 2374 SnmpInformHandler cb, 2375 SnmpOid trapOid, 2376 SnmpVarBindList varBindList) 2377 throws IllegalStateException , IOException , SnmpStatusException { 2378 if (!isActive()) { 2379 throw new IllegalStateException ( 2380 "Start SNMP adaptor server before carrying out this operation"); 2381 } 2382 if (isTraceOn()) { 2383 trace("snmpInformRequest", "trapOid=" + trapOid); 2384 } 2385 2386 SnmpVarBindList fullVbl ; 2390 if (varBindList != null) 2391 fullVbl = (SnmpVarBindList)varBindList.clone() ; 2392 else 2393 fullVbl = new SnmpVarBindList(2) ; 2394 SnmpTimeticks sysUpTimeValue = new SnmpTimeticks(getSysUpTime()) ; 2395 fullVbl.insertElementAt(new SnmpVarBind(snmpTrapOidOid, trapOid), 0) ; 2396 fullVbl.insertElementAt(new SnmpVarBind(sysUpTimeOid, sysUpTimeValue), 2397 0); 2398 2399 openInformSocketIfNeeded() ; 2402 return informSession.makeAsyncRequest(addr, cs, cb, fullVbl, port) ; 2403 } 2404 2405 2406 2409 synchronized void openInformSocketIfNeeded() throws SocketException { 2410 if (informSession == null) { 2411 informSession = new SnmpSession(this) ; 2412 if (isTraceOn()) { 2413 trace("openInformSocketIfNeeded", 2414 "to send inform requests and receive inform responses"); 2415 } 2416 } 2417 } 2418 2419 2422 synchronized void closeInformSocketIfNeeded() { 2423 if ((informSession != null) && (state != ONLINE)) { 2424 informSession.destroySession() ; 2425 informSession = null ; 2426 } 2427 } 2428 2429 2434 InetAddress getAddress() { 2435 return address; 2436 } 2437 2438 2439 2442 2449 protected void finalize() { 2450 try { 2451 if (socket != null) { 2452 socket.close() ; 2453 socket = null ; 2454 } 2455 2456 threadService.terminate(); 2457 } catch (Exception e) { 2458 trace("finalize","Exception in finalizer: " +e); 2459 } 2460 } 2461 2462 2465 2468 String makeDebugTag() { 2469 return "SnmpAdaptorServer["+ getProtocol() + ":" + getPort() + "]"; 2470 } 2471 2472 void updateRequestCounters(int pduType) { 2473 switch(pduType) { 2474 2475 case pduGetRequestPdu: 2476 snmpInGetRequests++; 2477 break; 2478 case pduGetNextRequestPdu: 2479 snmpInGetNexts++; 2480 break; 2481 case pduSetRequestPdu: 2482 snmpInSetRequests++; 2483 break; 2484 default: 2485 break; 2486 } 2487 snmpInPkts++ ; 2488 } 2489 2490 void updateErrorCounters(int errorStatus) { 2491 switch(errorStatus) { 2492 2493 case snmpRspNoError: 2494 snmpOutGetResponses++; 2495 break; 2496 case snmpRspGenErr: 2497 snmpOutGenErrs++; 2498 break; 2499 case snmpRspBadValue: 2500 snmpOutBadValues++; 2501 break; 2502 case snmpRspNoSuchName: 2503 snmpOutNoSuchNames++; 2504 break; 2505 case snmpRspTooBig: 2506 snmpOutTooBigs++; 2507 break; 2508 default: 2509 break; 2510 } 2511 snmpOutPkts++ ; 2512 } 2513 2514 void updateVarCounters(int pduType, int n) { 2515 switch(pduType) { 2516 2517 case pduGetRequestPdu: 2518 case pduGetNextRequestPdu: 2519 case pduGetBulkRequestPdu: 2520 snmpInTotalReqVars += n ; 2521 break ; 2522 case pduSetRequestPdu: 2523 snmpInTotalSetVars += n ; 2524 break ; 2525 } 2526 } 2527 2528 void incSnmpInASNParseErrs(int n) { 2529 snmpInASNParseErrs += n ; 2530 } 2531 2532 void incSnmpInBadVersions(int n) { 2533 snmpInBadVersions += n ; 2534 } 2535 2536 void incSnmpInBadCommunityUses(int n) { 2537 snmpInBadCommunityUses += n ; 2538 } 2539 2540 void incSnmpInBadCommunityNames(int n) { 2541 snmpInBadCommunityNames += n ; 2542 } 2543 2544 void incSnmpSilentDrops(int n) { 2545 snmpSilentDrops += n ; 2546 } 2547 2550 2554 long getSysUpTime() { 2555 return (System.currentTimeMillis() - startUpTime) / 10 ; 2556 } 2557 2558 2561 private void readObject(ObjectInputStream stream) 2562 throws IOException , ClassNotFoundException { 2563 2564 stream.defaultReadObject(); 2567 2568 mibs = new Vector () ; 2573 } 2574 2575 2578 private void init(Object acl, int p, InetAddress a) { 2579 2580 root= new SnmpMibTree(); 2581 2582 root.setDefaultAgent(new SnmpErrorHandlerAgent()); 2584 2585 startUpTime= java.lang.System.currentTimeMillis(); 2588 maxActiveClientCount = 10; 2589 2590 pduFactory = new SnmpPduFactoryBER() ; 2592 2593 port = p ; 2594 ipacl = acl ; 2595 address = a ; 2596 2597 if ((ipacl == null) && (useAcl == true)) 2598 throw new IllegalArgumentException ("ACL object cannot be null") ; 2599 2600 threadService = new ThreadService(threadNumber); 2601 } 2602 2603 SnmpMibAgent getAgentMib(SnmpOid oid) { 2604 return root.getAgentMib(oid); 2605 } 2606 2607 protected Thread createMainThread() { 2608 final Thread t = super.createMainThread(); 2609 t.setDaemon(true); 2610 return t; 2611 } 2612 2613} 2614 2615 | Popular Tags |