Skip to content

Commit

Permalink
Reworked get-listener to use netip.AddrPort
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed May 28, 2024
1 parent d5317b5 commit 37c5df8
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 37 deletions.
6 changes: 3 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- [x] Rework broadcast address as netip.AddrPort
- [x] Rework listen address as netip.AddrPort
- [x] Set socket.SO_REUSEADDR
- [ ] Rework Address as netip.AddrPort
- [x] Rework Address as netip.AddrPort
- [x] Rename to `ControllerAddr`
- [x] Rework as struct with embedded netip.AddrPort
- [x] uhppote-core
Expand All @@ -28,8 +28,8 @@
- [x] Rework uhppoted-lib::config Device Address
- [x] Rework uhppote-cli::command Device
- [x] Rework uhppoted-httpd::controller EndPoint
- [ ] Rework GetListenerResponse to use AddrPort
- Remove types.Listener `// FIXME remove (unused)`
- [x] Remove types.Listener
- [x] Rework Get/SetListener to use AddrPort

- [ ] CLI get-devices EOF
```
Expand Down
5 changes: 2 additions & 3 deletions messages/get_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package messages

import (
"github.com/uhppoted/uhppote-core/types"
"net"
"net/netip"
)

type GetListenerRequest struct {
Expand All @@ -13,6 +13,5 @@ type GetListenerRequest struct {
type GetListenerResponse struct {
MsgType types.MsgType `uhppote:"value:0x92"`
SerialNumber types.SerialNumber `uhppote:"offset:4"`
Address net.IP `uhppote:"offset:8"`
Port uint16 `uhppote:"offset:12"`
AddrPort netip.AddrPort `uhppote:"offset:8"`
}
17 changes: 6 additions & 11 deletions messages/get_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package messages

import (
codec "github.com/uhppoted/uhppote-core/encoding/UTO311-L0x"
"net"
"net/netip"
"reflect"
"testing"
)
Expand Down Expand Up @@ -73,19 +73,15 @@ func TestUnmarshalGetListenerResponse(t *testing.T) {
}

if reply.MsgType != 0x92 {
t.Errorf("Incorrect 'message type' - expected:%02X, got:%02x\n", 0x92, reply.MsgType)
t.Errorf("Incorrect 'message type' - expected:%02X, got:%02x", 0x92, reply.MsgType)
}

if reply.SerialNumber != 423187757 {
t.Errorf("Incorrect 'serial number' - expected:%v, got:%v\n", 423187757, reply.SerialNumber)
t.Errorf("Incorrect 'serial number' - expected:%v, got:%v", 423187757, reply.SerialNumber)
}

if !reflect.DeepEqual(reply.Address, net.IPv4(192, 168, 0, 225)) {
t.Errorf("Incorrect IP address - expected:'%v', got:'%v'\n", net.IPv4(192, 168, 0, 225), reply.Address)
}

if reply.Port != 9874 {
t.Errorf("Incorrect 'port' - expected:%d, got:%v\n", 9874, reply.Port)
if reply.AddrPort != netip.MustParseAddrPort("192.168.0.225:9874") {
t.Errorf("Incorrect IPv4 address:port - expected:'%v', got:'%v'", netip.MustParseAddrPort("192.168.0.225:9874"), reply.AddrPort)
}
}

Expand All @@ -100,8 +96,7 @@ func TestFactoryUnmarshalGetListenerResponse(t *testing.T) {
expected := GetListenerResponse{
MsgType: 0x92,
SerialNumber: 423187757,
Address: net.IPv4(192, 168, 0, 225),
Port: 9874,
AddrPort: netip.MustParseAddrPort("192.168.0.225:9874"),
}

response, err := UnmarshalResponse(message)
Expand Down
16 changes: 0 additions & 16 deletions types/listener.go

This file was deleted.

5 changes: 1 addition & 4 deletions uhppote/get_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ func (u *uhppote) GetListener(serialNumber uint32) (netip.AddrPort, error) {
if reply, err := sendto[messages.GetListenerResponse](u, serialNumber, request); err != nil {
return netip.AddrPort{}, err
} else {
addr := reply.Address.To4()
slice := [4]byte{addr[0], addr[1], addr[2], addr[3]}

return netip.AddrPortFrom(netip.AddrFrom4(slice), reply.Port), nil
return reply.AddrPort, nil
}
}

0 comments on commit 37c5df8

Please sign in to comment.