1 // NAME
2 // $RCSfile: AsnEncoderv2c.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 v2c 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 AsnEncoderv2c extends AsnEncoderBase {
78 private static final String version_id = "@(#)$Id: AsnEncoderv2c.java,v 3.3 2006/02/09 14:16:36 birgit Exp $ Copyright Westhawk Ltd";
79
80 /**
81 * Encode SNMPv2c packet into bytes.
82 */
83 ByteArrayOutputStream EncodeSNMPv2c(SnmpContextv2c context, byte msg_type,
84 int pduId, int errstat, int errind, 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_2c));
92 asnTopSeq.add(new AsnOctets(context.getCommunity())); // community
93
94 // Create PDU sequence.
95 AsnObject asnPduObject = EncodePdu(msg_type, pduId, errstat, errind, ve);
96 asnTopSeq.add(asnPduObject);
97
98 if (AsnObject.debug > 10) {
99 System.out.println("\n" + getClass().getName() + ".EncodeSNMPv2c(): ");
100 }
101 // Write SNMP object
102 bout = new ByteArrayOutputStream();
103 asnTopSeq.write(bout);
104 return bout;
105 }
106
107 }