KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J - MPv2c.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.security.SecurityModels;
41 import org.snmp4j.security.SecurityLevel;
42 import org.snmp4j.asn1.BEROutputStream;
43 import java.nio.ByteBuffer JavaDoc;
44
45 /**
46  * The <code>MPv2c</code> is the message processing model for SNMPv2c
47  * (community based SNMPv2).
48  *
49  * @author Frank Fock
50  * @version 1.0
51  */

52 public class MPv2c implements MessageProcessingModel {
53
54   public static final int ID = MessageProcessingModel.MPv2c;
55   private static final LogAdapter logger = LogFactory.getLogger(MPv2c.class);
56
57   public MPv2c() {
58   }
59
60   public int getID() {
61     return ID;
62   }
63
64   public int prepareOutgoingMessage(Address transportAddress,
65                                     int maxMessageSize,
66                                     int messageProcessingModel,
67                                     int securityModel,
68                                     byte[] securityName,
69                                     int securityLevel,
70                                     PDU pdu,
71                                     boolean expectResponse,
72                                     PduHandle sendPduHandle,
73                                     Address destTransportAddress,
74                                     BEROutputStream outgoingMessage)
75       throws IOException JavaDoc
76   {
77     if ((securityLevel != SecurityLevel.NOAUTH_NOPRIV) ||
78         (securityModel != SecurityModel.SECURITY_MODEL_SNMPv2c)) {
79       logger.error("MPv2c used with unsupported security model");
80       return SnmpConstants.SNMP_MP_UNSUPPORTED_SECURITY_MODEL;
81     }
82     if (pdu instanceof ScopedPDU) {
83       String JavaDoc txt = "ScopedPDU must not be used with MPv2c";
84       logger.error(txt);
85       throw new IllegalArgumentException JavaDoc(txt);
86     }
87
88     if (!isProtocolVersionSupported(messageProcessingModel)) {
89       logger.error("MPv2c used with unsupported SNMP version");
90       return SnmpConstants.SNMP_MP_UNSUPPORTED_SECURITY_MODEL;
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
155     MutableByte mutableByte = new MutableByte();
156     int length = BER.decodeHeader(wholeMsg, mutableByte);
157     int startPos = (int)wholeMsg.getPosition();
158
159     if (mutableByte.getValue() != BER.SEQUENCE) {
160       String JavaDoc txt = "SNMPv2c PDU must start with a SEQUENCE";
161       logger.error(txt);
162       throw new IOException JavaDoc(txt);
163     }
164     Integer32 version = new Integer32();
165     version.decodeBER(wholeMsg);
166
167     securityName.decodeBER(wholeMsg);
168     securityLevel.setValue(SecurityLevel.NOAUTH_NOPRIV);
169     securityModel.setValue(SecurityModel.SECURITY_MODEL_SNMPv2c);
170     messageProcessingModel.setValue(ID);
171
172     PDU v2cPDU = new PDU();
173     pdu.setPdu(v2cPDU);
174     v2cPDU.decodeBER(wholeMsg);
175
176     BER.checkSequenceLength(length,
177                             (int)wholeMsg.getPosition() - startPos,
178                             v2cPDU);
179
180     sendPduHandle.setTransactionID(v2cPDU.getRequestID().getValue());
181
182     // create state reference
183
StateReference stateRef =
184         new StateReference(sendPduHandle,
185                            transportAddress,
186                            null,
187                            SecurityModels.getInstance().getSecurityModel(securityModel),
188                            securityName.getValue(),
189                            SnmpConstants.SNMP_ERROR_SUCCESS);
190     stateReference.setStateReference(stateRef);
191
192     return SnmpConstants.SNMP_MP_OK;
193   }
194
195   public boolean isProtocolVersionSupported(int snmpProtocolVersion) {
196     return (snmpProtocolVersion == SnmpConstants.version2c);
197   }
198
199   public void releaseStateReference(PduHandle pduHandle) {
200     // we do not cache state information -> do nothing
201
}
202 }
203
Popular Tags