Created
January 26, 2024 15:35
-
-
Save mpenick/91080bb0af39a13b052bffd25ab8b13b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"net" | |
) | |
func main() { | |
ip := net.ParseIP("1.2.3.4") | |
mask := net.IPMask(net.ParseIP("255.255.0.0")) | |
octetIP := net.ParseIP("123.123.123.123") | |
swapped := combineMaskedIP(ip.Mask(inverseMask(mask)), octetIP.Mask(mask)) | |
fmt.Println(swapped) // Expect: 123.123.3.4 | |
} | |
func inverseMask(a net.IPMask) net.IPMask { | |
r := make(net.IPMask, len(a)) | |
for i := range a { | |
r[i] = ^a[i] | |
} | |
return r | |
} | |
func combineMaskedIP(a, b net.IP) net.IP { | |
if len(a) != len(b) { | |
panic("addresses must be the same type") | |
} | |
r := make(net.IP, len(a)) | |
for i := range a { | |
r[i] = a[i] | b[i] | |
} | |
return r | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment