1 // NAME 2 // $RCSfile: SnmpContextv3Discovery.java,v $ 3 // DESCRIPTION 4 // [given below in javadoc format] 5 // DELTA 6 // $Revision: 3.11 $ 7 // CREATED 8 // $Date: 2009/03/05 13:12:50 $ 9 // COPYRIGHT 10 // Westhawk Ltd 11 // TO DO 12 // 13 14 /* 15 * Copyright (C) 2005 - 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 */ 26 27 package uk.co.westhawk.snmp.stack; 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 java.io.*; 52 import uk.co.westhawk.snmp.beans.*; 53 54 /** 55 * This class contains the SNMP v3 discovery context that is used by 56 * UsmBeingDiscoveredBean, when this stack is being discovered. 57 * Most of the work is done by SnmpContextv3Basis. 58 * 59 * <p> 60 * Now that the stack can send traps and receive requests, 61 * it needs to be able to act as an 62 * authoritative SNMP engine. This is done via the interface UsmAgent. 63 * The DefaultUsmAgent is not guaranteed to work; agents (or rather 64 * authoritative engines) <em>should</em> provide a better implementation. 65 * </p> 66 * 67 * @see DefaultUsmAgent 68 * @see UsmBeingDiscoveredBean 69 * 70 * @since 4_14 71 * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a> 72 * @version $Revision: 3.11 $ $Date: 2009/03/05 13:12:50 $ 73 */ 74 public class SnmpContextv3Discovery extends SnmpContextv3Basis { 75 private static final String version_id = "@(#)$Id: SnmpContextv3Discovery.java,v 3.11 2009/03/05 13:12:50 birgita Exp $ Copyright Westhawk Ltd"; 76 77 /** 78 * Constructor. 79 * 80 * @param host The host to which the PDU will be sent 81 * @param port The port where the SNMP server will be 82 * @see AbstractSnmpContext#AbstractSnmpContext(String, int) 83 */ 84 public SnmpContextv3Discovery(String host, int port) throws IOException { 85 super(host, port); 86 } 87 88 /** 89 * Constructor. 90 * Parameter typeSocketA should be either STANDARD_SOCKET, TCP_SOCKET or a 91 * fully qualified classname. 92 * 93 * @param host The host to which the Pdu will be sent 94 * @param port The port where the SNMP server will be 95 * @param typeSocketA The local address the server will bind to 96 * 97 * @see AbstractSnmpContext#AbstractSnmpContext(String, int, String) 98 */ 99 public SnmpContextv3Discovery(String host, int port, String typeSocketA) 100 throws IOException { 101 super(host, port, typeSocketA); 102 } 103 104 /** 105 * Constructor. 106 * Parameter typeSocketA should be either STANDARD_SOCKET, TCP_SOCKET or a 107 * fully qualified classname. 108 * 109 * @param host The host to which the PDU will be sent 110 * @param port The port where the SNMP server will be 111 * @param bindAddress The local address the server will bind to 112 * @param typeSocketA The type of socket to use. 113 * 114 * @see AbstractSnmpContext#AbstractSnmpContext(String, int, String, String) 115 * @see SnmpContextBasisFace#STANDARD_SOCKET 116 * @see SnmpContextBasisFace#TCP_SOCKET 117 * @since 4_14 118 */ 119 public SnmpContextv3Discovery(String host, int port, String bindAddress, String typeSocketA) 120 throws IOException { 121 super(host, port, bindAddress, typeSocketA); 122 } 123 124 /** 125 * Processes an incoming Discovery (and only Discovery) PDU. 126 * <p> 127 * See <a href="http://www.ietf.org/rfc/rfc3414.txt">SNMP-USER-BASED-SM-MIB</a>. 128 * </p> 129 * 130 * @see #rawPduReceived 131 */ 132 public Pdu processIncomingPdu(byte[] message) 133 throws DecodingException, IOException { 134 String msg = checkContextSanity(); 135 if (msg != null) { 136 throw new DecodingException(msg); 137 } 138 int l = message.length; 139 byte[] copyOfMessage = new byte[l]; 140 System.arraycopy(message, 0, copyOfMessage, 0, l); 141 142 AsnDecoderv3 rpdu = new AsnDecoderv3(); 143 ByteArrayInputStream in = new ByteArrayInputStream(message); 144 AsnSequence asnTopSeq = rpdu.DecodeSNMPv3(in); 145 int msgId = rpdu.getMessageId(asnTopSeq); 146 AsnPduSequence pduSeq = rpdu.processSNMPv3(this, asnTopSeq, copyOfMessage, true); 147 148 Pdu pdu = null; 149 if (pduSeq != null) { 150 byte type = pduSeq.getRespType(); 151 if (type == SnmpConstants.GET_REQ_MSG && pduSeq.isSnmpv3Discovery() == true) { 152 pdu = new GetPdu(this); 153 } else { 154 /* 155 * These cannot be sent as discovery pdu; 156 * SnmpConstants.GETNEXT_REQ_MSG 157 * SnmpConstants.SET_REQ_MSG 158 * SnmpConstants.GETBULK_REQ_MSG 159 * SnmpConstants.INFORM_REQ_MSG 160 * SnmpConstants.GET_RSP_MSG 161 * SnmpConstants.GET_RPRT_MSG 162 * SnmpConstants.TRPV2_REQ_MSG 163 */ 164 165 if (AsnObject.debug > 3) { 166 System.out.println(getClass().getName() 167 + ".ProcessIncomingPdu(): PDU received with type " 168 + pduSeq.getRespTypeString() 169 + ". Ignoring it."); 170 } 171 } 172 173 if (pdu != null) { 174 pdu.fillin(pduSeq); 175 pdu.snmpv3MsgId = new Integer(msgId); 176 } 177 } 178 return pdu; 179 } 180 181 /** 182 * Returns a clone of this SnmpContextv3. 183 * 184 * @exception CloneNotSupportedException Thrown when the constructor 185 * generates an IOException 186 */ 187 public Object clone() throws CloneNotSupportedException { 188 SnmpContextv3Discovery clContext = null; 189 try { 190 clContext = new SnmpContextv3Discovery(hostname, hostPort, bindAddr, typeSocket); 191 clContext = (SnmpContextv3Discovery) cloneParameters(clContext); 192 } catch (IOException exc) { 193 throw new CloneNotSupportedException("IOException " 194 + exc.getMessage()); 195 } 196 return clContext; 197 } 198 199 }