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.

net.go 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright (c) 2012-2014 Jeremy Latt
  2. // Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
  3. // released under the MIT license
  4. package utils
  5. import (
  6. "net"
  7. "strings"
  8. )
  9. var (
  10. // subnet mask for an ipv6 /128:
  11. mask128 = net.CIDRMask(128, 128)
  12. )
  13. // IPString returns a simple IP string from the given net.Addr.
  14. func IPString(addr net.Addr) string {
  15. addrStr := addr.String()
  16. ipaddr, _, err := net.SplitHostPort(addrStr)
  17. //TODO(dan): Why is this needed, does this happen?
  18. if err != nil {
  19. return addrStr
  20. }
  21. return ipaddr
  22. }
  23. // AddrLookupHostname returns the hostname (if possible) or address for the given `net.Addr`.
  24. func AddrLookupHostname(addr net.Addr) string {
  25. if AddrIsUnix(addr) {
  26. return "localhost"
  27. }
  28. return LookupHostname(IPString(addr))
  29. }
  30. // AddrIsLocal returns whether the address is from a trusted local connection (loopback or unix).
  31. func AddrIsLocal(addr net.Addr) bool {
  32. if tcpaddr, ok := addr.(*net.TCPAddr); ok {
  33. return tcpaddr.IP.IsLoopback()
  34. }
  35. _, ok := addr.(*net.UnixAddr)
  36. return ok
  37. }
  38. // AddrToIP returns the IP address for a net.Addr, or nil if it's a unix domain socket.
  39. func AddrToIP(addr net.Addr) net.IP {
  40. if tcpaddr, ok := addr.(*net.TCPAddr); ok {
  41. return tcpaddr.IP
  42. }
  43. return nil
  44. }
  45. // AddrIsUnix returns whether the address is a unix domain socket.
  46. func AddrIsUnix(addr net.Addr) bool {
  47. _, ok := addr.(*net.UnixAddr)
  48. return ok
  49. }
  50. // LookupHostname returns the hostname for `addr` if it has one. Otherwise, just returns `addr`.
  51. func LookupHostname(addr string) string {
  52. names, err := net.LookupAddr(addr)
  53. if err == nil && len(names) > 0 {
  54. candidate := strings.TrimSuffix(names[0], ".")
  55. if IsHostname(candidate) {
  56. return candidate
  57. }
  58. }
  59. // return original address if no hostname found
  60. if len(addr) > 0 && addr[0] == ':' {
  61. // fix for IPv6 hostnames (so they don't start with a colon), same as all other IRCds
  62. addr = "0" + addr
  63. }
  64. return addr
  65. }
  66. var allowedHostnameChars = "abcdefghijklmnopqrstuvwxyz1234567890-."
  67. // IsHostname returns whether we consider `name` a valid hostname.
  68. func IsHostname(name string) bool {
  69. // IRC hostnames specifically require a period
  70. if !strings.Contains(name, ".") || len(name) < 1 || len(name) > 253 {
  71. return false
  72. }
  73. // ensure each part of hostname is valid
  74. for _, part := range strings.Split(name, ".") {
  75. if len(part) < 1 || len(part) > 63 || strings.HasPrefix(part, "-") || strings.HasSuffix(part, "-") {
  76. return false
  77. }
  78. }
  79. // ensure all chars of hostname are valid
  80. for _, char := range strings.Split(strings.ToLower(name), "") {
  81. if !strings.Contains(allowedHostnameChars, char) {
  82. return false
  83. }
  84. }
  85. return true
  86. }
  87. // NormalizeIPToNet represents an address (v4 or v6) as the v6 /128 CIDR
  88. // containing only it.
  89. func NormalizeIPToNet(addr net.IP) (network net.IPNet) {
  90. // represent ipv4 addresses as ipv6 addresses, using the 4-in-6 prefix
  91. // (actually this should be a no-op for any address returned by ParseIP)
  92. addr = addr.To16()
  93. // the network corresponding to this address is now an ipv6 /128:
  94. return net.IPNet{
  95. IP: addr,
  96. Mask: mask128,
  97. }
  98. }
  99. // NormalizeNet normalizes an IPNet to a v6 CIDR, using the 4-in-6 prefix.
  100. // (this is like IP.To16(), but for IPNet instead of IP)
  101. func NormalizeNet(network net.IPNet) (result net.IPNet) {
  102. if len(network.IP) == 16 {
  103. return network
  104. }
  105. ones, _ := network.Mask.Size()
  106. return net.IPNet{
  107. IP: network.IP.To16(),
  108. // include the 96 bits of the 4-in-6 prefix
  109. Mask: net.CIDRMask(96+ones, 128),
  110. }
  111. }
  112. // Given a network, produce a human-readable string
  113. // (i.e., CIDR if it's actually a network, IPv6 address if it's a v6 /128,
  114. // dotted quad if it's a v4 /32).
  115. func NetToNormalizedString(network net.IPNet) string {
  116. ones, bits := network.Mask.Size()
  117. if ones == bits && ones == len(network.IP)*8 {
  118. // either a /32 or a /128, output the address:
  119. return network.IP.String()
  120. }
  121. return network.String()
  122. }
  123. // Parse a human-readable description (an address or CIDR, either v4 or v6)
  124. // into a normalized v6 net.IPNet.
  125. func NormalizedNetFromString(str string) (result net.IPNet, err error) {
  126. _, network, err := net.ParseCIDR(str)
  127. if err == nil {
  128. return NormalizeNet(*network), nil
  129. }
  130. ip := net.ParseIP(str)
  131. if ip == nil {
  132. err = &net.AddrError{
  133. Err: "Couldn't interpret as either CIDR or address",
  134. Addr: str,
  135. }
  136. return
  137. }
  138. return NormalizeIPToNet(ip), nil
  139. }