You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2020 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // Released under the MIT license
  3. package flatip
  4. // begin ad-hoc utilities
  5. // ParseToNormalizedNet attempts to interpret a string either as an IP
  6. // network in CIDR notation, returning an IPNet, or as an IP address,
  7. // returning an IPNet that contains only that address.
  8. func ParseToNormalizedNet(netstr string) (ipnet IPNet, err error) {
  9. _, ipnet, err = ParseCIDR(netstr)
  10. if err == nil {
  11. return
  12. }
  13. ip, err := ParseIP(netstr)
  14. if err == nil {
  15. ipnet.IP = ip
  16. ipnet.PrefixLen = 128
  17. }
  18. return
  19. }
  20. // IPInNets is a convenience function for testing whether an IP is contained
  21. // in any member of a slice of IPNet's.
  22. func IPInNets(addr IP, nets []IPNet) bool {
  23. for _, net := range nets {
  24. if net.Contains(addr) {
  25. return true
  26. }
  27. }
  28. return false
  29. }