Skip to content

Commit

Permalink
feat: add device name and bpf checkers
Browse files Browse the repository at this point in the history
  • Loading branch information
md-irohas committed Jul 4, 2023
1 parent cad3446 commit 8faa900
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
49 changes: 47 additions & 2 deletions rcap/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package rcap

import (
"errors"
"fmt"
"log"
"os"
"time"

"github.com/go-playground/validator/v10"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/pelletier/go-toml"
)

Expand Down Expand Up @@ -43,6 +45,46 @@ type RcapConfig struct {
UseSystemTime bool `toml:"useSystemTime" default:"false"` // Use system time or packet-captured time.
}

func isValidDevice(name string) bool {
devices, err := pcap.FindAllDevs()
if err != nil {
panic(err)
}

for _, device := range devices {
if device.Name == name {
return true
}
}

return false
}

func isValidBpf(bpf string, device string, captureLength uint) bool {
h, err := pcap.OpenLive(device, int32(captureLength), false, pcap.BlockForever)
if err == nil {
defer h.Close()
return h.SetBPFFilter(bpf) == nil
}

// For test
linkType := layers.LinkTypeEthernet
log.Printf("The device linktype could not be detected. '%v' is used anyway.", linkType)
_, err = pcap.CompileBPFFilter(linkType, int(captureLength), bpf)

return err == nil
}

func CheckDeviceAndBpf(device string, bpf string, captureLength uint) error {
if !isValidDevice(device) {
return fmt.Errorf("invalid device: '%v'", device)
}
if !isValidBpf(bpf, device, captureLength) {
return fmt.Errorf("invalid BPF: '%v'", bpf)
}
return nil
}

// CheckAndFormat method checks and formats the values in the configuration,
// and returns an error if the configuration is invalid.
func (c *Config) CheckAndFormat() error {
Expand All @@ -51,6 +93,9 @@ func (c *Config) CheckAndFormat() error {
if err := validate.Struct(c); err != nil {
return err
}
if err := CheckDeviceAndBpf(c.Rcap.Device, c.Rcap.BpfRules, c.Rcap.SnapLen); err != nil {
return err
}

// no error is returned from LoadLocation because validator checks timezone value.
c.Rcap.Location, _ = time.LoadLocation(c.Rcap.Timezone)
Expand Down Expand Up @@ -103,7 +148,7 @@ func LoadConfig(filename string) (*Config, error) {
log.Println(valErr.Error())
}
}
return nil, errors.New("invalid config values")
return nil, fmt.Errorf("invalid config values: %w", err)
}

return config, nil
Expand Down
5 changes: 3 additions & 2 deletions rcap/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ func NewRunner(c *Config) (*Runner, error) {
func (r *Runner) Reload() error {
if r.config.Filename == "" {
err := errors.New("no config file is set.")
log.Printf("failed to reload config: %v\n", err)
log.Printf("failed to reload config: %v", err)
return err
}

newConfig, err := LoadConfig(r.config.Filename)
if err != nil {
log.Printf("failed to reload config: %v\n", err)
log.Printf("failed to reload config: %v", err)
log.Println("use the previous config instead.")
return err
}

Expand Down

0 comments on commit 8faa900

Please sign in to comment.