-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser_linux.go
211 lines (174 loc) · 3.92 KB
/
parser_linux.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//go:build linux
// +build linux
package gons
import (
"bufio"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"strconv"
"strings"
)
type ParseOptionSet func(option *ParseOption)
type ParseOption struct {
ipv6 bool
}
func WithIPV6() ParseOptionSet {
return func(option *ParseOption) {
option.ipv6 = true
}
}
// ParseTCP parse tcp connections from proc filesystem.
func ParseTCP(device io.Reader) ([]Socket, error) {
sockets, err := Parse(device)
if err != nil {
return nil, err
}
for index, _ := range sockets {
sockets[index].Proto = PROTO_TCP
}
return sockets, nil
}
// ParseTCP6 parse tcp6 connections from proc filesystem.
func ParseTCP6(device io.Reader) ([]Socket, error) {
sockets, err := Parse(device, WithIPV6())
if err != nil {
return nil, err
}
for index, _ := range sockets {
sockets[index].Proto = PROTO_TCP
}
return sockets, nil
}
// ParseUDP parse udp connections from proc filesystem.
func ParseUDP(device io.Reader) ([]Socket, error) {
sockets, err := Parse(device)
if err != nil {
return nil, err
}
for index, _ := range sockets {
sockets[index].Proto = PROTO_UDP
}
return sockets, nil
}
// ParseUDP6 parse udp6 connections from proc filesystem.
func ParseUDP6(device io.Reader) ([]Socket, error) {
sockets, err := Parse(device, WithIPV6())
if err != nil {
return nil, err
}
for index, _ := range sockets {
sockets[index].Proto = PROTO_UDP
}
return sockets, nil
}
// Parse parse connections from proc filesystem.
func Parse(device io.Reader, opts ...ParseOptionSet) ([]Socket, error) {
o := new(ParseOption)
for _, fn := range opts {
fn(o)
}
scanner := bufio.NewScanner(device)
var sockets []Socket
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) < 12 {
continue
} else if fields[0] == "sl" {
continue
}
var socket Socket
// raw
socket.Raw = RawSocket{
fields: fields,
}
var err error
// local address
socket.LocalAddress.IP, socket.LocalAddress.Port, err = parseIPPort(fields[1])
if err != nil {
continue
}
// foreign address
socket.ForeignAddress.IP, socket.ForeignAddress.Port, err = parseIPPort(fields[2])
if err != nil {
continue
}
// state
state, err := strconv.ParseInt(fields[3], 16, 32)
if err != nil {
continue
}
socket.State = SocketStateType(state)
// recv-q & send-q
split := strings.Split(fields[4], ":")
if len(split) != 2 {
continue
}
recvq, err := strconv.ParseInt(split[0], 16, 32)
if err != nil {
continue
}
sendq, err := strconv.ParseInt(split[1], 16, 32)
if err != nil {
continue
}
socket.RecvQ = int(recvq)
socket.SendQ = int(sendq)
sockets = append(sockets, socket)
}
return sockets, nil
}
func parseIPPort(ipport string) (ip net.IP, port int, err error) {
split := strings.Split(ipport, ":")
if len(split) != 2 {
return nil, 0, &net.ParseError{
Type: "invalid format",
Text: fmt.Sprintf("ipport string %q can only have 1 colon", ipport),
}
}
ipStr, portStr := split[0], split[1]
switch len(ipStr) {
case 8:
ip, err = parseIPv4(ipStr)
case 32:
ip, err = parseIPv6(ipStr)
default:
err = &net.ParseError{
Type: "IP address",
Text: fmt.Sprintf("len of ip string %q invalid", ipStr),
}
}
if err != nil {
return nil, 0, err
}
p, err := strconv.ParseInt(portStr, 16, 32)
if err != nil {
return nil, 0, err
}
return ip, int(p), err
}
func parseIPv4(ips string) (net.IP, error) {
v, err := strconv.ParseUint(ips, 16, 32)
if err != nil {
return nil, err
}
ip := make([]byte, net.IPv4len)
binary.LittleEndian.PutUint32(ip, uint32(v))
return ip, nil
}
func parseIPv6(ips string) (net.IP, error) {
if len([]byte(ips)) != 32 {
return nil, errors.New("parse: ipv6 length not match")
}
ip := make([]byte, net.IPv6len)
for i := 0; i < 4; i++ {
v, err := strconv.ParseUint(ips[i*8:(i+1)*8], 16, 32)
if err != nil {
return nil, err
}
binary.LittleEndian.PutUint32(ip[i*4:], uint32(v))
}
return ip, nil
}