-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_ip.go
173 lines (127 loc) · 3.58 KB
/
net_ip.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
package proc
import (
"errors"
"net"
"regexp"
"strconv"
"strings"
)
var (
ipv4RegExp = regexp.MustCompile("^[0-9a-fA-F]{8}:[0-9a-fA-F]{4}$") // Regex for NetIPv4Decoder
ipv6RegExp = regexp.MustCompile("^[0-9a-fA-F]{32}:[0-9a-fA-F]{4}$") // Regex for NetIPv6Decoder
)
type NetIPDecoder func(string) (string, error) // Either NetIPv4Decoder or NetIPv6Decoder
type NetSocket struct {
LocalAddress string `json:"local_address"`
RemoteAddress string `json:"remote_address"`
Status uint8 `json:"st"`
TxQueue uint64 `json:"tx_queue"`
RxQueue uint64 `json:"rx_queue"`
Uid uint32 `json:"uid"`
Inode uint64 `json:"inode"`
SocketReferenceCount uint64 `json:"ref"`
}
func parseNetSocket(f []string, ip NetIPDecoder) (*NetSocket, error) {
if len(f) < 11 {
return nil, errors.New("Cannot parse net socket line: " + strings.Join(f, " "))
}
if strings.Index(f[4], ":") == -1 {
return nil, errors.New("Cannot parse tx/rx queues: " + f[4])
}
q := strings.Split(f[4], ":")
socket := &NetSocket{}
var s uint64 // socket.Status
var u uint64 // socket.Uid
var err error // parse error
if socket.LocalAddress, err = ip(f[1]); err != nil {
return nil, err
}
if socket.RemoteAddress, err = ip(f[2]); err != nil {
return nil, err
}
if s, err = strconv.ParseUint(f[3], 16, 8); err != nil {
return nil, err
}
if socket.TxQueue, err = strconv.ParseUint(q[0], 16, 64); err != nil {
return nil, err
}
if socket.RxQueue, err = strconv.ParseUint(q[1], 16, 64); err != nil {
return nil, err
}
if u, err = strconv.ParseUint(f[7], 10, 32); err != nil {
return nil, err
}
if socket.Inode, err = strconv.ParseUint(f[9], 10, 64); err != nil {
return nil, err
}
if socket.SocketReferenceCount, err = strconv.ParseUint(f[10], 10, 64); err != nil {
return nil, err
}
socket.Status = uint8(s)
socket.Uid = uint32(u)
return socket, nil
}
// Decode an IPv4 address with port from a given hex string
// NOTE: This function match NetIPDecoder type
func NetIPv4Decoder(s string) (string, error) {
if !ipv4RegExp.MatchString(s) {
return "", errors.New("Cannot decode ipv4 address: " + s)
}
i := strings.Split(s, ":")
b := make([]byte, 4)
for j := 0; j < 4; j++ {
x := j * 2
y := x + 2
z := 3 - j
// Extract 2 characters from hex string, 4 times.
//
// s: "0100007F" -> [
// h: "01", h: "00", h: "00", h: "7F",
// ]
h := i[0][x:y]
// Reverse byte order
n, _ := strconv.ParseUint(h, 16, 8)
b[z] = byte(n)
}
h := net.IP(b).String()
n, _ := strconv.ParseUint(i[1], 16, 64)
p := strconv.FormatUint(n, 10)
// ipv4:port
v := h + ":" + p
return v, nil
}
// Decode an IPv6 address with port from a given hex string
// NOTE: This function match NetIPDecoder type
func NetIPv6Decoder(s string) (string, error) {
if !ipv6RegExp.MatchString(s) {
return "", errors.New("Cannot decode ipv6 address: " + s)
}
i := strings.Split(s, ":")
b := make([]byte, 16)
for j := 0; j < 4; j++ {
x := j * 8
y := x + 8
// Extract 8 characters from hex string, 4 times.
//
// s: "350E012A900F122E85EDEAADA64DAAD1" -> [
// h: "350E012A", h: "900F122E",
// h: "85EDEAAD", h: "A64DAAD1",
// ]
h := i[0][x:y]
for k := 0; k < 4; k++ {
// Reverse byte order
// "350E012A" -> [ 0x2A, 0x01, 0x0E, 0x35 ]
z := (j * 4) + k
g := 8 - (k * 2)
f := g - 2
n, _ := strconv.ParseUint(h[f:g], 16, 8)
b[z] = byte(n)
}
}
h := net.IP(b).String()
n, _ := strconv.ParseUint(i[1], 16, 64)
p := strconv.FormatUint(n, 10)
// ipv6:port
v := h + ":" + p
return v, nil
}