2016-03-01 22:38:13 +08:00
|
|
|
// +build freebsd darwin
|
2015-10-11 20:57:53 +08:00
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2017-12-31 14:25:49 +08:00
|
|
|
"context"
|
2015-10-11 20:57:53 +08:00
|
|
|
"strings"
|
|
|
|
|
2015-10-19 11:40:01 +08:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2015-10-11 20:57:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Return a list of network connections opened.
|
2016-03-22 22:09:12 +08:00
|
|
|
func Connections(kind string) ([]ConnectionStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return ConnectionsWithContext(context.Background(), kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) {
|
2016-03-22 22:09:12 +08:00
|
|
|
return ConnectionsPid(kind, 0)
|
2015-10-11 20:57:53 +08:00
|
|
|
}
|
|
|
|
|
2016-12-12 01:59:26 +08:00
|
|
|
// Return a list of network connections opened returning at most `max`
|
|
|
|
// connections for each running process.
|
|
|
|
func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return ConnectionsMaxWithContext(context.Background(), kind, max)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) {
|
2016-12-12 01:59:26 +08:00
|
|
|
return []ConnectionStat{}, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
2015-10-11 20:57:53 +08:00
|
|
|
// Return a list of network connections opened by a process.
|
2016-03-22 22:09:12 +08:00
|
|
|
func ConnectionsPid(kind string, pid int32) ([]ConnectionStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return ConnectionsPidWithContext(context.Background(), kind, pid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConnectionsPidWithContext(ctx context.Context, kind string, pid int32) ([]ConnectionStat, error) {
|
2016-03-22 22:09:12 +08:00
|
|
|
var ret []ConnectionStat
|
2015-10-11 20:57:53 +08:00
|
|
|
|
|
|
|
args := []string{"-i"}
|
|
|
|
switch strings.ToLower(kind) {
|
|
|
|
default:
|
|
|
|
fallthrough
|
|
|
|
case "":
|
|
|
|
fallthrough
|
|
|
|
case "all":
|
|
|
|
fallthrough
|
|
|
|
case "inet":
|
|
|
|
args = append(args, "tcp", "-i", "udp")
|
|
|
|
case "inet4":
|
|
|
|
args = append(args, "4")
|
|
|
|
case "inet6":
|
|
|
|
args = append(args, "6")
|
|
|
|
case "tcp":
|
|
|
|
args = append(args, "tcp")
|
|
|
|
case "tcp4":
|
|
|
|
args = append(args, "4tcp")
|
|
|
|
case "tcp6":
|
|
|
|
args = append(args, "6tcp")
|
|
|
|
case "udp":
|
|
|
|
args = append(args, "udp")
|
|
|
|
case "udp4":
|
|
|
|
args = append(args, "6udp")
|
|
|
|
case "udp6":
|
|
|
|
args = append(args, "6udp")
|
|
|
|
case "unix":
|
2016-04-01 20:34:39 +08:00
|
|
|
return ret, common.ErrNotImplementedError
|
2015-10-11 20:57:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
r, err := common.CallLsof(invoke, pid, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, rr := range r {
|
|
|
|
if strings.HasPrefix(rr, "COMMAND") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
n, err := parseNetLine(rr)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
2016-12-12 01:59:26 +08:00
|
|
|
|
|
|
|
// Return up to `max` network connections opened by a process.
|
|
|
|
func ConnectionsPidMax(kind string, pid int32, max int) ([]ConnectionStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return ConnectionsPidMaxWithContext(context.Background(), kind, pid, max)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConnectionsPidMaxWithContext(ctx context.Context, kind string, pid int32, max int) ([]ConnectionStat, error) {
|
2016-12-12 01:59:26 +08:00
|
|
|
return []ConnectionStat{}, common.ErrNotImplementedError
|
|
|
|
}
|