From ed6d40f16353379e3d5ae4308681cf5f72f8bb47 Mon Sep 17 00:00:00 2001 From: Edznux Date: Tue, 23 Oct 2018 05:32:51 +0200 Subject: [PATCH] refactor if/else in network and packets packages --- network/lan_endpoint.go | 17 +++++++++++------ network/net_gateway.go | 15 +++++++-------- network/net_linux.go | 6 ++++-- network/wifi.go | 12 ++++++++---- packets/dhcp6.go | 6 +++--- packets/queue.go | 3 +-- 6 files changed, 34 insertions(+), 25 deletions(-) diff --git a/network/lan_endpoint.go b/network/lan_endpoint.go index 025995cb..81b525fd 100644 --- a/network/lan_endpoint.go +++ b/network/lan_endpoint.go @@ -147,9 +147,11 @@ func (t *Endpoint) String() string { if t.HwAddress == "" { return t.IpAddress - } else if t.Vendor == "" { + } + if t.Vendor == "" { return fmt.Sprintf("%s%s", ipPart, t.HwAddress) - } else if t.Hostname == "" { + } + if t.Hostname == "" { return fmt.Sprintf("%s%s ( %s )", ipPart, t.HwAddress, t.Vendor) } return fmt.Sprintf("%s%s ( %s ) - %s", ipPart, t.HwAddress, t.Vendor, tui.Bold(t.Hostname)) @@ -159,10 +161,13 @@ func (t *Endpoint) OnMeta(meta map[string]string) { host := "" for k, v := range meta { // simple heuristics to get the longest candidate name - if strings.HasSuffix(k, ":hostname") && len(v) > len(host) { - host = v - } else if k == "mdns:md" && len(v) > len(host) { - host = v + if len(v) > len(host) { + if strings.HasSuffix(k, ":hostname"){ + host = v + } + if k == "mdns:md"{ + host = v + } } t.Meta.Set(k, v) } diff --git a/network/net_gateway.go b/network/net_gateway.go index 4b2cb268..931bd4c1 100644 --- a/network/net_gateway.go +++ b/network/net_gateway.go @@ -31,15 +31,14 @@ func FindGateway(iface *Endpoint) (*Endpoint, error) { if gateway == iface.IpAddress { Debug("gateway is the interface") return iface, nil - } else { - // we have the address, now we need its mac - mac, err := ArpLookup(ifName, gateway, false) - if err != nil { - return nil, err - } - Debug("gateway is %s[%s]", gateway, mac) - return NewEndpoint(gateway, mac), nil } + // we have the address, now we need its mac + mac, err := ArpLookup(ifName, gateway, false) + if err != nil { + return nil, err + } + Debug("gateway is %s[%s]", gateway, mac) + return NewEndpoint(gateway, mac), nil }) } } diff --git a/network/net_linux.go b/network/net_linux.go index decbc022..302fc232 100644 --- a/network/net_linux.go +++ b/network/net_linux.go @@ -50,7 +50,8 @@ func SetInterfaceChannel(iface string, channel int) error { out, err := core.Exec("iwconfig", []string{iface, "channel", fmt.Sprintf("%d", channel)}) if err != nil { return err - } else if out != "" { + } + if out != "" { return fmt.Errorf("Unexpected output while setting interface %s to channel %d: %s", iface, channel, out) } @@ -63,7 +64,8 @@ func processSupportedFrequencies(output string, err error) ([]int, error) { freqs := make([]int, 0) if err != nil { return freqs, err - } else if output != "" { + } + if output != "" { scanner := bufio.NewScanner(strings.NewReader(output)) for scanner.Scan() { line := scanner.Text() diff --git a/network/wifi.go b/network/wifi.go index 389b8960..ba1f4738 100644 --- a/network/wifi.go +++ b/network/wifi.go @@ -10,9 +10,11 @@ import ( func Dot11Freq2Chan(freq int) int { if freq <= 2472 { return ((freq - 2412) / 5) + 1 - } else if freq == 2484 { + } + if freq == 2484 { return 14 - } else if freq >= 5035 && freq <= 5865 { + } + if freq >= 5035 && freq <= 5865 { return ((freq - 5035) / 5) + 7 } return 0 @@ -21,9 +23,11 @@ func Dot11Freq2Chan(freq int) int { func Dot11Chan2Freq(channel int) int { if channel <= 13 { return ((channel - 1) * 5) + 2412 - } else if channel == 14 { + } + if channel == 14 { return 2484 - } else if channel <= 173 { + } + if channel <= 173 { return ((channel - 7) * 5) + 5035 } diff --git a/packets/dhcp6.go b/packets/dhcp6.go index 01056597..1b67e471 100644 --- a/packets/dhcp6.go +++ b/packets/dhcp6.go @@ -38,11 +38,11 @@ func DHCP6For(what dhcp6.MessageType, to dhcp6.Packet, duid []byte) (err error, } var rawCID []byte - if raw, found := to.Options[dhcp6.OptionClientID]; !found || len(raw) < 1 { + raw, found := to.Options[dhcp6.OptionClientID] + if !found || len(raw) < 1 { return ErrNoCID, p - } else { - rawCID = raw[0] } + rawCID = raw[0] p.Options.AddRaw(dhcp6.OptionClientID, rawCID) p.Options.AddRaw(dhcp6.OptionServerID, duid) diff --git a/packets/queue.go b/packets/queue.go index 94aef1a1..5035d67a 100644 --- a/packets/queue.go +++ b/packets/queue.go @@ -233,9 +233,8 @@ func (q *Queue) Send(raw []byte) error { if err := q.handle.WritePacketData(raw); err != nil { q.TrackError() return err - } else { - q.TrackSent(uint64(len(raw))) } + q.TrackSent(uint64(len(raw))) return nil }