-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christopher Kwandrans
committed
Jan 11, 2023
1 parent
64f0d26
commit a24db86
Showing
1 changed file
with
35 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,47 @@ | ||
package ipformat | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// ToV4 converts ipv6 to ipv4 | ||
// func (ip IP) ToV4() (IP, error) { | ||
// if ip.TypeV6 == false { | ||
// return ip, nil | ||
// } | ||
func (ip IP) ToV4() (IP, error) { | ||
if ip.TypeV6 == false { | ||
return ip, nil | ||
} | ||
|
||
// // start of ipv4 | ||
// var newParts []string | ||
// start of ipv4 | ||
var newParts []string | ||
|
||
// // Loop through the old parts converting to ipv4 | ||
// for i, part := range ip.Parts { | ||
// Loop through the old parts converting to ipv4 | ||
for i, part := range ip.Parts { | ||
|
||
// // turn the string into a int | ||
// intConverted, _ := strconv.ParseInt(part, 16, 0) | ||
// ip.Parts[i] = strconv.FormatInt(intConverted, 10) | ||
// turn the string into a int | ||
intConverted, _ := strconv.ParseInt(part, 16, 0) | ||
ip.Parts[i] = strconv.FormatInt(intConverted, 10) | ||
|
||
// // Make sure that no 0s are missing | ||
// if len(ip.Parts[i]) == 1 { | ||
// ip.Parts[i] = "0" + ip.Parts[i] | ||
// } | ||
// } | ||
// Make sure that no 0s are missing | ||
if len(ip.Parts[i]) == 1 { | ||
ip.Parts[i] = "0" + ip.Parts[i] | ||
} | ||
} | ||
|
||
// //Set the parts to ipv6 | ||
// newParts = append(newParts, ip.Parts[0]+ip.Parts[1]) | ||
// newParts = append(newParts, ip.Parts[2]+ip.Parts[3]) | ||
// ip.Parts = newParts | ||
//Set the parts to ipv6 | ||
newParts = append(newParts, ip.Parts[0]+ip.Parts[1]) | ||
newParts = append(newParts, ip.Parts[2]+ip.Parts[3]) | ||
ip.Parts = newParts | ||
|
||
// ip.TypeV6 = true | ||
ip.TypeV6 = true | ||
|
||
// ip.Address = strings.Join(ip.Parts, ":") | ||
ip.Address = strings.Join(ip.Parts, ":") | ||
|
||
// if ip.Range { | ||
// // Convert cidr to ipv6 | ||
// ip.CIDR = 128 - (32 - ip.CIDR) | ||
// ip.Address = ip.Address + "/" + strconv.FormatInt(ip.CIDR, 10) | ||
// } | ||
if ip.Range { | ||
// Convert cidr to ipv6 | ||
ip.CIDR = 128 - (32 - ip.CIDR) | ||
ip.Address = ip.Address + "/" + strconv.FormatInt(ip.CIDR, 10) | ||
} | ||
|
||
// // return the new ip address | ||
// return ip, nil | ||
// } | ||
// return the new ip address | ||
return ip, nil | ||
} |