View Javadoc
1   // NAME
2   //      $RCSfile: AsnDecoderBase.java,v $
3   // DESCRIPTION
4   //      [given below in javadoc format]
5   // DELTA
6   //      $Revision: 3.3 $
7   // CREATED
8   //      $Date: 2007/10/17 10:36:47 $
9   // COPYRIGHT
10  //      Westhawk Ltd
11  // TO DO
12  //
13  
14  /*
15   * Copyright (C) 1995, 1996 by West Consulting BV
16   *
17   * Permission to use, copy, modify, and distribute this software
18   * for any purpose and without fee is hereby granted, provided
19   * that the above copyright notices appear in all copies and that
20   * both the copyright notice and this permission notice appear in
21   * supporting documentation.
22   * This software is provided "as is" without express or implied
23   * warranty.
24   * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
25   * original version by hargrave@dellgate.us.dell.com (Jordan Hargrave)
26   */
27  
28  /*
29   * Copyright (C) 1996 - 2006 by Westhawk Ltd
30   * <a href="www.westhawk.co.uk">www.westhawk.co.uk</a>
31   *
32   * Permission to use, copy, modify, and distribute this software
33   * for any purpose and without fee is hereby granted, provided
34   * that the above copyright notices appear in all copies and that
35   * both the copyright notice and this permission notice appear in
36   * supporting documentation.
37   * This software is provided "as is" without express or implied
38   * warranty.
39   * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
40   */
41  
42  package uk.co.westhawk.snmp.stack;
43  
44  /*-
45   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
46   * SNMP Java Client
47   * ჻჻჻჻჻჻
48   * Copyright 2023 MetricsHub, Westhawk
49   * ჻჻჻჻჻჻
50   * This program is free software: you can redistribute it and/or modify
51   * it under the terms of the GNU Lesser General Public License as
52   * published by the Free Software Foundation, either version 3 of the
53   * License, or (at your option) any later version.
54   *
55   * This program is distributed in the hope that it will be useful,
56   * but WITHOUT ANY WARRANTY; without even the implied warranty of
57   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
58   * GNU General Lesser Public License for more details.
59   *
60   * You should have received a copy of the GNU General Lesser Public
61   * License along with this program.  If not, see
62   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
63   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
64   */
65  
66  import uk.co.westhawk.snmp.util.*;
67  import java.io.*;
68  import java.util.*;
69  
70  /**
71   * This class contains the general methods to decode bytes into a Pdu.
72   * We split the original class AsnDecoder into four classes.
73   *
74   * @since 4_14
75   * @author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
76   * @version $Revision: 3.3 $ $Date: 2007/10/17 10:36:47 $
77   */
78  class AsnDecoderBase extends Object {
79      private static final String version_id = "@(#)$Id: AsnDecoderBase.java,v 3.3 2007/10/17 10:36:47 birgita Exp $ Copyright Westhawk Ltd";
80  
81      /**
82       * Reads the input into an asn sequence.
83       */
84      AsnSequence getAsnSequence(InputStream in)
85              throws IOException, DecodingException {
86          AsnSequence asnTopSeq = null;
87          AsnSequence dummy = new AsnSequence();
88          AsnObject obj = dummy.AsnReadHeader(in);
89          if (obj instanceof AsnSequence) {
90              asnTopSeq = (AsnSequence) obj;
91          } else {
92              String msg = "AsnSequence was expected";
93              if (obj != null) {
94                  msg += " instead of " + obj.getRespTypeString();
95              } else {
96                  msg += ", but is null";
97              }
98              throw new DecodingException(msg);
99          }
100         return asnTopSeq;
101     }
102 
103     /**
104      * Returns the SNMP version number of the asn sequence.
105      */
106     int getSNMPVersion(AsnSequence asnTopSeq) throws DecodingException {
107         int version = -1;
108         AsnObject obj = asnTopSeq.getObj(0);
109         if (obj instanceof AsnInteger) {
110             AsnInteger v = (AsnInteger) obj;
111             version = v.getValue();
112         } else {
113             String msg = "SNMP version should be of type AsnInteger"
114                     + " instead of " + obj.getRespTypeString();
115             throw new DecodingException(msg);
116         }
117         return version;
118     }
119 
120     /**
121      * Returns the SNMP v1 and v2c community of the asn sequence.
122      */
123     String getCommunity(AsnSequence asnTopSeq) throws DecodingException {
124         String comm = "";
125         AsnObject obj = asnTopSeq.getObj(1);
126         if (obj instanceof AsnOctets) {
127             AsnOctets estat = (AsnOctets) obj;
128             comm = estat.getValue();
129         } else {
130             String msg = "community should be of type AsnOctets"
131                     + " instead of " + obj.getRespTypeString();
132             throw new DecodingException(msg);
133         }
134         return comm;
135     }
136 
137     AsnSequence getAsnHeaderData(AsnSequence asnTopSeq) throws DecodingException {
138         AsnSequence asnHeaderData = null;
139         AsnObject obj = asnTopSeq.getObj(1);
140         if (obj instanceof AsnSequence) {
141             asnHeaderData = (AsnSequence) obj;
142         } else {
143             String msg = "asnHeaderData should be of type AsnSequence"
144                     + " instead of " + obj.getRespTypeString();
145             throw new DecodingException(msg);
146         }
147         return asnHeaderData;
148     }
149 
150 }