1 29 30 31 package snmp; 32 33 import java.io.*; 34 import java.net.*; 35 36 37 42 43 public class SNMPTrapSenderInterface 44 { 45 public static final int SNMP_TRAP_PORT = 162; 46 47 private DatagramSocket dSocket; 48 49 50 53 54 public SNMPTrapSenderInterface() 55 throws SocketException 56 { 57 dSocket = new DatagramSocket(); 58 } 59 60 61 62 66 67 public SNMPTrapSenderInterface(int localPort) 68 throws SocketException 69 { 70 dSocket = new DatagramSocket(localPort); 71 } 72 73 74 75 80 81 public void sendTrap(int version, InetAddress hostAddress, String community, SNMPv1TrapPDU pdu) 82 throws IOException 83 { 84 SNMPMessage message = new SNMPMessage(version, community, pdu); 85 86 byte[] messageEncoding = message.getBEREncoding(); 87 88 94 95 DatagramPacket outPacket = new DatagramPacket(messageEncoding, messageEncoding.length, hostAddress, SNMP_TRAP_PORT); 96 97 107 108 dSocket.send(outPacket); 109 110 } 111 112 113 114 118 119 public void sendTrap(InetAddress hostAddress, String community, SNMPv1TrapPDU pdu) 120 throws IOException 121 { 122 int version = 0; 123 124 sendTrap(version, hostAddress, community, pdu); 125 } 126 127 128 129 133 134 public void sendTrap(int version, InetAddress hostAddress, String community, SNMPv2TrapPDU pdu) 135 throws IOException 136 { 137 SNMPMessage message = new SNMPMessage(version, community, pdu); 138 139 byte[] messageEncoding = message.getBEREncoding(); 140 141 147 148 DatagramPacket outPacket = new DatagramPacket(messageEncoding, messageEncoding.length, hostAddress, SNMP_TRAP_PORT); 149 150 160 161 dSocket.send(outPacket); 162 163 } 164 165 166 167 171 172 public void sendTrap(InetAddress hostAddress, String community, SNMPv2TrapPDU pdu) 173 throws IOException 174 { 175 int version = 1; 176 177 sendTrap(version, hostAddress, community, pdu); 178 } 179 180 181 182 private String hexByte(byte b) 183 { 184 int pos = b; 185 if (pos < 0) 186 pos += 256; 187 String returnString = new String (); 188 returnString += Integer.toHexString(pos/16); 189 returnString += Integer.toHexString(pos%16); 190 return returnString; 191 } 192 193 194 195 196 197 198 private String getHex(byte theByte) 199 { 200 int b = theByte; 201 202 if (b < 0) 203 b += 256; 204 205 String returnString = new String (Integer.toHexString(b)); 206 207 if (returnString.length()%2 == 1) 209 returnString = "0" + returnString; 210 211 return returnString; 212 } 213 214 215 216 } | Popular Tags |