mirror of
https://github.com/KusakabeShi/EtherGuard-VPN.git
synced 2024-11-22 15:23:08 +01:00
129 lines
2.2 KiB
Go
129 lines
2.2 KiB
Go
package path
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"errors"
|
|
|
|
"github.com/KusakabeSi/EtherGuard-VPN/mtypes"
|
|
)
|
|
|
|
const EgHeaderLen = 7
|
|
|
|
type EgHeader struct {
|
|
buf []byte
|
|
}
|
|
|
|
type Usage uint8
|
|
|
|
const (
|
|
MessageInitiationType Usage = iota
|
|
MessageResponseType
|
|
MessageCookieReplyType
|
|
MessageTransportType
|
|
|
|
NormalPacket
|
|
|
|
Register //Send to server
|
|
ServerUpdate //Comes from server
|
|
|
|
PingPacket //Comes from other peer
|
|
PongPacket //Send to everyone, include server
|
|
QueryPeer
|
|
BroadcastPeer
|
|
)
|
|
|
|
func (v Usage) IsNormal() bool {
|
|
return v == NormalPacket
|
|
}
|
|
|
|
func (v Usage) IsControl() bool {
|
|
switch v {
|
|
case Register:
|
|
return true
|
|
case ServerUpdate:
|
|
return true
|
|
case PingPacket:
|
|
return true
|
|
case PongPacket:
|
|
return true
|
|
case QueryPeer:
|
|
return true
|
|
case BroadcastPeer:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func (v Usage) IsControl_Super2Edge() bool {
|
|
switch v {
|
|
case ServerUpdate:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func (v Usage) IsControl_Edge2Super() bool {
|
|
switch v {
|
|
case Register:
|
|
return true
|
|
case PongPacket:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func (v Usage) IsControl_Edge2Edge() bool {
|
|
switch v {
|
|
case PingPacket:
|
|
return true
|
|
case PongPacket:
|
|
return true
|
|
case QueryPeer:
|
|
return true
|
|
case BroadcastPeer:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func NewEgHeader(pac []byte) (e EgHeader, err error) {
|
|
if len(pac) != EgHeaderLen {
|
|
err = errors.New("invalid packet size")
|
|
return
|
|
}
|
|
e.buf = pac
|
|
return
|
|
}
|
|
|
|
func (e EgHeader) GetDst() mtypes.Vertex {
|
|
return mtypes.Vertex(binary.BigEndian.Uint16(e.buf[0:2]))
|
|
}
|
|
func (e EgHeader) SetDst(node_ID mtypes.Vertex) {
|
|
binary.BigEndian.PutUint16(e.buf[0:2], uint16(node_ID))
|
|
}
|
|
|
|
func (e EgHeader) GetSrc() mtypes.Vertex {
|
|
return mtypes.Vertex(binary.BigEndian.Uint16(e.buf[2:4]))
|
|
}
|
|
func (e EgHeader) SetSrc(node_ID mtypes.Vertex) {
|
|
binary.BigEndian.PutUint16(e.buf[2:4], uint16(node_ID))
|
|
}
|
|
|
|
func (e EgHeader) GetTTL() uint8 {
|
|
return e.buf[4]
|
|
}
|
|
func (e EgHeader) SetTTL(ttl uint8) {
|
|
e.buf[4] = ttl
|
|
}
|
|
|
|
func (e EgHeader) GetPacketLength() uint16 {
|
|
return binary.BigEndian.Uint16(e.buf[5:7])
|
|
}
|
|
func (e EgHeader) SetPacketLength(length uint16) {
|
|
binary.BigEndian.PutUint16(e.buf[5:7], length)
|
|
}
|