1 20 21 package org.snmp4j; 22 23 import java.io.*; 24 import java.util.*; 25 import org.snmp4j.asn1.*; 26 import org.snmp4j.asn1.BER.*; 27 import org.snmp4j.smi.*; 28 import org.snmp4j.smi.OID; 29 import org.snmp4j.mp.SnmpConstants; 31 32 50 public class PDUv1 extends PDU { 51 52 private static final long serialVersionUID = -6478805117911347898L; 53 54 60 public static final int COLDSTART = 0; 61 62 68 public static final int WARMSTART = 1; 69 70 78 public static final int LINKDOWN = 2; 79 80 88 public static final int LINKUP = 3; 89 90 98 public static final int AUTHENTICATIONFAILURE = 4; 99 100 104 public static final int ENTERPRISE_SPECIFIC = 6; 105 106 private static final String OPERATION_NOT_SUPPORTED = 107 "Operation not supported for SNMPv1 PDUs"; 108 109 private OID enterprise = new OID(); 110 private IpAddress agentAddress = new IpAddress("0.0.0.0"); 111 private Integer32 genericTrap = new Integer32(0); 112 private Integer32 specificTrap = new Integer32(0); 113 private TimeTicks timestamp = new TimeTicks(0); 114 115 116 public PDUv1() { 117 setType(V1TRAP); 118 } 119 120 127 public void decodeBER(BERInputStream inputStream) throws IOException { 128 MutableByte pduType = new MutableByte(); 129 int length = BER.decodeHeader(inputStream, pduType); 130 int pduStartPos = (int)inputStream.getPosition(); 131 132 switch (pduType.getValue()) { 133 case PDU.SET: 134 case PDU.GET: 135 case PDU.GETNEXT: 136 case PDU.V1TRAP: 137 case PDU.RESPONSE: 138 break; 139 default: 140 throw new IOException("Unsupported PDU type: "+pduType.getValue()); 141 } 142 this.setType(pduType.getValue()); 143 if (getType() == PDU.V1TRAP) { 144 enterprise.decodeBER(inputStream); 145 agentAddress.decodeBER(inputStream); 146 genericTrap.decodeBER(inputStream); 147 specificTrap.decodeBER(inputStream); 148 timestamp.decodeBER(inputStream); 149 } 150 else { 151 requestID.decodeBER(inputStream); 152 errorStatus.decodeBER(inputStream); 153 errorIndex.decodeBER(inputStream); 154 155 } 156 pduType = new BER.MutableByte(); 158 int vbLength = BER.decodeHeader(inputStream, pduType); 159 if (pduType.getValue() != BER.SEQUENCE) { 160 throw new IOException("Encountered invalid tag, SEQUENCE expected: "+ 161 pduType.getValue()); 162 } 163 int startPos = (int)inputStream.getPosition(); 165 variableBindings = new Vector(); 166 while (inputStream.getPosition() - startPos < vbLength) { 167 VariableBinding vb = new VariableBinding(); 168 vb.decodeBER(inputStream); 169 if (vb.getSyntax() == SMIConstants.SYNTAX_COUNTER64) { 170 throw new MessageException("Counter64 encountered in SNMPv1 PDU "+ 171 "(RFC 2576 §4.1.2.1)"); 172 } 173 variableBindings.add(vb); 174 } 175 if (BER.isCheckSequenceLength()) { 176 BER.checkSequenceLength(vbLength, 177 (int) inputStream.getPosition() - startPos, this); 178 BER.checkSequenceLength(length, 179 (int) inputStream.getPosition() - pduStartPos, this); 180 } 181 } 182 183 189 public void encodeBER(OutputStream outputStream) throws IOException { 190 BER.encodeHeader(outputStream, type, getBERPayloadLength()); 191 192 if (type == PDU.V1TRAP) { 193 enterprise.encodeBER(outputStream); 194 agentAddress.encodeBER(outputStream); 195 genericTrap.encodeBER(outputStream); 196 specificTrap.encodeBER(outputStream); 197 timestamp.encodeBER(outputStream); 198 } 199 else { 200 requestID.encodeBER(outputStream); 201 errorStatus.encodeBER(outputStream); 202 errorIndex.encodeBER(outputStream); 203 } 204 int vbLength = 0; 205 for (int i=0; i<variableBindings.size(); i++) { 206 vbLength += ((VariableBinding)variableBindings.get(i)).getBERLength(); 207 } 208 BER.encodeHeader(outputStream, BER.SEQUENCE, vbLength); 209 for (int i=0; i<variableBindings.size(); i++) { 210 VariableBinding vb = (VariableBinding)variableBindings.get(i); 211 if (vb.getVariable() instanceof Counter64) { 212 throw new IOException("Cannot encode Counter64 into a SNMPv1 PDU"); 213 } 214 vb.encodeBER(outputStream); 215 } 216 } 217 218 protected int getBERPayloadLengthPDU() { 219 if (getType() != PDU.V1TRAP) { 220 return super.getBERPayloadLengthPDU(); 221 } 222 else { 223 int length = 0; 224 for (int i = 0; i < variableBindings.size(); i++) { 226 length += ((VariableBinding)variableBindings.get(i)).getBERLength(); 227 } 228 length += BER.getBERLengthOfLength(length) + 1; 229 length += agentAddress.getBERLength(); 230 length += enterprise.getBERLength(); 231 length += genericTrap.getBERLength(); 232 length += specificTrap.getBERLength(); 233 length += timestamp.getBERLength(); 234 return length; 235 } 236 } 237 238 245 public int getMaxRepetitions() { 246 throw new UnsupportedOperationException (OPERATION_NOT_SUPPORTED); 247 } 248 249 255 public void setMaxRepetitions(int maxRepetitions) { 256 throw new UnsupportedOperationException (OPERATION_NOT_SUPPORTED); 257 } 258 259 266 public void setMaxSizeScopedPDU(int maxSizeScopedPDU) { 267 throw new UnsupportedOperationException (OPERATION_NOT_SUPPORTED); 268 } 269 270 277 public void setNonRepeaters(int nonRepeaters) { 278 throw new UnsupportedOperationException (OPERATION_NOT_SUPPORTED); 279 } 280 281 private void checkV1TRAP() { 282 if (getType() != PDU.V1TRAP) { 283 throw new UnsupportedOperationException ( 284 "Operation is only supported for SNMPv1 trap PDUs (V1TRAP)"); 285 } 286 } 287 288 300 public OID getEnterprise() { 301 checkV1TRAP(); 302 return enterprise; 303 } 304 305 317 public void setEnterprise(org.snmp4j.smi.OID enterprise) { 318 checkV1TRAP(); 319 checkNull(enterprise); 320 this.enterprise = (OID) enterprise.clone(); 321 } 322 323 334 public org.snmp4j.smi.IpAddress getAgentAddress() { 335 checkV1TRAP(); 336 return agentAddress; 337 } 338 339 348 public void setAgentAddress(org.snmp4j.smi.IpAddress agentAddress) { 349 checkV1TRAP(); 350 checkNull(agentAddress); 351 this.agentAddress = agentAddress; 352 } 353 354 363 public int getGenericTrap() { 364 checkV1TRAP(); 365 return genericTrap.getValue(); 366 } 367 368 377 public void setGenericTrap(int genericTrap) { 378 checkV1TRAP(); 379 this.genericTrap.setValue(genericTrap); 380 } 381 382 390 public int getSpecificTrap() { 391 checkV1TRAP(); 392 return specificTrap.getValue(); 393 } 394 404 public void setSpecificTrap(int specificTrap) { 405 checkV1TRAP(); 406 this.specificTrap.setValue(specificTrap); 407 } 408 409 418 public long getTimestamp() { 419 checkV1TRAP(); 420 return timestamp.getValue(); 421 } 422 423 430 public void setTimestamp(long timeStamp) { 431 checkV1TRAP(); 432 this.timestamp.setValue(timeStamp); 433 } 434 435 441 protected void checkNull(Object parameter) { 442 if (parameter == null) { 443 throw new NullPointerException ("Members of PDUv1 must not be null"); 444 } 445 } 446 447 public String toString() { 448 if (type == PDU.V1TRAP) { 449 StringBuffer buf = new StringBuffer (); 450 buf.append(getTypeString(type)); 451 buf.append("[reqestID="); 452 buf.append(requestID); 453 buf.append(",timestamp="); 454 buf.append(timestamp); 455 buf.append(",enterprise="); 456 buf.append(enterprise); 457 buf.append(",genericTrap="); 458 buf.append(genericTrap); 459 buf.append(",specificTrap="); 460 buf.append(specificTrap); 461 buf.append(", VBS["); 462 for (int i = 0; i < variableBindings.size(); i++) { 463 buf.append(variableBindings.get(i)); 464 if (i + 1 < variableBindings.size()) { 465 buf.append("; "); 466 } 467 } 468 buf.append("]]"); 469 return buf.toString(); 470 } 471 return super.toString(); 472 } 473 474 } 475 | Popular Tags |