|
@@ -19,25 +19,41 @@ func (n *NetworkInterfaceNotFound) Error() string {
|
|
|
return fmt.Sprintf("Interface: '%s' not found.", n.Passed)
|
|
|
}
|
|
|
|
|
|
+func getNextAddr(addr net.IP) (net.IP, error) {
|
|
|
+ next, err := netip.ParseAddr(addr.String())
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ return net.ParseIP(next.Next().String()), nil
|
|
|
+}
|
|
|
+
|
|
|
/*
|
|
|
Recursive function to get all of the IPv4 addresses for each IPv4 network that the host is on
|
|
|
|
|
|
:param addr: the address to recursively find the next address for
|
|
|
:param out: a pointer to a struct containing a list of addresses
|
|
|
*/
|
|
|
-func addressRecurse(addr netip.Addr, out *AllAddress) {
|
|
|
- ref := net.ParseIP(addr.String())
|
|
|
- next := net.ParseIP(addr.Next().String())
|
|
|
- v, err := netip.ParseAddr(next.String())
|
|
|
+func addressRecurse(ipmap *IpSubnetMapper, max int) {
|
|
|
+
|
|
|
+ if len(ipmap.Ipv4s) > max {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ next, err := getNextAddr(ipmap.NetworkAddr)
|
|
|
if err != nil {
|
|
|
- log.Fatal(err)
|
|
|
+ log.Println(err)
|
|
|
+ return
|
|
|
}
|
|
|
-
|
|
|
- if ref.Mask(ref.DefaultMask()).String() == next.Mask(next.DefaultMask()).String() {
|
|
|
- out.Addr = append(out.Addr, next)
|
|
|
- addressRecurse(v, out)
|
|
|
+ ip, net, err := net.ParseCIDR(next.String())
|
|
|
+ if err != nil {
|
|
|
+ log.Println(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if ip.Mask(net.Mask).String() != ipmap.NetworkAddr.String() {
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
+ ipmap.Ipv4s = append(ipmap.Ipv4s, next)
|
|
|
+ addressRecurse(ipmap, max)
|
|
|
}
|
|
|
|
|
|
/*
|
|
@@ -62,7 +78,7 @@ func getAddressByInterface(name string) ([]net.Addr, error) {
|
|
|
Utilized a recursive function to find all addresses in the address space that the host belongs.
|
|
|
Returns a pointer to an AllAddresses struct who has a list of net.IP structs inside
|
|
|
*/
|
|
|
-func GetAllAddresses(name string) (*AllAddress, error) {
|
|
|
+func GetAllAddresses(name string, maxDepth int) (*AllAddress, error) {
|
|
|
addresses, err := getAddressByInterface(name)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
@@ -77,7 +93,7 @@ func GetAllAddresses(name string) (*AllAddress, error) {
|
|
|
if root.IsLoopback() {
|
|
|
continue
|
|
|
}
|
|
|
- addressRecurse(root, out)
|
|
|
+ // addressRecurse(ip, ip, out, maxDepth)
|
|
|
}
|
|
|
return out, nil
|
|
|
}
|
|
@@ -86,11 +102,15 @@ func GetAllAddresses(name string) (*AllAddress, error) {
|
|
|
Utilized a recursive function to find all addresses in the address space that the host belongs.
|
|
|
Returns a pointer to an AllAddresses struct who has a list of net.IP structs inside
|
|
|
*/
|
|
|
-func GetAllRemoteAddresses(addrs []string) (*AllAddress, error) {
|
|
|
+func GetAllRemoteAddresses(addrs []string, maxDepth int) (*AllAddress, error) {
|
|
|
out := &AllAddress{}
|
|
|
var addresses []net.IP
|
|
|
for i := range addrs {
|
|
|
- addresses = append(addresses, net.ParseIP(addrs[i]))
|
|
|
+ ip, _, err := net.ParseCIDR(addrs[i])
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ addresses = append(addresses, ip)
|
|
|
}
|
|
|
|
|
|
for idx := range addresses {
|
|
@@ -103,8 +123,33 @@ func GetAllRemoteAddresses(addrs []string) (*AllAddress, error) {
|
|
|
if root.IsLoopback() {
|
|
|
continue
|
|
|
}
|
|
|
- addressRecurse(root, out)
|
|
|
+ // addressRecurse(ip, ip, out, maxDepth)
|
|
|
|
|
|
}
|
|
|
return out, nil
|
|
|
}
|
|
|
+
|
|
|
+type IpSubnetMapper struct {
|
|
|
+ Ipv4s []net.IP
|
|
|
+ NetworkAddr net.IP
|
|
|
+ Mask net.IPMask
|
|
|
+}
|
|
|
+
|
|
|
+func RefactorGetAllRemAddr(addr string) (*AllAddress, error) {
|
|
|
+ // out := &AllAddress{}
|
|
|
+ ipmap := &IpSubnetMapper{Ipv4s: []net.IP{}}
|
|
|
+ ip, net, err := net.ParseCIDR(addr)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ ipmap.NetworkAddr = ip.Mask(net.Mask)
|
|
|
+ ipmap.Mask = ip.DefaultMask()
|
|
|
+ fmt.Printf("%+v\n", ip.Mask(net.Mask))
|
|
|
+ fmt.Println(ip.DefaultMask())
|
|
|
+ fmt.Printf("%s\n", net.IP.DefaultMask())
|
|
|
+
|
|
|
+ addressRecurse(ipmap, 2000)
|
|
|
+
|
|
|
+ return nil, nil
|
|
|
+
|
|
|
+}
|