/
iphdr.go
274 lines (222 loc) · 5.59 KB
/
iphdr.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package ip
import (
"encoding/binary"
"encoding/json"
"fmt"
)
/* Standard well-defined IP protocols. */
const (
IPPROTO_IP = 0
IPPROTO_ICMP = 1
IPPROTO_TCP = 6
IPPROTO_UDP = 17
IPPROTO_RAW = 255
)
const MAX_IPHEADER = 20
const MAX_IP6HEADER = 40
type IP4Header struct {
Version uint8
HeadLen uint8
Tos uint8
TotLen uint16
Id uint16
FragOff uint16
TTL uint8
Protocal uint8
Check uint16
SAddr IP4
DAddr IP4
}
type IP6Header struct {
Version uint8
Priority uint8
FlowLabel uint32
PlayLoad uint16
NextHdr uint8
HopLimit uint8
SAddr IP6
DAddr IP6
}
type IPType int
const (
_ IPType = iota
Ping
IPv4
IPv6
IPCtrl
)
func IPHeaderType(buff byte) IPType {
switch (buff >> 4) {
case 0:return IPCtrl
case 1:return Ping
case 4:return IPv4
case 6:return IPv6
default:
return IPCtrl
}
}
func IP6HeaderDecoder(buff []byte) *IP6Header {
if len(buff) < 40 {
return nil
}
iphdr := new(IP6Header)
return iphdr.Decoder(buff)
}
func IP6HeaderCoder(iphdr *IP6Header) []byte {
buff := make([]byte, 40)
iphdr.Coder(buff)
return buff
}
func (iphdr *IP6Header)Decoder(buff []byte) *IP6Header {
iphdr.Version = buff[0] >> 4
iphdr.Priority = buff[0] & 0x0f
iphdr.SAddr = make([]byte, 16)
iphdr.DAddr = make([]byte, 16)
if NativelyLittle() {
iphdr.FlowLabel = uint32(buff[1]) << 16 | uint32(buff[2]) << 8 | uint32(buff[3])
iphdr.PlayLoad = binary.BigEndian.Uint16(buff[4:])
} else {
iphdr.FlowLabel = uint32(buff[3]) << 16 | uint32(buff[2]) << 8 | uint32(buff[1])
iphdr.PlayLoad = binary.LittleEndian.Uint16(buff[4:])
}
iphdr.NextHdr = buff[6]
iphdr.HopLimit = buff[7]
copy(iphdr.SAddr, buff[8:24])
copy(iphdr.DAddr, buff[24:40])
return iphdr
}
func (iphdr *IP6Header)Coder(buff []byte) {
buff[0] = iphdr.Version << 4 | iphdr.Priority & 0x0f
if NativelyLittle() {
buff[1] = byte(iphdr.FlowLabel >> 16)
buff[2] = byte(iphdr.FlowLabel >> 8)
buff[3] = byte(iphdr.FlowLabel )
binary.BigEndian.PutUint16(buff[4:], iphdr.PlayLoad)
} else {
buff[3] = byte(iphdr.FlowLabel >> 16)
buff[2] = byte(iphdr.FlowLabel >> 8)
buff[1] = byte(iphdr.FlowLabel )
binary.LittleEndian.PutUint16(buff[4:], iphdr.PlayLoad)
}
buff[6] = iphdr.NextHdr
buff[7] = iphdr.HopLimit
copy(buff[8:24], iphdr.SAddr)
copy(buff[24:40], iphdr.DAddr)
}
func (iphdr *IP6Header)String() string {
output, _ :=json.Marshal(iphdr)
return string(output)
}
func IP4HeaderDecoder(buff []byte) *IP4Header {
if len(buff) < MAX_IPHEADER {
return nil
}
iphdr := new(IP4Header)
return iphdr.Decoder(buff)
}
func (iphdr *IP4Header)MakeCheckSum() {
iphdr.Check = 0
body := IP4HeaderCoder(iphdr)
sumb := make([]uint32, 5)
for i:=0; i<5; i++ {
sumb[i] = binary.LittleEndian.Uint32(body[i*4:])
}
iphdr.Check = CheckSum(sumb)
}
func (iphdr *IP4Header)CheckSum() bool {
checkold := iphdr.Check
iphdr.MakeCheckSum()
checknew := iphdr.Check
if checkold != checknew {
iphdr.Check = checkold
fmt.Printf("old checksum : %x, new checksum: %x", checkold, checknew)
return false
}
return true
}
func (iphdr *IP4Header)Decoder(buff []byte) *IP4Header {
iphdr.Version = buff[0] >> 4
iphdr.HeadLen = buff[0] & 0x0f
iphdr.Tos = buff[1]
iphdr.TTL = buff[8]
iphdr.Protocal = buff[9]
if NativelyLittle() {
iphdr.TotLen = binary.BigEndian.Uint16(buff[2:])
iphdr.Id = binary.BigEndian.Uint16(buff[4:])
iphdr.FragOff = binary.BigEndian.Uint16(buff[6:])
iphdr.Check = binary.BigEndian.Uint16(buff[10:])
iphdr.SAddr = IP4(binary.BigEndian.Uint32(buff[12:]))
iphdr.DAddr = IP4(binary.BigEndian.Uint32(buff[16:]))
} else {
iphdr.TotLen = binary.LittleEndian.Uint16(buff[2:])
iphdr.Id = binary.LittleEndian.Uint16(buff[4:])
iphdr.FragOff = binary.LittleEndian.Uint16(buff[6:])
iphdr.Check = binary.LittleEndian.Uint16(buff[10:])
iphdr.SAddr = IP4(binary.LittleEndian.Uint32(buff[12:]))
iphdr.DAddr = IP4(binary.LittleEndian.Uint32(buff[16:]))
}
return iphdr
}
func (iphdr *IP4Header)Coder(buff []byte) {
buff[0] = iphdr.Version << 4 + iphdr.HeadLen & 0x0f
buff[1] = iphdr.Tos
buff[8] = iphdr.TTL
buff[9] = iphdr.Protocal
if NativelyLittle() {
binary.BigEndian.PutUint16(buff[2:], iphdr.TotLen)
binary.BigEndian.PutUint16(buff[4:], iphdr.Id)
binary.BigEndian.PutUint16(buff[6:], iphdr.FragOff)
binary.BigEndian.PutUint16(buff[10:], iphdr.Check)
binary.BigEndian.PutUint32(buff[12:], uint32(iphdr.SAddr))
binary.BigEndian.PutUint32(buff[16:], uint32(iphdr.DAddr))
} else {
binary.LittleEndian.PutUint16(buff[2:], iphdr.TotLen)
binary.LittleEndian.PutUint16(buff[4:], iphdr.Id)
binary.LittleEndian.PutUint16(buff[6:], iphdr.FragOff)
binary.LittleEndian.PutUint16(buff[10:], iphdr.Check)
binary.LittleEndian.PutUint32(buff[12:], uint32(iphdr.SAddr))
binary.LittleEndian.PutUint32(buff[16:], uint32(iphdr.DAddr))
}
}
func (iphdr *IP4Header)String() string {
output, _ :=json.Marshal(iphdr)
return string(output)
}
func IP4HeaderCoder(iphdr *IP4Header) []byte {
buff := make([]byte, 20)
iphdr.Coder(buff)
return buff
}
func CheckSum(body []uint32) uint16 {
var sum uint32
var t1, t2 uint16
for _, v := range body {
sum += v
if ( sum < v) {
sum++
}
}
t1 = uint16(sum)
t2 = uint16(sum >> 16)
t1 += t2
if t1 < t2 {
t1++
}
return (^t1 >> 8) | ((^t1 & 0xff) << 8)
}
func (iphdr *IP4Header)DecrementTTL() error {
iphdr.TTL = iphdr.TTL - 1
if iphdr.TTL == 0 {
return fmt.Errorf("Discarding IP fragment %s -> %s due to zero TTL\n",
iphdr.SAddr, iphdr.DAddr)
}
if iphdr.Check >= 0xfeff {
iphdr.Check += 0x1
}
iphdr.Check += 0x100
/*
if iphdr.CheckSum() == false {
return fmt.Errorf("check sum fail")
}*/
return nil
}