ifaddr.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package sockaddr
  2. import "strings"
  3. // ifAddrAttrMap is a map of the IfAddr type-specific attributes.
  4. var ifAddrAttrMap map[AttrName]func(IfAddr) string
  5. var ifAddrAttrs []AttrName
  6. func init() {
  7. ifAddrAttrInit()
  8. }
  9. // GetPrivateIP returns a string with a single IP address that is part of RFC
  10. // 6890 and has a default route. If the system can't determine its IP address
  11. // or find an RFC 6890 IP address, an empty string will be returned instead.
  12. // This function is the `eval` equivalent of:
  13. //
  14. // ```
  15. // $ sockaddr eval -r '{{GetPrivateInterfaces | attr "address"}}'
  16. /// ```
  17. func GetPrivateIP() (string, error) {
  18. privateIfs, err := GetPrivateInterfaces()
  19. if err != nil {
  20. return "", err
  21. }
  22. if len(privateIfs) < 1 {
  23. return "", nil
  24. }
  25. ifAddr := privateIfs[0]
  26. ip := *ToIPAddr(ifAddr.SockAddr)
  27. return ip.NetIP().String(), nil
  28. }
  29. // GetPrivateIPs returns a string with all IP addresses that are part of RFC
  30. // 6890 (regardless of whether or not there is a default route, unlike
  31. // GetPublicIP). If the system can't find any RFC 6890 IP addresses, an empty
  32. // string will be returned instead. This function is the `eval` equivalent of:
  33. //
  34. // ```
  35. // $ sockaddr eval -r '{{GetAllInterfaces | include "RFC" "6890" | join "address" " "}}'
  36. /// ```
  37. func GetPrivateIPs() (string, error) {
  38. ifAddrs, err := GetAllInterfaces()
  39. if err != nil {
  40. return "", err
  41. } else if len(ifAddrs) < 1 {
  42. return "", nil
  43. }
  44. ifAddrs, _ = FilterIfByType(ifAddrs, TypeIP)
  45. if len(ifAddrs) == 0 {
  46. return "", nil
  47. }
  48. OrderedIfAddrBy(AscIfType, AscIfNetworkSize).Sort(ifAddrs)
  49. ifAddrs, _, err = IfByRFC("6890", ifAddrs)
  50. if err != nil {
  51. return "", err
  52. } else if len(ifAddrs) == 0 {
  53. return "", nil
  54. }
  55. _, ifAddrs, err = IfByRFC(ForwardingBlacklistRFC, ifAddrs)
  56. if err != nil {
  57. return "", err
  58. } else if len(ifAddrs) == 0 {
  59. return "", nil
  60. }
  61. ips := make([]string, 0, len(ifAddrs))
  62. for _, ifAddr := range ifAddrs {
  63. ip := *ToIPAddr(ifAddr.SockAddr)
  64. s := ip.NetIP().String()
  65. ips = append(ips, s)
  66. }
  67. return strings.Join(ips, " "), nil
  68. }
  69. // GetPublicIP returns a string with a single IP address that is NOT part of RFC
  70. // 6890 and has a default route. If the system can't determine its IP address
  71. // or find a non RFC 6890 IP address, an empty string will be returned instead.
  72. // This function is the `eval` equivalent of:
  73. //
  74. // ```
  75. // $ sockaddr eval -r '{{GetPublicInterfaces | attr "address"}}'
  76. /// ```
  77. func GetPublicIP() (string, error) {
  78. publicIfs, err := GetPublicInterfaces()
  79. if err != nil {
  80. return "", err
  81. } else if len(publicIfs) < 1 {
  82. return "", nil
  83. }
  84. ifAddr := publicIfs[0]
  85. ip := *ToIPAddr(ifAddr.SockAddr)
  86. return ip.NetIP().String(), nil
  87. }
  88. // GetPublicIPs returns a string with all IP addresses that are NOT part of RFC
  89. // 6890 (regardless of whether or not there is a default route, unlike
  90. // GetPublicIP). If the system can't find any non RFC 6890 IP addresses, an
  91. // empty string will be returned instead. This function is the `eval`
  92. // equivalent of:
  93. //
  94. // ```
  95. // $ sockaddr eval -r '{{GetAllInterfaces | exclude "RFC" "6890" | join "address" " "}}'
  96. /// ```
  97. func GetPublicIPs() (string, error) {
  98. ifAddrs, err := GetAllInterfaces()
  99. if err != nil {
  100. return "", err
  101. } else if len(ifAddrs) < 1 {
  102. return "", nil
  103. }
  104. ifAddrs, _ = FilterIfByType(ifAddrs, TypeIP)
  105. if len(ifAddrs) == 0 {
  106. return "", nil
  107. }
  108. OrderedIfAddrBy(AscIfType, AscIfNetworkSize).Sort(ifAddrs)
  109. _, ifAddrs, err = IfByRFC("6890", ifAddrs)
  110. if err != nil {
  111. return "", err
  112. } else if len(ifAddrs) == 0 {
  113. return "", nil
  114. }
  115. ips := make([]string, 0, len(ifAddrs))
  116. for _, ifAddr := range ifAddrs {
  117. ip := *ToIPAddr(ifAddr.SockAddr)
  118. s := ip.NetIP().String()
  119. ips = append(ips, s)
  120. }
  121. return strings.Join(ips, " "), nil
  122. }
  123. // GetInterfaceIP returns a string with a single IP address sorted by the size
  124. // of the network (i.e. IP addresses with a smaller netmask, larger network
  125. // size, are sorted first). This function is the `eval` equivalent of:
  126. //
  127. // ```
  128. // $ sockaddr eval -r '{{GetAllInterfaces | include "name" <<ARG>> | sort "type,size" | include "flag" "forwardable" | attr "address" }}'
  129. /// ```
  130. func GetInterfaceIP(namedIfRE string) (string, error) {
  131. ifAddrs, err := GetAllInterfaces()
  132. if err != nil {
  133. return "", err
  134. }
  135. ifAddrs, _, err = IfByName(namedIfRE, ifAddrs)
  136. if err != nil {
  137. return "", err
  138. }
  139. ifAddrs, _, err = IfByFlag("forwardable", ifAddrs)
  140. if err != nil {
  141. return "", err
  142. }
  143. ifAddrs, err = SortIfBy("+type,+size", ifAddrs)
  144. if err != nil {
  145. return "", err
  146. }
  147. if len(ifAddrs) == 0 {
  148. return "", err
  149. }
  150. ip := ToIPAddr(ifAddrs[0].SockAddr)
  151. if ip == nil {
  152. return "", err
  153. }
  154. return IPAddrAttr(*ip, "address"), nil
  155. }
  156. // GetInterfaceIPs returns a string with all IPs, sorted by the size of the
  157. // network (i.e. IP addresses with a smaller netmask, larger network size, are
  158. // sorted first), on a named interface. This function is the `eval` equivalent
  159. // of:
  160. //
  161. // ```
  162. // $ sockaddr eval -r '{{GetAllInterfaces | include "name" <<ARG>> | sort "type,size" | join "address" " "}}'
  163. /// ```
  164. func GetInterfaceIPs(namedIfRE string) (string, error) {
  165. ifAddrs, err := GetAllInterfaces()
  166. if err != nil {
  167. return "", err
  168. }
  169. ifAddrs, _, err = IfByName(namedIfRE, ifAddrs)
  170. if err != nil {
  171. return "", err
  172. }
  173. ifAddrs, err = SortIfBy("+type,+size", ifAddrs)
  174. if err != nil {
  175. return "", err
  176. }
  177. if len(ifAddrs) == 0 {
  178. return "", err
  179. }
  180. ips := make([]string, 0, len(ifAddrs))
  181. for _, ifAddr := range ifAddrs {
  182. ip := *ToIPAddr(ifAddr.SockAddr)
  183. s := ip.NetIP().String()
  184. ips = append(ips, s)
  185. }
  186. return strings.Join(ips, " "), nil
  187. }
  188. // IfAddrAttrs returns a list of attributes supported by the IfAddr type
  189. func IfAddrAttrs() []AttrName {
  190. return ifAddrAttrs
  191. }
  192. // IfAddrAttr returns a string representation of an attribute for the given
  193. // IfAddr.
  194. func IfAddrAttr(ifAddr IfAddr, attrName AttrName) string {
  195. fn, found := ifAddrAttrMap[attrName]
  196. if !found {
  197. return ""
  198. }
  199. return fn(ifAddr)
  200. }
  201. // ifAddrAttrInit is called once at init()
  202. func ifAddrAttrInit() {
  203. // Sorted for human readability
  204. ifAddrAttrs = []AttrName{
  205. "flags",
  206. "name",
  207. }
  208. ifAddrAttrMap = map[AttrName]func(ifAddr IfAddr) string{
  209. "flags": func(ifAddr IfAddr) string {
  210. return ifAddr.Interface.Flags.String()
  211. },
  212. "name": func(ifAddr IfAddr) string {
  213. return ifAddr.Interface.Name
  214. },
  215. }
  216. }