pull/16/head v0.0.2
Bruce Leidl 9 years ago
parent 7f94d3189e
commit 2d4c127f4e

@ -4,21 +4,21 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/subgraph/fw-daemon/Godeps/_workspace/src/github.com/op/go-logging"
"io/ioutil"
"net"
"strconv"
"strings"
"github.com/subgraph/fw-daemon/Godeps/_workspace/src/github.com/op/go-logging"
)
var log = logging.MustGetLogger("proc")
func SetLogger(logger *logging.Logger) {
log = logger
}
var pcache = &pidCache{}
func LookupUDPSocketProcess(srcPort uint16) *ProcInfo {
ss := findUDPSocket(srcPort)
if ss == nil {
@ -63,7 +63,6 @@ func (sa *socketAddr) parse(s string) error {
return nil
}
func ParseIp(ip string) (net.IP, error) {
var result net.IP
dst, err := hex.DecodeString(ip)
@ -88,7 +87,7 @@ func ParsePort(port string) (uint16, error) {
}
func getConnections() ([]*ConnectionInfo, error) {
conns,err := readConntrack()
conns, err := readConntrack()
if err != nil {
return nil, err
}
@ -98,7 +97,7 @@ func getConnections() ([]*ConnectionInfo, error) {
func resolveProcinfo(conns []*ConnectionInfo) {
var sockets []*socketStatus
for _,line := range getSocketLines("tcp") {
for _, line := range getSocketLines("tcp") {
if len(strings.TrimSpace(line)) == 0 {
continue
}
@ -116,7 +115,7 @@ func resolveProcinfo(conns []*ConnectionInfo) {
*/
}
}
for _,ci := range conns {
for _, ci := range conns {
ss := findContrackSocket(ci, sockets)
if ss == nil {
continue
@ -129,7 +128,7 @@ func resolveProcinfo(conns []*ConnectionInfo) {
}
func findContrackSocket(ci *ConnectionInfo, sockets []*socketStatus) *socketStatus {
for _,ss := range sockets {
for _, ss := range sockets {
if ss.local.port == ci.local.port && ss.remote.ip.Equal(ci.remote.ip) && ss.remote.port == ci.remote.port {
return ss
}
@ -145,8 +144,8 @@ func readConntrack() ([]*ConnectionInfo, error) {
}
var result []*ConnectionInfo
lines := strings.Split(string(data), "\n")
for _,line := range(lines) {
ci,err := parseConntrackLine(line)
for _, line := range lines {
ci, err := parseConntrackLine(line)
if err != nil {
return nil, err
}
@ -163,24 +162,24 @@ func parseConntrackLine(line string) (*ConnectionInfo, error) {
return nil, nil
}
local,err := conntrackAddr(parts[4], parts[6])
local, err := conntrackAddr(parts[4], parts[6])
if err != nil {
return nil, err
}
remote,err := conntrackAddr(parts[5], parts[7])
remote, err := conntrackAddr(parts[5], parts[7])
if err != nil {
return nil, err
}
return &ConnectionInfo{
local: local,
remote: remote,
},nil
}, nil
}
func conntrackAddr(ip_str, port_str string) (*socketAddr, error) {
ip := net.ParseIP(stripLabel(ip_str))
if ip == nil {
return nil, errors.New("Could not parse IP: "+ip_str)
return nil, errors.New("Could not parse IP: " + ip_str)
}
i64, err := strconv.Atoi(stripLabel(port_str))
if err != nil {
@ -189,7 +188,7 @@ func conntrackAddr(ip_str, port_str string) (*socketAddr, error) {
return &socketAddr{
ip: ip,
port: uint16(i64),
},nil
}, nil
}
func stripLabel(s string) string {

@ -1,17 +1,16 @@
package proc
import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"fmt"
"strings"
"path"
"io/ioutil"
"sync"
"syscall"
)
type ProcInfo struct {
Uid int
Pid int
@ -28,12 +27,12 @@ type pidCache struct {
func (pc *pidCache) lookup(inode uint64) *ProcInfo {
pc.lock.Lock()
defer pc.lock.Unlock()
pi,ok := pc.cacheMap[inode]
pi, ok := pc.cacheMap[inode]
if ok && pi.loadProcessInfo() {
return pi
}
pc.cacheMap = loadCache()
pi,ok = pc.cacheMap[inode]
pi, ok = pc.cacheMap[inode]
if ok && pi.loadProcessInfo() {
return pi
}
@ -46,7 +45,7 @@ func loadCache() map[uint64]*ProcInfo {
pid := toPid(n)
if pid != 0 {
pinfo := &ProcInfo{Pid: pid}
for _,inode := range inodesFromPid(pid) {
for _, inode := range inodesFromPid(pid) {
cmap[inode] = pinfo
}
}
@ -60,7 +59,7 @@ func toPid(name string) int {
return 0
}
fdpath := fmt.Sprintf("/proc/%d/fd", pid)
fi,err := os.Stat(fdpath)
fi, err := os.Stat(fdpath)
if err != nil {
return 0
}
@ -91,8 +90,8 @@ func extractSocket(name string) uint64 {
if !strings.HasPrefix(name, "socket:[") || !strings.HasSuffix(name, "]") {
return 0
}
val := name[8:len(name)-1]
inode,err := strconv.ParseUint(val, 10, 64)
val := name[8 : len(name)-1]
inode, err := strconv.ParseUint(val, 10, 64)
if err != nil {
log.Warning("Error parsing inode value from %s: %v", name, err)
return 0
@ -101,7 +100,7 @@ func extractSocket(name string) uint64 {
}
func readdir(dir string) []string {
d,err := os.Open(dir)
d, err := os.Open(dir)
if err != nil {
log.Warning("Error opening directory %s: %v", dir, err)
return nil

@ -1,11 +1,12 @@
package proc
import (
"net"
"errors"
"fmt"
"io/ioutil"
"strings"
"errors"
"net"
"strconv"
"strings"
)
type socketAddr struct {
@ -43,7 +44,7 @@ func findTCPSocket(srcPort uint16, dstAddr net.IP, dstPort uint16) *socketStatus
func findSocket(proto string, matcher func(socketStatus) bool) *socketStatus {
var ss socketStatus
for _,line := range getSocketLines(proto) {
for _, line := range getSocketLines(proto) {
if len(line) == 0 {
continue
}
@ -83,7 +84,6 @@ func (ss *socketStatus) parseLine(line string) error {
return nil
}
func getSocketLines(proto string) []string {
path := fmt.Sprintf("/proc/net/%s", proto)
data, err := ioutil.ReadFile(path)

Loading…
Cancel
Save