1 // NAME
2 // $RCSfile: DefaultUsmAgent.java,v $
3 // DESCRIPTION
4 // [given below in javadoc format]
5 // DELTA
6 // $Revision: 3.10 $
7 // CREATED
8 // $Date: 2009/03/05 15:51:42 $
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 package uk.co.westhawk.snmp.stack;
29
30 /*-
31 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
32 * SNMP Java Client
33 * ჻჻჻჻჻჻
34 * Copyright 2023 MetricsHub, Westhawk
35 * ჻჻჻჻჻჻
36 * This program is free software: you can redistribute it and/or modify
37 * it under the terms of the GNU Lesser General Public License as
38 * published by the Free Software Foundation, either version 3 of the
39 * License, or (at your option) any later version.
40 *
41 * This program is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 * GNU General Lesser Public License for more details.
45 *
46 * You should have received a copy of the GNU General Lesser Public
47 * License along with this program. If not, see
48 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
49 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
50 */
51
52 import uk.co.westhawk.snmp.beans.UsmDiscoveryBean;
53
54 /**
55 * This implementation of UsmAgent tries to discover the parameters by
56 * doing the default USM discovery process on localhost.
57 *
58 * <p>
59 * Note that it is not guaranteed that the agent will allow discovery
60 * by itself. Also, if the SNMP agent reboots while the stack is
61 * running, it will not pick up the new boots and time.
62 * </p>
63 *
64 * <p>
65 * Users are advised and encouraged to provide a better, more accurate
66 * implementation of UsmAgent.
67 * </p>
68 *
69 * <p>
70 * See <a href="http://www.ietf.org/rfc/rfc3414.txt">SNMP-USER-BASED-SM-MIB</a>.
71 * </p>
72 * @see SnmpContextv3
73 *
74 * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a>
75 * @version $Revision: 3.10 $ $Date: 2009/03/05 15:51:42 $
76 */
77 public class DefaultUsmAgent implements UsmAgent {
78 static final String version_id = "@(#)$Id: DefaultUsmAgent.java,v 3.10 2009/03/05 15:51:42 birgita Exp $ Copyright Westhawk Ltd";
79
80 /**
81 * The default name of the local host, <em>localhost</em>.
82 */
83 public final static String LOCAL_HOST = "localhost";
84
85 /**
86 * The default port number of the local host, <em>161</em>.
87 */
88 public final static int LOCAL_PORT = 161;
89
90 private SnmpContextv3Basis context;
91 private String hostname;
92 private String hostaddress;
93 private int port;
94
95 public DefaultUsmAgent() {
96 try {
97 setAgentName(LOCAL_HOST);
98 } catch (java.net.UnknownHostException exc) {
99 hostname = LOCAL_HOST;
100 hostaddress = "127.0.0.1";
101 }
102 setAgentPort(LOCAL_PORT);
103 }
104
105 /**
106 * Returns the authoritative SNMP Engine ID. If the discovery failed,
107 * <em>null</em> will be returned.
108 *
109 * @return The Engine ID
110 */
111 public String getSnmpEngineId() {
112 TimeWindow tWindow = TimeWindow.getCurrent();
113 String engineId = tWindow.getSnmpEngineId(hostaddress, port);
114 return engineId;
115 }
116
117 /**
118 * Returns the authoritative Engine Boots. If the discovery failed,
119 * <em>1</em> will be returned.
120 *
121 * @return The Engine Boots
122 */
123 public int getSnmpEngineBoots() {
124 int boots = 1;
125 TimeWindowNode node = null;
126 TimeWindow tWindow = TimeWindow.getCurrent();
127 String engineId = tWindow.getSnmpEngineId(hostaddress, port);
128 if (engineId != null) {
129 node = tWindow.getTimeLine(engineId);
130 }
131 if (node != null) {
132 boots = node.getSnmpEngineBoots();
133 }
134 return boots;
135 }
136
137 /**
138 * Returns the authoritative Engine Time. If the discovery failed,
139 * <em>1</em> will be returned.
140 *
141 * @return The Engine Time
142 */
143 public int getSnmpEngineTime() {
144 int time = 1;
145 TimeWindowNode node = null;
146 TimeWindow tWindow = TimeWindow.getCurrent();
147 String engineId = tWindow.getSnmpEngineId(hostaddress, port);
148 if (engineId != null) {
149 node = tWindow.getTimeLine(engineId);
150 }
151 if (node != null) {
152 time = node.getSnmpEngineTime();
153 }
154 return time;
155 }
156
157 /**
158 * Sets the SNMP context. It will do a discovery if needed.
159 */
160 public void setSnmpContext(SnmpContextv3Basis c) {
161 context = c;
162 try {
163 discoverIfNeeded();
164 } catch (PduException exc) {
165 if (AsnObject.debug > 4) {
166 System.out.println(getClass().getName() + ".setSnmpContext(): "
167 + exc.getMessage());
168 }
169 } catch (java.io.IOException exc) {
170 if (AsnObject.debug > 4) {
171 System.out.println(getClass().getName() + ".setSnmpContext(): "
172 + exc.getMessage());
173 }
174 }
175 }
176
177 /**
178 * Sets my own hostname, i.e. the name of the agent or authoritative
179 * engine. By default <em>localhost</em> is used.
180 * Sets the hostaddress as well, using java.net.InetAddress.
181 *
182 * @see #LOCAL_HOST
183 * @exception java.net.UnknownHostException Thrown when java.net.InetAddress
184 * cannot
185 * resolve the name
186 */
187 public void setAgentName(String host)
188 throws java.net.UnknownHostException {
189 hostname = host;
190 java.net.InetAddress ipAddr = java.net.InetAddress.getByName(hostname);
191 hostaddress = ipAddr.getHostAddress();
192 }
193
194 /**
195 * Sets my own port number, i.e. the port number of the agent
196 * or authoritative engine. By default <em>161</em> is used.
197 *
198 * @see #LOCAL_PORT
199 */
200 public void setAgentPort(int p) {
201 port = p;
202 }
203
204 /**
205 * This discovers the USM timeliness of hostname and port as set by
206 * setAgentName() and setAgentPort(), but with all the other details
207 * taken from the SNMPv3 context.
208 *
209 * @see #setAgentName
210 * @see #setAgentPort
211 */
212 void discoverIfNeeded()
213 throws java.io.IOException, PduException {
214 UsmDiscoveryBean discBean = null;
215 boolean isNeeded = false;
216
217 TimeWindow tWindow = TimeWindow.getCurrent();
218 String engineId = tWindow.getSnmpEngineId(hostaddress, port);
219 if (engineId == null) {
220 isNeeded = true;
221 discBean = new UsmDiscoveryBean(hostname,
222 port, context.getBindAddress(), context.getTypeSocket());
223 }
224
225 if (context.isUseAuthentication()) {
226 if (isNeeded) {
227 discBean.setAuthenticationDetails(context.getUserName(),
228 context.getUserAuthenticationPassword(),
229 context.getAuthenticationProtocol());
230 } else if (tWindow.isTimeLineKnown(engineId) == false) {
231 isNeeded = true;
232 discBean = new UsmDiscoveryBean(
233 hostname, port, context.getBindAddress(),
234 context.getTypeSocket());
235 discBean.setAuthenticationDetails(context.getUserName(),
236 context.getUserAuthenticationPassword(),
237 context.getAuthenticationProtocol());
238 }
239
240 if (isNeeded && context.isUsePrivacy()) {
241 discBean.setPrivacyDetails(context.getUserPrivacyPassword(),
242 context.getPrivacyProtocol());
243 }
244 }
245
246 if (isNeeded) {
247 discBean.startDiscovery();
248 discBean.freeResources();
249 }
250 }
251
252 /**
253 * @since 4_14
254 */
255 public long getUsmStatsUnknownEngineIDs() {
256 return 0;
257 }
258
259 /**
260 * @since 4_14
261 */
262 public long getUsmStatsNotInTimeWindows() {
263 return 0;
264 }
265
266 }