-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathipv4.go
151 lines (113 loc) · 3.37 KB
/
ipv4.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
package proto
import (
"net"
)
// IPv4Packet represents an IPv4 packet.
type IPv4Packet struct {
Version uint8 `json:"version"`
InternetHeaderLength uint8 `json:"internetHeaderLength"`
DSCP uint8 `json:"dscp"`
ECN uint8 `json:"ecn"`
Length uint16 `json:"length"`
Identification uint16 `json:"indentification"`
Flags uint8 `json:"flags"`
FragmentationOffset uint16 `json:"fragmentationOffset"`
TimeToLive uint8 `json:"timeToLive"`
Protocol uint8 `json:"protocol"`
HeaderChecksum uint16 `json:"headerChecksum"`
Source net.IP `json:"source"`
Destination net.IP `json:"destination"`
Options []byte `json:"options"`
Payload []byte `json:"payload"`
}
const minIPv4PacketSize = 1 + 1 + 1 + 1 + 2 + 2 + 1 + 2 + 1 + 1 + 2 + 4 + 4
// DecodeIPv4 decodes an IPv4 packet.
func DecodeIPv4(b []byte) (IPv4Packet, error) {
packet := IPv4Packet{}
if len(b) < minIPv4PacketSize {
return packet, ErrorNotEnoughBytes
}
i := 0
packet.Version = uint8(b[i] >> 4)
packet.InternetHeaderLength = uint8(b[i] & 0xf)
i++
packet.DSCP = uint8(b[i] >> 2)
packet.ECN = uint8(b[i] & 0x3)
i++
packet.Length = uint16(b[i])<<8 | uint16(b[i+1])
i += 2
packet.Identification = uint16(b[i])<<8 | uint16(b[i+1])
i += 2
packet.Flags = uint8(b[i] >> 5)
packet.FragmentationOffset = uint16(b[i]&0x1f)<<8 | uint16(b[i+1])
i += 2
packet.TimeToLive = uint8(b[i])
i++
packet.Protocol = uint8(b[i])
i++
packet.HeaderChecksum = uint16(b[i])<<8 | uint16(b[i+1])
i += 2
packet.Source = net.IP(b[i : i+4])
i += 4
packet.Destination = net.IP(b[i : i+4])
i += 4
packet.Options = b[i : int(packet.InternetHeaderLength)*4]
i = int(packet.InternetHeaderLength) * 4
packet.Payload = b[i:]
return packet, nil
}
// Bytes returns an encoded IPv4 packet.
func (p IPv4Packet) Bytes() []byte {
i := 0
var b []byte
if p.InternetHeaderLength > 5 {
b = make([]byte, (24)+len(p.Payload))
} else {
b = make([]byte, (20)+len(p.Payload))
}
b[i] = byte(p.Version)<<4 | byte(p.InternetHeaderLength)
i++
b[i] = byte(p.DSCP)<<2 | byte(p.ECN)
i++
b[i] = byte(p.Length >> 8)
b[i+1] = byte(p.Length)
i += 2
b[i] = byte(p.Identification >> 8)
b[i+1] = byte(p.Identification)
i += 2
b[i] = p.Flags<<5 | byte(p.FragmentationOffset>>8)
b[i+1] = byte(p.FragmentationOffset)
i += 2
b[i] = p.TimeToLive
i++
b[i] = p.Protocol
i++
b[i] = byte(p.HeaderChecksum >> 8)
b[i+1] = byte(p.HeaderChecksum)
i += 2
copy(b[i:], p.Source)
i += 4
copy(b[i:], p.Destination)
i += 4
if p.InternetHeaderLength > 5 {
copy(b[i:], p.Options)
i += 4
}
copy(b[i:], p.Payload)
i += len(p.Payload)
return b[:i]
}
// ComputeChecksum returns the checksum for the IPv4 packet.
func (p IPv4Packet) ComputeChecksum() uint16 {
sum := ((uint32(p.Version)<<4|uint32(p.InternetHeaderLength))<<8 |
(uint32(p.DSCP)<<2 | uint32(p.ECN))) +
uint32(p.Length) +
uint32(p.Identification) +
(uint32(p.Flags)<<13 | uint32(p.FragmentationOffset)) +
(uint32(p.TimeToLive)<<8 | uint32(p.Protocol)) +
(uint32(p.Source[0])<<8 | uint32(p.Source[1])) +
(uint32(p.Source[2])<<8 | uint32(p.Source[3])) +
(uint32(p.Destination[0])<<8 | uint32(p.Destination[1])) +
(uint32(p.Destination[2])<<8 | uint32(p.Destination[3]))
return uint16(0xffff) ^ (uint16(sum>>16) + uint16(sum))
}