1 29 30 31 package snmp; 32 33 import java.util.*; 34 import java.math.*; 35 36 37 38 39 157 158 159 public class SNMPv1TrapPDU extends SNMPSequence 160 { 161 162 166 167 public SNMPv1TrapPDU(SNMPObjectIdentifier enterpriseOID, SNMPIPAddress agentAddress, int genericTrap, int specificTrap, SNMPTimeTicks timestamp, SNMPSequence varList) 168 throws SNMPBadValueException 169 { 170 super(); 171 172 tag = SNMPBERCodec.SNMPTRAP; 173 174 Vector contents = new Vector(); 175 176 contents.addElement(enterpriseOID); 177 contents.addElement(agentAddress); 178 contents.addElement(new SNMPInteger(genericTrap)); 179 contents.addElement(new SNMPInteger(specificTrap)); 180 contents.addElement(timestamp); 181 contents.addElement(varList); 182 183 this.setValue(contents); 184 } 185 186 187 188 192 193 public SNMPv1TrapPDU(SNMPObjectIdentifier enterpriseOID, SNMPIPAddress agentAddress, int genericTrap, int specificTrap, SNMPTimeTicks timestamp) 194 throws SNMPBadValueException 195 { 196 super(); 197 198 tag = SNMPBERCodec.SNMPTRAP; 199 200 Vector contents = new Vector(); 201 202 contents.addElement(enterpriseOID); 203 contents.addElement(agentAddress); 204 contents.addElement(new SNMPInteger(genericTrap)); 205 contents.addElement(new SNMPInteger(specificTrap)); 206 contents.addElement(timestamp); 207 contents.addElement(new SNMPVarBindList()); 208 209 this.setValue(contents); 210 } 211 212 213 214 215 219 220 protected SNMPv1TrapPDU(byte[] enc) 221 throws SNMPBadValueException 222 { 223 tag = SNMPBERCodec.SNMPTRAP; 224 extractFromBEREncoding(enc); 225 226 Vector contents = (Vector)(this.getValue()); 228 229 if (contents.size() != 6) 230 { 231 throw new SNMPBadValueException("Bad Trap PDU"); 232 } 233 234 if (!(contents.elementAt(0) instanceof SNMPObjectIdentifier)) 235 { 236 throw new SNMPBadValueException("Bad Trap PDU: bad enterprise OID"); 237 } 238 239 if (!(contents.elementAt(1) instanceof SNMPIPAddress)) 240 { 241 throw new SNMPBadValueException("Bad Trap PDU: bad agent address"); 242 } 243 244 if (!(contents.elementAt(2) instanceof SNMPInteger)) 245 { 246 throw new SNMPBadValueException("Bad Trap PDU: bad generic trap code"); 247 } 248 249 if (!(contents.elementAt(3) instanceof SNMPInteger)) 250 { 251 throw new SNMPBadValueException("Bad Trap PDU: bad specific trap code"); 252 } 253 254 if (!(contents.elementAt(4) instanceof SNMPTimeTicks)) 255 { 256 throw new SNMPBadValueException("Bad Trap PDU: bad timestamp"); 257 } 258 259 if (!(contents.elementAt(5) instanceof SNMPSequence)) 260 { 261 throw new SNMPBadValueException("Bad Trap PDU: bad variable binding list"); 262 } 263 264 SNMPSequence varBindList = this.getVarBindList(); 267 for (int i = 0; i < varBindList.size(); i++) 268 { 269 SNMPObject element = varBindList.getSNMPObjectAt(i); 270 271 if (!(element instanceof SNMPSequence)) 273 { 274 throw new SNMPBadValueException("Bad Trap PDU: bad variable binding at index" + i); 275 } 276 277 SNMPSequence varBind = (SNMPSequence)element; 279 if ((varBind.size() != 2) || !(varBind.getSNMPObjectAt(0) instanceof SNMPObjectIdentifier)) 280 { 281 throw new SNMPBadValueException("Bad Trap PDU: bad variable binding at index" + i); 282 } 283 } 284 } 285 286 287 288 289 295 296 public SNMPSequence getVarBindList() 297 { 298 Vector contents = (Vector)(this.getValue()); 299 return (SNMPSequence)(contents.elementAt(5)); 300 } 301 302 303 304 307 308 public SNMPObjectIdentifier getEnterpriseOID() 309 { 310 Vector contents = (Vector)(this.getValue()); 311 return (SNMPObjectIdentifier)contents.elementAt(0); 312 } 313 314 315 316 319 320 public SNMPIPAddress getAgentAddress() 321 { 322 Vector contents = (Vector)(this.getValue()); 323 return (SNMPIPAddress)contents.elementAt(1); 324 } 325 326 327 328 331 332 public int getGenericTrap() 333 { 334 Vector contents = (Vector)(this.getValue()); 335 return ((BigInteger)((SNMPInteger)(contents.elementAt(2))).getValue()).intValue(); 336 } 337 338 339 342 343 public int getSpecificTrap() 344 { 345 Vector contents = (Vector)(this.getValue()); 346 return ((BigInteger)((SNMPInteger)(contents.elementAt(3))).getValue()).intValue(); 347 } 348 349 350 353 354 public long getTimestamp() 355 { 356 Vector contents = (Vector)(this.getValue()); 357 return ((BigInteger)((SNMPTimeTicks)(contents.elementAt(4))).getValue()).longValue(); 358 } 359 360 361 } | Popular Tags |