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
29
30
31
32
33
34
35
36
37
38
39
40
41 package uk.co.westhawk.snmp.stack;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 import java.net.*;
66 import java.io.*;
67 import java.util.*;
68 import uk.co.westhawk.snmp.event.*;
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 public class SnmpContext extends AbstractSnmpContext
86 implements SnmpContextFace, Cloneable {
87 private static final String version_id = "@(#)$Id: SnmpContext.java,v 3.25 2009/03/05 12:51:29 birgita Exp $ Copyright Westhawk Ltd";
88
89 String community = SnmpContextFace.DEFAULT_COMMUNITY;
90
91
92
93
94
95
96
97
98 public SnmpContext(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 SnmpContext(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 SnmpContext(String host, int port, String bindAddress, String typeSocketA)
134 throws IOException {
135 super(host, port, bindAddress, typeSocketA);
136 }
137
138 public int getVersion() {
139 return SnmpConstants.SNMP_VERSION_1;
140 }
141
142 public String getCommunity() {
143 return community;
144 }
145
146 public void setCommunity(String newCommunity) {
147 community = newCommunity;
148 }
149
150 public byte[] encodePacket(byte msg_type, int rId, int errstat,
151 int errind, Enumeration ve, Object obj)
152 throws IOException, EncodingException {
153 byte[] packet = null;
154 if (isDestroyed == true) {
155 throw new EncodingException("Context can no longer be used, since it is already destroyed");
156 } else {
157 AsnEncoderv1 enc = new AsnEncoderv1();
158 ByteArrayOutputStream bay = enc.EncodeSNMP(this, msg_type, rId, errstat,
159 errind, ve);
160
161 int sz = bay.size();
162 if (sz > maxRecvSize) {
163 throw new EncodingException("Packet size (" + sz
164 + ") is > maximum size (" + maxRecvSize + ")");
165 }
166 packet = bay.toByteArray();
167 }
168 return packet;
169 }
170
171 public byte[] encodePacket(byte msg_type, String enterprise,
172 byte[] IpAddress, int generic_trap, int specific_trap,
173 long timeTicks, Enumeration ve)
174 throws IOException, EncodingException {
175 byte[] packet = null;
176 AsnEncoderv1 enc = new AsnEncoderv1();
177 ByteArrayOutputStream bay = enc.EncodeSNMP(this, msg_type, enterprise,
178 IpAddress, generic_trap, specific_trap, timeTicks, ve);
179
180 int sz = bay.size();
181 if (sz > maxRecvSize) {
182 throw new EncodingException("Packet size (" + sz
183 + ") is > maximum size (" + maxRecvSize + ")");
184 }
185 packet = bay.toByteArray();
186 return packet;
187 }
188
189
190
191
192 protected void processIncomingResponse(ByteArrayInputStream in)
193 throws DecodingException, IOException {
194 AsnDecoderv1 rpdu = new AsnDecoderv1();
195 AsnSequence seqPdu = rpdu.DecodeSNMP(in, getCommunity());
196 if (seqPdu instanceof AsnPduSequence) {
197 AsnPduSequence pduSeq = (AsnPduSequence) seqPdu;
198 if (pduSeq != null) {
199
200 Integer rid = new Integer(pduSeq.getReqId());
201 Pdu answ = getPdu(rid);
202 if (answ != null) {
203 answ.fillin(pduSeq);
204 } else {
205 if (AsnObject.debug > 3) {
206 System.out.println(getClass().getName() + ".processIncomingResponse(): No Pdu with reqid "
207 + rid.intValue());
208 }
209 }
210 } else {
211 if (AsnObject.debug > 3) {
212 System.out.println(getClass().getName() + ".processIncomingResponse(): Error - missing seq input");
213 }
214 }
215 } else {
216
217 }
218 }
219
220 public Pdu processIncomingPdu(byte[] message)
221 throws DecodingException, IOException {
222 AsnDecoderv1 rpdu = new AsnDecoderv1();
223 ByteArrayInputStream in = new ByteArrayInputStream(message);
224
225 Pdu pdu = null;
226 AsnSequence seqPdu = rpdu.DecodeSNMP(in, getCommunity());
227 if (seqPdu instanceof AsnTrapPduv1Sequence) {
228 AsnTrapPduv1Sequence pduSeq = (AsnTrapPduv1Sequence) seqPdu;
229 if (pduSeq != null) {
230 TrapPduv1 trapPdu = new TrapPduv1(this);
231 trapPdu.fillin(pduSeq);
232 pdu = trapPdu;
233 }
234 } else {
235
236 AsnPduSequence pduSeq = (AsnPduSequence) seqPdu;
237 if (pduSeq != null) {
238 byte type = pduSeq.getRespType();
239 switch (type) {
240 case SnmpConstants.GET_REQ_MSG:
241 pdu = new GetPdu(this);
242 break;
243 case SnmpConstants.GETNEXT_REQ_MSG:
244 pdu = new GetNextPdu(this);
245 break;
246 case SnmpConstants.SET_REQ_MSG:
247 pdu = new SetPdu(this);
248 break;
249
250
251
252
253
254
255
256
257
258
259
260
261 default:
262 if (AsnObject.debug > 3) {
263 System.out.println(getClass().getName()
264 + ".ProcessIncomingPdu(): PDU received with type "
265 + pduSeq.getRespTypeString()
266 + ". Ignoring it.");
267 }
268 }
269
270 if (pdu != null) {
271 pdu.fillin(pduSeq);
272 }
273 }
274 }
275 return pdu;
276 }
277
278
279
280
281
282
283
284 public Object clone() throws CloneNotSupportedException {
285 SnmpContext clContext = null;
286 try {
287 clContext = new SnmpContext(hostname, hostPort, bindAddr, typeSocket);
288 clContext.setCommunity(new String(community));
289 } catch (IOException exc) {
290 throw new CloneNotSupportedException("IOException "
291 + exc.getMessage());
292 }
293 return clContext;
294 }
295
296
297
298
299
300
301
302
303 public String getHashKey() {
304 String str = hostname
305 + "_" + hostPort
306 + "_" + bindAddr
307 + "_" + typeSocket
308 + "_" + community
309 + "_v" + getVersion();
310 return str;
311 }
312
313
314
315
316
317
318 public String toString() {
319 StringBuffer buffer = new StringBuffer("SnmpContext[");
320 buffer.append("host=").append(hostname);
321 buffer.append(", port=").append(hostPort);
322 buffer.append(", bindAddress=").append(bindAddr);
323 buffer.append(", socketType=").append(typeSocket);
324 buffer.append(", community=").append(community);
325 buffer.append(", #trapListeners=").append(trapSupport.getListenerCount());
326 buffer.append(", #pduListeners=").append(pduSupport.getListenerCount());
327 buffer.append("]");
328 return buffer.toString();
329 }
330
331 }