forked from brotherpowers/ipsubnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hostPortion.go
33 lines (26 loc) · 970 Bytes
/
hostPortion.go
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
package ipsubnet
import (
"fmt"
"strings"
)
func (s *Ip) GetHostPortion() string {
return s.hostCalculation("%d", ".")
}
func (s *Ip) GetHostPortionQuards() []int {
return convertQuardsToInt(strings.Split(s.hostCalculation("%d", "."), "."))
}
func (s *Ip) GetHostPortionHex() string {
return s.hostCalculation("%02X", "")
}
func (s *Ip) GetHostPortionBinary() string {
return s.hostCalculation("%08b", "")
}
func (s *Ip) hostCalculation(format, separator string) string {
splits := s.GetIPAddressQuads()
networkQuards := []string{}
networkQuards = append(networkQuards, fmt.Sprintf(format, splits[0] & ^(s.subnet_mask>>24)))
networkQuards = append(networkQuards, fmt.Sprintf(format, splits[1] & ^(s.subnet_mask>>16)))
networkQuards = append(networkQuards, fmt.Sprintf(format, splits[2] & ^(s.subnet_mask>>8)))
networkQuards = append(networkQuards, fmt.Sprintf(format, splits[3] & ^(s.subnet_mask>>0)))
return strings.Join(networkQuards, separator)
}