View Javadoc
1   // NAME
2   //      $RCSfile: UsmBeingDiscoveredBean.java,v $
3   // DESCRIPTION
4   //      [given below in javadoc format]
5   // DELTA
6   //      $Revision: 1.8 $
7   // CREATED
8   //      $Date: 2009/03/05 15:51:42 $
9   // COPYRIGHT
10  //      Westhawk Ltd
11  // TO DO
12  //
13  
14  /*
15   * Copyright (C) 2005 - 2006 by Westhawk Ltd
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   */
26  
27  package uk.co.westhawk.snmp.beans;
28  
29  /*-
30   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
31   * SNMP Java Client
32   * ჻჻჻჻჻჻
33   * Copyright 2023 MetricsHub, Westhawk
34   * ჻჻჻჻჻჻
35   * This program is free software: you can redistribute it and/or modify
36   * it under the terms of the GNU Lesser General Public License as
37   * published by the Free Software Foundation, either version 3 of the
38   * License, or (at your option) any later version.
39   *
40   * This program is distributed in the hope that it will be useful,
41   * but WITHOUT ANY WARRANTY; without even the implied warranty of
42   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
43   * GNU General Lesser Public License for more details.
44   *
45   * You should have received a copy of the GNU General Lesser Public
46   * License along with this program.  If not, see
47   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
48   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
49   */
50  
51  import uk.co.westhawk.snmp.stack.*;
52  import uk.co.westhawk.snmp.pdu.*;
53  import uk.co.westhawk.snmp.event.*;
54  import uk.co.westhawk.snmp.util.*;
55  import java.util.*;
56  
57  /**
58   * <p>
59   * This bean handles being discovered as an authoritative engine by an
60   * non-authoritative engine (SNMPv3 only).
61   * </p>
62   *
63   * <p>
64   * The discovery process consists of two steps: 
65   * 1. first the SNMP engine ID has to be discovered, 
66   * second the timeline details of the SNMP engine ID have to be discovered. 
67   * <br/>
68   * 2. For the last step the username of the principal is needed. 
69   * <br/>
70   * All these parameters should be provided by (your own) UsmAgent.
71   * </p>
72   *
73   * <p>
74   * This class is not very efficient. The private context
75   * discEngineIdContextIn will be the same for every SnmpContextv3 that
76   * creates this bean.
77   * </p>
78   *
79   * <p>
80   * See <a href="http://www.ietf.org/rfc/rfc3414.txt">SNMP-USER-BASED-SM-MIB</a>.
81   * </p>
82   *
83   * @see SnmpContextv3#addRequestPduListener
84   * @since 4_14
85   * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a>
86   * @version $Revision: 1.8 $ $Date: 2009/03/05 15:51:42 $
87   */
88  public class UsmBeingDiscoveredBean implements RequestPduListener {
89      private static final String version_id = "@(#)$Id: UsmBeingDiscoveredBean.java,v 1.8 2009/03/05 15:51:42 birgita Exp $ Copyright Westhawk Ltd";
90  
91      private UsmAgent usmAgent = null;
92      private SnmpContextv3 context = null;
93  
94      // the context to receive incoming requests
95      private SnmpContextv3Discovery discEngineIdContextIn;
96      private SnmpContextv3Discovery discTimeLineContextIn;
97  
98      // the context to send the answer back
99      private SnmpContextv3 discEngineIdContextOut;
100     private SnmpContextv3 discTimeLineContextOut;
101 
102     /**
103      * Constructor.
104      *
105      * @param myUsmAgent The usmAgent that will provide all the
106      *                   authoritative engine parameters
107      * @param myContext  The context
108      *
109      * @see SnmpContextv3#addRequestPduListener
110      */
111     public UsmBeingDiscoveredBean(SnmpContextv3 myContext, UsmAgent myUsmAgent)
112             throws java.io.IOException {
113         usmAgent = myUsmAgent;
114         context = myContext;
115 
116         if (AsnObject.debug > 4) {
117             System.out.println(getClass().getName() + " Constructor:"
118                     + "usmAgent=" + usmAgent.toString()
119                     + "context=" + context.toString());
120         }
121         usmAgent.setSnmpContext(context);
122 
123         discEngineIdContextIn = new SnmpContextv3Discovery(context.getSendToHostAddress(),
124                 context.getPort(), context.getBindAddress(), context.getTypeSocket());
125         discEngineIdContextIn.setUserName("");
126         discEngineIdContextIn.setUseAuthentication(false);
127         discEngineIdContextIn.setUsePrivacy(false);
128         discEngineIdContextIn.setContextEngineId(SnmpUtilities.toBytes(usmAgent.getSnmpEngineId()));
129         discEngineIdContextIn.setContextName("");
130         discEngineIdContextIn.setUsmAgent(usmAgent);
131 
132         discTimeLineContextIn = new SnmpContextv3Discovery(context.getSendToHostAddress(),
133                 context.getPort(), context.getBindAddress(), context.getTypeSocket());
134         discTimeLineContextIn.setUserName(context.getUserName());
135         discTimeLineContextIn.setUseAuthentication(context.isUseAuthentication());
136         discTimeLineContextIn.setAuthenticationProtocol(context.getAuthenticationProtocol());
137         discTimeLineContextIn.setUserAuthenticationPassword(context.getUserAuthenticationPassword());
138         discTimeLineContextIn.setUsePrivacy(context.isUsePrivacy());
139         discTimeLineContextIn.setPrivacyProtocol(context.getPrivacyProtocol());
140         discTimeLineContextIn.setUserPrivacyPassword(context.getUserPrivacyPassword());
141         discTimeLineContextIn.setContextEngineId(SnmpUtilities.toBytes(usmAgent.getSnmpEngineId()));
142         discTimeLineContextIn.setContextName("");
143         discTimeLineContextIn.setUsmAgent(usmAgent);
144     }
145 
146     /**
147      * @param lcontext The listening context for incoming (discovery) requests
148      * @see SnmpContextv3#addRequestPduListener
149      */
150     public void addRequestPduListener(ListeningContextPool lcontext)
151             throws java.io.IOException {
152         if (AsnObject.debug > 4) {
153             System.out.println(getClass().getName()
154                     + ".addRequestPduListener(" + lcontext.toString() + ")");
155         }
156         discEngineIdContextIn.addRequestPduListener(this, lcontext);
157         discTimeLineContextIn.addRequestPduListener(this, lcontext);
158     }
159 
160     /**
161      * @param lcontext Stop listening on this listening context for incoming
162      *                 (discovery) requests
163      * @see SnmpContextv3#removeRequestPduListener
164      */
165     public void removeRequestPduListener(ListeningContextPool lcontext)
166             throws java.io.IOException {
167         if (AsnObject.debug > 4) {
168             System.out.println(getClass().getName()
169                     + ".removeRequestPduListener(" + lcontext.toString() + ")");
170         }
171         discEngineIdContextIn.removeRequestPduListener(this, lcontext);
172         discTimeLineContextIn.removeRequestPduListener(this, lcontext);
173     }
174 
175     /**
176      * Receiving an incoming (discovery) PDU request.
177      */
178     public void requestPduReceived(RequestPduEvent evt) {
179         Object src = evt.getSource();
180         int port = evt.getHostPort();
181         Pdu orgPdu = evt.getPdu();
182         if (src == discEngineIdContextIn) {
183             sendEngineIdReport(orgPdu, port);
184         } else {
185             sendTimeLineReport(orgPdu, port);
186         }
187     }
188 
189     /**
190      * Send back the snmp engine ID.
191      */
192     protected void sendEngineIdReport(Pdu orgPdu, int port) {
193         if (AsnObject.debug > 4) {
194             System.out.println(getClass().getName() + ".sendEngineIdReport()");
195         }
196 
197         try {
198             if (discEngineIdContextOut != null) {
199                 discEngineIdContextOut.destroy();
200             }
201             discEngineIdContextOut = new SnmpContextv3(discEngineIdContextIn.getHost(),
202                     port, discEngineIdContextIn.getBindAddress(),
203                     discEngineIdContextIn.getTypeSocket());
204             discEngineIdContextOut = (SnmpContextv3) discEngineIdContextIn.cloneParameters(discEngineIdContextOut);
205 
206             Pdu repPdu = new ReportPdu(discEngineIdContextOut, orgPdu);
207             repPdu.addOid(usmStatsConstants.usmStatsUnknownEngineIDs,
208                     new AsnUnsInteger(usmAgent.getUsmStatsUnknownEngineIDs()));
209             repPdu.send();
210         } catch (java.io.IOException iexc) {
211             if (AsnObject.debug > 4) {
212                 System.out.println(getClass().getName() + ".sendEngineIdReport(): "
213                         + "IOException: " + iexc.getMessage());
214             }
215         } catch (PduException pexc) {
216             if (AsnObject.debug > 4) {
217                 System.out.println(getClass().getName() + ".sendEngineIdReport(): "
218                         + "PduException: " + pexc.getMessage());
219             }
220         }
221     }
222 
223     /**
224      * Send back the time lininess
225      */
226     protected void sendTimeLineReport(Pdu orgPdu, int port) {
227         if (AsnObject.debug > 4) {
228             System.out.println(getClass().getName() + ".sendTimeLineReport()");
229         }
230 
231         try {
232             if (discTimeLineContextOut != null) {
233                 discTimeLineContextOut.destroy();
234             }
235             discTimeLineContextOut = new SnmpContextv3(discTimeLineContextIn.getHost(),
236                     port, discTimeLineContextIn.getBindAddress(),
237                     discTimeLineContextIn.getTypeSocket());
238             discTimeLineContextOut = (SnmpContextv3) discTimeLineContextIn.cloneParameters(discTimeLineContextOut);
239 
240             Pdu repPdu = new ReportPdu(discTimeLineContextOut, orgPdu);
241             repPdu.addOid(usmStatsConstants.usmStatsNotInTimeWindows,
242                     new AsnUnsInteger(usmAgent.getUsmStatsNotInTimeWindows()));
243             repPdu.send();
244         } catch (java.io.IOException iexc) {
245             if (AsnObject.debug > 4) {
246                 System.out.println(getClass().getName() + ".sendTimeLineReport(): "
247                         + "IOException: " + iexc.getMessage());
248             }
249         } catch (PduException pexc) {
250             if (AsnObject.debug > 4) {
251                 System.out.println(getClass().getName() + ".sendEngineIdReport(): "
252                         + "PduException: " + pexc.getMessage());
253             }
254         }
255     }
256 
257     /**
258      * Destroys all the contexts in use.
259      */
260     public void freeResources() {
261         discEngineIdContextIn.destroy();
262         discEngineIdContextIn = null;
263 
264         discTimeLineContextIn.destroy();
265         discTimeLineContextIn = null;
266 
267         if (discEngineIdContextOut != null) {
268             discEngineIdContextOut.destroy();
269             discEngineIdContextOut = null;
270         }
271         if (discTimeLineContextOut != null) {
272             discTimeLineContextOut.destroy();
273             discTimeLineContextOut = null;
274         }
275     }
276 
277 }