1 // NAME
2 // $RCSfile: AsnEncoderv1.java,v $
3 // DESCRIPTION
4 // [given below in javadoc format]
5 // DELTA
6 // $Revision: 3.3 $
7 // CREATED
8 // $Date: 2006/02/09 14:16:36 $
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 import uk.co.westhawk.snmp.util.*;
66 import java.io.*;
67 import java.util.*;
68
69 /**
70 * This class contains the v1 specific methods to encode a Pdu into bytes.
71 * We split the original class AsnEncoder into four classes.
72 *
73 * @since 4_14
74 * @author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
75 * @version $Revision: 3.3 $ $Date: 2006/02/09 14:16:36 $
76 */
77 class AsnEncoderv1 extends AsnEncoderBase {
78 private static final String version_id = "@(#)$Id: AsnEncoderv1.java,v 3.3 2006/02/09 14:16:36 birgit Exp $ Copyright Westhawk Ltd";
79
80 /**
81 * Encode SNMPv1 Trap packet into bytes.
82 */
83 ByteArrayOutputStream EncodeSNMP(SnmpContext context, byte msg_type,
84 String enterprise, byte[] IpAddress, int generic_trap, int specific_trap, long timeTicks, Enumeration ve)
85 throws IOException, EncodingException {
86 ByteArrayOutputStream bout;
87 AsnSequence asnTopSeq;
88
89 // Create authentication
90 asnTopSeq = new AsnSequence();
91 asnTopSeq.add(new AsnInteger(SnmpConstants.SNMP_VERSION_1));
92 asnTopSeq.add(new AsnOctets(context.getCommunity())); // community
93
94 // Create PDU sequence.
95 AsnObject asnPduObject = EncodeTrap1Pdu(msg_type, enterprise,
96 IpAddress, generic_trap, specific_trap, timeTicks, ve);
97
98 asnTopSeq.add(asnPduObject);
99
100 if (AsnObject.debug > 10) {
101 System.out.println("\n" + getClass().getName() + ".EncodeSNMP(): ");
102 }
103 // Write SNMP object
104 bout = new ByteArrayOutputStream();
105 asnTopSeq.write(bout);
106 return bout;
107 }
108
109 /**
110 * Encode Trapv1 PDU itself packet into bytes.
111 */
112 private AsnObject EncodeTrap1Pdu(byte msg_type,
113 String enterprise, byte[] IpAddress, int generic_trap, int specific_trap, long timeTicks, Enumeration ve)
114 throws IOException {
115 AsnObject asnPduObject, asnVBObject;
116
117 // kind of request
118 asnPduObject = new AsnSequence(msg_type);
119 asnPduObject.add(new AsnObjectId(enterprise)); // enterprise
120
121 // agent-addr (thanks Donnie Love (dlove@idsonline.com) for
122 // pointing out that we should have used IPADDRESS type)
123 asnPduObject.add(new AsnOctets(IpAddress, AsnObject.IPADDRESS));
124
125 asnPduObject.add(new AsnInteger(generic_trap)); // generic-trap
126 asnPduObject.add(new AsnInteger(specific_trap)); // specific-trap
127 asnPduObject.add(new AsnUnsInteger(timeTicks)); // time-stap
128
129 // Create VarbindList sequence
130 AsnObject asnVBLObject = asnPduObject.add(new AsnSequence());
131
132 // Add variable bindings
133 while (ve.hasMoreElements()) {
134 asnVBObject = asnVBLObject.add(new AsnSequence());
135 varbind vb = (varbind) ve.nextElement();
136 asnVBObject.add(vb.getOid());
137 asnVBObject.add(vb.getValue());
138 }
139
140 return asnPduObject;
141 }
142
143 /**
144 * Encode SNMPv1 packet into bytes.
145 */
146 ByteArrayOutputStream EncodeSNMP(SnmpContext context, byte msg_type,
147 int pduId, int errstat, int errind, Enumeration ve)
148 throws IOException, EncodingException {
149 ByteArrayOutputStream bout;
150 AsnSequence asnTopSeq;
151
152 // Create authentication
153 asnTopSeq = new AsnSequence();
154 asnTopSeq.add(new AsnInteger(AsnObject.SNMP_VERSION_1));
155 asnTopSeq.add(new AsnOctets(context.getCommunity())); // community
156
157 // Create PDU sequence.
158 AsnObject asnPduObject = EncodePdu(msg_type, pduId, errstat, errind, ve);
159 asnTopSeq.add(asnPduObject);
160
161 if (AsnObject.debug > 10) {
162 System.out.println("\n" + getClass().getName() + ".EncodeSNMP(): ");
163 }
164 // Write SNMP object
165 bout = new ByteArrayOutputStream();
166 asnTopSeq.write(bout);
167 return bout;
168 }
169
170 }