Pass Slice by reference, not value
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user