1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 package uk.co.westhawk.snmp.stack;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 import uk.co.westhawk.snmp.util.*;
66 import java.io.*;
67 import java.util.*;
68
69
70
71
72
73
74
75
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
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
90 asnTopSeq = new AsnSequence();
91 asnTopSeq.add(new AsnInteger(SnmpConstants.SNMP_VERSION_1));
92 asnTopSeq.add(new AsnOctets(context.getCommunity()));
93
94
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
104 bout = new ByteArrayOutputStream();
105 asnTopSeq.write(bout);
106 return bout;
107 }
108
109
110
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
118 asnPduObject = new AsnSequence(msg_type);
119 asnPduObject.add(new AsnObjectId(enterprise));
120
121
122
123 asnPduObject.add(new AsnOctets(IpAddress, AsnObject.IPADDRESS));
124
125 asnPduObject.add(new AsnInteger(generic_trap));
126 asnPduObject.add(new AsnInteger(specific_trap));
127 asnPduObject.add(new AsnUnsInteger(timeTicks));
128
129
130 AsnObject asnVBLObject = asnPduObject.add(new AsnSequence());
131
132
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
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
153 asnTopSeq = new AsnSequence();
154 asnTopSeq.add(new AsnInteger(AsnObject.SNMP_VERSION_1));
155 asnTopSeq.add(new AsnOctets(context.getCommunity()));
156
157
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
165 bout = new ByteArrayOutputStream();
166 asnTopSeq.write(bout);
167 return bout;
168 }
169
170 }