Skip to content

Commit

Permalink
Merge pull request #44 from RomainMou/fix/icmp_count
Browse files Browse the repository at this point in the history
Fix ICMP count and cleanup.
  • Loading branch information
syepes authored Mar 16, 2024
2 parents 1900697 + e30a570 commit 8ba5f2f
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 40 deletions.
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

0 comments on commit 8ba5f2f

Please sign in to comment.