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-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
|
|
|
|
|
2021-09-30 23:15:23 +02:00
|
|
|
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
|
2021-09-30 23:15:23 +02:00
|
|
|
BroadcastPeer
|
2021-08-16 20:58:15 +02:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-10 18:35:44 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-10 18:35:44 +01:00
|
|
|
func (e EgHeader) GetPacketLength() (ret uint16) {
|
|
|
|
ret = binary.BigEndian.Uint16(e.buf[5:7])
|
|
|
|
return
|
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
|
|
|
}
|