From 6d146140437e55afc402e928d1a15b4778b77700 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Fri, 31 Mar 2023 16:22:01 +0200 Subject: [PATCH] Pass Slice by reference, not value --- cmd/app/main.go | 6 +++--- lib/wgchecks/netchecks/netchecks.go | 12 ++++++------ lib/wgchecks/wgchecks.go | 20 ++++++++++---------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/cmd/app/main.go b/cmd/app/main.go index 78798b0..8a54ea1 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -201,13 +201,13 @@ func handleRouteEvents(routeSubChan <-chan netlink.RouteUpdate, filterOptions Fi } // Check if other peer already has exact same dst - if peer, err := wgChecks.PeerByIPNet(wgDevice.Peers, *route.Dst); err == nil { + if peer, err := wgChecks.PeerByIPNet(&wgDevice.Peers, route.Dst); err == nil { logger.Warn.Printf("dst-IPNet already set for Peer '%s', ignoring", peer.PublicKey) continue } // Get peer containing gateway-addr - peer, err := wgChecks.PeerByIP(wgDevice.Peers, route.Gw) + peer, err := wgChecks.PeerByIP(&wgDevice.Peers, &route.Gw) if(err != nil){ logger.Warn.Printf("No peer found containing gw-IP '%s', ignoring", route.Gw) continue @@ -223,7 +223,7 @@ func handleRouteEvents(routeSubChan <-chan netlink.RouteUpdate, filterOptions Fi case unix.RTM_DELROUTE: // Get peer containing dst-NetIP - peerIndex, ipNetIndex, err := wgChecks.PeerIndexByIPNet(wgDevice.Peers, *route.Dst) + peerIndex, ipNetIndex, err := wgChecks.PeerIndexByIPNet(&wgDevice.Peers, route.Dst) if(err != nil){ logger.Warn.Printf("No peer found having dst-IPNet '%s', ignoring", route.Dst) continue diff --git a/lib/wgchecks/netchecks/netchecks.go b/lib/wgchecks/netchecks/netchecks.go index 6e747cd..59a3094 100644 --- a/lib/wgchecks/netchecks/netchecks.go +++ b/lib/wgchecks/netchecks/netchecks.go @@ -6,18 +6,18 @@ import ( "reflect" ) -func IPNetIndexByIP(list []net.IPNet, ip net.IP) (int, error) { - for index, ipNetEntry := range list { - if ipNetEntry.Contains(ip) { +func IPNetIndexByIP(list *[]net.IPNet, ip *net.IP) (int, error) { + for index, ipNetEntry := range *list { + if ipNetEntry.Contains(*ip) { return index, nil } } return -1, fmt.Errorf("ip not in ipNet-list") } -func IPNetIndexByIPNet(list []net.IPNet, ipNet net.IPNet) (int, error) { - for index, ipNetEntry := range list { - if reflect.DeepEqual(ipNetEntry, ipNet) { +func IPNetIndexByIPNet(list *[]net.IPNet, ipNet *net.IPNet) (int, error) { + for index, ipNetEntry := range *list { + if reflect.DeepEqual(ipNetEntry, *ipNet) { return index, nil } } diff --git a/lib/wgchecks/wgchecks.go b/lib/wgchecks/wgchecks.go index 4bc69a9..1e4d375 100644 --- a/lib/wgchecks/wgchecks.go +++ b/lib/wgchecks/wgchecks.go @@ -9,34 +9,34 @@ import ( "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) -func PeerIndexByIP(peers []wgtypes.Peer, ip net.IP) (int, int, error) { - for index, peer := range peers { - if ipIndex, err := netchecks.IPNetIndexByIP(peer.AllowedIPs, ip); err == nil { +func PeerIndexByIP(peers *[]wgtypes.Peer, ip *net.IP) (int, int, error) { + for index, peer := range *peers { + if ipIndex, err := netchecks.IPNetIndexByIP(&peer.AllowedIPs, ip); err == nil { return index, ipIndex, nil } } return -1, -1, fmt.Errorf("no peer by ip in list") } -func PeerByIP(peers []wgtypes.Peer, ip net.IP) (*wgtypes.Peer, error) { +func PeerByIP(peers *[]wgtypes.Peer, ip *net.IP) (*wgtypes.Peer, error) { index, _, err := PeerIndexByIP(peers, ip) if(err != nil) { return nil, err } - return &peers[index], nil + return &(*peers)[index], nil } -func PeerIndexByIPNet(peers []wgtypes.Peer, ipNet net.IPNet) (int, int, error) { - for index, peer := range peers { - if ipNetIndex, err := netchecks.IPNetIndexByIPNet(peer.AllowedIPs, ipNet); err == nil { +func PeerIndexByIPNet(peers *[]wgtypes.Peer, ipNet *net.IPNet) (int, int, error) { + for index, peer := range *peers { + if ipNetIndex, err := netchecks.IPNetIndexByIPNet(&peer.AllowedIPs, ipNet); err == nil { return index, ipNetIndex, nil } } return -1, -1, fmt.Errorf("no peer by ipNet in list") } -func PeerByIPNet(peers []wgtypes.Peer, ipNet net.IPNet) (*wgtypes.Peer, error) { +func PeerByIPNet(peers *[]wgtypes.Peer, ipNet *net.IPNet) (*wgtypes.Peer, error) { index, _, err := PeerIndexByIPNet(peers, ipNet) if(err != nil) { return nil, err } - return &peers[index], nil + return &(*peers)[index], nil }