View Javadoc
1   package org.metricshub.ipmi.core.coding.protocol.encoder;
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.protocol.AuthenticationType;
26  import org.metricshub.ipmi.core.common.TypeConverter;
27  
28  /**
29   * Adds IPMI session header to encrypted payload. Should be used to encode
30   * regular RMCP+ messages (excluding Open Session and RAKP Messages)
31   */
32  public abstract class ProtocolEncoder implements IpmiEncoder {
33  
34  	protected byte encodeAuthenticationType(AuthenticationType authenticationType) {
35  		return TypeConverter.intToByte(authenticationType.getCode());
36  	}
37  
38  	/**
39  	 * Converts integer value into byte array using little endian convention and
40  	 * inserts it into message at given offset.
41  	 *
42  	 * @param value
43  	 * @param message
44  	 *        - IPMI message being created
45  	 * @param offset
46  	 * @throws IndexOutOfBoundsException
47  	 *         when message is too short to hold value at given offset
48  	 */
49  	protected void encodeInt(int value, byte[] message, int offset) {
50  		byte[] array = TypeConverter.intToLittleEndianByteArray(value);
51  
52  		if (array.length + offset > message.length) {
53  			throw new IndexOutOfBoundsException("Message is too short");
54  		}
55  
56  		System.arraycopy(array, 0, message, offset, array.length);
57  	}
58  
59  	/**
60  	 * Encodes session sequence number and inserts it into message at given
61  	 * offset.
62  	 *
63  	 * @param value
64  	 * @param message
65  	 *        - IPMI message being created
66  	 * @param offset
67  	 * @throws IndexOutOfBoundsException
68  	 *         when message is too short to hold value at given offset
69  	 */
70  	protected void encodeSessionSequenceNumber(int value, byte[] message, int offset) {
71  		encodeInt(value, message, offset);
72  	}
73  
74  	/**
75  	 * Encodes session id and inserts it into message at given offset.
76  	 *
77  	 * @param value
78  	 * @param message
79  	 *        - IPMI message being created
80  	 * @param offset
81  	 * @throws IndexOutOfBoundsException
82  	 *         when message is too short to hold value at given offset
83  	 */
84  	protected void encodeSessionId(int value, byte[] message, int offset) {
85  		encodeInt(value, message, offset);
86  	}
87  
88  	/**
89  	 * Encodes payload length and inserts it into message at given offset.
90  	 *
91  	 * @param value
92  	 * @param message
93  	 *        - IPMI message being created
94  	 * @param offset
95  	 * @throws IndexOutOfBoundsException
96  	 *         when message is too short to hold value at given offset
97  	 */
98  	protected abstract void encodePayloadLength(int value, byte[] message, int offset);
99  
100 	/**
101 	 * Encodes payload and inserts it into message at given offset. <br>
102 	 * When payload == null it will not be inserted.
103 	 *
104 	 * @param payload
105 	 *        - IPMI payload to encode.
106 	 * @param message
107 	 *        - IPMI message being created.
108 	 * @param offset
109 	 * @throws IndexOutOfBoundsException
110 	 *         when message is too short to hold value at given offset
111 	 * @return offset + encoded message length
112 	 */
113 	protected int encodePayload(byte[] payload, byte[] message, int offset) {
114 		if (payload == null) {
115 			return offset;
116 		}
117 		if (payload.length + offset > message.length) {
118 			throw new IndexOutOfBoundsException("Message is too short");
119 		}
120 		System.arraycopy(payload, 0, message, offset, payload.length);
121 		return offset + payload.length;
122 	}
123 }