2024-05-17 17:43:28 +02:00
|
|
|
package messages
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-05-23 13:24:02 +02:00
|
|
|
MsgTypeHello MsgType = 0
|
|
|
|
MsgTypeHelloResponse MsgType = 1
|
|
|
|
MsgTypeTransport MsgType = 2
|
2024-05-17 17:43:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrInvalidMessageLength = fmt.Errorf("invalid message length")
|
|
|
|
)
|
|
|
|
|
|
|
|
type MsgType byte
|
|
|
|
|
2024-05-19 12:41:06 +02:00
|
|
|
func (m MsgType) String() string {
|
|
|
|
switch m {
|
|
|
|
case MsgTypeHello:
|
|
|
|
return "hello"
|
2024-05-23 13:24:02 +02:00
|
|
|
case MsgTypeHelloResponse:
|
|
|
|
return "hello response"
|
2024-05-19 12:41:06 +02:00
|
|
|
case MsgTypeTransport:
|
|
|
|
return "transport"
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-17 17:43:28 +02:00
|
|
|
func DetermineClientMsgType(msg []byte) (MsgType, error) {
|
|
|
|
// todo: validate magic byte
|
|
|
|
msgType := MsgType(msg[0])
|
|
|
|
switch msgType {
|
|
|
|
case MsgTypeHello:
|
|
|
|
return msgType, nil
|
|
|
|
case MsgTypeTransport:
|
|
|
|
return msgType, nil
|
|
|
|
default:
|
2024-06-03 20:14:39 +02:00
|
|
|
return 0, fmt.Errorf("invalid msg type, len: %d", len(msg))
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DetermineServerMsgType(msg []byte) (MsgType, error) {
|
|
|
|
// todo: validate magic byte
|
|
|
|
msgType := MsgType(msg[0])
|
|
|
|
switch msgType {
|
2024-05-21 15:51:37 +02:00
|
|
|
case MsgTypeHelloResponse:
|
|
|
|
return msgType, nil
|
2024-05-17 17:43:28 +02:00
|
|
|
case MsgTypeTransport:
|
|
|
|
return msgType, nil
|
|
|
|
default:
|
2024-05-21 15:51:37 +02:00
|
|
|
return 0, fmt.Errorf("invalid msg type (len: %d)", len(msg))
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalHelloMsg initial hello message
|
2024-05-23 13:24:02 +02:00
|
|
|
func MarshalHelloMsg(peerID []byte) ([]byte, error) {
|
|
|
|
if len(peerID) != IDSize {
|
|
|
|
return nil, fmt.Errorf("invalid peerID length")
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
|
|
|
msg := make([]byte, 1, 1+len(peerID))
|
|
|
|
msg[0] = byte(MsgTypeHello)
|
2024-05-23 13:24:02 +02:00
|
|
|
msg = append(msg, peerID...)
|
2024-05-17 17:43:28 +02:00
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2024-05-23 13:24:02 +02:00
|
|
|
func UnmarshalHelloMsg(msg []byte) ([]byte, error) {
|
2024-05-17 17:43:28 +02:00
|
|
|
if len(msg) < 2 {
|
2024-05-23 13:24:02 +02:00
|
|
|
return nil, fmt.Errorf("invalid 'hello' messge")
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
2024-05-23 13:24:02 +02:00
|
|
|
return msg[1:], nil
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
|
|
|
|
2024-05-21 15:51:37 +02:00
|
|
|
func MarshalHelloResponse() []byte {
|
|
|
|
msg := make([]byte, 1)
|
|
|
|
msg[0] = byte(MsgTypeHelloResponse)
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2024-05-23 13:24:02 +02:00
|
|
|
// Transport message
|
2024-05-17 17:43:28 +02:00
|
|
|
|
2024-05-23 13:24:02 +02:00
|
|
|
func MarshalTransportMsg(peerID []byte, payload []byte) []byte {
|
|
|
|
if len(peerID) != IDSize {
|
|
|
|
return nil
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
|
|
|
|
2024-05-23 13:24:02 +02:00
|
|
|
msg := make([]byte, 1+IDSize, 1+IDSize+len(payload))
|
2024-05-17 17:43:28 +02:00
|
|
|
msg[0] = byte(MsgTypeTransport)
|
2024-05-23 13:24:02 +02:00
|
|
|
copy(msg[1:], peerID)
|
2024-05-17 17:43:28 +02:00
|
|
|
msg = append(msg, payload...)
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2024-05-17 23:41:47 +02:00
|
|
|
func UnmarshalTransportPayload(buf []byte) ([]byte, error) {
|
2024-05-23 13:24:02 +02:00
|
|
|
headerSize := 1 + IDSize
|
|
|
|
if len(buf) < headerSize {
|
2024-05-17 23:41:47 +02:00
|
|
|
return nil, ErrInvalidMessageLength
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
2024-05-23 13:24:02 +02:00
|
|
|
return buf[headerSize:], nil
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
|
|
|
|
2024-05-23 13:24:02 +02:00
|
|
|
func UnmarshalTransportID(buf []byte) ([]byte, error) {
|
|
|
|
headerSize := 1 + IDSize
|
|
|
|
if len(buf) < headerSize {
|
|
|
|
return nil, ErrInvalidMessageLength
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
2024-05-23 13:24:02 +02:00
|
|
|
return buf[1:headerSize], nil
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
|
|
|
|
2024-05-23 13:24:02 +02:00
|
|
|
func UpdateTransportMsg(msg []byte, peerID []byte) error {
|
|
|
|
if len(msg) < 1+len(peerID) {
|
2024-05-17 17:43:28 +02:00
|
|
|
return ErrInvalidMessageLength
|
|
|
|
}
|
2024-05-23 13:24:02 +02:00
|
|
|
copy(msg[1:], peerID)
|
2024-05-17 17:43:28 +02:00
|
|
|
return nil
|
|
|
|
}
|