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 package uk.co.westhawk.snmp.stack;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 import java.io.*;
52
53 import uk.co.westhawk.snmp.event.*;
54 import uk.co.westhawk.snmp.beans.*;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public class SnmpContextv3 extends SnmpContextv3Basis {
87 private static final String version_id = "@(#)$Id: SnmpContextv3.java,v 3.31 2009/03/05 13:12:50 birgita Exp $ Copyright Westhawk Ltd";
88
89 private UsmBeingDiscoveredBean myDiscBean = null;
90
91
92
93
94
95
96
97
98 public SnmpContextv3(String host, int port) throws IOException {
99 super(host, port);
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113 public SnmpContextv3(String host, int port, String typeSocketA)
114 throws IOException {
115 super(host, port, typeSocketA);
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 public SnmpContextv3(String host, int port, String bindAddress, String typeSocketA)
134 throws IOException {
135 super(host, port, bindAddress, typeSocketA);
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 public void addRequestPduListener(RequestPduListener l, ListeningContextPool lcontext)
157 throws IOException {
158 super.addRequestPduListener(l, lcontext);
159
160 if (myDiscBean == null) {
161 myDiscBean = new UsmBeingDiscoveredBean(this, usmAgent);
162 }
163 myDiscBean.addRequestPduListener(lcontext);
164 }
165
166
167
168
169
170
171
172
173
174 public void removeRequestPduListener(RequestPduListener l, ListeningContextPool lcontext)
175 throws IOException {
176 super.removeRequestPduListener(l, lcontext);
177 if (myDiscBean != null) {
178 myDiscBean.removeRequestPduListener(lcontext);
179 myDiscBean.freeResources();
180 myDiscBean = null;
181 }
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 public Pdu processIncomingPdu(byte[] message)
202 throws DecodingException, IOException {
203 String msg = checkContextSanity();
204 if (msg != null) {
205 throw new DecodingException(msg);
206 }
207 int l = message.length;
208 byte[] copyOfMessage1 = new byte[l];
209 byte[] copyOfMessage2 = new byte[l];
210 System.arraycopy(message, 0, copyOfMessage1, 0, l);
211 System.arraycopy(message, 0, copyOfMessage2, 0, l);
212
213 AsnDecoderv3 rpdu = new AsnDecoderv3();
214 ByteArrayInputStream in = new ByteArrayInputStream(message);
215 AsnSequence asnTopSeq = rpdu.DecodeSNMPv3(in);
216 int messageId = rpdu.getMessageId(asnTopSeq);
217
218 Pdu pdu = null;
219
220 DecodingException encryptionDecodingException1 = null;
221 IOException encryptionIOException1 = null;
222 try {
223 pdu = processPotentialTrap(rpdu, asnTopSeq, copyOfMessage1);
224 } catch (DecodingException exc) {
225 encryptionDecodingException1 = exc;
226 if (AsnObject.debug > 3) {
227 System.out.println(getClass().getName()
228 + ".processPotentialTrap(): DecodingException: "
229 + exc.getMessage());
230 }
231 } catch (IOException exc) {
232 encryptionIOException1 = exc;
233 if (AsnObject.debug > 3) {
234 System.out.println(getClass().getName()
235 + ".processPotentialTrap(): IOException: "
236 + exc.getMessage());
237 }
238 }
239
240 DecodingException encryptionDecodingException2 = null;
241 IOException encryptionIOException2 = null;
242 if (pdu == null) {
243 try {
244 pdu = processPotentialRequest(rpdu, asnTopSeq, copyOfMessage2);
245 } catch (DecodingException exc) {
246 encryptionDecodingException2 = exc;
247 if (AsnObject.debug > 3) {
248 System.out.println(getClass().getName()
249 + ".processPotentialRequest(): DecodingException: "
250 + exc.getMessage());
251 }
252 } catch (IOException exc) {
253 encryptionIOException2 = exc;
254 if (AsnObject.debug > 3) {
255 System.out.println(getClass().getName()
256 + ".processPotentialRequest(): IOException: "
257 + exc.getMessage());
258 }
259 }
260 }
261
262 if (pdu != null) {
263 pdu.snmpv3MsgId = new Integer(messageId);
264 } else {
265 if (encryptionIOException2 != null) {
266 throw encryptionIOException2;
267 }
268 if (encryptionDecodingException2 != null) {
269 throw encryptionDecodingException2;
270 }
271 if (encryptionIOException1 != null) {
272 throw encryptionIOException1;
273 }
274 if (encryptionDecodingException1 != null) {
275 throw encryptionDecodingException1;
276 }
277 }
278 return pdu;
279 }
280
281
282
283
284
285
286
287
288
289
290
291 public Pdu processPotentialTrap(AsnDecoderv3 rpdu, AsnSequence asnTopSeq,
292 byte[] message)
293 throws DecodingException, IOException {
294
295 AsnPduSequence pduSeq = rpdu.processSNMPv3(this, asnTopSeq, message, false);
296 Pdu pdu = null;
297 if (pduSeq != null) {
298 byte type = pduSeq.getRespType();
299 if (type == SnmpConstants.TRPV2_REQ_MSG) {
300 pdu = new TrapPduv2(this);
301 pdu.fillin(pduSeq);
302
303 if (AsnObject.debug > 3) {
304 System.out.println(getClass().getName()
305 + ".processPotentialTrap(): PDU received with type "
306 + pduSeq.getRespTypeString()
307 + ". Not ignoring it!");
308 }
309 } else {
310 if (AsnObject.debug > 3) {
311 System.out.println(getClass().getName()
312 + ".processPotentialTrap(): PDU received is not TRPV2_REQ_MSG"
313 + ". Ignoring it.");
314 }
315 }
316 } else {
317 if (AsnObject.debug > 3) {
318 System.out.println(getClass().getName()
319 + ".processPotentialTrap(): pduSeq == null"
320 + ". Ignoring it.");
321 }
322 }
323 return pdu;
324 }
325
326
327
328
329
330
331
332
333
334
335
336 public Pdu processPotentialRequest(AsnDecoderv3 rpdu, AsnSequence asnTopSeq,
337 byte[] message)
338 throws DecodingException, IOException {
339
340 AsnPduSequence pduSeq = rpdu.processSNMPv3(this, asnTopSeq, message, true);
341 Pdu pdu = null;
342 if (pduSeq != null) {
343 byte type = pduSeq.getRespType();
344 if (type == SnmpConstants.GET_REQ_MSG && pduSeq.isSnmpv3Discovery() == true) {
345 if (AsnObject.debug > 3) {
346 System.out.println(getClass().getName()
347 + ".ProcessIncomingPdu(): received discovery pdu"
348 + ". Ignoring it.");
349 }
350 } else {
351 switch (type) {
352 case SnmpConstants.GET_REQ_MSG:
353 pdu = new GetPdu(this);
354 pdu.fillin(pduSeq);
355 break;
356 case SnmpConstants.GETNEXT_REQ_MSG:
357 pdu = new GetNextPdu(this);
358 pdu.fillin(pduSeq);
359 break;
360 case SnmpConstants.SET_REQ_MSG:
361 pdu = new SetPdu(this);
362 pdu.fillin(pduSeq);
363 break;
364 case SnmpConstants.GETBULK_REQ_MSG:
365 pdu = new GetBulkPdu(this);
366 pdu.fillin(pduSeq);
367 break;
368 case SnmpConstants.INFORM_REQ_MSG:
369 pdu = new InformPdu(this);
370 pdu.fillin(pduSeq);
371 break;
372
373
374
375
376
377
378
379
380
381 default:
382 if (AsnObject.debug > 3) {
383 System.out.println(getClass().getName()
384 + ".processPotentialRequest(): PDU received with type "
385 + pduSeq.getRespTypeString()
386 + ". Ignoring it.");
387 }
388 }
389
390 if (pdu != null) {
391 if (AsnObject.debug > 3) {
392 System.out.println(getClass().getName()
393 + ".processPotentialRequest(): PDU received with type "
394 + pduSeq.getRespTypeString()
395 + ". Not ignoring it!");
396 }
397 }
398 }
399 } else {
400 if (AsnObject.debug > 3) {
401 System.out.println(getClass().getName()
402 + "..processPotentialRequest(): pduSeq == null"
403 + ". Ignoring it.");
404 }
405 }
406 return pdu;
407 }
408
409
410
411
412
413
414
415 public Object clone() throws CloneNotSupportedException {
416 SnmpContextv3 clContext = null;
417 try {
418 clContext = new SnmpContextv3(hostname, hostPort, bindAddr, typeSocket);
419 clContext = (SnmpContextv3) cloneParameters(clContext);
420 } catch (IOException exc) {
421 throw new CloneNotSupportedException("IOException "
422 + exc.getMessage());
423 }
424 return clContext;
425 }
426
427 }