UDP transport with broadcast support, -udp and -iface flags on info
This commit is contained in:
@@ -73,12 +73,16 @@ func printInfo(dev string, info *client.ResponseInfo) {
|
|||||||
func cmdInfo(args []string) error {
|
func cmdInfo(args []string) error {
|
||||||
fs := flag.NewFlagSet("info", flag.ExitOnError)
|
fs := flag.NewFlagSet("info", flag.ExitOnError)
|
||||||
udpAddr := fs.String("udp", "", "connect via UDP to this IP address")
|
udpAddr := fs.String("udp", "", "connect via UDP to this IP address")
|
||||||
|
iface := fs.String("iface", "", "bind to this network interface (for broadcast)")
|
||||||
fs.Parse(args)
|
fs.Parse(args)
|
||||||
|
|
||||||
if *udpAddr != "" {
|
if *udpAddr != "" {
|
||||||
log := slog.With("addr", *udpAddr)
|
log := slog.With("addr", *udpAddr)
|
||||||
|
if *iface != "" {
|
||||||
|
log = log.With("iface", *iface)
|
||||||
|
}
|
||||||
log.Info("connecting via UDP")
|
log.Info("connecting via UDP")
|
||||||
c, err := client.NewUDP(*udpAddr, 2*time.Second)
|
c, err := client.NewUDP(*udpAddr, *iface, 2*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,30 +5,91 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
const PicomapPort = 28781
|
const PicomapPort = 28781
|
||||||
|
|
||||||
type udpTransport struct {
|
type udpTransport struct {
|
||||||
conn *net.UDPConn
|
conn *net.UDPConn
|
||||||
|
addr *net.UDPAddr
|
||||||
buf bytes.Buffer
|
buf bytes.Buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUDP(addr string, timeout time.Duration) (*Client, error) {
|
func isBroadcast(ip net.IP) bool {
|
||||||
|
if ip.Equal(net.IPv4bcast) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
ip4 := ip.To4()
|
||||||
|
return ip4 != nil && ip4[3] == 255
|
||||||
|
}
|
||||||
|
|
||||||
|
func interfaceIPv4(name string) (net.IP, error) {
|
||||||
|
ifi, err := net.InterfaceByName(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("interface %s: %w", name, err)
|
||||||
|
}
|
||||||
|
addrs, err := ifi.Addrs()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("interface %s addrs: %w", name, err)
|
||||||
|
}
|
||||||
|
for _, a := range addrs {
|
||||||
|
if ipnet, ok := a.(*net.IPNet); ok {
|
||||||
|
if ip4 := ipnet.IP.To4(); ip4 != nil {
|
||||||
|
return ip4, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("interface %s has no IPv4 address", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func enableBroadcast(conn *net.UDPConn) error {
|
||||||
|
raw, err := conn.SyscallConn()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("syscall conn: %w", err)
|
||||||
|
}
|
||||||
|
var serr error
|
||||||
|
raw.Control(func(fd uintptr) {
|
||||||
|
serr = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_BROADCAST, 1)
|
||||||
|
})
|
||||||
|
return serr
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUDP(addr string, iface string, timeout time.Duration) (*Client, error) {
|
||||||
raddr, err := net.ResolveUDPAddr("udp4", fmt.Sprintf("%s:%d", addr, PicomapPort))
|
raddr, err := net.ResolveUDPAddr("udp4", fmt.Sprintf("%s:%d", addr, PicomapPort))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("resolve %s: %w", addr, err)
|
return nil, fmt.Errorf("resolve %s: %w", addr, err)
|
||||||
}
|
}
|
||||||
conn, err := net.DialUDP("udp4", nil, raddr)
|
|
||||||
|
var laddr *net.UDPAddr
|
||||||
|
if iface != "" {
|
||||||
|
ip, err := interfaceIPv4(iface)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("dial %s: %w", addr, err)
|
return nil, err
|
||||||
}
|
}
|
||||||
return &Client{transport: &udpTransport{conn: conn}, timeout: timeout}, nil
|
laddr = &net.UDPAddr{IP: ip, Port: 0}
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := net.ListenUDP("udp4", laddr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("listen: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if isBroadcast(raddr.IP) {
|
||||||
|
if err := enableBroadcast(conn); err != nil {
|
||||||
|
conn.Close()
|
||||||
|
return nil, fmt.Errorf("SO_BROADCAST: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Client{transport: &udpTransport{conn: conn, addr: raddr}, timeout: timeout}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *udpTransport) Send(data []byte) error {
|
func (t *udpTransport) Send(data []byte) error {
|
||||||
_, err := t.conn.Write(data)
|
_, err := t.conn.WriteToUDP(data, t.addr)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user