-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathclient.go
266 lines (210 loc) · 5.58 KB
/
client.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package ipmi
import (
"context"
"fmt"
"sync"
"time"
"golang.org/x/net/proxy"
)
type Interface string
const (
InterfaceLan Interface = "lan"
InterfaceLanplus Interface = "lanplus"
InterfaceOpen Interface = "open"
InterfaceTool Interface = "tool"
DefaultExchangeTimeoutSec int = 20
DefaultKeepAliveIntervalSec int = 30
DefaultBufferSize int = 1024
)
type Client struct {
Host string
Port int
Username string // length must <= 16
Password string
Interface Interface
debug bool
maxPrivilegeLevel PrivilegeLevel
responderAddr uint8
responderLUN uint8
requesterAddr uint8
requesterLUN uint8
openipmi *openipmi
session *session
// this flags controls which IPMI version (1.5 or 2.0) be used by Client to send Request
v20 bool
udpClient *UDPClient
timeout time.Duration
bufferSize int
l sync.Mutex
// closedCh is closed when Client.Close() is called.
// used to notify other goroutines that Client is closed.
closedCh chan bool
}
func NewOpenClient() (*Client, error) {
myAddr := BMC_SA
return &Client{
Interface: "open",
timeout: time.Second * time.Duration(DefaultExchangeTimeoutSec),
bufferSize: DefaultBufferSize,
openipmi: &openipmi{
myAddr: myAddr,
targetAddr: myAddr,
},
}, nil
}
// NewToolClient creates an IPMI client based ipmitool.
// You should pass the file path of ipmitool binary or path of a wrapper script
// that would be executed.
func NewToolClient(path string) (*Client, error) {
return &Client{
Host: path,
Interface: "tool",
}, nil
}
func NewClient(host string, port int, user string, pass string) (*Client, error) {
if len(user) > IPMI_MAX_USER_NAME_LENGTH {
return nil, fmt.Errorf("user name (%s) too long, exceed (%d) characters", user, IPMI_MAX_USER_NAME_LENGTH)
}
c := &Client{
Host: host,
Port: port,
Username: user,
Password: pass,
Interface: InterfaceLanplus,
v20: true,
timeout: time.Second * time.Duration(DefaultExchangeTimeoutSec),
bufferSize: DefaultBufferSize,
maxPrivilegeLevel: PrivilegeLevelUnspecified,
responderAddr: BMC_SA,
responderLUN: uint8(IPMB_LUN_BMC),
requesterAddr: RemoteConsole_SWID,
requesterLUN: 0x00,
session: &session{
// IPMI Request Sequence, start from 1
ipmiSeq: 1,
v20: v20{
state: SessionStatePreSession,
cipherSuiteID: CipherSuiteIDReserved,
},
v15: v15{
active: false,
},
},
closedCh: make(chan bool),
}
c.udpClient = &UDPClient{
Host: host,
Port: port,
timeout: c.timeout,
bufferSize: c.bufferSize,
}
return c, nil
}
func (c *Client) WithInterface(intf Interface) *Client {
c.Interface = intf
return c
}
func (c *Client) WithDebug(debug bool) *Client {
c.debug = debug
return c
}
func (c *Client) WithUDPProxy(proxy proxy.Dialer) *Client {
if c.udpClient != nil {
c.udpClient.SetProxy(proxy)
}
return c
}
func (c *Client) WithTimeout(timeout time.Duration) *Client {
c.timeout = timeout
if c.udpClient != nil {
c.udpClient.timeout = timeout
}
return c
}
func (c *Client) WithBufferSize(bufferSize int) *Client {
c.bufferSize = bufferSize
if c.udpClient != nil {
c.udpClient.bufferSize = bufferSize
}
return c
}
// WithCipherSuiteID sets a custom cipher suite which is used during OpenSession command.
// It is only valid for client with IPMI lanplus interface.
// For the custom cipherSuiteID to take effect, you must call WithCipherSuiteID before calling Connect method.
func (c *Client) WithCipherSuiteID(cipherSuiteID CipherSuiteID) *Client {
if c.session != nil {
c.session.v20.cipherSuiteID = cipherSuiteID
}
return c
}
// WithMaxPrivilegeLevel sets a specified session privilege level to use.
func (c *Client) WithMaxPrivilegeLevel(privilegeLevel PrivilegeLevel) *Client {
c.maxPrivilegeLevel = privilegeLevel
return c
}
func (c *Client) WithResponderAddr(responderAddr, responderLUN uint8) {
c.responderAddr = responderAddr
c.responderLUN = responderLUN
}
func (c *Client) WithRequesterAddr(requesterAddr, requesterLUN uint8) {
c.requesterAddr = requesterAddr
c.requesterLUN = requesterLUN
}
func (c *Client) SessionPrivilegeLevel() PrivilegeLevel {
return c.maxPrivilegeLevel
}
// Connect connects to the bmc by specified Interface.
func (c *Client) Connect(ctx context.Context) error {
// Optional RMCP Ping/Pong mechanism
// pongRes, err := c.RmcpPing()
// if err != nil {
// return fmt.Errorf("RMCP Ping failed, err: %w", err)
// }
// if pongRes.IPMISupported {
// return fmt.Errorf("ipmi not supported")
// }
switch c.Interface {
case "", InterfaceOpen:
var devnum int32 = 0
return c.ConnectOpen(ctx, devnum)
case InterfaceTool:
var devnum int32 = 0
return c.ConnectTool(ctx, devnum)
case InterfaceLanplus:
c.v20 = true
return c.Connect20(ctx)
case InterfaceLan:
c.v20 = false
return c.Connect15(ctx)
default:
return fmt.Errorf("not supported interface, supported: lan,lanplus,open")
}
}
func (c *Client) Close(ctx context.Context) error {
switch c.Interface {
case "", InterfaceOpen:
return c.closeOpen(ctx)
case InterfaceTool:
return c.closeTool(ctx)
case InterfaceLan, InterfaceLanplus:
return c.closeLAN(ctx)
}
return nil
}
func (c *Client) Exchange(ctx context.Context, request Request, response Response) error {
switch c.Interface {
case "", InterfaceOpen:
return c.exchangeOpen(ctx, request, response)
case InterfaceTool:
return c.exchangeTool(ctx, request, response)
case InterfaceLan, InterfaceLanplus:
return c.exchangeLAN(ctx, request, response)
}
return nil
}
func (c *Client) lock() {
c.l.Lock()
}
func (c *Client) unlock() {
c.l.Unlock()
}