123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- /*
- GNU GENERAL PUBLIC LICENSE
- Version 3, 29 June 2007
- kyoketsu, a Client-To-Client Network Enumeration System
- Copyright (C) 2024 Russell Hrubesky, ChiralWorks Software LLC
- Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
- Everyone is permitted to copy and distribute verbatim copies
- of this license document, but changing it is not allowed.
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License,
- or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- See the GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package kyoketsu
- import (
- "fmt"
- "log"
- "net"
- "net/netip"
- "strconv"
- "strings"
- )
- type NetworkInterfaceNotFound struct{ Passed string }
- // Implementing error interface
- func (n *NetworkInterfaceNotFound) Error() string {
- return fmt.Sprintf("Interface: '%s' not found.", n.Passed)
- }
- type IpSubnetMapper struct {
- Ipv4s []net.IP `json:"addresses"`
- NetworkAddr net.IP
- Current net.IP
- Mask int
- }
- /*
- Get the next IPv4 address of the address specified in the 'addr' argument,
- :param addr: the address to get the next address of
- */
- func getNextAddr(addr string) string {
- parsed, err := netip.ParseAddr(addr)
- if err != nil {
- log.Fatal("failed while parsing address in getNextAddr() ", err, "\n")
- }
- return parsed.Next().String()
- }
- /*
- get the network address of the ip address in 'addr' with the subnet mask from 'cidr'
- :param addr: the ipv4 address to get the network address of
- :param cidr: the CIDR notation of the subbet
- */
- func getNetwork(addr string, cidr int) string {
- addr = fmt.Sprintf("%s/%v", addr, cidr)
- ip, net, err := net.ParseCIDR(addr)
- if err != nil {
- log.Fatal("failed whilst attempting to parse cidr in getNetwork() ", err, "\n")
- }
- return ip.Mask(net.Mask).String()
- }
- /*
- Recursive function to get all of the IPv4 addresses for each IPv4 network that the host is on
- :param ipmap: a pointer to an IpSubnetMapper struct which contains domain details such as
- the subnet mask, the original network mask, and the current IP address used in the
- recursive function
- :param max: This is safety feature to prevent stack overflows, so you can manually set the depth to
- call the function
- */
- func addressRecurse(ipmap *IpSubnetMapper) {
- next := getNextAddr(ipmap.Current.String())
- nextNet := getNetwork(next, ipmap.Mask)
- currentNet := ipmap.NetworkAddr.String()
- if nextNet != currentNet {
- return
- }
- ipmap.Current = net.ParseIP(next)
- ipmap.Ipv4s = append(ipmap.Ipv4s, net.ParseIP(next))
- addressRecurse(ipmap)
- }
- /*
- Get all of the IPv4 addresses in the network that 'addr' belongs to. YOU MUST PASS THE ADDRESS WITH CIDR NOTATION
- i.e. '192.168.50.1/24'
- :param addr: the ipv4 address to use for subnet discovery
- */
- func GetNetworkAddresses(addr string) (*IpSubnetMapper, error) {
- ipmap := &IpSubnetMapper{Ipv4s: []net.IP{}}
- ip, net, err := net.ParseCIDR(addr)
- if err != nil {
- return nil, err
- }
- mask, err := strconv.Atoi(strings.Split(addr, "/")[1])
- if err != nil {
- return nil, err
- }
- ipmap.NetworkAddr = ip.Mask(net.Mask)
- ipmap.Mask = mask
- ipmap.Current = ip.Mask(net.Mask)
- addressRecurse(ipmap)
- return ipmap, nil
- }
- type PromptEntry struct {
- HostAddress string
- NetworkAddress string
- Cidr int
- SubnetMask string
- InterfaceName string
- }
- type TuiSelectionFeed struct {
- Choice []PromptEntry
- }
- func RetrieveLocalAddresses() (TuiSelectionFeed, error) {
- var tuidata TuiSelectionFeed
- ifaces, err := net.Interfaces()
- if err != nil {
- return tuidata, err
- }
- for i := range ifaces {
- fmt.Printf("%+v\n", ifaces[i])
- }
- return tuidata, nil
- }
|