Skip to content

Commit

Permalink
Implementing reviews
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Meis <[email protected]>
  • Loading branch information
ChriMarMe committed Aug 8, 2024
1 parent 4fecbd3 commit c2d0e87
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 41 deletions.
6 changes: 3 additions & 3 deletions cmds/exp/traceroute/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down
26 changes: 13 additions & 13 deletions cmds/exp/traceroute/traceroute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion pkg/traceroute/icmp4.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
3 changes: 1 addition & 2 deletions pkg/traceroute/icmp6.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package traceroute
import (
"bytes"
"encoding/binary"
"fmt"
"log"
"net"
"time"
Expand All @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion pkg/traceroute/tcp4.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions pkg/traceroute/tcp6.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion pkg/traceroute/traceroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/traceroute/traceroute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 3 additions & 7 deletions pkg/traceroute/udp4.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package traceroute
import (
"bytes"
"encoding/binary"
"fmt"
"log"
"math/rand"
"net"
Expand All @@ -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{
Expand Down Expand Up @@ -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]
Expand Down
7 changes: 2 additions & 5 deletions pkg/traceroute/udp6.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package traceroute
import (
"bytes"
"encoding/binary"
"fmt"
"log"
"math/rand"
"net"
Expand All @@ -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()

Expand Down Expand Up @@ -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:])
Expand Down
10 changes: 5 additions & 5 deletions pkg/traceroute/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c2d0e87

Please sign in to comment.