1 // NAME
2 // $RCSfile: BlockPdu.java,v $
3 // DESCRIPTION
4 // [given below in javadoc format]
5 // DELTA
6 // $Revision: 3.13 $
7 // CREATED
8 // $Date: 2006/01/17 17:43:53 $
9 // COPYRIGHT
10 // Westhawk Ltd
11 // TO DO
12 //
13
14 /*
15 * Copyright (C) 2000 - 2006 by Westhawk Ltd
16 * <a href="www.westhawk.co.uk">www.westhawk.co.uk</a>
17 *
18 * Permission to use, copy, modify, and distribute this software
19 * for any purpose and without fee is hereby granted, provided
20 * that the above copyright notices appear in all copies and that
21 * both the copyright notice and this permission notice appear in
22 * supporting documentation.
23 * This software is provided "as is" without express or implied
24 * warranty.
25 * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
26 */
27
28 package uk.co.westhawk.snmp.pdu;
29
30 /*-
31 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
32 * SNMP Java Client
33 * ჻჻჻჻჻჻
34 * Copyright 2023 MetricsHub, Westhawk
35 * ჻჻჻჻჻჻
36 * This program is free software: you can redistribute it and/or modify
37 * it under the terms of the GNU Lesser General Public License as
38 * published by the Free Software Foundation, either version 3 of the
39 * License, or (at your option) any later version.
40 *
41 * This program is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 * GNU General Lesser Public License for more details.
45 *
46 * You should have received a copy of the GNU General Lesser Public
47 * License along with this program. If not, see
48 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
49 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
50 */
51 import uk.co.westhawk.snmp.stack.*;
52 import java.util.*;
53
54 /**
55 * The BlockPdu class is a wrapper class that will block until it
56 * receives the answer.
57 *
58 * @see Pdu
59 *
60 * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a>
61 * @version $Revision: 3.13 $ $Date: 2006/01/17 17:43:53 $
62 */
63 public class BlockPdu extends Object {
64 private static final String version_id = "@(#)$Id: BlockPdu.java,v 3.13 2006/01/17 17:43:53 birgit Exp $ Copyright Westhawk Ltd";
65
66 /**
67 * The GET PDU type.
68 */
69 public final static int GET = 0;
70 /**
71 * The SET PDU type.
72 */
73 public final static int SET = 1;
74 /**
75 * The GETNEXT PDU type.
76 */
77 public final static int GETNEXT = 2;
78 /**
79 * The GETBULK PDU type. Note, the getBulkRequest was introduced in
80 * SNMPv2c. For that reason it should not be sent with SNMPv1.
81 */
82 public final static int GETBULK = 3;
83
84 protected Vector reqVarbinds;
85 private SnmpContextBasisFace context;
86 private Pdu pdu;
87 private int type = GET;
88 private int non_rep = 0;
89 private int max_rep = 0;
90 private int retry_intervals[] = null;
91
92 /**
93 * Constructor.
94 *
95 * @param con The context of the request
96 */
97 public BlockPdu(SnmpContextBasisFace con) {
98 context = con;
99 reqVarbinds = new Vector(1, 1);
100 }
101
102 /**
103 * Adds an OID to the PDU.
104 *
105 * @param oid The oid
106 * @see Pdu#addOid(String)
107 */
108 public void addOid(String oid) {
109 varbind vb = new varbind(oid);
110 addOid(vb);
111 }
112
113 /**
114 * Adds an OID to the PDU and the value that has to be set.
115 * This is only useful for the SET type.
116 *
117 * @param oid The oid
118 * @see SetPdu#addOid(String, AsnObject)
119 * @see #SET
120 */
121 public void addOid(String oid, AsnObject val) {
122 varbind vb = new varbind(oid, val);
123 addOid(vb);
124 }
125
126 /**
127 * Adds an OID (object identifier) to the PDU and the value that has
128 * to be set.
129 * This is only useful for the SET type.
130 *
131 * @param oid The oid
132 * @param val The value
133 * @see Pdu#addOid(AsnObjectId, AsnObject)
134 * @see varbind
135 * @since 4_12
136 */
137 public void addOid(AsnObjectId oid, AsnObject val) {
138 varbind vb = new varbind(oid, val);
139 addOid(vb);
140 }
141
142 /**
143 * Adds an OID (object identifier) to the PDU. The OID indicates which
144 * MIB variable we request or which MIB variable should be set.
145 *
146 * @param oid The oid
147 * @see Pdu#addOid(AsnObjectId)
148 * @see varbind
149 * @since 4_12
150 */
151 public void addOid(AsnObjectId oid) {
152 varbind vb = new varbind(oid);
153 addOid(vb);
154 }
155
156 /**
157 * Adds an OID (object identifier) to the PDU.
158 *
159 * @param var The varbind
160 * @see #addOid(String)
161 */
162 public void addOid(varbind var) {
163 reqVarbinds.addElement(var);
164 }
165
166 /**
167 * Sets the getBulkRequest parameters.
168 * This is only useful for the GETBULK type.
169 * If these parameters are not set, they both default to zero.
170 *
171 * @param nr The non repeaters.
172 * @param mr The max repetitions.
173 * @see GetBulkPdu#setMaxRepetitions(int)
174 * @see GetBulkPdu#setNonRepeaters(int)
175 * @see #GETBULK
176 */
177 public void setBulkParameters(int nr, int mr) {
178 non_rep = nr;
179 max_rep = mr;
180 }
181
182 /**
183 * Adds a list of OIDs to the PDU.
184 *
185 * @param oids The OIDs to be added
186 */
187 public void addOid(String[] oids) {
188 for (int i = 0; i < oids.length; i++) {
189 varbind vb = new varbind(oids[i]);
190 reqVarbinds.addElement(vb);
191 }
192 }
193
194 /**
195 * Sets the type of PDU. The type indicates the kind of request, i.e.
196 * the getRequest (the default), the setRequest, the getNextRequest, or
197 * the getBulkRequest.
198 *
199 * @param newType The type of request
200 * @see #GET
201 * @see #SET
202 * @see #GETNEXT
203 * @see #GETBULK
204 */
205 public void setPduType(int newType) {
206 type = newType;
207 }
208
209 /**
210 * Sets the retry intervals of the PDU. This method overwrites the
211 * default values in the PDU class.
212 *
213 * @param retryIntervals The interval in msec of each retry
214 * @see Pdu#setRetryIntervals(int[])
215 */
216 public void setRetryIntervals(int retryIntervals[]) {
217 retry_intervals = retryIntervals;
218 }
219
220 /**
221 * Sends the request and waits (blocks) for the response. Returns the
222 * value of the first variable binding in the response. If no response was
223 * received, this will be null.
224 *
225 * @return The value of the first variable binding in the response
226 * @see #sendAndWait()
227 */
228 public AsnObject getResponseVariable()
229 throws PduException, java.io.IOException {
230 AsnObject value = null;
231 varbind[] vars = sendAndWait();
232 if (vars != null && vars.length > 0) {
233 value = vars[0].getValue();
234 }
235 return value;
236 }
237
238 /**
239 * Sends the request and waits (blocks) for the response. Returns the
240 * values of the variable bindings in the response. If no response was
241 * received, this will be null.
242 *
243 * @return The values of the variable bindings in the response
244 * @see #sendAndWait()
245 */
246 public AsnObject[] getResponseVariables()
247 throws PduException, java.io.IOException {
248 AsnObject[] values = null;
249 varbind[] vars = sendAndWait();
250 if (vars != null) {
251 values = new AsnObject[vars.length];
252 for (int i = 0; i < vars.length; i++) {
253 values[i] = vars[i].getValue();
254 }
255 }
256 return values;
257 }
258
259 /**
260 * Sends the request and waits (blocks) for the response. Returns the
261 * first variable binding in the response. If no response was received, this
262 * will be null.
263 *
264 * @return The first variable binding in the response
265 * @see #sendAndWait()
266 */
267 public varbind getResponseVariableBinding()
268 throws PduException, java.io.IOException {
269 varbind var = null;
270 varbind[] vars = sendAndWait();
271 if (vars != null && vars.length > 0) {
272 var = vars[0];
273 }
274 return var;
275 }
276
277 /**
278 * Sends the request and waits (blocks) for the response. Returns the
279 * variable bindings in the response. If no response was received, this
280 * will be null.
281 *
282 * @return The variable bindings in the response
283 * @see #sendAndWait()
284 */
285 public varbind[] getResponseVariableBindings()
286 throws PduException, java.io.IOException {
287 varbind[] vars = sendAndWait();
288 return vars;
289 }
290
291 /**
292 * Sends the request and waits (blocks) for the response. This is the core
293 * of the blocking getResponseVariableX() methods.
294 *
295 * @return The response, can be null if there was a timeout
296 * @see Pdu#send()
297 * @see Pdu#waitForSelf()
298 * @see Pdu#getResponseVarbinds()
299 */
300 protected synchronized varbind[] sendAndWait()
301 throws PduException, java.io.IOException {
302 int sz = reqVarbinds.size();
303 switch (type) {
304 case SET: {
305 pdu = new SetPdu_vec(context, sz);
306 break;
307 }
308 case GETNEXT: {
309 pdu = new GetNextPdu_vec(context, sz);
310 break;
311 }
312 case GETBULK: {
313 pdu = new GetBulkPdu(context);
314 ((GetBulkPdu) pdu).setNonRepeaters(non_rep);
315 ((GetBulkPdu) pdu).setMaxRepetitions(max_rep);
316 break;
317 }
318 default: {
319 pdu = new GetPdu_vec(context, sz);
320 }
321 }
322 for (int i = 0; i < sz; i++) {
323 varbind var = (varbind) reqVarbinds.elementAt(i);
324 pdu.addOid(var);
325 }
326 if (retry_intervals != null) {
327 pdu.setRetryIntervals(retry_intervals);
328 }
329 pdu.send();
330 pdu.waitForSelf();
331 varbind[] vars = pdu.getResponseVarbinds();
332 return vars;
333 }
334
335 /**
336 * Returns the error index of the PDU.
337 * This will only have been set after the response is received.
338 * Thanks to Maga Hegde (mhegde@zumanetworks.com) to requesting this method.
339 *
340 * @see Pdu#getErrorIndex()
341 * @return The error index
342 */
343 public int getErrorIndex() {
344 return pdu.getErrorIndex();
345 }
346
347 /**
348 * Returns the error status of the PDU.
349 * This will only have been set after the response is received.
350 *
351 * @see Pdu#getErrorStatus()
352 * @return The error status
353 */
354 public int getErrorStatus() {
355 return pdu.getErrorStatus();
356 }
357
358 /**
359 * Returns the error status string of the PDU.
360 * This will only have been set after the response is received.
361 *
362 * @see Pdu#getErrorStatusString()
363 * @return The error status string
364 */
365 public String getErrorStatusString() {
366 return pdu.getErrorStatusString();
367 }
368
369 }