|
@@ -30,12 +30,15 @@ package kyoketsu
|
|
|
import (
|
|
|
"fmt"
|
|
|
"log"
|
|
|
+ "math"
|
|
|
"net"
|
|
|
"net/netip"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
+const IPV4_BITLEN = 32
|
|
|
+
|
|
|
type NetworkInterfaceNotFound struct{ Passed string }
|
|
|
|
|
|
// Implementing error interface
|
|
@@ -134,24 +137,115 @@ func GetNetworkAddresses(addr string) (*IpSubnetMapper, error) {
|
|
|
type PromptEntry struct {
|
|
|
HostAddress string
|
|
|
NetworkAddress string
|
|
|
- Cidr int
|
|
|
+ Cidr string
|
|
|
SubnetMask string
|
|
|
InterfaceName string
|
|
|
+ MacAddress string
|
|
|
}
|
|
|
|
|
|
type TuiSelectionFeed struct {
|
|
|
Choice []PromptEntry
|
|
|
}
|
|
|
|
|
|
+func matchAddressToMac(ip string, intfs []net.Interface) (*PromptEntry, error) {
|
|
|
+ for i := range intfs {
|
|
|
+ intfsAddr, err := intfs[i].Addrs()
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ for x := range intfsAddr {
|
|
|
+ fmt.Println(intfsAddr[x].String())
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return nil, nil
|
|
|
+}
|
|
|
+
|
|
|
+func bitsToMask(ones int, bits int) string {
|
|
|
+ var bitmask []int
|
|
|
+
|
|
|
+ for i := 0; i < ones; i++ {
|
|
|
+ bitmask = append(bitmask, 1)
|
|
|
+ }
|
|
|
+ for i := ones; i < bits; i++ {
|
|
|
+ bitmask = append(bitmask, 0)
|
|
|
+ }
|
|
|
+
|
|
|
+ octets := []string{
|
|
|
+ strconv.Itoa(base2to10(bitmask[0:8])),
|
|
|
+ strconv.Itoa(base2to10(bitmask[8:16])),
|
|
|
+ strconv.Itoa(base2to10(bitmask[16:24])),
|
|
|
+ strconv.Itoa(base2to10(bitmask[24:32])),
|
|
|
+ }
|
|
|
+ return strings.Join(octets, ".")
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+convert a base 2 number (represented as an array) to a base 10 integer
|
|
|
+
|
|
|
+ :param bits: the slice of ints split into an array, e.g. '11110000' would be [1 1 1 1 0 0 0 0]
|
|
|
+*/
|
|
|
+func base2to10(bits []int) int {
|
|
|
+ var sum int
|
|
|
+ sum = 0
|
|
|
+ for i := range bits {
|
|
|
+ bits[i] = bits[i] * powerInt(2, len(bits)-1-i)
|
|
|
+ }
|
|
|
+ for i := range bits {
|
|
|
+ sum = sum + bits[i]
|
|
|
+ }
|
|
|
+ return sum
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+ Wrapper func for getting the value of x to the power of y, as int opposed to float64
|
|
|
+ :param x: the base number to operate on
|
|
|
+ :param y: the exponent
|
|
|
+*/
|
|
|
+func powerInt(x int, y int) int {
|
|
|
+ return int(math.Pow(float64(x), float64(y)))
|
|
|
+}
|
|
|
+
|
|
|
func RetrieveLocalAddresses() (TuiSelectionFeed, error) {
|
|
|
var tuidata TuiSelectionFeed
|
|
|
+ intf, err := net.Interfaces()
|
|
|
+ if err != nil {
|
|
|
+ return tuidata, err
|
|
|
+ }
|
|
|
|
|
|
- ifaces, err := net.Interfaces()
|
|
|
+ addrs, err := net.InterfaceAddrs()
|
|
|
if err != nil {
|
|
|
return tuidata, err
|
|
|
}
|
|
|
- for i := range ifaces {
|
|
|
- fmt.Printf("%+v\n", ifaces[i])
|
|
|
+ for x := range addrs {
|
|
|
+ var mac string
|
|
|
+ var interfacename string
|
|
|
+ ip, net, err := net.ParseCIDR(addrs[x].String())
|
|
|
+ if err != nil {
|
|
|
+ return tuidata, err
|
|
|
+ }
|
|
|
+ for i := range intf {
|
|
|
+ intfAddrs, err := intf[i].Addrs()
|
|
|
+ if err != nil {
|
|
|
+ return tuidata, err
|
|
|
+ }
|
|
|
+ for y := range intfAddrs {
|
|
|
+ if strings.Contains(intfAddrs[y].String(), strings.Split(ip.String(), "/")[0]) {
|
|
|
+ interfacename = intf[i].Name
|
|
|
+ mac = intf[i].HardwareAddr.String()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ tuidata.Choice = append(tuidata.Choice, PromptEntry{
|
|
|
+ HostAddress: ip.String(),
|
|
|
+ NetworkAddress: ip.Mask(net.Mask).String(),
|
|
|
+ Cidr: strings.Split(addrs[x].String(), "/")[1],
|
|
|
+ SubnetMask: bitsToMask(net.Mask.Size()),
|
|
|
+ MacAddress: mac,
|
|
|
+ InterfaceName: interfacename,
|
|
|
+ })
|
|
|
+
|
|
|
}
|
|
|
|
|
|
return tuidata, nil
|