packet: handle "any" interface option
This commit is contained in:
parent
cbf92bec2e
commit
20a248af16
|
@ -25,6 +25,16 @@ func Listen(group string, port uint16, ifaces ...string) (Conn, error) {
|
|||
if err != nil {
|
||||
return Conn{}, err
|
||||
}
|
||||
if len(ifaces) == 1 && ifaces[0] == "any" {
|
||||
netifs, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return Conn{}, err
|
||||
}
|
||||
ifaces = make([]string, 0, len(netifs))
|
||||
for _, netif := range netifs {
|
||||
ifaces = append(ifaces, netif.Name)
|
||||
}
|
||||
}
|
||||
for _, iface := range ifaces {
|
||||
if err = conn.JoinGroup(iface, group); err != nil {
|
||||
conn.Close()
|
||||
|
@ -65,7 +75,7 @@ func (c Conn) Close() error {
|
|||
func (c Conn) JoinGroup(ifname string, addr string) error {
|
||||
ifi, err := net.InterfaceByName(ifname)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("InterfaceByName(%s): %w", ifname, err)
|
||||
}
|
||||
|
||||
ip, err := netaddr.ParseIP(addr)
|
||||
|
|
Loading…
Reference in New Issue