Skip to content

Commit

Permalink
re-added ipv4 functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Kwandrans committed Jan 11, 2023
1 parent 64f0d26 commit a24db86
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions ipv4.go
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
}

0 comments on commit a24db86

Please sign in to comment.