Skip to content

Commit

Permalink
refactor test IP and registration
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelemusiani committed Apr 2, 2024
1 parent 520aaf0 commit 569dcce
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 45 deletions.
7 changes: 2 additions & 5 deletions cmd/macpassd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"time"

"github.com/j-keck/arping"
"github.com/musianisamuele/macpass/cmd/macpassd/config"
"github.com/musianisamuele/macpass/cmd/macpassd/registration"
)
Expand Down Expand Up @@ -118,9 +117,7 @@ func deleteOldIps() {
ipsThatDidNotAnswered := make([]net.IP, 0)

for _, ip := range e.Ips {
_, _, err := arping.Ping(ip)
if err != nil {
slog.With("ip", ip, "err", err).Debug("error during arping")
if !isIPStillConnected(ip) {
ipsThatDidNotAnswered = append(ipsThatDidNotAnswered, ip)
}
}
Expand All @@ -145,7 +142,7 @@ func deleteDisconnected() {
discTime := config.Get().DisconnectionTime

for _, e := range entries {
if !isStillConnected(e) {
if !isRegistrationStillConnected(e) {
if !e.IsDown {
slog.Info(e.User + " disconnected")
registration.SetHostDown(e)
Expand Down
78 changes: 38 additions & 40 deletions cmd/macpassd/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,50 +64,13 @@ func isInSubnet(ip net.IP, network *net.IPNet) bool {
return ip.Mask(network.Mask).Equal(network.IP.Mask(network.Mask))
}

func isStillConnected(e registration.Registration) bool {
func isRegistrationStillConnected(e registration.Registration) bool {
arping.SetTimeout(1 * time.Second) // should be put in config

slog.With("Registration", e.String()).Debug("Checking registration")
for _, ip := range e.Ips {
slog.With("ip", ip).Debug("Checking ip")
if ip.To4() != nil { // Is an IPv4
mac, _, err := arping.Ping(ip)
if err != nil {
slog.With("ip", ip, "err", err).Debug("error during arping")
} else {
if e.Mac != mac.String() {
// In this case another host has reponded to the arping. It is possible
// that multiples hosts have the same ip or that the previous host has
// changed ip and another host has now his old ip. We assume the first
// one
slog.With("registration", e.String(), "new mac", mac.String()).Debug("Different mac responded to arping")

// TODO

return false
}
return true
}
} else { // Is an IPv6
// To check IPv6 we try to ping the host
p, err := ping.New(ip.String())
if err != nil {
slog.With("ip", ip.String(), "err", err).Error("Could not construct ping object")
continue
}
p.SetCount(1)

r, err := p.Run()
if err != nil {
slog.With("ip", ip.String(), "err", err).Error("Could not run ping to IPv6")
continue
}

for pr := range r {
if pr.Err != nil {
return true
}
}
if isIPStillConnected(ip) {
return true
}
}

Expand Down Expand Up @@ -136,3 +99,38 @@ func isIpPrenset(set []net.IP, ip net.IP) bool {
}
return false
}

func isIPStillConnected(ip net.IP) bool {
slog.With("ip", ip).Debug("Checking ip")

if ip.To4() != nil { // Is an IPv4
_, _, err := arping.Ping(ip)
if err != nil {
slog.With("ip", ip, "err", err).Debug("error during arping")
return false
}
return true
}

// Is an IPv6
// To check IPv6 we try to ping the host
p, err := ping.New(ip.String())
if err != nil {
slog.With("ip", ip.String(), "err", err).Error("Could not construct ping object")
return false
}
p.SetCount(1)

r, err := p.Run()
if err != nil {
slog.With("ip", ip.String(), "err", err).Error("Could not run ping to IPv6")
return false
}

for pr := range r {
if pr.Err != nil {
return true
}
}
return false
}

0 comments on commit 569dcce

Please sign in to comment.