-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipv4.go
60 lines (51 loc) · 1.05 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
package main
import (
"bytes"
"encoding/binary"
)
func calculateChecksum(bytes []byte) uint16 {
var sum uint32
for i := range bytes {
if i%2 != 0 {
continue
}
a := uint16(bytes[i])
b := uint16(bytes[i+1])
sum += uint32(b + (a << 8))
}
for sum > 0xffff {
carry := sum >> 16
sum = sum & 0xffff
sum += carry
}
sum = ^sum
return uint16(sum)
}
type ipv4Datagram struct {
VersionHL uint8
Tos uint8
TotalLength uint16
ID uint16
FlagsFragOff uint16
TTL uint8
Protocol uint8
ChkSum uint16
SourceAddr [4]byte
DestAddr [4]byte
}
func makeIpv4Packet(sourceAddr [4]byte, destAddr [4]byte, length uint16, id uint16, protocol uint8) ipv4Datagram {
var ret ipv4Datagram
ret.VersionHL = 69
ret.TotalLength = length
ret.ID = id
ret.TTL = 64
ret.Protocol = protocol
ret.SourceAddr = sourceAddr
ret.DestAddr = destAddr
// Checksum calculation
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, ret)
check(err)
ret.ChkSum = calculateChecksum(buf.Bytes())
return ret
}