Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ICMP count and cleanup. #44

Merged
merged 1 commit into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions pkg/ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import (
)

// Ping ICMP Operation
func Ping(addr string, ip string, srcAddr string, count int, interval time.Duration, timeout time.Duration, icmpID int) (*PingResult, error) {
func Ping(addr string, ip string, srcAddr string, count int, timeout time.Duration, icmpID int) (*PingResult, error) {
var out PingResult

pingOptions := &PingOptions{}
pingOptions.SetCount(count)
pingOptions.SetTimeout(timeout)
pingOptions.SetInterval(interval)

out, err := runPing(addr, ip, srcAddr, icmpID, pingOptions)
if err != nil {
Expand All @@ -26,11 +25,10 @@ func Ping(addr string, ip string, srcAddr string, count int, interval time.Durat
}

// PingString ICMP Operation
func PingString(addr string, ip string, srcAddr string, count int, timeout time.Duration, interval time.Duration, icmpID int) (result string, err error) {
func PingString(addr string, ip string, srcAddr string, count int, timeout time.Duration, icmpID int) (result string, err error) {
pingOptions := &PingOptions{}
pingOptions.SetCount(count)
pingOptions.SetTimeout(timeout)
pingOptions.SetInterval(interval)

var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("Start %v, PING %v (%v)\n", time.Now().Format("2006-01-02 15:04:05"), addr, addr))
Expand All @@ -57,7 +55,6 @@ func runPing(ipAddr string, ip string, srcAddr string, icmpID int, option *PingO
// Avoid collisions/interference caused by multiple coroutines initiating mtr
pid := icmpID
timeout := option.Timeout()
interval := option.Interval()
ttl := defaultTTL
pingReturn := PingReturn{}

Expand All @@ -83,7 +80,6 @@ func runPing(ipAddr string, ip string, srcAddr string, icmpID int, option *PingO
pingReturn.success = true

seq++
time.Sleep(interval)
}

pingResult.Success = pingReturn.success
Expand Down
15 changes: 0 additions & 15 deletions pkg/ping/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ping
import "time"

const defaultTimeout = 5 * time.Second
const defaultInterval = 10 * time.Millisecond
const defaultPackerSize = 56
const defaultCount = 10
const defaultTTL = 128
Expand Down Expand Up @@ -42,7 +41,6 @@ type PingReturn struct {
type PingOptions struct {
count int
timeout time.Duration
interval time.Duration
packetSize int
}

Expand Down Expand Up @@ -72,19 +70,6 @@ func (options *PingOptions) SetTimeout(timeout time.Duration) {
options.timeout = timeout
}

// Interval Getter
func (options *PingOptions) Interval() time.Duration {
if options.interval == 0 {
options.interval = defaultInterval
}
return options.interval
}

// SetInterval Setter
func (options *PingOptions) SetInterval(interval time.Duration) {
options.interval = interval
}

// PacketSize Getter
func (options *PingOptions) PacketSize() int {
if options.packetSize == 0 {
Expand Down
3 changes: 1 addition & 2 deletions pkg/tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (
)

// Port TCP Operation
func Port(destAddr string, ip string, srcAddr string, port string, interval time.Duration, timeout time.Duration) (*TCPPortReturn, error) {
func Port(destAddr string, ip string, srcAddr string, port string, timeout time.Duration) (*TCPPortReturn, error) {
var out TCPPortReturn
var d net.Dialer
var err error

tcpOptions := &TCPPortOptions{}
tcpOptions.SetInterval(interval)
tcpOptions.SetTimeout(timeout)

out.DestAddr = destAddr
Expand Down
15 changes: 0 additions & 15 deletions pkg/tcp/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tcp
import "time"

const defaultTimeout = 5 * time.Second
const defaultInterval = 10 * time.Millisecond

// TCPPortReturn Calculated results
type TCPPortReturn struct {
Expand All @@ -18,7 +17,6 @@ type TCPPortReturn struct {
// TCPPortOptions ICMP Options
type TCPPortOptions struct {
timeout time.Duration
interval time.Duration
}

// Timeout Getter
Expand All @@ -33,16 +31,3 @@ func (options *TCPPortOptions) Timeout() time.Duration {
func (options *TCPPortOptions) SetTimeout(timeout time.Duration) {
options.timeout = timeout
}

// Interval Getter
func (options *TCPPortOptions) Interval() time.Duration {
if options.interval == 0 {
options.interval = defaultInterval
}
return options.interval
}

// SetInterval Setter
func (options *TCPPortOptions) SetInterval(interval time.Duration) {
options.interval = interval
}
2 changes: 1 addition & 1 deletion target/target_ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (t *PING) Stop() {

func (t *PING) ping() {
icmpID := int(t.icmpID.Get())
data, err := ping.Ping(t.host, t.ip, t.srcAddr, t.count, t.interval, t.timeout, icmpID)
data, err := ping.Ping(t.host, t.ip, t.srcAddr, t.count, t.timeout, icmpID)
if err != nil {
level.Error(t.logger).Log("type", "ICMP", "func", "ping", "msg", fmt.Sprintf("%s", err))
}
Expand Down
2 changes: 1 addition & 1 deletion target/target_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (t *TCPPort) Stop() {
}

func (t *TCPPort) portCheck() {
data, err := tcp.Port(t.host, t.ip, t.srcAddr, t.port, t.interval, t.timeout)
data, err := tcp.Port(t.host, t.ip, t.srcAddr, t.port, t.timeout)
if err != nil {
level.Error(t.logger).Log("type", "TCP", "func", "port", "msg", fmt.Sprintf("%s", err))
}
Expand Down