-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket_dev.go
More file actions
43 lines (38 loc) · 918 Bytes
/
socket_dev.go
File metadata and controls
43 lines (38 loc) · 918 Bytes
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
package l2
import (
"encoding/binary"
"io"
)
// This type is a generic wrapper around a io.ReadWriteCloser which allows
// ethernet frames to be tunneled over it.
type socketDevice struct {
io.ReadWriter
}
// A utility function to transform a ReadWriter into a FrameReadWriter.
func WrapReadWriter(rw io.ReadWriter) FrameReadWriter {
return &socketDevice{rw}
}
func (s *socketDevice) WriteFrame(data EthFrame) error {
err := binary.Write(s, binary.BigEndian, int16(len(data)))
if err != nil {
return err
}
for written := 0; written < len(data); {
n, err := s.Write(data[written:])
if err != nil {
return err
}
written += n
}
return nil
}
func (s *socketDevice) ReadFrame() (EthFrame, error) {
var size int16
err := binary.Read(s, binary.BigEndian, &size)
if err != nil {
return nil, err
}
buffer := EthFrame(make([]byte, size))
_, err = io.ReadFull(s, buffer)
return buffer, err
}