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

Commit 3855454

Browse files
author
Juanjo Alvarez
committed
Fix for non linux OS build
Signed-off-by: Juanjo Alvarez <[email protected]>
1 parent 6aa453e commit 3855454

File tree

6 files changed

+54
-33
lines changed

6 files changed

+54
-33
lines changed

internal/sockstate/netstat.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package sockstate
33
import (
44
"fmt"
55
"net"
6+
7+
"gopkg.in/src-d/go-errors.v1"
68
)
79

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

21+
// ErrSocketCheckNotImplemented will be returned for OS where the socket checks is not implemented yet
22+
var ErrSocketCheckNotImplemented = errors.NewKind("socket checking not implemented for this OS")
23+
1924
// Socket states
2025
const (
2126
Established skState = 0x01

internal/sockstate/netstat_darwin.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
package sockstate
44

5-
import "github.com/sirupsen/logrus"
5+
import (
6+
"net"
7+
8+
"github.com/sirupsen/logrus"
9+
)
610

711
// tcpSocks returns a slice of active TCP sockets containing only those
812
// elements that satisfy the accept function
@@ -11,3 +15,7 @@ func tcpSocks(accept AcceptFn) ([]sockTabEntry, error) {
1115
logrus.Info("Connection checking not implemented for Darwin")
1216
return []sockTabEntry{}, nil
1317
}
18+
19+
func GetConnInode(c *net.TCPConn) (n uint64, err error) {
20+
return 0, ErrSocketCheckNotImplemented.New()
21+
}

internal/sockstate/netstat_linux.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,27 @@ func tcpSocks(accept AcceptFn) ([]sockTabEntry, error) {
208208
extractProcInfo(tabs)
209209
return tabs, nil
210210
}
211+
212+
// GetConnInode returns the Linux inode number of a TCP connection
213+
func GetConnInode(c *net.TCPConn) (n uint64, err error) {
214+
f, err := c.File()
215+
if err != nil {
216+
return
217+
}
218+
219+
socketStr := fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), f.Fd())
220+
socketLnk, err := os.Readlink(socketStr)
221+
if err != nil {
222+
return
223+
}
224+
225+
if strings.HasPrefix(socketLnk, sockPrefix) {
226+
_, err = fmt.Sscanf(socketLnk, sockPrefix+"%d]", &n)
227+
if err != nil {
228+
return
229+
}
230+
} else {
231+
err = ErrNoSocketLink.New()
232+
}
233+
return
234+
}

internal/sockstate/netstat_windows.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
package sockstate
44

5-
import "github.com/sirupsen/logrus"
5+
import (
6+
"net"
7+
8+
"github.com/sirupsen/logrus"
9+
)
610

711
// tcpSocks returns a slice of active TCP sockets containing only those
812
// elements that satisfy the accept function
@@ -11,3 +15,7 @@ func tcpSocks(accept AcceptFn) ([]sockTabEntry, error) {
1115
logrus.Info("Connection checking not implemented for Windows")
1216
return []sockTabEntry{}, nil
1317
}
18+
19+
func GetConnInode(c *net.TCPConn) (n uint64, err error) {
20+
return 0, ErrSocketCheckNotImplemented.New()
21+
}

internal/sockstate/sockstate.go

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
package sockstate
22

33
import (
4-
"fmt"
5-
"net"
6-
"os"
7-
"strconv"
8-
"strings"
9-
104
"gopkg.in/src-d/go-errors.v1"
5+
"strconv"
116
)
127

138
type SockState uint8
@@ -21,30 +16,6 @@ const (
2116

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

24-
// GetConnInode returns the Linux inode number of a TCP connection
25-
func GetConnInode(c *net.TCPConn) (n uint64, err error) {
26-
f, err := c.File()
27-
if err != nil {
28-
return
29-
}
30-
31-
socketStr := fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), f.Fd())
32-
socketLnk, err := os.Readlink(socketStr)
33-
if err != nil {
34-
return
35-
}
36-
37-
if strings.HasPrefix(socketLnk, sockPrefix) {
38-
_, err = fmt.Sscanf(socketLnk, sockPrefix+"%d]", &n)
39-
if err != nil {
40-
return
41-
}
42-
} else {
43-
err = ErrNoSocketLink.New()
44-
}
45-
return
46-
}
47-
4819
// ErrMultipleSocketsForInode is returned when more than one socket is found for an inode
4920
var ErrMultipleSocketsForInode = errors.NewKind("more than one socket found for inode")
5021

server/handler.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,12 @@ func (h *Handler) ComQuery(
194194

195195
inode, err := sockstate.GetConnInode(tcpConn)
196196
if err != nil || inode == 0 {
197-
errChan <- err
197+
if sockstate.ErrSocketCheckNotImplemented.Is(err) {
198+
logrus.Warn("Connection checker exiting, not supported in this OS")
199+
} else {
200+
errChan <- err
201+
}
202+
return
198203
}
199204

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

0 commit comments

Comments
 (0)