]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/tracker.go
Add arm32 to goreleaser config
[rbdr/mobius] / hotline / tracker.go
index bd7aa00da966d52b21f1a2b872d3153e78348884..6ee2a9f502010ccd7479f430284feb75a152b1c3 100644 (file)
@@ -4,49 +4,49 @@ import (
        "bufio"
        "encoding/binary"
        "fmt"
        "bufio"
        "encoding/binary"
        "fmt"
-       "github.com/jhalter/mobius/concat"
+       "io"
        "net"
        "net"
+       "slices"
        "strconv"
        "time"
 )
 
 // TrackerRegistration represents the payload a Hotline server sends to a Tracker to register
 type TrackerRegistration struct {
        "strconv"
        "time"
 )
 
 // TrackerRegistration represents the payload a Hotline server sends to a Tracker to register
 type TrackerRegistration struct {
-       Port        [2]byte // Server listening port number
+       Port        [2]byte // Server's listening TCP port number
        UserCount   int     // Number of users connected to this particular server
        UserCount   int     // Number of users connected to this particular server
-       PassID      []byte  // Random number generated by the server
-       Name        string  // Server name
+       PassID      [4]byte // Random number generated by the server
+       Name        string  // Server Name
        Description string  // Description of the server
 }
 
        Description string  // Description of the server
 }
 
-// TODO: reimplement as io.Reader
-func (tr *TrackerRegistration) Read() []byte {
+// Read implements io.Reader to write tracker registration payload bytes to slice
+func (tr *TrackerRegistration) Read(p []byte) (int, error) {
        userCount := make([]byte, 2)
        binary.BigEndian.PutUint16(userCount, uint16(tr.UserCount))
 
        userCount := make([]byte, 2)
        binary.BigEndian.PutUint16(userCount, uint16(tr.UserCount))
 
-       return concat.Slices(
-               []byte{0x00, 0x01},
+       return copy(p, slices.Concat(
+               []byte{0x00, 0x01}, // Magic number, always 1
                tr.Port[:],
                userCount,
                tr.Port[:],
                userCount,
-               []byte{0x00, 0x00},
-               tr.PassID,
+               []byte{0x00, 0x00}, // Magic number, always 0
+               tr.PassID[:],
                []byte{uint8(len(tr.Name))},
                []byte(tr.Name),
                []byte{uint8(len(tr.Description))},
                []byte(tr.Description),
                []byte{uint8(len(tr.Name))},
                []byte(tr.Name),
                []byte{uint8(len(tr.Description))},
                []byte(tr.Description),
-       )
+       )[:]), io.EOF
 }
 
 func register(tracker string, tr *TrackerRegistration) error {
        conn, err := net.Dial("udp", tracker)
        if err != nil {
 }
 
 func register(tracker string, tr *TrackerRegistration) error {
        conn, err := net.Dial("udp", tracker)
        if err != nil {
-               return err
+               return fmt.Errorf("failed to dial tracker: %w", err)
        }
        }
+       defer conn.Close()
 
 
-       b := tr.Read()
-
-       if _, err := conn.Write(b); err != nil {
-               return err
+       if _, err := io.Copy(conn, tr); err != nil {
+               return fmt.Errorf("failed to write to connection: %w", err)
        }
 
        return nil
        }
 
        return nil
@@ -67,7 +67,7 @@ type TrackerHeader struct {
        Version  [2]byte // Old protocol (1) or new (2)
 }
 
        Version  [2]byte // Old protocol (1) or new (2)
 }
 
-// Message type        2       1       Sending list of servers
+// Message type                        2       1       Sending list of servers
 // Message data size   2               Remaining size of this request
 // Number of servers   2               Number of servers in the server list
 // Number of servers   2               Same as previous field
 // Message data size   2               Remaining size of this request
 // Number of servers   2               Number of servers in the server list
 // Number of servers   2               Same as previous field
@@ -83,8 +83,8 @@ type ServerRecord struct {
        Port            [2]byte
        NumUsers        [2]byte // Number of users connected to this particular server
        Unused          [2]byte
        Port            [2]byte
        NumUsers        [2]byte // Number of users connected to this particular server
        Unused          [2]byte
-       NameSize        byte   // Length of name string
-       Name            []byte // Server name
+       NameSize        byte   // Length of Name string
+       Name            []byte // Server Name
        DescriptionSize byte
        Description     []byte
 }
        DescriptionSize byte
        Description     []byte
 }
@@ -125,7 +125,7 @@ func GetListing(addr string) ([]ServerRecord, error) {
        for {
                scanner.Scan()
                var srv ServerRecord
        for {
                scanner.Scan()
                var srv ServerRecord
-               _, err = srv.Read(scanner.Bytes())
+               _, err = srv.Write(scanner.Bytes())
                if err != nil {
                        return nil, err
                }
                if err != nil {
                        return nil, err
                }
@@ -175,8 +175,8 @@ func serverScanner(data []byte, _ bool) (advance int, token []byte, err error) {
        return 12 + nameLen + descLen, data[0 : 12+nameLen+descLen], nil
 }
 
        return 12 + nameLen + descLen, data[0 : 12+nameLen+descLen], nil
 }
 
-// Read implements io.Reader for ServerRecord
-func (s *ServerRecord) Read(b []byte) (n int, err error) {
+// Write implements io.Writer for ServerRecord
+func (s *ServerRecord) Write(b []byte) (n int, err error) {
        copy(s.IPAddr[:], b[0:4])
        copy(s.Port[:], b[4:6])
        copy(s.NumUsers[:], b[6:8])
        copy(s.IPAddr[:], b[0:4])
        copy(s.Port[:], b[4:6])
        copy(s.NumUsers[:], b[6:8])