-
Notifications
You must be signed in to change notification settings - Fork 7
/
rmcp.go
109 lines (90 loc) · 2.28 KB
/
rmcp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package virtualbmc
import (
"bytes"
"encoding/binary"
"fmt"
"io"
)
type remoteManagementControlProtocolHeader struct {
Version uint8
Reserved uint8
Sequence uint8
Class uint8
}
const (
RmcpVersion1 = 0x06
)
const (
RmcpClassAsf = 0x06
RmcpClassIpmi = 0x07
RmcpClassOem = 0x08
)
func handleRMCPRequest(buf io.Reader, machine Machine, session *rmcpPlusSessionHolder, bmcUser *bmcUserHolder) ([]byte, error) {
rmcp, err := newRMCP(buf)
if err != nil {
return nil, err
}
res, err := rmcp.handle(buf, machine, session, bmcUser)
if err != nil {
return nil, err
}
return res, nil
}
func newRMCP(buf io.Reader) (*remoteManagementControlProtocolHeader, error) {
header, err := deserializeRMCPHeader(buf)
if err != nil {
return nil, err
}
return header, nil
}
func (r *remoteManagementControlProtocolHeader) handle(buf io.Reader, machine Machine, session *rmcpPlusSessionHolder, bmcUser *bmcUserHolder) ([]byte, error) {
var class string
switch r.Class {
case RmcpClassIpmi:
ipmiSession, err := newIPMISession(buf)
if err != nil {
return nil, err
}
res, err := ipmiSession.handle(buf, machine, session, bmcUser)
if err != nil {
return nil, err
}
return appendRMCPHeader(res)
case RmcpClassAsf:
class = "ASF"
case RmcpClassOem:
class = "OEM"
}
return nil, fmt.Errorf("unsupported Class: %s %d", class, r.Class)
}
func appendRMCPHeader(response []byte) ([]byte, error) {
obuf := bytes.Buffer{}
rmcp := buildUpRMCPForIPMI()
if err := serializeRMCP(&obuf, rmcp); err != nil {
return nil, err
}
if err := binary.Write(&obuf, binary.LittleEndian, response); err != nil {
return nil, err
}
return obuf.Bytes(), nil
}
func deserializeRMCPHeader(buf io.Reader) (*remoteManagementControlProtocolHeader, error) {
header := &remoteManagementControlProtocolHeader{}
if err := binary.Read(buf, binary.LittleEndian, header); err != nil {
return nil, err
}
return header, nil
}
func serializeRMCP(buf *bytes.Buffer, header remoteManagementControlProtocolHeader) error {
if err := binary.Write(buf, binary.LittleEndian, header); err != nil {
return err
}
return nil
}
func buildUpRMCPForIPMI() (rmcp remoteManagementControlProtocolHeader) {
rmcp.Version = RmcpVersion1
rmcp.Reserved = 0x00
rmcp.Sequence = 0xff
rmcp.Class = RmcpClassIpmi
return rmcp
}