fix(inputs.ping): Check addr length to avoid crash (#15601)

This commit is contained in:
Joshua Powers 2024-07-10 07:50:44 -06:00 committed by GitHub
parent a768de8562
commit 3a05c38fd8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 20 additions and 17 deletions

View File

@ -147,6 +147,26 @@ func (p *Ping) nativePing(destination string) (*pingStats, error) {
}
}
// Support either an IP address or interface name
if p.Interface != "" && p.sourceAddress == "" {
if addr := net.ParseIP(p.Interface); addr != nil {
p.sourceAddress = p.Interface
} else {
i, err := net.InterfaceByName(p.Interface)
if err != nil {
return nil, fmt.Errorf("failed to get interface: %w", err)
}
addrs, err := i.Addrs()
if err != nil {
return nil, fmt.Errorf("failed to get the address of interface: %w", err)
}
if len(addrs) == 0 {
return nil, fmt.Errorf("no address found for interface %s", p.Interface)
}
p.sourceAddress = addrs[0].(*net.IPNet).IP.String()
}
}
pinger.Source = p.sourceAddress
pinger.Interval = p.calcInterval
@ -292,23 +312,6 @@ func (p *Ping) Init() error {
p.calcTimeout = time.Duration(p.Timeout) * time.Second
}
// Support either an IP address or interface name
if p.Interface != "" {
if addr := net.ParseIP(p.Interface); addr != nil {
p.sourceAddress = p.Interface
} else {
i, err := net.InterfaceByName(p.Interface)
if err != nil {
return fmt.Errorf("failed to get interface: %w", err)
}
addrs, err := i.Addrs()
if err != nil {
return fmt.Errorf("failed to get the address of interface: %w", err)
}
p.sourceAddress = addrs[0].(*net.IPNet).IP.String()
}
}
return nil
}