Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Fix for non linux OS build #808

Merged
merged 1 commit into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/sockstate/netstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package sockstate
import (
"fmt"
"net"

"gopkg.in/src-d/go-errors.v1"
)

// OS independent part of the netstat_[OS].go modules
Expand All @@ -16,6 +18,9 @@ func (s skState) String() string {
return skStates[s]
}

// ErrSocketCheckNotImplemented will be returned for OS where the socket checks is not implemented yet
var ErrSocketCheckNotImplemented = errors.NewKind("socket checking not implemented for this OS")

// Socket states
const (
Established skState = 0x01
Expand Down
10 changes: 9 additions & 1 deletion internal/sockstate/netstat_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

package sockstate

import "github.com/sirupsen/logrus"
import (
"net"

"github.com/sirupsen/logrus"
)

// tcpSocks returns a slice of active TCP sockets containing only those
// elements that satisfy the accept function
Expand All @@ -11,3 +15,7 @@ func tcpSocks(accept AcceptFn) ([]sockTabEntry, error) {
logrus.Info("Connection checking not implemented for Darwin")
return []sockTabEntry{}, nil
}

func GetConnInode(c *net.TCPConn) (n uint64, err error) {
return 0, ErrSocketCheckNotImplemented.New()
}
24 changes: 24 additions & 0 deletions internal/sockstate/netstat_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,27 @@ func tcpSocks(accept AcceptFn) ([]sockTabEntry, error) {
extractProcInfo(tabs)
return tabs, nil
}

// GetConnInode returns the Linux inode number of a TCP connection
func GetConnInode(c *net.TCPConn) (n uint64, err error) {
f, err := c.File()
if err != nil {
return
}

socketStr := fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), f.Fd())
socketLnk, err := os.Readlink(socketStr)
if err != nil {
return
}

if strings.HasPrefix(socketLnk, sockPrefix) {
_, err = fmt.Sscanf(socketLnk, sockPrefix+"%d]", &n)
if err != nil {
return
}
} else {
err = ErrNoSocketLink.New()
}
return
}
10 changes: 9 additions & 1 deletion internal/sockstate/netstat_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

package sockstate

import "github.com/sirupsen/logrus"
import (
"net"

"github.com/sirupsen/logrus"
)

// tcpSocks returns a slice of active TCP sockets containing only those
// elements that satisfy the accept function
Expand All @@ -11,3 +15,7 @@ func tcpSocks(accept AcceptFn) ([]sockTabEntry, error) {
logrus.Info("Connection checking not implemented for Windows")
return []sockTabEntry{}, nil
}

func GetConnInode(c *net.TCPConn) (n uint64, err error) {
return 0, ErrSocketCheckNotImplemented.New()
}
31 changes: 1 addition & 30 deletions internal/sockstate/sockstate.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package sockstate

import (
"fmt"
"net"
"os"
"strconv"
"strings"

"gopkg.in/src-d/go-errors.v1"
"strconv"
)

type SockState uint8
Expand All @@ -21,30 +16,6 @@ const (

var ErrNoSocketLink = errors.NewKind("couldn't resolve file descriptor link to socket")

// GetConnInode returns the Linux inode number of a TCP connection
func GetConnInode(c *net.TCPConn) (n uint64, err error) {
f, err := c.File()
if err != nil {
return
}

socketStr := fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), f.Fd())
socketLnk, err := os.Readlink(socketStr)
if err != nil {
return
}

if strings.HasPrefix(socketLnk, sockPrefix) {
_, err = fmt.Sscanf(socketLnk, sockPrefix+"%d]", &n)
if err != nil {
return
}
} else {
err = ErrNoSocketLink.New()
}
return
}

// ErrMultipleSocketsForInode is returned when more than one socket is found for an inode
var ErrMultipleSocketsForInode = errors.NewKind("more than one socket found for inode")

Expand Down
7 changes: 6 additions & 1 deletion server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,12 @@ func (h *Handler) ComQuery(

inode, err := sockstate.GetConnInode(tcpConn)
if err != nil || inode == 0 {
errChan <- err
if sockstate.ErrSocketCheckNotImplemented.Is(err) {
logrus.Warn("Connection checker exiting, not supported in this OS")
} else {
errChan <- err
}
return
}

t, ok := nc.NetConn.LocalAddr().(*net.TCPAddr)
Expand Down