1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package uk.co.westhawk.snmp.stack;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 import java.net.*;
53 import java.io.*;
54 import java.util.*;
55 import uk.co.westhawk.snmp.event.*;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public class SnmpContextv2c extends SnmpContext
73 implements SnmpContextv2cFace, Cloneable {
74 private static final String version_id = "@(#)$Id: SnmpContextv2c.java,v 3.16 2009/03/05 12:54:04 birgita Exp $ Copyright Westhawk Ltd";
75
76
77
78
79
80
81
82
83 public SnmpContextv2c(String host, int port) throws IOException {
84 super(host, port);
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98 public SnmpContextv2c(String host, int port, String typeSocketA)
99 throws IOException {
100 super(host, port, typeSocketA);
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 public SnmpContextv2c(String host, int port, String bindAddress, String typeSocketA)
119 throws IOException {
120 super(host, port, bindAddress, typeSocketA);
121 }
122
123 public int getVersion() {
124 return SnmpConstants.SNMP_VERSION_2c;
125 }
126
127 public byte[] encodePacket(byte msg_type, int rId, int errstat,
128 int errind, Enumeration ve, Object obj)
129 throws IOException, EncodingException {
130 byte[] packet = null;
131 if (isDestroyed == true) {
132 throw new EncodingException("Context can no longer be used, since it is already destroyed");
133 } else {
134 AsnEncoderv2c enc = new AsnEncoderv2c();
135 ByteArrayOutputStream bay = enc.EncodeSNMPv2c(this, msg_type, rId, errstat,
136 errind, ve);
137
138 int sz = bay.size();
139 if (sz > maxRecvSize) {
140 throw new EncodingException("Packet size (" + sz
141 + ") is > maximum size (" + maxRecvSize + ")");
142 }
143 packet = bay.toByteArray();
144 }
145 return packet;
146 }
147
148
149
150
151 protected void processIncomingResponse(ByteArrayInputStream in)
152 throws DecodingException, IOException {
153 AsnDecoderv2c rpdu = new AsnDecoderv2c();
154 AsnPduSequence pduSeq = rpdu.DecodeSNMPv2c(in, getCommunity());
155 if (pduSeq != null) {
156
157 Integer rid = new Integer(pduSeq.getReqId());
158 Pdu answ = getPdu(rid);
159 if (answ != null) {
160 answ.fillin(pduSeq);
161 } else {
162 if (AsnObject.debug > 3) {
163 System.out.println(
164 getClass().getName() + ".processIncomingResponse(): No Pdu with reqid " + rid.intValue());
165 }
166 }
167 } else {
168 if (AsnObject.debug > 3) {
169 System.out.println(getClass().getName() + ".processIncomingResponse(): Error - missing seq input");
170 }
171 }
172 }
173
174 public Pdu processIncomingPdu(byte[] message)
175 throws DecodingException, IOException {
176 AsnDecoderv2c rpdu = new AsnDecoderv2c();
177 ByteArrayInputStream in = new ByteArrayInputStream(message);
178 AsnPduSequence pduSeq = rpdu.DecodeSNMPv2c(in, getCommunity());
179
180 Pdu pdu = null;
181 if (pduSeq != null) {
182 byte type = pduSeq.getRespType();
183 switch (type) {
184 case SnmpConstants.GET_REQ_MSG:
185 pdu = new GetPdu(this);
186 break;
187 case SnmpConstants.GETNEXT_REQ_MSG:
188 pdu = new GetNextPdu(this);
189 break;
190 case SnmpConstants.SET_REQ_MSG:
191 pdu = new SetPdu(this);
192 break;
193 case SnmpConstants.GETBULK_REQ_MSG:
194 pdu = new GetBulkPdu(this);
195 break;
196 case SnmpConstants.INFORM_REQ_MSG:
197 pdu = new InformPdu(this);
198 break;
199
200
201
202
203
204
205 case SnmpConstants.TRPV2_REQ_MSG:
206 pdu = new TrapPduv2(this);
207 break;
208 default:
209 if (AsnObject.debug > 3) {
210 System.out.println(getClass().getName()
211 + ".ProcessIncomingPdu(): PDU received with type "
212 + pduSeq.getRespTypeString()
213 + ". Ignoring it.");
214 }
215 }
216
217 if (pdu != null) {
218 pdu.fillin(pduSeq);
219 }
220 }
221 return pdu;
222 }
223
224
225
226
227
228
229
230 public Object clone() throws CloneNotSupportedException {
231 SnmpContextv2c clContext = null;
232 try {
233 clContext = new SnmpContextv2c(hostname, hostPort, bindAddr, typeSocket);
234 clContext.setCommunity(new String(community));
235 } catch (IOException exc) {
236 throw new CloneNotSupportedException("IOException "
237 + exc.getMessage());
238 }
239 return clContext;
240 }
241
242
243
244
245
246
247
248
249 public String getHashKey() {
250 return super.getHashKey();
251 }
252
253
254
255
256
257
258 public String toString() {
259 StringBuffer buffer = new StringBuffer("SnmpContextv2c[");
260 buffer.append("host=").append(hostname);
261 buffer.append(", port=").append(hostPort);
262 buffer.append(", bindAddress=").append(bindAddr);
263 buffer.append(", socketType=").append(typeSocket);
264 buffer.append(", community=").append(community);
265 buffer.append(", #trapListeners=").append(trapSupport.getListenerCount());
266 buffer.append(", #pduListeners=").append(pduSupport.getListenerCount());
267 buffer.append("]");
268 return buffer.toString();
269 }
270
271 }