/
auth.go
84 lines (64 loc) · 1.76 KB
/
auth.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
package sscp
import (
"crypto/md5"
"encoding/binary"
)
type LoginResponse struct {
ProtoVersion uint8
MaxDataSize uint16
RightGroup uint8
ImageGUID [16]byte
// Optional Fields
DeviceName *string
SSCPAddress *uint8
SSCPTCPPort *uint16
SSCPSSLPort *uint16
}
// This functionality defined on the section 5.5.4 of the specification
func (self *PLCConnection) Login(username string, password string, proxyId string, maxDataSize uint16) (*LoginResponse, error) {
// Get username as byte buffer
_username := []byte(username)
_username_len := len(_username)
// Get password hash as byte buffer
h := md5.New()
h.Write([]byte(password))
_password := h.Sum(nil)
_password_len := len(_password)
_proxyId := []byte(proxyId)
_proxyId_len := len(_proxyId)
payload := make([]byte, 6+_username_len+_password_len+_proxyId_len)
payload[0] = 0x07
binary.BigEndian.PutUint16(payload[1:], maxDataSize)
// Username
payload[3] = byte(_username_len)
if _username_len > 0 {
copy(payload[4:], _username)
}
// Password
payload[4+_username_len] = byte(_password_len)
if _password_len > 0 {
copy(payload[5+_username_len:], _password)
}
// Proxy Id
payload[5+_username_len+_password_len] = byte(_proxyId_len)
if _proxyId_len > 0 {
copy(payload[6+_username_len+_password_len:], _proxyId)
}
// Recieve Response
res, err := self.makeRequest(0x0100, payload)
if err != nil {
return nil, err
}
result := LoginResponse{
ProtoVersion: res[0],
MaxDataSize: binary.BigEndian.Uint16(res[1:3]),
RightGroup: res[3],
}
copy(result.ImageGUID[:], res[4:20])
return &result, nil
}
// This functionality defined on the section 5.5.5 of the specification
func (self *PLCConnection) Logout() error {
_, err := self.makeRequest(0x0101, []byte{})
return err
}