Class IpmiConnector
java.lang.Object
org.metricshub.ipmi.core.api.sync.IpmiConnector
Synchronous API for connecting to BMC via IPMI.
Creating connection consists of the following steps:
- Create a
Connectionand get itsConnectionHandlewithcreateConnection(InetAddress) - Get
CipherSuites that are available for the connection viagetAvailableCipherSuites(ConnectionHandle) - Pick
CipherSuiteandPrivilegeLevelthat will be used during session and getGetChannelAuthenticationCapabilitiesResponseDatato find out allowed authentication options viagetChannelAuthenticationCapabilities(ConnectionHandle, CipherSuite, PrivilegeLevel) - Provide username,
password and (if the BMC needs it) the BMC Kg key and start session via
openSession(ConnectionHandle, String, String, byte[])oropenSession(ConnectionHandle, String, byte[], byte[])(password as bytes, which the caller can clear), which also raises it to the requested Operator or Administrator privilege
Send commands with sendMessage(ConnectionHandle, IpmiCommandCoder), which returns the typed response of
the command, and close the session with closeSession(ConnectionHandle), in a finally block: BMCs have few
session slots.
When done with work, clean up via tearDown()
-
Constructor Summary
ConstructorsConstructorDescriptionIpmiConnector(int port) StartsIpmiConnectorand initiates theConnectionManagerat the given port.IpmiConnector(int port, long pingPeriod) StartsIpmiConnectorand initiates theConnectionManagerat the given port.IpmiConnector(int port, InetAddress address) StartsIpmiConnectorand initiates theConnectionManagerat the given port and IP interface. -
Method Summary
Modifier and TypeMethodDescriptionvoidcloseConnection(ConnectionHandle handle) Closes the connection with the given handle, without closing its session: the requests still waiting for a reply on the connection fail, with aConnectionExceptionor anIllegalStateException.voidcloseSession(ConnectionHandle connectionHandle) Closes the session with the remote host if it is currently in open state, and wipes the password and the BMC key (Kg) the handle keeps.createConnection(InetAddress address) Creates connection to the remote host on default IPMI port.createConnection(InetAddress address, int port) Creates connection to the remote host on specified port.createConnection(InetAddress address, int port, CipherSuite cipherSuite, PrivilegeLevel privilegeLevel) Creates connection to the remote host, with pre setCipherSuiteandPrivilegeLevel, skipping the getAvailableCipherSuites and getChannelAuthenticationCapabilities phases.createConnection(InetAddress address, CipherSuite cipherSuite, PrivilegeLevel privilegeLevel) Creates connection to the remote host, with pre setCipherSuiteandPrivilegeLevel, skipping the getAvailableCipherSuites and getChannelAuthenticationCapabilities phases.getAvailableCipherSuites(ConnectionHandle connectionHandle) GetsCipherSuites available for the connection with the remote host.getChannelAuthenticationCapabilities(ConnectionHandle connectionHandle, CipherSuite cipherSuite, PrivilegeLevel requestedPrivilegeLevel) Declares the cipher suite and the privilege of the session, and returns the authentication capabilities of the remote host thatgetAvailableCipherSuites(ConnectionHandle)read (requested at the User level): sends nothing.getExistingSessionForCriteria(InetAddress remoteAddress, int remotePort, String user) Returns session already bound to given connection handle fulfilling given criteria.intReturns how many times a message without a reply is sent again.intgetTimeout(ConnectionHandle handle) Returns the timeout of a single message on the connection with the given handle.openSession(ConnectionHandle connectionHandle, String username, byte[] password, byte[] bmcKey) Establishes the session with the remote host, with the password as bytes: the caller can then clear them once the session is open, which an immutableStringdoes not allow.openSession(ConnectionHandle connectionHandle, String username, String password, byte[] bmcKey) Establishes the session with the remote host, and raises it to the requested privilege when that is Operator or Administrator (Set Session Privilege Level, IPMI 2.0 section 22.18: every session starts at User).voidRegistersInboundMessageListenerthat will react on any request sent from remote system to the application.retryMessage(ConnectionHandle connectionHandle, byte tag, PayloadType messagePayloadType) Re-sends message with given tag having givenPayloadType, using passedConnectionHandle.<R extends ResponseData>
RsendMessage(ConnectionHandle connectionHandle, IpmiCommandCoder<R> request) Sends the IPMI command to the remote host and returns its typed response.<R extends ResponseData>
RsendMessage(ConnectionHandle connectionHandle, IpmiCommandCoder<R> request, int resends) Sends the IPMI command to the remote host with its own number of resends when no reply comes, instead ofgetRetries(): 0 for a command the BMC must not receive twice (a chassis reset, whose reply can be lost after the BMC carried it out).sendMessage(ConnectionHandle connectionHandle, PayloadCoder request) Sends the IPMI message to the remote host.voidsendOneWayMessage(ConnectionHandle connectionHandle, PayloadCoder request) Sends the IPMI message to the remote host and doesn't wait for any response.voidsetRetries(int retries) Sets how many times a message without a reply is sent again, or a login whose step got no reply is started again from Open Session.voidsetTimeout(int timeout) Sets the timeout of the messages of the connections created from now on;setTimeout(ConnectionHandle, int)changes it for an existing connection.voidsetTimeout(ConnectionHandle handle, int timeout) Changes the timeout value for connection with the given handle.voidtearDown()Finalizes the connector and closes all connections, without closing their sessions: the requests still waiting for a reply fail, with aConnectionExceptionor anIllegalStateException.voidUnregisters anInboundMessageListener, which then no longer receives the requests of the remote systems.
-
Constructor Details
-
IpmiConnector
StartsIpmiConnectorand initiates theConnectionManagerat the given port. Wildcard IP address will be used. Keep-alive messages are sent every 30 000 ms (ConnectionManager.DEFAULT_PING_PERIOD).- Parameters:
port- - the local UDP port thatIpmiAsyncConnectorbinds to communicate with the remote hosts, 0 for any free port- Throws:
IOException- when the port cannot be bound
-
IpmiConnector
StartsIpmiConnectorand initiates theConnectionManagerat the given port and IP interface. Keep-alive messages are sent every 30 000 ms (ConnectionManager.DEFAULT_PING_PERIOD).- Parameters:
port- - the local UDP port thatIpmiAsyncConnectorbinds to communicate with the remote hosts, 0 for any free portaddress- - the IP address that will be used byIpmiAsyncConnectorto communicate with the remote hosts.- Throws:
IOException- when the port cannot be bound
-
IpmiConnector
StartsIpmiConnectorand initiates theConnectionManagerat the given port. Wildcard IP address will be used.- Parameters:
port- the local UDP port thatIpmiAsyncConnectorbinds to communicate with the remote hosts, 0 for any free portpingPeriod- the period in milliseconds used to send the keep alive messages.
0 If keep-alive messages should be disabled.- Throws:
IOException- When IpmiAsyncConnector cannot be created.
-
-
Method Details
-
createConnection
Creates connection to the remote host on default IPMI port.- Parameters:
address- -InetAddressof the remote host- Returns:
- handle to the connection to the remote host
- Throws:
IOException- when the connection cannot be created
-
createConnection
Creates connection to the remote host on specified port.- Parameters:
address- -InetAddressof the remote hostport- - remote UDP port- Returns:
- handle to the connection to the remote host
- Throws:
IOException- when the connection cannot be created
-
createConnection
public ConnectionHandle createConnection(InetAddress address, CipherSuite cipherSuite, PrivilegeLevel privilegeLevel) throws IOException Creates connection to the remote host, with pre setCipherSuiteandPrivilegeLevel, skipping the getAvailableCipherSuites and getChannelAuthenticationCapabilities phases.- Parameters:
address- -InetAddressof the remote hostcipherSuite- - the cipher suite of the sessionprivilegeLevel- - the privilege level requested for the session- Returns:
- handle to the connection to the remote host
- Throws:
IOException- when the connection cannot be created
-
createConnection
public ConnectionHandle createConnection(InetAddress address, int port, CipherSuite cipherSuite, PrivilegeLevel privilegeLevel) throws IOException Creates connection to the remote host, with pre setCipherSuiteandPrivilegeLevel, skipping the getAvailableCipherSuites and getChannelAuthenticationCapabilities phases.- Parameters:
address- -InetAddressof the remote hostport- - remote UDP portcipherSuite- - the cipher suite of the sessionprivilegeLevel- - the privilege level requested for the session- Returns:
- handle to the connection to the remote host
- Throws:
IOException- when the connection cannot be created
-
getAvailableCipherSuites
public List<CipherSuite> getAvailableCipherSuites(ConnectionHandle connectionHandle) throws Exception GetsCipherSuites available for the connection with the remote host.- Parameters:
connectionHandle-ConnectionHandleto the connection created before- Returns:
- list of the
CipherSuites that are allowed during the connection - Throws:
Ipmi20UnsupportedException- when the BMC does not support IPMI 2.0 (RMCP+): Get Channel Authentication Capabilities, sent first in the IPMI v1.5 format, reports no IPMI v2.0 connectionsException- when sending message to the managed system fails- See Also:
-
getChannelAuthenticationCapabilities
public GetChannelAuthenticationCapabilitiesResponseData getChannelAuthenticationCapabilities(ConnectionHandle connectionHandle, CipherSuite cipherSuite, PrivilegeLevel requestedPrivilegeLevel) throws Exception Declares the cipher suite and the privilege of the session, and returns the authentication capabilities of the remote host thatgetAvailableCipherSuites(ConnectionHandle)read (requested at the User level): sends nothing.- Parameters:
connectionHandle- -ConnectionHandleassociated with the hostcipherSuite- -CipherSuitethat will be used during the connectionrequestedPrivilegeLevel- -PrivilegeLevelthat is requested for the session- Returns:
- -
GetChannelAuthenticationCapabilitiesResponseData - Throws:
ConnectionException- when connection is in the state that does not allow to perform this operation.Exception- when the state machine fails
-
openSession
public Session openSession(ConnectionHandle connectionHandle, String username, String password, byte[] bmcKey) throws Exception Establishes the session with the remote host, and raises it to the requested privilege when that is Operator or Administrator (Set Session Privilege Level, IPMI 2.0 section 22.18: every session starts at User). ForPrivilegeLevel.MaximumAvailable, the session is requested at Administrator, then at Operator and at User when the BMC refuses the role to the account, and set to User right after the handshake: the caller raises it when a command needs more.- Parameters:
connectionHandle- -ConnectionHandleassociated with the remote host.username- - the usernamepassword- - password matching the usernamebmcKey- - the key that should be provided if the two-key authentication is enabled, null otherwise.- Returns:
- object representing newly created
Session - Throws:
ConnectionException- when connection is in the state that does not allow to perform this operation, or no reply came after all the triesSessionException- when the BMC refuses to raise the session to the requested privilege: the session is closed, and the cause is theIPMIExceptionof the completion codeException- when sending message to the managed system or initializing one of the cipherSuite's algorithms fails
-
openSession
public Session openSession(ConnectionHandle connectionHandle, String username, byte[] password, byte[] bmcKey) throws Exception Establishes the session with the remote host, with the password as bytes: the caller can then clear them once the session is open, which an immutableStringdoes not allow. When the requested privilege is Operator or Administrator, the session is raised to it with Set Session Privilege Level right after the handshake (IPMI 2.0 section 22.18: every session starts at User); a refusal closes the session. ForPrivilegeLevel.MaximumAvailable, the session is requested at Administrator, then at Operator and at User when the BMC refuses the role to the account, and set to User right after the handshake: the caller raises it when a command needs more.- Parameters:
connectionHandle- -ConnectionHandleassociated with the remote host.username- - the usernamepassword- - password matching the username, as sent to the BMC (UTF-8 for a text password), at most 20 bytesbmcKey- - the key that should be provided if the two-key authentication is enabled, null otherwise.- Returns:
- object representing newly created
Session - Throws:
ConnectionException- when connection is in the state that does not allow to perform this operation, or no reply came after all the triesSessionException- when the BMC refuses to raise the session to the requested privilege: the session is closed, and the cause is theIPMIExceptionof the completion codeException- when sending message to the managed system or initializing one of the cipherSuite's algorithms fails
-
getExistingSessionForCriteria
public Session getExistingSessionForCriteria(InetAddress remoteAddress, int remotePort, String user) Returns session already bound to given connection handle fulfilling given criteria.- Parameters:
remoteAddress- IP addres of the managed systemremotePort- UDP port of the managed systemuser- IPMI user for whom the connection is established- Returns:
- session object fulfilling given criteria, or null if no session was registered for such connection.
-
closeSession
Closes the session with the remote host if it is currently in open state, and wipes the password and the BMC key (Kg) the handle keeps.- Parameters:
connectionHandle- -ConnectionHandleassociated with the remote host.- Throws:
ConnectionException- when connection is in the state that does not allow to perform this operation.Exception- when sending message to the managed system or initializing one of the cipherSuite's algorithms fails
-
sendMessage
public ResponseData sendMessage(ConnectionHandle connectionHandle, PayloadCoder request) throws Exception Sends the IPMI message to the remote host.- Parameters:
connectionHandle- -ConnectionHandleassociated with the remote host.request- -PayloadCodercontaining the request to be sent- Returns:
ResponseDatafor the request- Throws:
ConnectionException- when connection is in the state that does not allow to perform this operation.Exception- when sending message to the managed system or initializing one of the cipherSuite's algorithms fails
-
sendMessage
public <R extends ResponseData> R sendMessage(ConnectionHandle connectionHandle, IpmiCommandCoder<R> request) throws Exception Sends the IPMI command to the remote host and returns its typed response.- Type Parameters:
R- the type of the response data of the command- Parameters:
connectionHandle- -ConnectionHandleassociated with the remote host.request- - the command to send- Returns:
- the response data of the command
- Throws:
ConnectionException- when connection is in the state that does not allow to perform this operation.IPMIException- when the BMC answers with an error completion codeException- when sending message to the managed system or initializing one of the cipherSuite's algorithms fails
-
sendMessage
public <R extends ResponseData> R sendMessage(ConnectionHandle connectionHandle, IpmiCommandCoder<R> request, int resends) throws Exception Sends the IPMI command to the remote host with its own number of resends when no reply comes, instead ofgetRetries(): 0 for a command the BMC must not receive twice (a chassis reset, whose reply can be lost after the BMC carried it out). The limit also covers aC3hreply (timeout on the BMC side, response unavailable), which says nothing about what was done; a busy reply, which says the command was not carried out, is still retriedgetRetries()times.- Type Parameters:
R- the type of the response data of the command- Parameters:
connectionHandle- -ConnectionHandleassociated with the remote host.request- - the command to sendresends- how many times the command is sent again when no reply comes within the timeout, 0 to send it once- Returns:
- the response data of the command
- Throws:
ConnectionException- when connection is in the state that does not allow to perform this operation, or no reply cameIPMIException- when the BMC answers with an error completion codeException- when sending message to the managed system or initializing one of the cipherSuite's algorithms fails
-
sendOneWayMessage
public void sendOneWayMessage(ConnectionHandle connectionHandle, PayloadCoder request) throws Exception Sends the IPMI message to the remote host and doesn't wait for any response.- Parameters:
connectionHandle- -ConnectionHandleassociated with the remote host.request- -PayloadCodercontaining the request to be sent- Throws:
ConnectionException- when connection is in the state that does not allow to perform this operation.Exception- when sending message to the managed system or initializing one of the cipherSuite's algorithms fails
-
retryMessage
public ResponseData retryMessage(ConnectionHandle connectionHandle, byte tag, PayloadType messagePayloadType) throws Exception Re-sends message with given tag having givenPayloadType, using passedConnectionHandle.- Parameters:
connectionHandle- -ConnectionHandleassociated with the remote host.tag- - tag of the message to retrymessagePayloadType- -PayloadTypeof the message that should be retried- Returns:
ResponseDatafor the re-sent request or null if message could not be resent.- Throws:
Exception- when sending message to the managed system fails
-
registerIncomingMessageListener
RegistersInboundMessageListenerthat will react on any request sent from remote system to the application.- Parameters:
listener- listener to be registered.
-
unregisterIncomingMessageListener
Unregisters anInboundMessageListener, which then no longer receives the requests of the remote systems.- Parameters:
listener- listener to be unregistered.
-
closeConnection
Closes the connection with the given handle, without closing its session: the requests still waiting for a reply on the connection fail, with aConnectionExceptionor anIllegalStateException. The password and the BMC key (Kg) the handle keeps are wiped.- Parameters:
handle- the handle of the connection
-
tearDown
public void tearDown()Finalizes the connector and closes all connections, without closing their sessions: the requests still waiting for a reply fail, with aConnectionExceptionor anIllegalStateException. The password and the BMC key (Kg) of the sessions are wiped. -
setTimeout
Changes the timeout value for connection with the given handle.- Parameters:
handle- -ConnectionHandleassociated with the remote host.timeout- - new timeout value in ms
-
getTimeout
Returns the timeout of a single message on the connection with the given handle.- Parameters:
handle- -ConnectionHandleassociated with the remote host.- Returns:
- the timeout in ms after which a message without a reply is reported as timed out
-
getRetries
public int getRetries()Returns how many times a message without a reply is sent again.- Returns:
- the number of resends
-
setRetries
public void setRetries(int retries) Sets how many times a message without a reply is sent again, or a login whose step got no reply is started again from Open Session.- Parameters:
retries- the number of resends, 0 to send every message once- Throws:
IllegalArgumentException- when negative
-
setTimeout
public void setTimeout(int timeout) Sets the timeout of the messages of the connections created from now on;setTimeout(ConnectionHandle, int)changes it for an existing connection. 5 000 ms by default.- Parameters:
timeout- the time in milliseconds after which a message without a reply is reported as timed out- Throws:
IllegalArgumentException- when the timeout is less than one millisecond
-