EtherGuard-VPN/path/header.go

73 lines
1.4 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-09-21 03:15:23 +02:00
const EgHeaderLen = 7
2021-08-16 20:58:15 +02:00
type EgHeader struct {
buf []byte
}
type Usage uint8
const (
2021-09-21 03:15:23 +02:00
MessageInitiationType Usage = iota
MessageResponseType
MessageCookieReplyType
MessageTransportType
NornalPacket
Register //Register to server
2021-08-20 19:32:50 +02:00
UpdatePeer //Comes from server
UpdateNhTable
2021-09-20 22:20:00 +02:00
UpdateError
2021-08-20 19:32:50 +02:00
PingPacket //Comes from other peer
PongPacket //Send to everyone, include server
2021-08-23 18:39:04 +02:00
QueryPeer
2021-08-20 19:32:50 +02:00
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 {
2021-09-20 22:20:00 +02:00
return config.Vertex(binary.BigEndian.Uint16(e.buf[0:2]))
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-09-20 22:20:00 +02:00
binary.BigEndian.PutUint16(e.buf[0:2], uint16(node_ID))
2021-08-16 20:58:15 +02:00
}
2021-08-20 19:32:50 +02:00
func (e EgHeader) GetSrc() config.Vertex {
2021-09-20 22:20:00 +02:00
return config.Vertex(binary.BigEndian.Uint16(e.buf[2:4]))
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-09-20 22:20:00 +02:00
binary.BigEndian.PutUint16(e.buf[2:4], uint16(node_ID))
2021-08-16 20:58:15 +02:00
}
func (e EgHeader) GetTTL() uint8 {
2021-09-20 22:20:00 +02:00
return e.buf[4]
2021-08-16 20:58:15 +02:00
}
func (e EgHeader) SetTTL(ttl uint8) {
2021-09-20 22:20:00 +02:00
e.buf[4] = ttl
2021-08-16 20:58:15 +02:00
}
func (e EgHeader) GetPacketLength() uint16 {
2021-09-21 03:15:23 +02:00
return binary.BigEndian.Uint16(e.buf[5:7])
2021-08-16 20:58:15 +02:00
}
func (e EgHeader) SetPacketLength(length uint16) {
2021-09-21 03:15:23 +02:00
binary.BigEndian.PutUint16(e.buf[5:7], length)
2021-08-21 16:54:24 +02:00
}