EtherGuard-VPN/path/header.go

151 lines
2.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
2021-12-02 18:13:48 +01:00
"github.com/KusakabeSi/EtherGuard-VPN/mtypes"
2021-08-16 20:58:15 +02:00
)
2021-12-12 19:40:21 +01:00
const EgHeaderLen = 4
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
NormalPacket
2021-08-20 19:32:50 +02:00
2021-12-04 03:32:59 +01:00
Register //Send to server
ServerUpdate //Comes from server
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
BroadcastPeer
2021-08-16 20:58:15 +02:00
)
2021-12-12 19:40:21 +01:00
func (v Usage) IsValid_EgType() bool {
if v >= NormalPacket && v <= BroadcastPeer {
return true
}
return false
}
func (v Usage) ToString() string {
switch v {
case MessageInitiationType:
return "MessageInitiationType"
case MessageResponseType:
return "MessageResponseType"
case MessageCookieReplyType:
return "MessageCookieReplyType"
case MessageTransportType:
return "MessageTransportType"
case NormalPacket:
return "NormalPacket"
case Register:
return "Register"
case ServerUpdate:
return "ServerUpdate"
case PingPacket:
return "PingPacket"
case PongPacket:
return "PongPacket"
case QueryPeer:
return "QueryPeer"
case BroadcastPeer:
return "BroadcastPeer"
default:
return "Unknown:" + string(uint8(v))
}
}
2021-12-09 21:23:02 +01:00
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, mtu uint16) (e EgHeader, err error) {
2021-08-16 20:58:15 +02:00
if len(pac) != EgHeaderLen {
2021-12-09 08:46:15 +01:00
err = errors.New("invalid packet size")
2021-08-16 20:58:15 +02:00
return
}
e.buf = pac
return
}
2021-12-02 18:13:48 +01:00
func (e EgHeader) GetDst() mtypes.Vertex {
return mtypes.Vertex(binary.BigEndian.Uint16(e.buf[0:2]))
2021-08-16 20:58:15 +02:00
}
2021-12-02 18:13:48 +01:00
func (e EgHeader) SetDst(node_ID mtypes.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-12-02 18:13:48 +01:00
func (e EgHeader) GetSrc() mtypes.Vertex {
return mtypes.Vertex(binary.BigEndian.Uint16(e.buf[2:4]))
2021-08-16 20:58:15 +02:00
}
2021-12-02 18:13:48 +01:00
func (e EgHeader) SetSrc(node_ID mtypes.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
}