forked from jonstout/ogo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
44 lines (37 loc) · 848 Bytes
/
message.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
package ogo
import (
"encoding/binary"
"net"
"time"
)
type LinkDiscovery struct {
SrcDPID net.HardwareAddr
Nsec int64 /* Number of nanoseconds elapsed since Jan 1, 1970. */
pad []byte
}
func NewLinkDiscovery() *LinkDiscovery {
d := new(LinkDiscovery)
d.SrcDPID = make([]byte, 8)
d.Nsec = time.Now().UnixNano()
return d
}
func (d *LinkDiscovery) Len() uint16 {
return 22
}
func (d *LinkDiscovery) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(d.Len()))
next := 0
copy(data[next:], d.SrcDPID)
next += len(d.SrcDPID)
binary.BigEndian.PutUint64(data[next:], uint64(d.Nsec))
next += 8
return
}
func (d *LinkDiscovery) UnmarshalBinary(data []byte) error {
next := 0
copy(d.SrcDPID, data[next:])
next += len(d.SrcDPID)
d.Nsec = int64(binary.BigEndian.Uint64(data[next:]))
next += 8
return nil
}