-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
108 lines (92 loc) · 2.13 KB
/
util.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
package packemon
import (
"bytes"
"encoding/binary"
"strconv"
"strings"
)
func WriteUint16(buf *bytes.Buffer, target uint16) {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, target)
buf.Write(b)
}
func WriteUint32(buf *bytes.Buffer, target uint32) {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, target)
buf.Write(b)
}
// stringのIPv4アドレスをbytesに変換
func StrIPToBytes(s string) ([]byte, error) {
b := make([]byte, 4)
src := strings.Split(s, ".")
for i := range src {
if len(src[i]) == 0 {
continue
}
ip, err := strconv.ParseUint(src[i], 10, 8)
if err != nil {
return nil, err
}
b[i] = byte(ip)
}
return b, nil
}
// TODO: rename or refactor
func StrHexToBytes(s string) ([]byte, error) {
n, err := strconv.ParseUint(s, 0, 48)
if err != nil {
return nil, err
}
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, n)
return buf[2:], nil
}
// TODO: rename or refactor
func StrHexToBytes2(s string) ([]byte, error) {
n, err := strconv.ParseUint(s, 0, 16)
if err != nil {
return nil, err
}
buf := make([]byte, 2)
binary.BigEndian.PutUint16(buf, uint16(n))
return buf, nil
}
// TODO: rename or refactor
func StrHexToBytes3(s string) (byte, error) {
n, err := strconv.ParseUint(s, 0, 8)
if err != nil {
return 0, err
}
return uint8(n), nil
}
func StrIntToUint16(s string) (uint16, error) {
n, err := strconv.ParseUint(s, 0, 16)
if err != nil {
return 0, err
}
return uint16(n), nil
}
// copy of https://github.com/sat0ken/go-curo/blob/main/utils.go#L18
func calculateChecksum(packet []byte) []byte {
// まず16ビット毎に足す
sum := sumByteArr(packet)
// あふれた桁を足す
sum = (sum & 0xffff) + sum>>16
// 論理否定を取った値をbyteにして返す
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, uint16(sum^0xffff))
return b
}
func sumByteArr(packet []byte) (sum uint) {
for i := range packet {
// ここ足した. icmpのreply返ってきてるし大丈夫そう
if (i == len(packet)-2) && (len(packet)%2 != 0) {
sum += uint(packet[i])
break
}
if i%2 == 0 {
sum += uint(binary.BigEndian.Uint16(packet[i:]))
}
}
return sum
}