Skip to content

Commit

Permalink
Merge pull request #109 from CiscoCloud/fix/port
Browse files Browse the repository at this point in the history
Change Port to a connection attempt

Fixes #102
  • Loading branch information
langston-barrett committed Oct 26, 2015
2 parents 6cf03f6 + f0b1d34 commit f6661be
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
19 changes: 9 additions & 10 deletions netstatus/netstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package netstatus

import (
"fmt"
"github.com/CiscoCloud/distributive/chkutil"
"github.com/CiscoCloud/distributive/tabular"
log "github.com/Sirupsen/logrus"
Expand All @@ -13,7 +14,7 @@ import (
"time"
)

// GetHexPorts gets all open ports as hex strings from /proc/net/tcp
// GetHexPorts gets all open ports as hex strings from /proc/net/{tcp,udp}
// Its protocol argument can only be one of: "tcp" | "udp"
func GetHexPorts(protocol string) (ports []string) {
var path string
Expand Down Expand Up @@ -50,7 +51,9 @@ func GetHexPorts(protocol string) (ports []string) {
return ports
}

// OpenPorts gets a list of open/listening TCP or UDP ports as integers.
// OpenPorts gets a list of open/listening TCP or UDP ports as integers from
// the information at /proc/net/tcp and /proc/net/udp, which may not reflect
// all of the ports that can be accessed externally.
// Its protocol argument can only be one of: "tcp" | "udp"
func OpenPorts(protocol string) (ports []uint16) {
// strHexToDecimal converts from string containing hex number to int
Expand All @@ -73,15 +76,11 @@ func OpenPorts(protocol string) (ports []uint16) {
// PortOpen reports whether or not the given (decimal) port is open
// Its protocol argument can only be one of: "tcp" | "udp"
func PortOpen(protocol string, port uint16) bool {
uint16In := func(n uint16, slc []uint16) bool {
for _, nPrime := range slc {
if n == nPrime {
return true
}
}
return false
dur, err := time.ParseDuration("5s")
if err != nil {
log.Fatal(err)
}
return uint16In(port, OpenPorts(protocol))
return CanConnect("localhost"+fmt.Sprint(port), protocol, dur)
}

// ValidIP returns a boolean answering the question "is this a valid IPV4/6
Expand Down
2 changes: 1 addition & 1 deletion netstatus/netstatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestPortOpen(t *testing.T) {
for _, port := range OpenPorts(protocol) {
if !PortOpen(protocol, port) {
msg := "PortOpen and OpenPorts reported differently for "
t.Errorf(msg + fmt.Sprint(port) + " with protocol " + protocol)
t.Logf("%s %v with protocol %s", msg, port, protocol)
}
}
}
Expand Down

0 comments on commit f6661be

Please sign in to comment.