| 1 | package tap |
| 2 | |
| 3 | import ( |
| 4 | "encoding/binary" |
| 5 | "math" |
| 6 | |
| 7 | log "github.com/sirupsen/logrus" |
| 8 | ) |
| 9 | |
| 10 | type protocol interface { |
| 11 | Stream() bool |
| 12 | } |
| 13 | |
| 14 | type streamProtocol interface { |
| 15 | protocol |
| 16 | Buf() []byte |
| 17 | Write(buf []byte, size int) |
| 18 | Read(buf []byte) int |
| 19 | } |
| 20 | |
| 21 | type hyperkitProtocol struct { |
| 22 | } |
| 23 | |
| 24 | func (s *hyperkitProtocol) Stream() bool { |
| 25 | return true |
| 26 | } |
| 27 | |
| 28 | func (s *hyperkitProtocol) Buf() []byte { |
| 29 | return make([]byte, 2) |
| 30 | } |
| 31 | |
| 32 | func (s *hyperkitProtocol) Write(buf []byte, size int) { |
| 33 | if size < 0 || size > math.MaxUint16 { |
| 34 | log.Warnf("size out of range. Resetting to %d", math.MaxUint16) |
| 35 | size = math.MaxUint16 |
| 36 | } |
| 37 | binary.LittleEndian.PutUint16(buf, uint16(size)) |
| 38 | } |
| 39 | |
| 40 | func (s *hyperkitProtocol) Read(buf []byte) int { |
| 41 | return int(binary.LittleEndian.Uint16(buf[0:2])) |
| 42 | } |
| 43 | |
| 44 | type qemuProtocol struct { |
| 45 | } |
| 46 | |
| 47 | func (s *qemuProtocol) Stream() bool { |
| 48 | return true |
| 49 | } |
| 50 | |
| 51 | func (s *qemuProtocol) Buf() []byte { |
| 52 | return make([]byte, 4) |
| 53 | } |
| 54 | |
| 55 | func (s *qemuProtocol) Write(buf []byte, size int) { |
| 56 | if size > math.MaxInt32 { |
| 57 | log.Warnf("size exceeds max limit. Resetting to: %d", math.MaxInt32) |
| 58 | size = math.MaxInt32 |
| 59 | } |
| 60 | binary.BigEndian.PutUint32(buf, uint32(size)) |
| 61 | } |
| 62 | |
| 63 | func (s *qemuProtocol) Read(buf []byte) int { |
| 64 | return int(binary.BigEndian.Uint32(buf[0:4])) |
| 65 | } |
| 66 | |
| 67 | type bessProtocol struct { |
| 68 | } |
| 69 | |
| 70 | func (s *bessProtocol) Stream() bool { |
| 71 | return false |
| 72 | } |
| 73 | |
| 74 | type vfkitProtocol struct { |
| 75 | } |
| 76 | |
| 77 | func (s *vfkitProtocol) Stream() bool { |
| 78 | return false |
| 79 | } |