-
Notifications
You must be signed in to change notification settings - Fork 15
/
MAC_type.go
55 lines (44 loc) · 1.23 KB
/
MAC_type.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
/* MAC_type.go: define methods to make a CustomType that stores Hardware addresses
*
* Author: J. Lowell Wofford <lowell@lanl.gov>
*
* This software is open source software available under the BSD-3 license.
* Copyright (c) 2018-2021, Triad National Security, LLC
* See LICENSE file for details.
*/
package customtypes
import (
fmt "fmt"
"net"
"strconv"
"strings"
"github.com/kraken-hpc/kraken/lib/types"
)
var _ (types.ExtensionCustomType) = IP{}
var _ (types.ExtensionCustomTypePtr) = (*IP)(nil)
type MAC struct {
net.HardwareAddr
}
func (m MAC) Marshal() ([]byte, error) {
return []byte(m.HardwareAddr), nil
}
func (m *MAC) MarshalTo(data []byte) (int, error) {
copy(data, []byte(m.HardwareAddr))
return 4, nil
}
func (m *MAC) Unmarshal(data []byte) error {
if len(data) > 20 { // IPoIB is longest we allow
return fmt.Errorf("incorrect hardware address lenght: %d > 20", len(data))
}
m.HardwareAddr = net.HardwareAddr(data)
return nil
}
func (m *MAC) Size() int { return len(m.HardwareAddr) }
func (m MAC) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(m.String())), nil
}
func (m *MAC) UnmarshalJSON(data []byte) (e error) {
s := strings.Trim(string(data), "\"'")
m.HardwareAddr, e = net.ParseMAC(s)
return
}