View Javadoc
1   // NAME
2   //      $RCSfile: TrapPduv1.java,v $
3   // DESCRIPTION
4   //      [given below in javadoc format]
5   // DELTA
6   //      $Revision: 3.14 $
7   // CREATED
8   //      $Date: 2007/10/17 10:44:09 $
9   // COPYRIGHT
10  //      Westhawk Ltd
11  // TO DO
12  //
13  
14  /*
15   * Copyright (C) 2001 - 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  
29  package uk.co.westhawk.snmp.stack;
30  
31  /*-
32   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
33   * SNMP Java Client
34   * ჻჻჻჻჻჻
35   * Copyright 2023 MetricsHub, Westhawk
36   * ჻჻჻჻჻჻
37   * This program is free software: you can redistribute it and/or modify
38   * it under the terms of the GNU Lesser General Public License as
39   * published by the Free Software Foundation, either version 3 of the
40   * License, or (at your option) any later version.
41   *
42   * This program is distributed in the hope that it will be useful,
43   * but WITHOUT ANY WARRANTY; without even the implied warranty of
44   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
45   * GNU General Lesser Public License for more details.
46   *
47   * You should have received a copy of the GNU General Lesser Public
48   * License along with this program.  If not, see
49   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
50   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
51   */
52  import java.util.*;
53  import java.io.*;
54  import java.net.*;
55  
56  import uk.co.westhawk.snmp.util.*;
57  
58  /**
59   * This class represents the ASN SNMPv1 Trap PDU object. 
60   * See <a href="http://www.ietf.org/rfc/rfc1157.txt">RFC1157-SNMP</a>.
61   * 
62   * <p>
63   * Note that the SNMPv1 Trap PDU is the only PDU with a different PDU
64   * format. It has additional fields like <code>enterprise</code>,
65   * <code>ipAddress</code>, <code>genericTrap</code>,
66   * <code>specificTrap</code> and <code>timeTicks</code>.
67   * </p>
68   *
69   * @see TrapPduv2
70   * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a>
71   * @version $Revision: 3.14 $ $Date: 2007/10/17 10:44:09 $
72   */
73  public class TrapPduv1 extends Pdu {
74      private static final String version_id = "@(#)$Id: TrapPduv1.java,v 3.14 2007/10/17 10:44:09 birgita Exp $ Copyright Westhawk Ltd";
75  
76      private String enterprise;
77      private byte[] IpAddress;
78      private int generic_trap;
79      private int specific_trap;
80      private long timeTicks;
81  
82      private final static String[] genericTrapStrings = {
83              "Cold Start",
84              "Warm Start",
85              "Link Down",
86              "Link Up",
87              "Authentication Failure",
88              "EGP Neighbor Loss",
89              "Enterprise Specific",
90      };
91  
92      /**
93       * Constructor.
94       *
95       * @param con The context (v1) of the TrapPduv1
96       * @see SnmpContext
97       */
98      public TrapPduv1(SnmpContext con) {
99          super(con);
100         setMsgType(AsnObject.TRP_REQ_MSG);
101 
102         generic_trap = AsnObject.SNMP_TRAP_WARMSTART;
103     }
104 
105     /**
106      * Constructor.
107      *
108      * @param con The context pool (v1) of the TrapPduv1
109      * @see SnmpContext
110      */
111     public TrapPduv1(SnmpContextPool con) {
112         super(con);
113         setMsgType(AsnObject.TRP_REQ_MSG);
114 
115         generic_trap = AsnObject.SNMP_TRAP_WARMSTART;
116     }
117 
118     /**
119      * Sets the type of object generating the trap. This parameter is based on
120      * the sysObjectId.
121      */
122     public void setEnterprise(String var) {
123         enterprise = var;
124     }
125 
126     /**
127      * Returns the type of object generating the trap.
128      */
129     public String getEnterprise() {
130         return enterprise;
131     }
132 
133     /**
134      * Sets the IP Address of the object generating the trap.
135      */
136     public void setIpAddress(byte[] var) {
137         IpAddress = var;
138     }
139 
140     /**
141      * Returns the IP Address of the object generating the trap.
142      */
143     public byte[] getIpAddress() {
144         return IpAddress;
145     }
146 
147     /**
148      * Sets the generic trap type. By default the warmStart is sent.
149      *
150      * @see SnmpConstants#SNMP_TRAP_COLDSTART
151      * @see SnmpConstants#SNMP_TRAP_WARMSTART
152      * @see SnmpConstants#SNMP_TRAP_LINKDOWN
153      * @see SnmpConstants#SNMP_TRAP_LINKUP
154      * @see SnmpConstants#SNMP_TRAP_AUTHFAIL
155      * @see SnmpConstants#SNMP_TRAP_EGPNEIGHBORLOSS
156      * @see SnmpConstants#SNMP_TRAP_ENTERPRISESPECIFIC
157      */
158     public void setGenericTrap(int var) {
159         generic_trap = var;
160     }
161 
162     /**
163      * Returns the generic trap type.
164      */
165     public int getGenericTrap() {
166         return generic_trap;
167     }
168 
169     /**
170      * Returns the string representation of the generic trap.
171      *
172      * @return the generic trap string
173      * @see #getGenericTrap
174      */
175     public String getGenericTrapString() {
176         String trapStr = "";
177         if (generic_trap > -1 && generic_trap < genericTrapStrings.length) {
178             trapStr = genericTrapStrings[generic_trap];
179         }
180         return trapStr;
181     }
182 
183     /**
184      * Sets the specific trap code.
185      */
186     public void setSpecificTrap(int var) {
187         specific_trap = var;
188     }
189 
190     /**
191      * Returns the specific trap code.
192      */
193     public int getSpecificTrap() {
194         return specific_trap;
195     }
196 
197     /**
198      * Sets the sysUpTime of the agent.
199      *
200      * <p>
201      * <a href="http://www.ietf.org/rfc/rfc1155.txt">RFC1155-SMI</a>: TimeTicks:
202      * <br/>
203      * This application-wide type represents a non-negative integer which
204      * counts the time in hundredths of a second since some epoch. When
205      * object types are defined in the MIB which use this ASN.1 type, the
206      * description of the object type identifies the reference epoch.
207      * </p>
208      */
209     public void setTimeTicks(long var) {
210         timeTicks = var;
211     }
212 
213     /**
214      * Returns the sysUpTime of the agent.
215      *
216      * @see #setTimeTicks
217      */
218     public long getTimeTicks() {
219         return timeTicks;
220     }
221 
222     /**
223      * Sends the TrapPduv1. Since the Trap v1 PDU has a different format
224      * (sigh), a different encoding message has to be called.
225      *
226      * Note that all properties of the context have to be set before this
227      * point.
228      */
229     public boolean send() throws IOException, PduException {
230         if (added == false) {
231             // Moved this statement from the constructor because it
232             // conflicts with the way the SnmpContextXPool works.
233             added = context.addPdu(this);
234         }
235         Enumeration vbs = reqVarbinds.elements();
236         encodedPacket = ((SnmpContext) context).encodePacket(msg_type, enterprise,
237                 IpAddress, generic_trap, specific_trap, timeTicks, vbs);
238         addToTrans();
239         return added;
240     }
241 
242     /**
243      * The trap PDU does not get a response back. So it should be sent once.
244      */
245     void transmit() {
246         transmit(false);
247     }
248 
249     /**
250      * Returns the string representation of this object.
251      *
252      * @return The string of the PDU
253      */
254     public String toString() {
255         String iPAddressStr = "null";
256         if (IpAddress != null) {
257             iPAddressStr = (new AsnOctets(IpAddress)).toIpAddress();
258         }
259         StringBuffer buffer = new StringBuffer(getClass().getName());
260         buffer.append("[");
261         buffer.append("context=").append(context);
262         buffer.append(", reqId=").append(getReqId());
263         buffer.append(", msgType=0x").append(SnmpUtilities.toHex(msg_type));
264         buffer.append(", enterprise=").append(enterprise);
265         buffer.append(", IpAddress=").append(iPAddressStr);
266         buffer.append(", generic_trap=").append(getGenericTrapString());
267         buffer.append(", specific_trap=").append(specific_trap);
268         buffer.append(", timeTicks=").append(timeTicks);
269 
270         buffer.append(", reqVarbinds=");
271         if (reqVarbinds != null) {
272             int sz = reqVarbinds.size();
273             buffer.append("[");
274             for (int i = 0; i < sz; i++) {
275                 varbind var = (varbind) reqVarbinds.elementAt(i);
276                 buffer.append(var.toString()).append(", ");
277             }
278             buffer.append("]");
279         } else {
280             buffer.append("null");
281         }
282 
283         buffer.append(", respVarbinds=");
284         if (respVarbinds != null) {
285             int sz = respVarbinds.size();
286             buffer.append("[");
287             for (int i = 0; i < sz; i++) {
288                 varbind var = (varbind) respVarbinds.elementAt(i);
289                 buffer.append(var.toString()).append(", ");
290             }
291             buffer.append("]");
292         } else {
293             buffer.append("null");
294         }
295 
296         buffer.append("]");
297         return buffer.toString();
298     }
299 
300     /**
301      * Fill in the received trap.
302      * 
303      * @see Pdu#getResponseVarbinds()
304      */
305     void fillin(AsnTrapPduv1Sequence seq) {
306         if (seq != null) {
307             try {
308                 AsnSequence varBind = seq.getVarBind();
309                 int size = varBind.getObjCount();
310                 respVarbinds = new Vector(size, 1);
311                 for (int n = 0; n < size; n++) {
312                     Object obj = varBind.getObj(n);
313                     if (obj instanceof AsnSequence) {
314                         AsnSequence varSeq = (AsnSequence) obj;
315                         try {
316                             varbind vb = new varbind(varSeq);
317                             respVarbinds.addElement(vb);
318                         } catch (IllegalArgumentException exc) {
319                         }
320                     }
321                 }
322 
323                 setEnterprise(seq.getEnterprise());
324                 setIpAddress(seq.getIPAddress());
325                 setGenericTrap(seq.getGenericTrap());
326                 setSpecificTrap(seq.getSpecificTrap());
327                 setTimeTicks(seq.getTimeTicks());
328             } catch (DecodingException exc) {
329                 setErrorStatus(AsnObject.SNMP_ERR_DECODINGASN_EXC, exc);
330             }
331         }
332     }
333 
334     /**
335      * Has no meaning, since there is not response.
336      */
337     protected void new_value(int n, varbind res) {
338     }
339 
340     /**
341      * Has no meaning, since there is not response.
342      */
343     protected void tell_them() {
344     }
345 
346     /**
347      * Returns that this type of PDU is <em>not</em> expecting a response.
348      * This method is used in AbstractSnmpContext to help determine whether
349      * or not to start a thread that listens for a response when sending this
350      * PDU.
351      * The default is <em>false</em>.
352      *
353      * @return true if a response is expected, false if not.
354      * @since 4_14
355      */
356     protected boolean isExpectingResponse() {
357         return false;
358     }
359 
360 }