forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
names.go
134 lines (121 loc) · 3.52 KB
/
names.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
package dns
// This file contains the name mapping data used to convert various DNS IDs to
// their string values.
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
mkdns "github.com/miekg/dns"
)
// dnsOpCodeToString converts a Opcode value to a string. If the type's
// string representation is unknown then the numeric value will be returned as
// a string.
func dnsOpCodeToString(opCode int) string {
s, exists := mkdns.OpcodeToString[opCode]
if !exists {
return strconv.Itoa(int(opCode))
}
return s
}
// dnsResponseCodeToString converts a Rcode value to a string. If
// the type's string representation is unknown then "Unknown <rcode value>"
// will be returned.
func dnsResponseCodeToString(rcode int) string {
s, exists := mkdns.RcodeToString[rcode]
if !exists {
return fmt.Sprintf("Unknown %d", int(rcode))
}
return s
}
// dnsTypeToString converts a RR type value to a string. If the type's
// string representation is unknown then the numeric value will be returned
// as a string.
func dnsTypeToString(t uint16) string {
s, exists := mkdns.TypeToString[uint16(t)]
if !exists {
return strconv.Itoa(int(t))
}
return s
}
// dnsClassToString converts a RR class value to a string. If the class'es
// string representation is unknown then the numeric value will be returned
// as a string.
func dnsClassToString(c uint16) string {
s, exists := mkdns.ClassToString[uint16(c)]
if !exists {
return strconv.Itoa(int(c))
}
return s
}
// dnsAlgorithmToString converts an algorithm value to a string. If the algorithm's
// string representation is unknown then the numeric value will be returned
// as a string.
func dnsAlgorithmToString(a uint8) string {
s, exists := mkdns.AlgorithmToString[uint8(a)]
if !exists {
return strconv.Itoa(int(a))
}
return s
}
// dnsHashToString converts a hash value to a string. If the hash's
// string representation is unknown then the numeric value will be returned
// as a string.
func dnsHashToString(h uint8) string {
s, exists := mkdns.HashToString[uint8(h)]
if !exists {
return strconv.Itoa(int(h))
}
return s
}
// dnsTypeBitsMapToString converts a map of type bits to a string. If the type's
// string representation is unknown then the numeric value will be returned
// as a string.
func dnsTypeBitsMapToString(t []uint16) string {
var s string
for i := 0; i < len(t); i++ {
s += dnsTypeToString(t[i]) + " "
}
return strings.TrimSuffix(s, " ")
}
// saltToString converts a NSECX salt to uppercase and
// returns "-" when it is empty
// func copied from miekg/dns because unexported
func dnsSaltToString(s string) string {
if len(s) == 0 {
return "-"
}
return strings.ToUpper(s)
}
// hexStringToString converts an hexadeciaml string to string. Bytes
// below 32 or above 126 are represented as an escaped base10 integer (\DDD).
// Back slashes and quotes are escaped. Tabs, carriage returns, and line feeds
// will be converted to \t, \r and \n respectively.
// Example:
func hexStringToString(hexString string) (string, error) {
bytes, err := hex.DecodeString(hexString)
if err != nil {
return hexString, err
}
var s []byte
for _, value := range bytes {
switch value {
default:
if value < 32 || value >= 127 {
// Unprintable characters are written as \\DDD (e.g. \\012).
s = append(s, []byte(fmt.Sprintf("\\%03d", int(value)))...)
} else {
s = append(s, value)
}
case '"', '\\':
s = append(s, '\\', value)
case '\t':
s = append(s, '\\', 't')
case '\r':
s = append(s, '\\', 'r')
case '\n':
s = append(s, '\\', 'n')
}
}
return string(s), nil
}