forked from weaveworks/procspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
procnet.go
170 lines (152 loc) · 3.73 KB
/
procnet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package procspy
import (
"bytes"
"net"
)
// ProcNet is an iterator to parse /proc/net/tcp{,6} files.
type ProcNet struct {
b []byte
c Connection
wantedState uint
bytesLocal, bytesRemote [16]byte
seen map[uint64]struct{}
}
// NewProcNet gives a new ProcNet parser.
func NewProcNet(b []byte, wantedState uint) *ProcNet {
return &ProcNet{
b: b,
c: Connection{},
wantedState: wantedState,
seen: map[uint64]struct{}{},
}
}
// Next returns the next connection. All buffers are re-used, so if you want
// to keep the IPs you have to copy them.
func (p *ProcNet) Next() *Connection {
again:
if len(p.b) == 0 {
return nil
}
b := p.b
if p.b[2] == 's' {
// Skip header
p.b = nextLine(b)
goto again
}
var (
local, remote, state, inode []byte
)
_, b = nextField(b) // 'sl' column
local, b = nextField(b)
remote, b = nextField(b)
state, b = nextField(b)
if parseHex(state) != p.wantedState {
p.b = nextLine(b)
goto again
}
_, b = nextField(b) // 'tx_queue' column
_, b = nextField(b) // 'rx_queue' column
_, b = nextField(b) // 'tr' column
_, b = nextField(b) // 'uid' column
_, b = nextField(b) // 'timeout' column
inode, b = nextField(b)
p.c.LocalAddress, p.c.LocalPort = scanAddressNA(local, &p.bytesLocal)
p.c.RemoteAddress, p.c.RemotePort = scanAddressNA(remote, &p.bytesRemote)
p.c.Inode = parseDec(inode)
p.b = nextLine(b)
if _, alreadySeen := p.seen[p.c.Inode]; alreadySeen {
goto again
}
p.seen[p.c.Inode] = struct{}{}
return &p.c
}
// scanAddressNA parses 'A12CF62E:00AA' to the address/port. Handles IPv4 and
// IPv6 addresses. The address is a big endian 32 bit ints, hex encoded. We
// just decode the hex and flip the bytes in every group of 4.
func scanAddressNA(in []byte, buf *[16]byte) (net.IP, uint16) {
col := bytes.IndexByte(in, ':')
if col == -1 {
return nil, 0
}
// Network address is big endian. Can be either ipv4 or ipv6.
address := hexDecode32bigNA(in[:col], buf)
return net.IP(address), uint16(parseHex(in[col+1:]))
}
// hexDecode32big decodes sequences of 32bit big endian bytes.
func hexDecode32bigNA(src []byte, buf *[16]byte) []byte {
blocks := len(src) / 8
for block := 0; block < blocks; block++ {
for i := 0; i < 4; i++ {
a := fromHexChar(src[block*8+i*2])
b := fromHexChar(src[block*8+i*2+1])
buf[block*4+3-i] = (a << 4) | b
}
}
return buf[:blocks*4]
}
func nextField(s []byte) ([]byte, []byte) {
// Skip whitespace.
for i, b := range s {
if b != ' ' {
s = s[i:]
break
}
}
// Up until the next whitespace field.
for i, b := range s {
if b == ' ' {
return s[:i], s[i:]
}
}
return nil, nil
}
func nextLine(s []byte) []byte {
i := bytes.IndexByte(s, '\n')
if i == -1 {
return nil
}
return s[i+1:]
}
// Simplified copy of strconv.ParseUint(16).
func parseHex(s []byte) uint {
n := uint(0)
for i := 0; i < len(s); i++ {
n *= 16
n += uint(fromHexChar(s[i]))
}
return n
}
// Simplified copy of strconv.ParseUint(10).
func parseDec(s []byte) uint64 {
n := uint64(0)
for _, c := range s {
n *= 10
n += uint64(c - '0')
}
return n
}
// hexDecode32big decodes sequences of 32bit big endian bytes.
func hexDecode32big(src []byte) []byte {
dst := make([]byte, len(src)/2)
blocks := len(src) / 8
for block := 0; block < blocks; block++ {
for i := 0; i < 4; i++ {
a := fromHexChar(src[block*8+i*2])
b := fromHexChar(src[block*8+i*2+1])
dst[block*4+3-i] = (a << 4) | b
}
}
return dst
}
// fromHexChar converts a hex character into its value.
func fromHexChar(c byte) uint8 {
switch {
case '0' <= c && c <= '9':
return c - '0'
case 'a' <= c && c <= 'f':
return c - 'a' + 10
case 'A' <= c && c <= 'F':
return c - 'A' + 10
}
return 0
}