-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
90 lines (83 loc) · 1.89 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// arplogger is a tool for Linux systems that listens for arp packets on
// the specified interface(s) to discover new hosts appearing on the
// local IPv4 network.
//
// Copyright (c) 2021-2022 Johannes Heimansberg
// SPDX-License-Identifier: MIT
package main
import (
"bufio"
"fmt"
"net"
"os"
"strings"
"sync"
)
type DB struct {
mu sync.RWMutex
databasePath string
}
func (db *DB) Init(dbPath string) error {
db.mu.Lock()
defer db.mu.Unlock()
db.databasePath = dbPath
f, err := os.OpenFile(db.databasePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err == nil {
f.Close()
}
return err
}
func (db *DB) Clear() error {
return os.Truncate(db.databasePath, 0)
}
// verifyIP checks if the supplied string has the correct format for an IP address
func (db *DB) verifyIP(ip string) error {
res := net.ParseIP(ip)
if res == nil {
return fmt.Errorf("Inavlid IP address")
}
return nil
}
// CheckMAC checks if the supplied MAC address is found in the database
func (db *DB) CheckMAC(mac string) (bool, error) {
macAddr, err := net.ParseMAC(mac)
if err != nil {
return false, err
}
mac = macAddr.String()
db.mu.RLock()
defer db.mu.RUnlock()
f, err := os.OpenFile(db.databasePath, os.O_RDONLY, 0644)
defer f.Close()
if err != nil {
return false, err
}
s := bufio.NewScanner(f)
s.Split(bufio.ScanLines)
for s.Scan() {
entry := strings.Split(s.Text(), " ")
if entry[0] == mac { // Match found
return true, nil
}
}
return false, nil
}
// Add adds a new entry to the database
func (db *DB) Add(mac string, ip string) error {
macAddr, err := net.ParseMAC(mac)
if err != nil {
return err
}
if err := db.verifyIP(ip); err != nil {
return err
}
db.mu.Lock()
defer db.mu.Unlock()
f, err := os.OpenFile(db.databasePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
defer f.Close()
if err != nil {
return err
}
fmt.Fprintf(f, "%s %s\n", macAddr.String(), ip)
return nil
}