aboutsummaryrefslogtreecommitdiffstats
path: root/internal/pkg/util/net.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/pkg/util/net.go')
-rw-r--r--internal/pkg/util/net.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/internal/pkg/util/net.go b/internal/pkg/util/net.go
new file mode 100644
index 0000000..1ff7fbb
--- /dev/null
+++ b/internal/pkg/util/net.go
@@ -0,0 +1,34 @@
+package util
+
+import (
+ "fmt"
+ "math/big"
+ "math/rand"
+ "net"
+ "time"
+)
+
+// GenerateMac generates mac address.
+func GenerateMac() string {
+ prefix := "00:00:00"
+ newRand := rand.New(rand.NewSource(time.Now().UnixNano()))
+ mac := fmt.Sprintf("%s:%02x:%02x:%02x", prefix, newRand.Intn(255), newRand.Intn(255), newRand.Intn(255))
+ return mac
+}
+
+// NextIP returns IP incremented by 1
+func NextIP(ip net.IP) net.IP {
+ i := ipToInt(ip)
+ return intToIP(i.Add(i, big.NewInt(1)))
+}
+
+func ipToInt(ip net.IP) *big.Int {
+ if v := ip.To4(); v != nil {
+ return big.NewInt(0).SetBytes(v)
+ }
+ return big.NewInt(0).SetBytes(ip.To16())
+}
+
+func intToIP(i *big.Int) net.IP {
+ return net.IP(i.Bytes())
+}