1 29 30 31 package snmp; 32 33 import java.util.*; 34 import java.math.*; 35 36 37 38 39 123 124 125 public class SNMPPDU extends SNMPSequence 126 { 127 128 129 133 134 public SNMPPDU(byte pduType, int requestID, int errorStatus, int errorIndex, SNMPSequence varList) 135 throws SNMPBadValueException 136 { 137 super(); 138 Vector contents = new Vector(); 139 tag = pduType; 140 contents.insertElementAt(new SNMPInteger(requestID), 0); 141 contents.insertElementAt(new SNMPInteger(errorStatus), 1); 142 contents.insertElementAt(new SNMPInteger(errorIndex), 2); 143 contents.insertElementAt(varList, 3); 144 this.setValue(contents); 145 } 146 147 148 149 150 154 155 protected SNMPPDU(byte[] enc, byte pduType) 156 throws SNMPBadValueException 157 { 158 tag = pduType; 159 extractFromBEREncoding(enc); 160 161 Vector contents = (Vector)(this.getValue()); 163 164 if (contents.size() != 4) 165 { 166 throw new SNMPBadValueException("Bad PDU"); 167 } 168 169 if (!(contents.elementAt(0) instanceof SNMPInteger)) 170 { 171 throw new SNMPBadValueException("Bad PDU: bad request ID"); 172 } 173 174 if (!(contents.elementAt(1) instanceof SNMPInteger)) 175 { 176 throw new SNMPBadValueException("Bad PDU: bad error status"); 177 } 178 179 if (!(contents.elementAt(2) instanceof SNMPInteger)) 180 { 181 throw new SNMPBadValueException("Bad PDU: bad error index"); 182 } 183 184 if (!(contents.elementAt(3) instanceof SNMPSequence)) 185 { 186 throw new SNMPBadValueException("Bad PDU: bad variable binding list"); 187 } 188 189 SNMPSequence varBindList = this.getVarBindList(); 192 for (int i = 0; i < varBindList.size(); i++) 193 { 194 SNMPObject element = varBindList.getSNMPObjectAt(i); 195 196 if (!(element instanceof SNMPSequence)) 198 { 199 throw new SNMPBadValueException("Bad PDU: bad variable binding at index" + i); 200 } 201 202 SNMPSequence varBind = (SNMPSequence)element; 204 if ((varBind.size() != 2) || !(varBind.getSNMPObjectAt(0) instanceof SNMPObjectIdentifier)) 205 { 206 throw new SNMPBadValueException("Bad PDU: bad variable binding at index" + i); 207 } 208 } 209 210 211 } 212 213 214 215 216 222 223 public SNMPSequence getVarBindList() 224 { 225 Vector contents = (Vector)(this.getValue()); 226 return (SNMPSequence)(contents.elementAt(3)); 227 } 228 229 230 231 234 235 public int getRequestID() 236 { 237 Vector contents = (Vector)(this.getValue()); 238 return ((BigInteger)((SNMPInteger)(contents.elementAt(0))).getValue()).intValue(); 239 } 240 241 242 243 247 248 public int getErrorStatus() 249 { 250 Vector contents = (Vector)(this.getValue()); 251 return ((BigInteger)((SNMPInteger)(contents.elementAt(1))).getValue()).intValue(); 252 } 253 254 255 256 259 260 public int getErrorIndex() 261 { 262 Vector contents = (Vector)(this.getValue()); 263 return ((BigInteger)((SNMPInteger)(contents.elementAt(2))).getValue()).intValue(); 264 } 265 266 267 268 271 272 public byte getPDUType() 273 { 274 return tag; 275 } 276 277 278 } | Popular Tags |