EtherGuard-VPN/path/header.go

82 lines
1.6 KiB
Go
Raw Normal View History

2021-08-16 20:58:15 +02:00
package path
import (
"encoding/binary"
"errors"
2021-08-20 19:32:50 +02:00
"github.com/KusakabeSi/EtherGuardVPN/config"
2021-08-16 20:58:15 +02:00
)
2021-08-21 16:54:24 +02:00
const EgHeaderLen = 16
2021-08-16 20:58:15 +02:00
type EgHeader struct {
buf []byte
}
type Usage uint8
const (
NornalPacket Usage = iota
2021-08-20 19:32:50 +02:00
Register //Register to server
UpdatePeer //Comes from server
UpdateNhTable
PingPacket //Comes from other peer
PongPacket //Send to everyone, include server
RequestPeer
BoardcastPeer
2021-08-16 20:58:15 +02:00
)
func NewEgHeader(pac []byte) (e EgHeader, err error) {
if len(pac) != EgHeaderLen {
err = errors.New("Invalid packet size")
return
}
e.buf = pac
return
}
2021-08-20 19:32:50 +02:00
func (e EgHeader) GetDst() config.Vertex {
return config.Vertex(binary.BigEndian.Uint32(e.buf[0:4]))
2021-08-16 20:58:15 +02:00
}
2021-08-20 19:32:50 +02:00
func (e EgHeader) SetDst(node_ID config.Vertex) {
2021-08-16 20:58:15 +02:00
binary.BigEndian.PutUint32(e.buf[0:4], uint32(node_ID))
}
2021-08-20 19:32:50 +02:00
func (e EgHeader) GetSrc() config.Vertex {
return config.Vertex(binary.BigEndian.Uint32(e.buf[4:8]))
2021-08-16 20:58:15 +02:00
}
2021-08-20 19:32:50 +02:00
func (e EgHeader) SetSrc(node_ID config.Vertex) {
2021-08-16 20:58:15 +02:00
binary.BigEndian.PutUint32(e.buf[4:8], uint32(node_ID))
}
func (e EgHeader) GetTTL() uint8 {
return e.buf[8]
}
func (e EgHeader) SetTTL(ttl uint8) {
e.buf[8] = ttl
}
2021-08-20 19:32:50 +02:00
func (e EgHeader) GetUsage() Usage {
return Usage(e.buf[9])
2021-08-16 20:58:15 +02:00
}
2021-08-20 19:32:50 +02:00
func (e EgHeader) SetUsage(usage Usage) {
e.buf[9] = uint8(usage)
2021-08-16 20:58:15 +02:00
}
func (e EgHeader) GetPacketLength() uint16 {
return binary.BigEndian.Uint16(e.buf[10:12])
}
func (e EgHeader) SetPacketLength(length uint16) {
binary.BigEndian.PutUint16(e.buf[10:12], length)
}
2021-08-21 16:54:24 +02:00
func (e EgHeader) GetMessageID() uint32 {
return binary.BigEndian.Uint32(e.buf[12:16])
}
func (e EgHeader) SetMessageID(MessageID uint32) {
binary.BigEndian.PutUint32(e.buf[12:16], MessageID)
}