Skip to content

Commit

Permalink
Migrating get/set-listener to netip.AddrPort
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed May 24, 2024
1 parent 3573422 commit f11a293
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- [x] Rework uhppote-cli::command Device
- [x] Rework uhppoted-httpd::controller EndPoint
- [ ] Rework GetListenerResponse to use AddrPort
- Remove types.Listener `// FIXME remove (unused)`

- [ ] CLI get-devices EOF
```
Expand Down
2 changes: 1 addition & 1 deletion encoding/UTO311-L0x/UT0311-L0x.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func marshal(s reflect.Value, bytes []byte) error {
if !v[1].IsNil() {
return v[1].Interface().(error)
} else if slice := v[0].Bytes(); len(slice) != 6 {
return fmt.Errorf("Invalid IPv4 address:port %v", slice)
return fmt.Errorf("invalid IPv4 address:port %v", slice)
} else {
copy(bytes[offset:offset+6], slice)
}
Expand Down
3 changes: 2 additions & 1 deletion types/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"net"
)

// FIXME remove (unused)
type Listener struct {
SerialNumber SerialNumber
Address net.UDPAddr
}

func (l *Listener) String() string {
return fmt.Sprintf("%s %s", l.SerialNumber, l.Address.String())
return fmt.Sprintf("%v %v", l.SerialNumber, l.Address)
}
16 changes: 8 additions & 8 deletions uhppote/get_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ package uhppote

import (
"fmt"
"net"
"net/netip"

"github.com/uhppoted/uhppote-core/messages"
"github.com/uhppoted/uhppote-core/types"
)

func (u *uhppote) GetListener(serialNumber uint32) (*types.Listener, error) {
func (u *uhppote) GetListener(serialNumber uint32) (netip.AddrPort, error) {
if serialNumber == 0 {
return nil, fmt.Errorf("invalid device ID (%v)", serialNumber)
return netip.AddrPort{}, fmt.Errorf("invalid device ID (%v)", serialNumber)
}

request := messages.GetListenerRequest{
SerialNumber: types.SerialNumber(serialNumber),
}

if reply, err := sendto[messages.GetListenerResponse](u, serialNumber, request); err != nil {
return nil, err
return netip.AddrPort{}, err
} else {
return &types.Listener{
SerialNumber: reply.SerialNumber,
Address: net.UDPAddr{IP: reply.Address, Port: int(reply.Port)},
}, nil
addr := reply.Address.To4()
slice := [4]byte{addr[0], addr[1], addr[2], addr[3]}

return netip.AddrPortFrom(netip.AddrFrom4(slice), reply.Port), nil
}
}
2 changes: 1 addition & 1 deletion uhppote/iuhppote.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type IUHPPOTE interface {
GetDevice(deviceID uint32) (*types.Device, error)

SetAddress(deviceID uint32, address, mask, gateway net.IP) (*types.Result, error)
GetListener(deviceID uint32) (*types.Listener, error)
GetListener(deviceID uint32) (netip.AddrPort, error)
SetListener(deviceID uint32, address net.UDPAddr) (*types.Result, error)
GetTime(deviceID uint32) (*types.Time, error)
SetTime(deviceID uint32, datetime time.Time) (*types.Time, error)
Expand Down

0 comments on commit f11a293

Please sign in to comment.