fix(udp): read WSJT-X packets that arrive through a relay
A forwarder (W&P, seen in the field in front of MSHV) prepends the origin as
plain text before re-broadcasting:
"127.0.0.1:2237|" + <the original, untouched WSJT-X packet>
That puts the magic 15 bytes in, so every datagram failed on "bad magic
0x3132372e" — those four bytes being ASCII "127." — and an operator running
MSHV behind the relay saw no decodes, no callsigns and no auto-logged QSOs.
No new service type: what follows the header IS a WSJT-X packet, so the parser
and everything downstream apply unchanged, and a separate type would duplicate
decode, status and logged-ADIF handling to strip 15 bytes. ParseWSJT skips the
header instead, which also covers any other relay that wraps traffic this way.
The match is deliberately narrow — the magic must fall within the first 64
bytes AND every byte before it must be printable ASCII. A corrupt or truncated
packet that merely contains those four bytes somewhere is not resurrected into
a QSO; it fails exactly as it did before.
Test data is the real captured datagram, header included.
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package udp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Bytes captured from a real W&P relay in front of MSHV: the origin as text,
|
||||
// a '|', then the untouched WSJT-X packet. This exact datagram produced
|
||||
// "bad magic 0x3132372e" — 0x3132372e being ASCII "127.".
|
||||
var wpDecode = []byte{
|
||||
// "127.0.0.1:2237|"
|
||||
0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x3a, 0x32, 0x32, 0x33, 0x37, 0x7c,
|
||||
// magic, schema 3, type 2 (Decode), id "MSHV"
|
||||
0xad, 0xbc, 0xcb, 0xda, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02,
|
||||
0x00, 0x00, 0x00, 0x04, 0x4d, 0x53, 0x48, 0x56,
|
||||
0x01, 0x01, 0x4b, 0x31, 0x28, 0x00, 0x00, 0x00, 0x16,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x8a,
|
||||
0x00, 0x00, 0x00, 0x03, 0x46, 0x54, 0x38, // "FT8"
|
||||
0x00, 0x00, 0x00, 0x0e, 0x54, 0x4e, 0x38, 0x47, 0x44, 0x20, 0x39, 0x41, 0x31, 0x4d, 0x4d, 0x20, 0x37, 0x33, // "TN8GD 9A1MM 73"
|
||||
0x00, 0x00,
|
||||
}
|
||||
|
||||
func TestParseWSJTBehindAForwarder(t *testing.T) {
|
||||
if _, _, err := ParseWSJT(wpDecode); err != nil {
|
||||
t.Fatalf("relayed packet still fails: %v", err)
|
||||
}
|
||||
// And the header is what was in the way: without it the same bytes parse.
|
||||
if _, _, err := ParseWSJT(wpDecode[15:]); err != nil {
|
||||
t.Fatalf("bare packet fails: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStripForwarderHeader(t *testing.T) {
|
||||
bare := wpDecode[15:]
|
||||
|
||||
if got := stripForwarderHeader(bare); !bytes.Equal(got, bare) {
|
||||
t.Error("a packet with no header must be returned untouched")
|
||||
}
|
||||
if got := stripForwarderHeader(wpDecode); !bytes.Equal(got, bare) {
|
||||
t.Error("the text header was not stripped")
|
||||
}
|
||||
|
||||
// A BINARY prefix is not a forwarder header — refusing it is what stops a
|
||||
// corrupt packet that merely contains the magic from becoming a QSO.
|
||||
binPrefix := append([]byte{0x00, 0x01, 0x02}, bare...)
|
||||
if got := stripForwarderHeader(binPrefix); !bytes.Equal(got, binPrefix) {
|
||||
t.Error("a non-printable prefix must not be treated as a header")
|
||||
}
|
||||
|
||||
// Beyond the window, the magic is ignored however printable the prefix is.
|
||||
far := append(bytes.Repeat([]byte("A"), maxFwdHeader+1), bare...)
|
||||
if got := stripForwarderHeader(far); !bytes.Equal(got, far) {
|
||||
t.Error("magic past maxFwdHeader must not be trusted")
|
||||
}
|
||||
|
||||
// No magic anywhere, and runt packets, must not panic or invent anything.
|
||||
for _, junk := range [][]byte{[]byte("127.0.0.1:2237|hello"), {}, {0xad}, {0xad, 0xbc, 0xcb}} {
|
||||
if got := stripForwarderHeader(junk); !bytes.Equal(got, junk) {
|
||||
t.Errorf("junk %q was altered", junk)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,12 +56,61 @@ type WSJTEvent struct {
|
||||
IsCQ bool // the decode was a CQ call
|
||||
}
|
||||
|
||||
// maxFwdHeader bounds how far into a packet the WSJT-X magic may sit behind a
|
||||
// forwarder's header. The one seen in the field ("127.0.0.1:2237|") is 15 bytes;
|
||||
// 64 leaves room for a longer address without ever scanning a real payload.
|
||||
const maxFwdHeader = 64
|
||||
|
||||
// stripForwarderHeader removes the origin header a UDP relay prepends.
|
||||
//
|
||||
// A relay that re-broadcasts WSJT-X traffic has to say where each datagram came
|
||||
// from, and it does so as plain text in front of the payload:
|
||||
//
|
||||
// "127.0.0.1:2237|" + <the original, untouched WSJT-X packet>
|
||||
//
|
||||
// The magic then sits 15 bytes in, every packet fails on "bad magic", and an
|
||||
// operator running MSHV behind such a relay gets nothing at all. There is no
|
||||
// need for a separate service type: what follows the header IS a WSJT-X packet,
|
||||
// so the whole parser and everything downstream apply unchanged.
|
||||
//
|
||||
// Deliberately narrow. The magic must appear within maxFwdHeader bytes AND
|
||||
// everything before it must be printable ASCII — a truncated or corrupt packet
|
||||
// that happens to contain those four bytes somewhere is not resurrected into a
|
||||
// QSO. Anything else is returned untouched, and still fails as it did.
|
||||
func stripForwarderHeader(pkt []byte) []byte {
|
||||
if len(pkt) < 4 {
|
||||
return pkt
|
||||
}
|
||||
if binary.BigEndian.Uint32(pkt) == wsjtMagic {
|
||||
return pkt // no header — the overwhelmingly common case
|
||||
}
|
||||
limit := len(pkt) - 4
|
||||
if limit > maxFwdHeader {
|
||||
limit = maxFwdHeader
|
||||
}
|
||||
for i := 1; i <= limit; i++ {
|
||||
if binary.BigEndian.Uint32(pkt[i:]) != wsjtMagic {
|
||||
continue
|
||||
}
|
||||
for _, b := range pkt[:i] {
|
||||
if b < 0x20 || b >= 0x7f {
|
||||
return pkt // not a text header — leave it alone
|
||||
}
|
||||
}
|
||||
return pkt[i:]
|
||||
}
|
||||
return pkt
|
||||
}
|
||||
|
||||
// ParseWSJT decodes one UDP packet. Returns ok=false for messages we
|
||||
// don't care about (heartbeat, clears, etc.).
|
||||
func ParseWSJT(pkt []byte) (WSJTEvent, bool, error) {
|
||||
if len(pkt) < 12 {
|
||||
return WSJTEvent{}, false, fmt.Errorf("packet too short")
|
||||
}
|
||||
// A relay (W&P and friends) puts its own origin header in front — skip it so
|
||||
// the packet parses exactly as if it had arrived from WSJT-X directly.
|
||||
pkt = stripForwarderHeader(pkt)
|
||||
r := bytes.NewReader(pkt)
|
||||
var magic, schema, mtype uint32
|
||||
if err := binary.Read(r, binary.BigEndian, &magic); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user