forked from bougou/go-ipmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
216 lines (169 loc) · 4 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
package ipmi
import (
"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
DefaultBufferSize int = 1024
)
type Client struct {
Host string
Port int
Username string // length must <= 16
Password string
Interface Interface
debug bool
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
}
func NewOpenClient() (*Client, error) {
myAddr := BMC_SA
return &Client{
Interface: "open",
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)
}
if len(user) == 0 {
return nil, fmt.Errorf("empty username")
}
if len(pass) == 0 {
return nil, fmt.Errorf("empty password")
}
c := &Client{
Host: host,
Port: port,
Username: user,
Password: pass,
Interface: "",
v20: true,
timeout: time.Second * time.Duration(DefaultExchangeTimeoutSec),
bufferSize: DefaultBufferSize,
session: &session{
// IPMI Request Sequence, start from 1
ipmiSeq: 1,
v20: v20{
state: SessionStatePreSession,
},
v15: v15{
active: false,
},
},
}
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 {
c.udpClient.SetProxy(proxy)
return c
}
func (c *Client) WithTimeout(timeout time.Duration) *Client {
c.timeout = timeout
c.udpClient.timeout = timeout
return c
}
func (c *Client) WithBufferSize(bufferSize int) *Client {
c.bufferSize = bufferSize
c.udpClient.bufferSize = bufferSize
return c
}
func (c *Client) SessionPrivilegeLevel() PrivilegeLevel {
return c.session.v20.maxPrivilegeLevel
}
// Connect connects to the bmc by specified Interface.
func (c *Client) Connect() error {
// Optional RMCP Ping/Pong mechanism
// pongRes, err := c.RmcpPing()
// if err != nil {
// return fmt.Errorf("RMCP Ping failed, err: %s", err)
// }
// if pongRes.IPMISupported {
// return fmt.Errorf("ipmi not supported")
// }
switch c.Interface {
case "", InterfaceOpen:
var devnum int32 = 0
return c.ConnectOpen(devnum)
case InterfaceTool:
var devnum int32 = 0
return c.ConnectTool(devnum)
case InterfaceLanplus:
c.v20 = true
return c.Connect20()
case InterfaceLan:
c.v20 = false
return c.Connect15()
default:
return fmt.Errorf("not supported interface, supported: lan,lanplus,open")
}
}
func (c *Client) Close() error {
switch c.Interface {
case "", InterfaceOpen:
return c.closeOpen()
case InterfaceTool:
return c.closeTool()
case InterfaceLan, InterfaceLanplus:
return c.closeLAN()
}
return nil
}
func (c *Client) Exchange(request Request, response Response) error {
switch c.Interface {
case "", InterfaceOpen:
return c.exchangeOpen(request, response)
case InterfaceTool:
return c.exchangeTool(request, response)
case InterfaceLan, InterfaceLanplus:
return c.exchangeLAN(request, response)
}
return nil
}
func (c *Client) lock() {
c.l.Lock()
}
func (c *Client) unlock() {
c.l.Unlock()
}