net_aix.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. //go:build aix
  2. // +build aix
  3. package net
  4. import (
  5. "context"
  6. "fmt"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. "syscall"
  11. "github.com/shirou/gopsutil/v3/internal/common"
  12. )
  13. func IOCounters(pernic bool) ([]IOCountersStat, error) {
  14. return IOCountersWithContext(context.Background(), pernic)
  15. }
  16. // IOCountersByFile exists just for compatibility with Linux.
  17. func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
  18. return IOCountersByFileWithContext(context.Background(), pernic, filename)
  19. }
  20. func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
  21. return IOCounters(pernic)
  22. }
  23. func FilterCounters() ([]FilterStat, error) {
  24. return FilterCountersWithContext(context.Background())
  25. }
  26. func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) {
  27. return nil, common.ErrNotImplementedError
  28. }
  29. func ConntrackStats(percpu bool) ([]ConntrackStat, error) {
  30. return ConntrackStatsWithContext(context.Background(), percpu)
  31. }
  32. func ConntrackStatsWithContext(ctx context.Context, percpu bool) ([]ConntrackStat, error) {
  33. return nil, common.ErrNotImplementedError
  34. }
  35. func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
  36. return ProtoCountersWithContext(context.Background(), protocols)
  37. }
  38. func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) {
  39. return nil, common.ErrNotImplementedError
  40. }
  41. func parseNetstatNetLine(line string) (ConnectionStat, error) {
  42. f := strings.Fields(line)
  43. if len(f) < 5 {
  44. return ConnectionStat{}, fmt.Errorf("wrong line,%s", line)
  45. }
  46. var netType, netFamily uint32
  47. switch f[0] {
  48. case "tcp", "tcp4":
  49. netType = syscall.SOCK_STREAM
  50. netFamily = syscall.AF_INET
  51. case "udp", "udp4":
  52. netType = syscall.SOCK_DGRAM
  53. netFamily = syscall.AF_INET
  54. case "tcp6":
  55. netType = syscall.SOCK_STREAM
  56. netFamily = syscall.AF_INET6
  57. case "udp6":
  58. netType = syscall.SOCK_DGRAM
  59. netFamily = syscall.AF_INET6
  60. default:
  61. return ConnectionStat{}, fmt.Errorf("unknown type, %s", f[0])
  62. }
  63. laddr, raddr, err := parseNetstatAddr(f[3], f[4], netFamily)
  64. if err != nil {
  65. return ConnectionStat{}, fmt.Errorf("failed to parse netaddr, %s %s", f[3], f[4])
  66. }
  67. n := ConnectionStat{
  68. Fd: uint32(0), // not supported
  69. Family: uint32(netFamily),
  70. Type: uint32(netType),
  71. Laddr: laddr,
  72. Raddr: raddr,
  73. Pid: int32(0), // not supported
  74. }
  75. if len(f) == 6 {
  76. n.Status = f[5]
  77. }
  78. return n, nil
  79. }
  80. var portMatch = regexp.MustCompile(`(.*)\.(\d+)$`)
  81. // This function only works for netstat returning addresses with a "."
  82. // before the port (0.0.0.0.22 instead of 0.0.0.0:22).
  83. func parseNetstatAddr(local string, remote string, family uint32) (laddr Addr, raddr Addr, err error) {
  84. parse := func(l string) (Addr, error) {
  85. matches := portMatch.FindStringSubmatch(l)
  86. if matches == nil {
  87. return Addr{}, fmt.Errorf("wrong addr, %s", l)
  88. }
  89. host := matches[1]
  90. port := matches[2]
  91. if host == "*" {
  92. switch family {
  93. case syscall.AF_INET:
  94. host = "0.0.0.0"
  95. case syscall.AF_INET6:
  96. host = "::"
  97. default:
  98. return Addr{}, fmt.Errorf("unknown family, %d", family)
  99. }
  100. }
  101. lport, err := strconv.Atoi(port)
  102. if err != nil {
  103. return Addr{}, err
  104. }
  105. return Addr{IP: host, Port: uint32(lport)}, nil
  106. }
  107. laddr, err = parse(local)
  108. if remote != "*.*" { // remote addr exists
  109. raddr, err = parse(remote)
  110. if err != nil {
  111. return laddr, raddr, err
  112. }
  113. }
  114. return laddr, raddr, err
  115. }
  116. func parseNetstatUnixLine(f []string) (ConnectionStat, error) {
  117. if len(f) < 8 {
  118. return ConnectionStat{}, fmt.Errorf("wrong number of fields: expected >=8 got %d", len(f))
  119. }
  120. var netType uint32
  121. switch f[1] {
  122. case "dgram":
  123. netType = syscall.SOCK_DGRAM
  124. case "stream":
  125. netType = syscall.SOCK_STREAM
  126. default:
  127. return ConnectionStat{}, fmt.Errorf("unknown type: %s", f[1])
  128. }
  129. // Some Unix Socket don't have any address associated
  130. addr := ""
  131. if len(f) == 9 {
  132. addr = f[8]
  133. }
  134. c := ConnectionStat{
  135. Fd: uint32(0), // not supported
  136. Family: uint32(syscall.AF_UNIX),
  137. Type: uint32(netType),
  138. Laddr: Addr{
  139. IP: addr,
  140. },
  141. Status: "NONE",
  142. Pid: int32(0), // not supported
  143. }
  144. return c, nil
  145. }
  146. // Return true if proto is the corresponding to the kind parameter
  147. // Only for Inet lines
  148. func hasCorrectInetProto(kind, proto string) bool {
  149. switch kind {
  150. case "all", "inet":
  151. return true
  152. case "unix":
  153. return false
  154. case "inet4":
  155. return !strings.HasSuffix(proto, "6")
  156. case "inet6":
  157. return strings.HasSuffix(proto, "6")
  158. case "tcp":
  159. return proto == "tcp" || proto == "tcp4" || proto == "tcp6"
  160. case "tcp4":
  161. return proto == "tcp" || proto == "tcp4"
  162. case "tcp6":
  163. return proto == "tcp6"
  164. case "udp":
  165. return proto == "udp" || proto == "udp4" || proto == "udp6"
  166. case "udp4":
  167. return proto == "udp" || proto == "udp4"
  168. case "udp6":
  169. return proto == "udp6"
  170. }
  171. return false
  172. }
  173. func parseNetstatA(output string, kind string) ([]ConnectionStat, error) {
  174. var ret []ConnectionStat
  175. lines := strings.Split(string(output), "\n")
  176. for _, line := range lines {
  177. fields := strings.Fields(line)
  178. if len(fields) < 1 {
  179. continue
  180. }
  181. if strings.HasPrefix(fields[0], "f1") {
  182. // Unix lines
  183. if len(fields) < 2 {
  184. // every unix connections have two lines
  185. continue
  186. }
  187. c, err := parseNetstatUnixLine(fields)
  188. if err != nil {
  189. return nil, fmt.Errorf("failed to parse Unix Address (%s): %s", line, err)
  190. }
  191. ret = append(ret, c)
  192. } else if strings.HasPrefix(fields[0], "tcp") || strings.HasPrefix(fields[0], "udp") {
  193. // Inet lines
  194. if !hasCorrectInetProto(kind, fields[0]) {
  195. continue
  196. }
  197. // On AIX, netstat display some connections with "*.*" as local addresses
  198. // Skip them as they aren't real connections.
  199. if fields[3] == "*.*" {
  200. continue
  201. }
  202. c, err := parseNetstatNetLine(line)
  203. if err != nil {
  204. return nil, fmt.Errorf("failed to parse Inet Address (%s): %s", line, err)
  205. }
  206. ret = append(ret, c)
  207. } else {
  208. // Header lines
  209. continue
  210. }
  211. }
  212. return ret, nil
  213. }
  214. func Connections(kind string) ([]ConnectionStat, error) {
  215. return ConnectionsWithContext(context.Background(), kind)
  216. }
  217. func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) {
  218. args := []string{"-na"}
  219. switch strings.ToLower(kind) {
  220. default:
  221. fallthrough
  222. case "":
  223. kind = "all"
  224. case "all":
  225. // nothing to add
  226. case "inet", "inet4", "inet6":
  227. args = append(args, "-finet")
  228. case "tcp", "tcp4", "tcp6":
  229. args = append(args, "-finet")
  230. case "udp", "udp4", "udp6":
  231. args = append(args, "-finet")
  232. case "unix":
  233. args = append(args, "-funix")
  234. }
  235. out, err := invoke.CommandWithContext(ctx, "netstat", args...)
  236. if err != nil {
  237. return nil, err
  238. }
  239. ret, err := parseNetstatA(string(out), kind)
  240. if err != nil {
  241. return nil, err
  242. }
  243. return ret, nil
  244. }
  245. func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) {
  246. return ConnectionsMaxWithContext(context.Background(), kind, max)
  247. }
  248. func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) {
  249. return []ConnectionStat{}, common.ErrNotImplementedError
  250. }
  251. // Return a list of network connections opened, omitting `Uids`.
  252. // WithoutUids functions are reliant on implementation details. They may be altered to be an alias for Connections or be
  253. // removed from the API in the future.
  254. func ConnectionsWithoutUids(kind string) ([]ConnectionStat, error) {
  255. return ConnectionsWithoutUidsWithContext(context.Background(), kind)
  256. }
  257. func ConnectionsWithoutUidsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) {
  258. return ConnectionsMaxWithoutUidsWithContext(ctx, kind, 0)
  259. }
  260. func ConnectionsMaxWithoutUidsWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) {
  261. return ConnectionsPidMaxWithoutUidsWithContext(ctx, kind, 0, max)
  262. }
  263. func ConnectionsPidWithoutUids(kind string, pid int32) ([]ConnectionStat, error) {
  264. return ConnectionsPidWithoutUidsWithContext(context.Background(), kind, pid)
  265. }
  266. func ConnectionsPidWithoutUidsWithContext(ctx context.Context, kind string, pid int32) ([]ConnectionStat, error) {
  267. return ConnectionsPidMaxWithoutUidsWithContext(ctx, kind, pid, 0)
  268. }
  269. func ConnectionsPidMaxWithoutUids(kind string, pid int32, max int) ([]ConnectionStat, error) {
  270. return ConnectionsPidMaxWithoutUidsWithContext(context.Background(), kind, pid, max)
  271. }
  272. func ConnectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, pid int32, max int) ([]ConnectionStat, error) {
  273. return connectionsPidMaxWithoutUidsWithContext(ctx, kind, pid, max)
  274. }
  275. func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, pid int32, max int) ([]ConnectionStat, error) {
  276. return []ConnectionStat{}, common.ErrNotImplementedError
  277. }