View Javadoc
1   package org.metricshub.ipmi.core.sm.states;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * IPMI Java Client
6    * ჻჻჻჻჻჻
7    * Copyright 2023 Verax Systems, MetricsHub
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
23   */
24  
25  import org.metricshub.ipmi.core.coding.commands.session.GetChannelAuthenticationCapabilities;
26  import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanResponse;
27  import org.metricshub.ipmi.core.coding.protocol.AuthenticationType;
28  import org.metricshub.ipmi.core.coding.protocol.IpmiMessage;
29  import org.metricshub.ipmi.core.coding.protocol.decoder.ProtocolDecoder;
30  import org.metricshub.ipmi.core.coding.protocol.decoder.Protocolv15Decoder;
31  import org.metricshub.ipmi.core.coding.rmcp.RmcpMessage;
32  import org.metricshub.ipmi.core.common.TypeConverter;
33  import org.metricshub.ipmi.core.sm.StateMachine;
34  import org.metricshub.ipmi.core.sm.actions.ErrorAction;
35  import org.metricshub.ipmi.core.sm.actions.ResponseAction;
36  import org.metricshub.ipmi.core.sm.events.DefaultAck;
37  import org.metricshub.ipmi.core.sm.events.StateMachineEvent;
38  import org.metricshub.ipmi.core.sm.events.Timeout;
39  
40  /**
41   * Waiting for the {@link GetChannelAuthenticationCapabilities} response, the first one of the handshake. Back to
42   * {@link Uninitialized} on {@link Timeout}, on a reply that cannot be decoded or carries an error completion code, and
43   * on {@link DefaultAck} once the reply was read: Get Channel Cipher Suites comes next.
44   */
45  public class AuthcapWaiting extends State {
46  
47  	private int tag;
48  
49  	/**
50  	 * Instantiates a new {@link AuthcapWaiting} using the given tag
51  	 *
52  	 * @param tag int value representing the authentication capabilities
53  	 */
54  	public AuthcapWaiting(int tag) {
55  		this.tag = tag;
56  	}
57  
58  	@Override
59  	public void doTransition(StateMachine stateMachine, StateMachineEvent machineEvent) {
60  		if (machineEvent instanceof Timeout || machineEvent instanceof DefaultAck) {
61  			stateMachine.setCurrent(new Uninitialized());
62  		} else {
63  			stateMachine.doExternalAction(new ErrorAction(new IllegalArgumentException("Invalid transition")));
64  		}
65  	}
66  
67  	@Override
68  	public void doAction(StateMachine stateMachine, RmcpMessage message) {
69  		if (ProtocolDecoder.decodeAuthenticationType(message) == AuthenticationType.RMCPPlus) {
70  			return; // this isn't IPMI v1.5 message so we ignore it
71  		}
72  		Protocolv15Decoder decoder = new Protocolv15Decoder();
73  		IpmiMessage ipmiMessage = null;
74  		try {
75  			ipmiMessage = decoder.decode(message);
76  			GetChannelAuthenticationCapabilities capabilities = new GetChannelAuthenticationCapabilities();
77  			if (capabilities.isCommandResponse(ipmiMessage)
78  					&& TypeConverter.byteToInt(((IpmiLanResponse) ipmiMessage.getPayload()).getSequenceNumber()) == tag) {
79  				stateMachine.doExternalAction(new ResponseAction(capabilities.getResponseData(ipmiMessage)));
80  			}
81  		} catch (Exception e) {
82  			stateMachine.setCurrent(new Uninitialized());
83  			stateMachine.doExternalAction(new ErrorAction(e));
84  		}
85  	}
86  
87  }