dissertation-2-code/proxy/packet.go

40 lines
663 B
Go
Raw Normal View History

2020-10-22 21:19:26 +01:00
package proxy
import "mpbl3p/shared"
2020-10-22 21:19:26 +01:00
2020-11-25 19:35:31 +00:00
type Packet interface {
2020-11-26 18:55:29 +00:00
Marshal() []byte
2020-11-26 22:39:07 +00:00
Contents() []byte
2020-11-25 19:35:31 +00:00
}
2020-11-28 17:15:56 +00:00
type SimplePacket []byte
2020-10-22 21:19:26 +01:00
// get the raw data of the IP packet
2020-11-26 18:55:29 +00:00
func (p SimplePacket) Marshal() []byte {
2020-11-28 17:15:56 +00:00
return p
2020-11-26 18:55:29 +00:00
}
2020-10-22 21:19:26 +01:00
2020-11-26 22:39:07 +00:00
func (p SimplePacket) Contents() []byte {
2020-11-28 17:15:56 +00:00
return p
2020-11-26 22:39:07 +00:00
}
2020-11-26 18:55:29 +00:00
func AppendMac(b []byte, g MacGenerator) []byte {
mac := g.Generate(b)
2020-11-28 17:15:56 +00:00
return append(b, mac...)
2020-11-26 18:55:29 +00:00
}
2020-10-22 21:19:26 +01:00
2020-11-26 18:55:29 +00:00
func StripMac(b []byte, v MacVerifier) ([]byte, error) {
if len(b) < v.CodeLength() {
return nil, shared.ErrNotEnoughBytes
}
2020-11-26 18:55:29 +00:00
data := b[:len(b)-v.CodeLength()]
sum := b[len(b)-v.CodeLength():]
2020-10-22 21:19:26 +01:00
2020-11-26 18:55:29 +00:00
if err := v.Verify(data, sum); err != nil {
return nil, err
}
2020-10-22 21:19:26 +01:00
return data, nil
2020-10-22 21:19:26 +01:00
}