@ -37,23 +37,26 @@ type linuxImpl struct {
known map [ string ] * portMeta // inode string => metadata
known map [ string ] * portMeta // inode string => metadata
br * bufio . Reader
br * bufio . Reader
includeLocalhost bool
}
}
type portMeta struct {
type portMeta struct {
port Port
port Port
pid int
keep bool
keep bool
needsProcName bool
needsProcName bool
}
}
func newLinuxImplBase ( ) * linuxImpl {
func newLinuxImplBase ( includeLocalhost bool ) * linuxImpl {
return & linuxImpl {
return & linuxImpl {
br : bufio . NewReader ( eofReader ) ,
br : bufio . NewReader ( eofReader ) ,
known : map [ string ] * portMeta { } ,
known : map [ string ] * portMeta { } ,
includeLocalhost : includeLocalhost ,
}
}
}
}
func newLinuxImpl ( ) osImpl {
func newLinuxImpl ( includeLocalhost bool ) osImpl {
li := newLinuxImplBase ( )
li := newLinuxImplBase ( includeLocalhost )
for _ , name := range [ ] string {
for _ , name := range [ ] string {
"/proc/net/tcp" ,
"/proc/net/tcp" ,
"/proc/net/tcp6" ,
"/proc/net/tcp6" ,
@ -220,7 +223,7 @@ func (li *linuxImpl) parseProcNetFile(r *bufio.Reader, fileBase string) error {
// If a port is bound to localhost, ignore it.
// If a port is bound to localhost, ignore it.
// TODO: localhost is bigger than 1 IP, we need to ignore
// TODO: localhost is bigger than 1 IP, we need to ignore
// more things.
// more things.
if mem . HasPrefix ( local , mem . S ( v4Localhost ) ) || mem . HasPrefix ( local , mem . S ( v6Localhost ) ) {
if ! li . includeLocalhost && ( mem . HasPrefix ( local , mem . S ( v4Localhost ) ) || mem . HasPrefix ( local , mem . S ( v6Localhost ) ) ) {
continue
continue
}
}
@ -315,6 +318,9 @@ func (li *linuxImpl) findProcessNames(need map[string]*portMeta) error {
}
}
argv := strings . Split ( strings . TrimSuffix ( string ( bs ) , "\x00" ) , "\x00" )
argv := strings . Split ( strings . TrimSuffix ( string ( bs ) , "\x00" ) , "\x00" )
if p , err := mem . ParseInt ( pid , 10 , 0 ) ; err == nil {
pe . pid = int ( p )
}
pe . port . Process = argvSubject ( argv ... )
pe . port . Process = argvSubject ( argv ... )
pe . needsProcName = false
pe . needsProcName = false
delete ( need , string ( targetBuf [ : n ] ) )
delete ( need , string ( targetBuf [ : n ] ) )