1 29 30 31 package snmp; 32 33 34 import java.io.*; 35 36 37 42 43 44 45 public class SNMPBERCodec 46 { 47 48 public static final byte SNMPINTEGER = 0x02; 49 public static final byte SNMPBITSTRING = 0x03; 50 public static final byte SNMPOCTETSTRING = 0x04; 51 public static final byte SNMPNULL = 0x05; 52 public static final byte SNMPOBJECTIDENTIFIER = 0x06; 53 public static final byte SNMPSEQUENCE = 0x30; 54 55 public static final byte SNMPIPADDRESS = (byte)0x40; 56 public static final byte SNMPCOUNTER32 = (byte)0x41; 57 public static final byte SNMPGAUGE32 = (byte)0x42; 58 public static final byte SNMPTIMETICKS = (byte)0x43; 59 public static final byte SNMPOPAQUE = (byte)0x44; 60 public static final byte SNMPNSAPADDRESS = (byte)0x45; 61 public static final byte SNMPCOUNTER64 = (byte)0x46; 62 public static final byte SNMPUINTEGER32 = (byte)0x47; 63 64 public static final byte SNMPGETREQUEST = (byte)0xA0; 65 public static final byte SNMPGETNEXTREQUEST = (byte)0xA1; 66 public static final byte SNMPGETRESPONSE = (byte)0xA2; 67 public static final byte SNMPSETREQUEST = (byte)0xA3; 68 public static final byte SNMPTRAP = (byte)0xA4; 69 70 71 public static final byte SNMPv2pCOMMUNICATION = (byte)0xA2; 73 public static final byte SNMPv2pAUTHORIZEDMESSAGE = (byte)0xA1; 74 public static final byte SNMPv2pENCRYPTEDMESSAGE = (byte)0xA1; 75 public static final byte SNMPv2BULKREQUEST = (byte)0xA5; 76 public static final byte SNMPv2INFORMREQUEST = (byte)0xA6; 77 public static final byte SNMPv2TRAP = (byte)0xA7; 78 79 public static final byte SNMPv2pENCRYPTEDDATA = (byte)0xA1; 80 81 82 public static final byte SNMPUNKNOWNOBJECT = 0x00; 83 84 85 86 87 88 89 95 public static SNMPObject extractEncoding(SNMPTLV theTLV) 96 throws SNMPBadValueException 97 { 98 99 100 switch (theTLV.tag) 101 { 102 case SNMPINTEGER: 103 { 104 return new SNMPInteger(theTLV.value); 105 } 106 107 case SNMPSEQUENCE: 108 { 109 return new SNMPSequence(theTLV.value); 110 } 111 112 case SNMPOBJECTIDENTIFIER: 113 { 114 return new SNMPObjectIdentifier(theTLV.value); 115 } 116 117 case SNMPOCTETSTRING: 118 { 119 return new SNMPOctetString(theTLV.value); 120 } 121 122 case SNMPBITSTRING: 123 { 124 return new SNMPBitString(theTLV.value); 125 } 126 127 case SNMPIPADDRESS: 128 { 129 return new SNMPIPAddress(theTLV.value); 130 } 131 132 case SNMPCOUNTER32: 133 { 134 return new SNMPCounter32(theTLV.value); 135 } 136 137 case SNMPGAUGE32: 138 { 139 return new SNMPGauge32(theTLV.value); 140 } 141 142 case SNMPTIMETICKS: 143 { 144 return new SNMPTimeTicks(theTLV.value); 145 } 146 147 case SNMPNSAPADDRESS: 148 { 149 return new SNMPNSAPAddress(theTLV.value); 150 } 151 152 case SNMPCOUNTER64: 153 { 154 return new SNMPCounter64(theTLV.value); 155 } 156 157 case SNMPUINTEGER32: 158 { 159 return new SNMPUInteger32(theTLV.value); 160 } 161 162 case SNMPGETREQUEST: 163 case SNMPGETNEXTREQUEST: 164 case SNMPGETRESPONSE: 165 case SNMPSETREQUEST: 166 { 167 return new SNMPPDU(theTLV.value, theTLV.tag); 168 } 169 170 case SNMPTRAP: 171 { 172 return new SNMPv1TrapPDU(theTLV.value); 173 } 174 175 case SNMPv2TRAP: 176 { 177 return new SNMPv2TrapPDU(theTLV.value); 178 } 179 180 case SNMPv2INFORMREQUEST: 181 { 182 return new SNMPv2InformRequestPDU(theTLV.value); 183 } 184 185 case SNMPNULL: 186 case SNMPOPAQUE: 187 { 188 return new SNMPNull(); 189 } 190 191 default: 192 { 193 return new SNMPUnknownObject(theTLV.value); 196 } 197 } 198 199 } 200 201 202 203 204 205 210 211 public static SNMPTLV extractNextTLV(byte[] enc, int position) 212 throws SNMPBadValueException 213 { 214 SNMPTLV nextTLV = new SNMPTLV(); 215 int currentPos = position; 216 217 218 try 219 { 220 221 223 243 244 nextTLV.tag = enc[currentPos]; 246 currentPos++; 248 250 int dataLength; 251 252 int unsignedValue = enc[currentPos]; 253 if (unsignedValue < 0) 254 unsignedValue += 256; 255 256 if ((unsignedValue / 128) < 1) 257 { 258 dataLength = unsignedValue; 260 } 261 else 262 { 263 int numBytes = (unsignedValue % 128); 265 266 dataLength = 0; 267 268 for (int i = 0; i < numBytes; i++) 269 { 270 currentPos++; 271 unsignedValue = enc[currentPos]; 272 if (unsignedValue < 0) 273 unsignedValue += 256; 274 dataLength = dataLength * 256 + unsignedValue; 275 } 276 } 277 278 279 currentPos++; 281 nextTLV.totalLength = currentPos - position + dataLength; 283 284 286 ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); 287 outBytes.write(enc, currentPos, dataLength); 288 nextTLV.value = outBytes.toByteArray(); 289 290 291 return nextTLV; 292 293 } 294 catch (IndexOutOfBoundsException e) 295 { 296 throw new SNMPBadValueException("Problem while decoding SNMP: packet truncated or corrupt"); 298 } 299 catch (Exception e) 300 { 301 throw new SNMPBadValueException("Problem while decoding SNMP"); 303 } 304 305 } 306 307 308 309 310 313 314 public static byte[] encodeLength(int length) 315 { 316 ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); 317 318 if (length < 128) 321 { 322 byte[] len = {(byte)length}; 323 outBytes.write(len, 0, 1); 324 } 325 else 326 { 327 int numBytes = 0; 330 int temp = length; 331 while (temp > 0) 332 { 333 ++numBytes; 334 temp = (int)Math.floor(temp / 256); 335 } 336 337 byte num = (byte)numBytes; 338 num += 128; outBytes.write(num); 340 341 byte[] len = new byte[numBytes]; 342 for (int i = numBytes-1; i >= 0; --i) 343 { 344 len[i] = (byte)(length % 256); 345 length = (int)Math.floor(length / 256); 346 } 347 outBytes.write(len, 0, numBytes); 348 349 } 350 351 return outBytes.toByteArray(); 352 } 353 354 355 356 357 } | Popular Tags |