dissertation-2-code/proxy/packet.go

47 lines
790 B
Go
Raw Normal View History

2020-10-22 21:19:26 +01:00
package proxy
import (
"encoding/binary"
"time"
)
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 {
2020-11-28 17:15:56 +00:00
footer := make([]byte, 8)
unixTime := uint64(time.Now().Unix())
binary.LittleEndian.PutUint64(footer, unixTime)
b = append(b, footer...)
2020-11-26 18:55:29 +00:00
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) {
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
2020-11-29 22:07:30 +00:00
// TODO: Verify timestamp
2020-11-28 17:15:56 +00:00
return data[:len(data)-8], nil
2020-10-22 21:19:26 +01:00
}