151 lines
3.5 KiB
Go
151 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"math/rand"
|
|
"net"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/go-ping/ping"
|
|
|
|
"github.com/milosgajdos/tenus"
|
|
)
|
|
|
|
func init() {
|
|
|
|
ZoreideBridge.initializeHierarchy()
|
|
|
|
}
|
|
|
|
func (b *AbstractBridge) RefreshArp() {
|
|
|
|
// we want the program to recover in case of issues
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
|
|
fmt.Println("An error happened in <RefreshARP()>, but Zoreide recovered. ")
|
|
fmt.Println("Error was: ", r)
|
|
}
|
|
}()
|
|
|
|
b.GArp = new(Gratuitous)
|
|
|
|
var err error
|
|
b.GArp.IP, _, err = net.ParseCIDR(b.BridgeIpCIDR)
|
|
if err != nil {
|
|
log.Println("Error parsing CIDR: ", err.Error())
|
|
}
|
|
b.GArp.IfaceName = b.ExistingInterface
|
|
log.Println("ARP Interface name: ", b.GArp.IfaceName)
|
|
|
|
SendGratuitous(b.GArp)
|
|
|
|
}
|
|
|
|
func (b *AbstractBridge) initializeHierarchy() {
|
|
|
|
var letterRunes = []rune("123456789")
|
|
|
|
num := make([]rune, numberlenght)
|
|
for i := range num {
|
|
num[i] = letterRunes[rand.Intn(len(letterRunes))]
|
|
}
|
|
|
|
zz, err := strconv.ParseInt(string(num), 10, 64)
|
|
if err != nil {
|
|
log.Println("Error generating number: ", err.Error())
|
|
b.hIerarchyNumber = 0
|
|
return
|
|
} else {
|
|
b.hIerarchyNumber = zz
|
|
log.Println("Success generating number: ", b.hIerarchyNumber)
|
|
}
|
|
|
|
}
|
|
|
|
func (b *AbstractBridge) configureIpAndBridgeUp() {
|
|
// we want the program to recover in case of issues
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
|
|
fmt.Println("An error happened in <configureIpAndBridgeUp()>, but Zoreide recovered. ")
|
|
fmt.Println("Error was: ", r)
|
|
}
|
|
}()
|
|
|
|
// first we check the IP is free. Something weird could have happened in some
|
|
// other server
|
|
brIp, brIpNet, err := net.ParseCIDR(b.BridgeIpCIDR)
|
|
if err != nil {
|
|
log.Println("Error parsing CIDR: ", err.Error())
|
|
}
|
|
|
|
br, err := tenus.NewLinkFrom(b.ExistingInterface)
|
|
if err != nil {
|
|
log.Println("Error creating Ethernet Alias: ", err.Error())
|
|
log.Println("Problematic interface: ", b.ExistingInterface)
|
|
}
|
|
|
|
if err := br.SetLinkIp(brIp, brIpNet); err != nil {
|
|
log.Println("Error setting UP the IP: ", err.Error())
|
|
} else {
|
|
log.Printf("%s configured with %s\n", b.ExistingInterface, brIp.String())
|
|
}
|
|
|
|
// refresh ARP , should not be necessary, but we know, Cisco FA
|
|
b.RefreshArp()
|
|
}
|
|
|
|
func (b *AbstractBridge) removeIPandBridgeInt() {
|
|
|
|
// we want the program to recover in case of issues
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
|
|
fmt.Println("An error happened in <removeIPandBridgeInt()>, but Zoreide recovered. ")
|
|
fmt.Println("Error was: ", r)
|
|
}
|
|
}()
|
|
|
|
br, err := tenus.NewLinkFrom(b.ExistingInterface)
|
|
if err != nil {
|
|
log.Println("Error creating Ethernet Handle: ", err.Error())
|
|
log.Println("Problematic interface: ", b.ExistingInterface)
|
|
|
|
}
|
|
|
|
brIp, brIpNet, err := net.ParseCIDR(b.BridgeIpCIDR)
|
|
if err != nil {
|
|
log.Println("Error parsing CIDR: ", err.Error())
|
|
}
|
|
if err := br.UnsetLinkIp(brIp, brIpNet); err != nil {
|
|
log.Println("Error setting DOWN the IP: ", err.Error())
|
|
}
|
|
|
|
}
|
|
|
|
func isActive(bridgeip string) bool {
|
|
|
|
log.Println("Check for active IP: ", bridgeip)
|
|
|
|
pinger, err := ping.NewPinger(bridgeip)
|
|
if err != nil {
|
|
log.Println("Ping error: " + err.Error())
|
|
}
|
|
// just in case it doesn't stops alone
|
|
defer pinger.Stop()
|
|
pinger.Count = 5
|
|
pinger.Interval = time.Duration(10 * time.Millisecond)
|
|
pinger.Timeout = time.Duration(1 * time.Second)
|
|
pinger.Run() // blocks until finished
|
|
stats := pinger.Statistics()
|
|
log.Println("Ping results for: ", bridgeip)
|
|
log.Printf("%d packet sent, %d packet recv\n", pinger.Count, stats.PacketsRecv)
|
|
log.Println("IsACTIVE: ", stats.PacketsRecv == pinger.Count)
|
|
|
|
return stats.PacketsRecv == pinger.Count
|
|
|
|
}
|