-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
86 lines (81 loc) · 2.64 KB
/
Copy pathmain.go
File metadata and controls
86 lines (81 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"flag"
"fmt"
"ipapp/packages/ip"
"ipapp/packages/logger"
"ipapp/packages/printer"
"ipapp/packages/utils"
"math"
"os"
)
var maskNum int
func init() {
logger.InitLogger()
flag.IntVar(&maskNum, "m", 24, "mask number (24 by default)")
}
func main() {
flag.Parse()
if !flag.Parsed() {
flag.Usage()
os.Exit(1)
}
providedIps := os.Args[1:]
providedIps = utils.FilterOutFlags(providedIps)
if len(providedIps) < 1 {
fmt.Println("Ip addresses weren't provided")
os.Exit(1)
}
invalidIps := ""
for i := 0; i < len(providedIps); i++ {
if !ip.IsValid(providedIps[i]) {
invalidIps += fmt.Sprintf("%s\n", providedIps[i])
providedIps = append(providedIps[:i], providedIps[i+1:]...) // TODO: check for out of range
i--
}
}
if len(invalidIps) != 0 {
fmt.Printf("Some of the provided IPs are invalid:\n%s", invalidIps)
}
if len(providedIps) == 0 {
fmt.Println("No IPs to examine")
os.Exit(0)
}
if maskNum < 0 || maskNum > 32 {
fmt.Println("Mask cannot be less than 0 or greater than 32")
os.Exit(1)
}
var ips []ip.IP
for _, ipDecStr := range providedIps {
ips = append(ips, ip.ParseIPFromDecimalString(ipDecStr, maskNum))
}
const maxBits = 32
bitsReservedForHost := maxBits - maskNum
const numberOfReservedIps = 2
numberOfHosts := math.Pow(2, float64(bitsReservedForHost)) - numberOfReservedIps
if bitsReservedForHost < 2 {
numberOfHosts = 0
}
numberOfSubnetworks := math.Pow(2, float64(maskNum))
printer.PrintFormatted("Mask in binary representation", ip.ConvertMask(maskNum).GetIPInBin())
printer.PrintFormatted("Number of hosts", numberOfHosts)
printer.PrintFormatted("Number of subnetworks", numberOfSubnetworks)
fmt.Println()
for _, ip := range ips {
fmt.Printf("Examining %s...\n", ip.GetIPInDec())
examineNetwork(ip.GetNetworkPart())
fmt.Println()
}
for _, v := range ip.SortIpsByNetworks(ips) {
fmt.Printf("Ips in %s network:\n", v.Network.GetIPInDec())
for _, ip := range v.IPs {
fmt.Println(ip.GetIPInDec())
}
}
}
func examineNetwork(networkIP ip.IP) {
printer.PrintFormatted("Network ip address DEC | BIN", fmt.Sprintf("%s | %s", networkIP.GetIPInDec(), networkIP.GetIPInBin()))
printer.PrintFormatted("Minimum ip address DEC | BIN", fmt.Sprintf("%s | %s", networkIP.GetMinIPInNetwork().GetIPInDec(), networkIP.GetMinIPInNetwork().GetIPInBin()))
printer.PrintFormatted("Maximum ip address DEC | BIN", fmt.Sprintf("%s | %s", networkIP.GetMaxIPInNetwork().GetIPInDec(), networkIP.GetMaxIPInNetwork().GetIPInBin()))
printer.PrintFormatted("Broadcast address DEC | BIN", fmt.Sprintf("%s | %s", networkIP.GetBroadcastAddress().GetIPInDec(), networkIP.GetBroadcastAddress().GetIPInBin()))
}