From c2d0e87749b71e12d598bed6cddfc9b9d1490a0b Mon Sep 17 00:00:00 2001 From: Christopher Meis Date: Thu, 1 Aug 2024 08:18:33 +0200 Subject: [PATCH] Implementing reviews Signed-off-by: Christopher Meis --- cmds/exp/traceroute/main.go | 6 +++--- cmds/exp/traceroute/traceroute_test.go | 26 +++++++++++++------------- pkg/traceroute/icmp4.go | 2 +- pkg/traceroute/icmp6.go | 3 +-- pkg/traceroute/tcp4.go | 2 +- pkg/traceroute/tcp6.go | 4 ++-- pkg/traceroute/traceroute.go | 2 +- pkg/traceroute/traceroute_test.go | 2 +- pkg/traceroute/udp4.go | 10 +++------- pkg/traceroute/udp6.go | 7 ++----- pkg/traceroute/util.go | 10 +++++----- 11 files changed, 33 insertions(+), 41 deletions(-) diff --git a/cmds/exp/traceroute/main.go b/cmds/exp/traceroute/main.go index 4a08a501e9..be9a013c59 100644 --- a/cmds/exp/traceroute/main.go +++ b/cmds/exp/traceroute/main.go @@ -16,7 +16,7 @@ import ( "github.com/u-root/u-root/pkg/uroot/unixflag" ) -var errFlags = errors.New("invalid flag/arugment usage") +var errFlags = errors.New("invalid flag/argument usage") func parseFlags(args []string) (*traceroute.Flags, error) { flags := &traceroute.Flags{} @@ -43,7 +43,7 @@ func parseFlags(args []string) (*traceroute.Flags, error) { f.Parse(unixflag.ArgsToGoArgs(args[1:])) leftoverArgs := f.Args() - fmt.Println(len(leftoverArgs)) + if len(leftoverArgs) > 1 { // Error, print help and exit f.Usage() @@ -81,7 +81,7 @@ func run(args []string) error { return err } // Pass execution to pkg/traceroute. - // Setup can be quite complexe with such amount of flags + // Setup can be quite complex with such amount of flags // and the different modules. return traceroute.RunTraceroute(flags) } diff --git a/cmds/exp/traceroute/traceroute_test.go b/cmds/exp/traceroute/traceroute_test.go index c3d4cbd4f7..9e581c664a 100644 --- a/cmds/exp/traceroute/traceroute_test.go +++ b/cmds/exp/traceroute/traceroute_test.go @@ -19,7 +19,7 @@ func TestParseFlags(t *testing.T) { err error }{ { - name: "Simple", + name: "ModuleICMP4", cmdline: []string{"progName", "-4", "-m", "icmp", "www.google.com"}, exp: &traceroute.Flags{ Host: "www.google.com", @@ -28,7 +28,7 @@ func TestParseFlags(t *testing.T) { }, }, { - name: "Simple", + name: "DirectICMP4", cmdline: []string{"progName", "-4", "--icmp", "www.google.com"}, exp: &traceroute.Flags{ Host: "www.google.com", @@ -37,7 +37,7 @@ func TestParseFlags(t *testing.T) { }, }, { - name: "Simple", + name: "ModuleTCP4", cmdline: []string{"progName", "-4", "-m", "tcp", "www.google.com"}, exp: &traceroute.Flags{ Host: "www.google.com", @@ -46,7 +46,7 @@ func TestParseFlags(t *testing.T) { }, }, { - name: "Simple", + name: "DirectTCP4", cmdline: []string{"progName", "-4", "--tcp", "www.google.com"}, exp: &traceroute.Flags{ Host: "www.google.com", @@ -55,7 +55,7 @@ func TestParseFlags(t *testing.T) { }, }, { - name: "Simple", + name: "ModuleUDP4", cmdline: []string{"progName", "-4", "-m", "udp", "www.google.com"}, exp: &traceroute.Flags{ Host: "www.google.com", @@ -64,7 +64,7 @@ func TestParseFlags(t *testing.T) { }, }, { - name: "Simple", + name: "DirectUDP4", cmdline: []string{"progName", "-4", "--udp", "www.google.com"}, exp: &traceroute.Flags{ Host: "www.google.com", @@ -73,7 +73,7 @@ func TestParseFlags(t *testing.T) { }, }, { - name: "Simple", + name: "ModuleICMP6", cmdline: []string{"progName", "-6", "-m", "icmp", "www.google.com"}, exp: &traceroute.Flags{ Host: "www.google.com", @@ -82,7 +82,7 @@ func TestParseFlags(t *testing.T) { }, }, { - name: "Simple", + name: "DirectICMP6", cmdline: []string{"progName", "-6", "--icmp", "www.google.com"}, exp: &traceroute.Flags{ Host: "www.google.com", @@ -91,7 +91,7 @@ func TestParseFlags(t *testing.T) { }, }, { - name: "Simple", + name: "ModuleTCP6", cmdline: []string{"progName", "-6", "-m", "tcp", "www.google.com"}, exp: &traceroute.Flags{ Host: "www.google.com", @@ -100,7 +100,7 @@ func TestParseFlags(t *testing.T) { }, }, { - name: "Simple", + name: "DirectTCP6", cmdline: []string{"progName", "-6", "--tcp", "www.google.com"}, exp: &traceroute.Flags{ Host: "www.google.com", @@ -109,7 +109,7 @@ func TestParseFlags(t *testing.T) { }, }, { - name: "Simple", + name: "ModuleUDP6", cmdline: []string{"progName", "-6", "-m", "udp", "www.google.com"}, exp: &traceroute.Flags{ Host: "www.google.com", @@ -118,7 +118,7 @@ func TestParseFlags(t *testing.T) { }, }, { - name: "Simple", + name: "DirectUDP6", cmdline: []string{"progName", "-6", "--udp", "www.google.com"}, exp: &traceroute.Flags{ Host: "www.google.com", @@ -127,7 +127,7 @@ func TestParseFlags(t *testing.T) { }, }, { - name: "Simple", + name: "FailInvalidFlags", cmdline: []string{"progName", "-6", "--udp", "www.google.com", "random stuff to error out", "somemore"}, exp: &traceroute.Flags{ Host: "www.google.com", diff --git a/pkg/traceroute/icmp4.go b/pkg/traceroute/icmp4.go index 5c8b31f4ca..d95577e700 100644 --- a/pkg/traceroute/icmp4.go +++ b/pkg/traceroute/icmp4.go @@ -62,7 +62,7 @@ func (t *Trace) ReceiveTracesICMP4() { buf := make([]byte, 1500) n, raddr, err := recvICMPConn.ReadFrom(buf) if err != nil { - return + log.Fatal(err) } icmpType := buf[0] diff --git a/pkg/traceroute/icmp6.go b/pkg/traceroute/icmp6.go index 78722480c0..dabdac6f16 100644 --- a/pkg/traceroute/icmp6.go +++ b/pkg/traceroute/icmp6.go @@ -7,7 +7,6 @@ package traceroute import ( "bytes" "encoding/binary" - "fmt" "log" "net" "time" @@ -18,7 +17,7 @@ import ( func (t *Trace) SendTracesICMP6() { conn, err := net.ListenPacket("ip6:ipv6-icmp", t.SrcIP.String()) if err != nil { - fmt.Println(err) + log.Fatal(err) } defer conn.Close() diff --git a/pkg/traceroute/tcp4.go b/pkg/traceroute/tcp4.go index ab2f61f993..815338f583 100644 --- a/pkg/traceroute/tcp4.go +++ b/pkg/traceroute/tcp4.go @@ -130,7 +130,7 @@ func (t *Trace) IPv4TCPPing(seq uint32, dport uint16) { conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", t.DestIP.String(), dport), time.Second*2) if err != nil { - return + log.Fatal(err) } conn.Close() pbr := &Probe{ diff --git a/pkg/traceroute/tcp6.go b/pkg/traceroute/tcp6.go index 365e9ad4c1..9bab206c62 100644 --- a/pkg/traceroute/tcp6.go +++ b/pkg/traceroute/tcp6.go @@ -57,7 +57,7 @@ func (t *Trace) ReceiveTracesTCP6() { buf := make([]byte, 1500) n, raddr, err := recvTCPConn.ReadFrom(buf) if err != nil { - return + log.Fatal(err) } tcphdr, _ := ParseTCP(buf) @@ -123,7 +123,7 @@ func (t *Trace) IPv6TCPPing(seq uint32, dport uint16) { conn, err := net.DialTimeout("ip6:tcp", fmt.Sprintf("%s:%d", t.DestIP.String(), dport), time.Second*2) if err != nil { - return + log.Fatal(err) } conn.Close() diff --git a/pkg/traceroute/traceroute.go b/pkg/traceroute/traceroute.go index 377673570e..ede7bba5ec 100644 --- a/pkg/traceroute/traceroute.go +++ b/pkg/traceroute/traceroute.go @@ -56,7 +56,7 @@ func RunTraceroute(f *Flags) error { printMap := runTransmission(cc) - destTTL := FindDestinationTTL(printMap) + destTTL := DestTTL(printMap) fmt.Printf("traceroute to %s (%s), %d hops max, %d byte packets\n", f.Host, dAddr.String(), diff --git a/pkg/traceroute/traceroute_test.go b/pkg/traceroute/traceroute_test.go index b9a6e798de..bd677cd45e 100644 --- a/pkg/traceroute/traceroute_test.go +++ b/pkg/traceroute/traceroute_test.go @@ -187,7 +187,7 @@ func TestFindDestinationTTL(t *testing.T) { TTL: 14, }, } - ttl := traceroute.FindDestinationTTL(pbMap) + ttl := traceroute.DestTTL(pbMap) if ttl != 15 { t.Errorf("FindDestinationTTL() = %d, not %d", ttl, 15) diff --git a/pkg/traceroute/udp4.go b/pkg/traceroute/udp4.go index f78c236a58..901bf59855 100644 --- a/pkg/traceroute/udp4.go +++ b/pkg/traceroute/udp4.go @@ -7,7 +7,6 @@ package traceroute import ( "bytes" "encoding/binary" - "fmt" "log" "math/rand" "net" @@ -27,15 +26,13 @@ func (t *Trace) SendTracesUDP4() { for j := 0; j < t.TracesPerHop; j++ { conn, err := net.ListenPacket("ip4:udp", "") if err != nil { - log.Printf("net.ListenPacket() = %v", err) - return + log.Fatalf("net.ListenPacket() = %v", err) } defer conn.Close() rSock, err := ipv4.NewRawConn(conn) if err != nil { - log.Printf("ipv4.NewRawConn() = %v", err) - return + log.Fatalf("ipv4.NewRawConn() = %v", err) } pb := &Probe{ @@ -71,8 +68,7 @@ func (t *Trace) ReceiveTracesUDP4() { buf := make([]byte, 1500) n, raddr, err := recvICMPConn.ReadFrom(buf) if err != nil { - fmt.Println(err) - return + log.Fatal(err) } icmpType := buf[0] diff --git a/pkg/traceroute/udp6.go b/pkg/traceroute/udp6.go index fea334906e..050d6475c5 100644 --- a/pkg/traceroute/udp6.go +++ b/pkg/traceroute/udp6.go @@ -7,7 +7,6 @@ package traceroute import ( "bytes" "encoding/binary" - "fmt" "log" "math/rand" "net" @@ -26,8 +25,7 @@ func (t *Trace) SendTracesUDP6() { for j := 0; j < t.TracesPerHop; j++ { conn, err := net.ListenPacket("ip6:udp", "") if err != nil { - log.Printf("net.ListenPacket() = %v", err) - return + log.Fatalf("net.ListenPacket() = %v", err) } defer conn.Close() @@ -64,8 +62,7 @@ func (t *Trace) ReceiveTracesUDP6() { buf := make([]byte, 1500) n, raddr, err := recvICMPConn.ReadFrom(buf) if err != nil { - fmt.Println(err) - return + log.Fatal(err) } ip6hdr, _ := ipv6.ParseHeader(buf[8:]) diff --git a/pkg/traceroute/util.go b/pkg/traceroute/util.go index fe821dce2e..44ff7d4b55 100644 --- a/pkg/traceroute/util.go +++ b/pkg/traceroute/util.go @@ -65,25 +65,25 @@ func SrcAddr(proto string) (*net.IP, error) { return &sAddr.(*net.UDPAddr).IP, nil } -func FindDestinationTTL(printMap map[int]*Probe) int { +func DestTTL(printMap map[int]*Probe) int { icmp := false destttl := 1 - var icmpfinalpb *Probe + var icmpFinalPB *Probe for _, pb := range printMap { if destttl < pb.TTL { destttl = pb.TTL } if pb.TTL == 0 { // ICMP TCPProbe needs to increase return value by one - icmpfinalpb = pb + icmpFinalPB = pb icmp = true } } if icmp { destttl++ - newttl := destttl - icmpfinalpb.TTL = newttl + newTTL := destttl + icmpFinalPB.TTL = newTTL } return destttl