001: /*_############################################################################
002: _##
003: _## SNMP4J - StateReference.java
004: _##
005: _## Copyright (C) 2003-2008 Frank Fock and Jochen Katz (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.mp;
022:
023: import java.util.*;
024:
025: import org.snmp4j.*;
026: import org.snmp4j.security.*;
027: import org.snmp4j.smi.*;
028: import java.io.Serializable;
029:
030: /**
031: * The <code>StateReference</code> class represents state information associated
032: * with SNMP messages. The state reference is used to send response or report
033: * (SNMPv3 only). Depending on the security model not all fields may be filled.
034: *
035: * @author Frank Fock
036: * @version 1.2
037: */
038: public class StateReference implements Serializable {
039:
040: private static final long serialVersionUID = 7385215386971310699L;
041:
042: private Address address;
043: private transient TransportMapping transportMapping;
044: private byte[] contextEngineID;
045: private byte[] contextName;
046: private SecurityModel securityModel;
047: private byte[] securityName;
048: private int securityLevel;
049: private SecurityStateReference securityStateReference;
050: private int msgID;
051: private int maxSizeResponseScopedPDU;
052: private int msgFlags;
053: private PduHandle pduHandle;
054: private byte[] securityEngineID;
055: private int errorCode = 0;
056:
057: /**
058: * Default constructor.
059: */
060: public StateReference() {
061: }
062:
063: /**
064: * Creates a state reference for community based security models.
065: * @param pduHandle PduHandle
066: * @param peerAddress Address
067: * @param peerTransport
068: * the <code>TransportMapping</code> to be used to communicate with the
069: * peer.
070: * @param secModel SecurityModel
071: * @param secName
072: * a community string.
073: * @param errorCode
074: * an error code associated with the SNMP message.
075: */
076: public StateReference(PduHandle pduHandle, Address peerAddress,
077: TransportMapping peerTransport, SecurityModel secModel,
078: byte[] secName, int errorCode) {
079: this (0, 0, 65535, pduHandle, peerAddress, peerTransport, null,
080: secModel, secName, SecurityLevel.NOAUTH_NOPRIV, null,
081: null, null, errorCode);
082: }
083:
084: /**
085: * Creates a state reference for SNMPv3 messages.
086: * @param msgID int
087: * @param msgFlags int
088: * @param maxSizeResponseScopedPDU int
089: * @param pduHandle PduHandle
090: * @param peerAddress Address
091: * @param peerTransport
092: * the <code>TransportMapping</code> to be used to communicate with the
093: * peer.
094: * @param secEngineID byte[]
095: * @param secModel SecurityModel
096: * @param secName byte[]
097: * @param secLevel int
098: * @param contextEngineID byte[]
099: * @param contextName byte[]
100: * @param secStateReference SecurityStateReference
101: * @param errorCode int
102: */
103: public StateReference(int msgID, int msgFlags,
104: int maxSizeResponseScopedPDU, PduHandle pduHandle,
105: Address peerAddress, TransportMapping peerTransport,
106: byte[] secEngineID, SecurityModel secModel, byte[] secName,
107: int secLevel, byte[] contextEngineID, byte[] contextName,
108: SecurityStateReference secStateReference, int errorCode) {
109: this .msgID = msgID;
110: this .msgFlags = msgFlags;
111: this .maxSizeResponseScopedPDU = maxSizeResponseScopedPDU;
112: this .pduHandle = pduHandle;
113: this .address = peerAddress;
114: this .transportMapping = peerTransport;
115: this .securityEngineID = secEngineID;
116: this .securityModel = secModel;
117: this .securityName = secName;
118: this .securityLevel = secLevel;
119: this .contextEngineID = contextEngineID;
120: this .contextName = contextName;
121: this .securityStateReference = secStateReference;
122: this .errorCode = errorCode;
123: }
124:
125: public boolean isReportable() {
126: return ((msgFlags & 0x04) > 0);
127: }
128:
129: public Address getAddress() {
130: return address;
131: }
132:
133: public void setAddress(org.snmp4j.smi.Address address) {
134: this .address = address;
135: }
136:
137: public void setContextEngineID(byte[] contextEngineID) {
138: this .contextEngineID = contextEngineID;
139: }
140:
141: public byte[] getContextEngineID() {
142: return contextEngineID;
143: }
144:
145: public void setContextName(byte[] contextName) {
146: this .contextName = contextName;
147: }
148:
149: public byte[] getContextName() {
150: return contextName;
151: }
152:
153: public void setSecurityModel(SecurityModel securityModel) {
154: this .securityModel = securityModel;
155: }
156:
157: public SecurityModel getSecurityModel() {
158: return securityModel;
159: }
160:
161: public void setSecurityName(byte[] securityName) {
162: this .securityName = securityName;
163: }
164:
165: public byte[] getSecurityName() {
166: return securityName;
167: }
168:
169: public void setSecurityLevel(int securityLevel) {
170: this .securityLevel = securityLevel;
171: }
172:
173: public int getSecurityLevel() {
174: return securityLevel;
175: }
176:
177: public void setSecurityStateReference(
178: SecurityStateReference securityStateReference) {
179: this .securityStateReference = securityStateReference;
180: }
181:
182: public SecurityStateReference getSecurityStateReference() {
183: return securityStateReference;
184: }
185:
186: public void setMsgID(int msgID) {
187: this .msgID = msgID;
188: }
189:
190: public int getMsgID() {
191: return msgID;
192: }
193:
194: public void setMsgFlags(int msgFlags) {
195: this .msgFlags = msgFlags;
196: }
197:
198: public int getMsgFlags() {
199: return msgFlags;
200: }
201:
202: public void setMaxSizeResponseScopedPDU(int maxSizeResponseScopedPDU) {
203: this .maxSizeResponseScopedPDU = maxSizeResponseScopedPDU;
204: }
205:
206: public int getMaxSizeResponseScopedPDU() {
207: return maxSizeResponseScopedPDU;
208: }
209:
210: public PduHandle getPduHandle() {
211: return pduHandle;
212: }
213:
214: public byte[] getSecurityEngineID() {
215: return securityEngineID;
216: }
217:
218: public int getErrorCode() {
219: return errorCode;
220: }
221:
222: public TransportMapping getTransportMapping() {
223: return transportMapping;
224: }
225:
226: public void setPduHandle(PduHandle pduHandle) {
227: this .pduHandle = pduHandle;
228: }
229:
230: public void setSecurityEngineID(byte[] securityEngineID) {
231: this .securityEngineID = securityEngineID;
232: }
233:
234: public void setErrorCode(int errorCode) {
235: this .errorCode = errorCode;
236: }
237:
238: public void setTransportMapping(TransportMapping transportMapping) {
239: this .transportMapping = transportMapping;
240: }
241:
242: public boolean equals(Object o) {
243: if (o instanceof StateReference) {
244: StateReference other = (StateReference) o;
245: return ((msgID == other.msgID)
246: && ((pduHandle == null) && (other.pduHandle == null)) || (pduHandle != null)
247: && (pduHandle.equals(other.getPduHandle()))
248: && (Arrays.equals(securityEngineID,
249: other.securityEngineID))
250: && (securityModel.equals(other.securityModel))
251: && (Arrays.equals(securityName, other.securityName))
252: && (securityLevel == other.securityLevel)
253: && (Arrays.equals(contextEngineID,
254: other.contextEngineID))
255: && (Arrays.equals(contextName, other.contextName)));
256: }
257: return false;
258: }
259:
260: public int hashCode() {
261: return msgID;
262: }
263:
264: public String toString() {
265: return "StateReference[msgID=" + msgID + ",pduHandle="
266: + pduHandle + ",securityEngineID="
267: + OctetString.fromByteArray(securityEngineID)
268: + ",securityModel=" + securityModel + ",securityName="
269: + OctetString.fromByteArray(securityName)
270: + ",securityLevel=" + securityLevel
271: + ",contextEngineID="
272: + OctetString.fromByteArray(contextEngineID)
273: + ",contextName="
274: + OctetString.fromByteArray(contextName) + "]";
275: }
276: }
|