KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > mp > MPv1


1 /*_############################################################################
2   _##
3   _## SNMP4J - MPv1.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21
22
23
24
25 package org.snmp4j.mp;
26
27 import org.snmp4j.MessageDispatcher;
28 import org.snmp4j.smi.Address;
29 import org.snmp4j.asn1.BERInputStream;
30 import org.snmp4j.smi.Integer32;
31 import org.snmp4j.smi.OctetString;
32 import org.snmp4j.MutablePDU;
33 import java.io.IOException JavaDoc;
34 import org.snmp4j.PDU;
35 import org.snmp4j.log.*;
36 import org.snmp4j.ScopedPDU;
37 import org.snmp4j.asn1.BER;
38 import org.snmp4j.security.SecurityModel;
39 import org.snmp4j.asn1.BER.MutableByte;
40 import org.snmp4j.PDUv1;
41 import org.snmp4j.security.SecurityModels;
42 import org.snmp4j.security.SecurityLevel;
43 import org.snmp4j.asn1.BEROutputStream;
44 import java.nio.ByteBuffer JavaDoc;
45
46 /**
47  * The <code>MPv1</code> is the message processing model for SNMPv1.
48  * @author Frank Fock
49  * @version 1.0
50  */

51 public class MPv1 implements MessageProcessingModel {
52
53   public static final int ID = MessageProcessingModel.MPv1;
54   private static final LogAdapter logger = LogFactory.getLogger(MPv1.class);
55
56   public MPv1() {
57   }
58
59   public int getID() {
60     return ID;
61   }
62
63   public int prepareOutgoingMessage(Address transportAddress,
64                                     int maxMessageSize,
65                                     int messageProcessingModel,
66                                     int securityModel,
67                                     byte[] securityName,
68                                     int securityLevel,
69                                     PDU pdu,
70                                     boolean expectResponse,
71                                     PduHandle sendPduHandle,
72                                     Address destTransportAddress,
73                                     BEROutputStream outgoingMessage)
74       throws IOException JavaDoc
75   {
76     if ((securityLevel != SecurityLevel.NOAUTH_NOPRIV) ||
77         (securityModel != SecurityModel.SECURITY_MODEL_SNMPv1)) {
78       logger.error("MPv1 used with unsupported security model");
79       return SnmpConstants.SNMP_MP_UNSUPPORTED_SECURITY_MODEL;
80     }
81     if (pdu instanceof ScopedPDU) {
82       String JavaDoc txt = "ScopedPDU must not be used with MPv1";
83       logger.error(txt);
84       throw new IllegalArgumentException JavaDoc(txt);
85     }
86
87     if (!isProtocolVersionSupported(messageProcessingModel)) {
88       logger.error("MPv1 used with unsupported SNMP version");
89       return SnmpConstants.SNMP_MP_UNSUPPORTED_SECURITY_MODEL;
90     }
91
92
93     OctetString community = new OctetString(securityName);
94     Integer32 version = new Integer32(messageProcessingModel);
95     // compute total length
96
int length = pdu.getBERLength();
97     length += community.getBERLength();
98     length += version.getBERLength();
99
100     ByteBuffer JavaDoc buf = ByteBuffer.allocate(length +
101                                          BER.getBERLengthOfLength(length) + 1);
102     // set the buffer of the outgoing message
103
outgoingMessage.setBuffer(buf);
104
105     // encode the message
106
BER.encodeHeader(outgoingMessage, BER.SEQUENCE, length);
107     version.encodeBER(outgoingMessage);
108
109     community.encodeBER(outgoingMessage);
110     pdu.encodeBER(outgoingMessage);
111
112     return SnmpConstants.SNMP_MP_OK;
113   }
114
115   public int prepareResponseMessage(int messageProcessingModel,
116                                     int maxMessageSize,
117                                     int securityModel,
118                                     byte[] securityName,
119                                     int securityLevel,
120                                     PDU pdu,
121                                     int maxSizeResponseScopedPDU,
122                                     StateReference stateReference,
123                                     StatusInformation statusInformation,
124                                     BEROutputStream outgoingMessage)
125       throws IOException JavaDoc
126   {
127     return prepareOutgoingMessage(stateReference.getAddress(),
128                                   maxMessageSize,
129                                   messageProcessingModel,
130                                   securityModel,
131                                   securityName,
132                                   securityLevel,
133                                   pdu,
134                                   false,
135                                   stateReference.getPduHandle(),
136                                   null,
137                                   outgoingMessage);
138   }
139
140   public int prepareDataElements(MessageDispatcher messageDispatcher,
141                                  Address transportAddress,
142                                  BERInputStream wholeMsg,
143                                  Integer32 messageProcessingModel,
144                                  Integer32 securityModel,
145                                  OctetString securityName,
146                                  Integer32 securityLevel,
147                                  MutablePDU pdu,
148                                  PduHandle sendPduHandle,
149                                  Integer32 maxSizeResponseScopedPDU,
150                                  StatusInformation statusInformation,
151                                  MutableStateReference stateReference)
152       throws IOException JavaDoc
153   {
154     MutableByte mutableByte = new MutableByte();
155     int length = BER.decodeHeader(wholeMsg, mutableByte);
156     int startPos = (int)wholeMsg.getPosition();
157     if (mutableByte.getValue() != BER.SEQUENCE) {
158       String JavaDoc txt = "SNMPv1 PDU must start with a SEQUENCE";
159       logger.error(txt);
160       throw new IOException JavaDoc(txt);
161     }
162     Integer32 version = new Integer32();
163     version.decodeBER(wholeMsg);
164
165     securityName.decodeBER(wholeMsg);
166     securityLevel.setValue(SecurityLevel.NOAUTH_NOPRIV);
167     securityModel.setValue(SecurityModel.SECURITY_MODEL_SNMPv1);
168     messageProcessingModel.setValue(ID);
169
170     PDUv1 v1PDU = new PDUv1();
171     pdu.setPdu(v1PDU);
172     v1PDU.decodeBER(wholeMsg);
173
174     BER.checkSequenceLength(length, (int)wholeMsg.getPosition() - startPos,
175                             v1PDU);
176
177     sendPduHandle.setTransactionID(v1PDU.getRequestID().getValue());
178
179     // create state reference
180
StateReference stateRef =
181         new StateReference(sendPduHandle,
182                            transportAddress,
183                            null,
184                            SecurityModels.getInstance().getSecurityModel(securityModel),
185                            securityName.getValue(),
186                            SnmpConstants.SNMP_ERROR_SUCCESS);
187     stateReference.setStateReference(stateRef);
188
189     return SnmpConstants.SNMP_MP_OK;
190   }
191
192   public boolean isProtocolVersionSupported(int snmpProtocolVersion) {
193     return (snmpProtocolVersion == SnmpConstants.version1);
194   }
195
196   public void releaseStateReference(PduHandle pduHandle) {
197     // we do not cache state information -> do nothing
198
}
199
200 }
201
Popular Tags