| 1 |
package dnsutil |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strconv" |
| 6 |
"strings" |
| 7 |
|
| 8 |
"codeberg.org/miekg/dns" |
| 9 |
) |
| 10 |
|
| 11 |
// TypeToString converts the type to the text presentation, or to "TYPE"+value if the type is unknown. |
| 12 |
// Also see [ClassToString], [RcodeToString], [OpcodeToString], [CodeToString] for similar functions. |
| 13 |
func TypeToString(t uint16) string { |
| 14 |
if t1, ok := dns.TypeToString[t]; ok { |
| 15 |
return t1 |
| 16 |
} |
| 17 |
return "TYPE" + strconv.Itoa(int(t)) |
| 18 |
} |
| 19 |
|
| 20 |
// RcodeToString converts the code to the text presentation, or to "RCODE"+value if the rcode is unknown. |
| 21 |
// Also see [ClassToString], [TypeToString], [OpcodeToString], [CodeToString] for similar functions. |
| 22 |
func RcodeToString(r uint16) string { |
| 23 |
if r1, ok := dns.RcodeToString[r]; ok { |
| 24 |
return r1 |
| 25 |
} |
| 26 |
return "RCODE" + strconv.Itoa(int(r)) |
| 27 |
} |
| 28 |
|
| 29 |
// ClassToString converts the class to the text presentation, or to "CLASS"+value if the class is unknown. |
| 30 |
// Also see [RcodeToString], [TypeToString], [OpcodeToString], [CodeToString] for similar functions. |
| 31 |
func ClassToString(c uint16) string { |
| 32 |
if c1, ok := dns.ClassToString[c]; ok { |
| 33 |
return c1 |
| 34 |
} |
| 35 |
return "CLASS" + strconv.Itoa(int(c)) |
| 36 |
} |
| 37 |
|
| 38 |
// OpcodeToString converts the opcode to the text presentation, or to "OPCODE"+value if the opcode is unknown. |
| 39 |
// Also see [RcodeToString], [TypeToString], [ClassToString], [CodeToString] for similar functions. |
| 40 |
func OpcodeToString(o uint8) string { |
| 41 |
if o1, ok := dns.OpcodeToString[o]; ok { |
| 42 |
return o1 |
| 43 |
} |
| 44 |
return "OPCODE" + strconv.Itoa(int(o)) |
| 45 |
} |
| 46 |
|
| 47 |
// CodeToString converts the ENDS0 code to the text presentation, or to "CODE"+value if the code is unknown. |
| 48 |
// Also see [RcodeToString], [TypeToString], [ClassToString], [OpcodeToString] for similar functions. |
| 49 |
func CodeToString(c uint16) string { |
| 50 |
if c1, ok := dns.CodeToString[c]; ok { |
| 51 |
return c1 |
| 52 |
} |
| 53 |
return "CODE" + strconv.Itoa(int(c)) |
| 54 |
} |
| 55 |
|
| 56 |
// StringToType converts the text presentation or "TYPE"+value to a type. |
| 57 |
// Inverse of [TypeToString]. |
| 58 |
func StringToType(s string) (uint16, error) { |
| 59 |
s = strings.ToUpper(s) |
| 60 |
if t1, ok := dns.StringToType[s]; ok { |
| 61 |
return t1, nil |
| 62 |
} |
| 63 |
if !strings.HasPrefix(s, "TYPE") { |
| 64 |
return 0, fmt.Errorf("invalid type %q", s) |
| 65 |
} |
| 66 |
t2, err := strconv.ParseUint(s[4:], 10, 16) |
| 67 |
if err != nil { |
| 68 |
return 0, fmt.Errorf("invalid type %q", s) |
| 69 |
} |
| 70 |
return uint16(t2), nil |
| 71 |
} |
| 72 |
|
| 73 |
// StringToRcode converts the text presentation or "RCODE"+value to a code. |
| 74 |
// Inverse of [RcodeToString]. |
| 75 |
func StringToRcode(s string) (uint16, error) { |
| 76 |
s = strings.ToUpper(s) |
| 77 |
if r1, ok := dns.StringToRcode[s]; ok { |
| 78 |
return r1, nil |
| 79 |
} |
| 80 |
if !strings.HasPrefix(s, "RCODE") { |
| 81 |
return 0, fmt.Errorf("invalid rcode %q", s) |
| 82 |
} |
| 83 |
r2, err := strconv.ParseUint(s[5:], 10, 16) |
| 84 |
if err != nil { |
| 85 |
return 0, fmt.Errorf("invalid rcode %q", s) |
| 86 |
} |
| 87 |
return uint16(r2), nil |
| 88 |
} |
| 89 |
|
| 90 |
// StringToClass converts the text presentation or "CLASS"+value to a class. |
| 91 |
// Inverse of [ClassToString]. |
| 92 |
func StringToClass(s string) (uint16, error) { |
| 93 |
s = strings.ToUpper(s) |
| 94 |
if c1, ok := dns.StringToClass[s]; ok { |
| 95 |
return c1, nil |
| 96 |
} |
| 97 |
if !strings.HasPrefix(s, "CLASS") { |
| 98 |
return 0, fmt.Errorf("invalid class %q", s) |
| 99 |
} |
| 100 |
c2, err := strconv.ParseUint(s[5:], 10, 16) |
| 101 |
if err != nil { |
| 102 |
return 0, fmt.Errorf("invalid class %q", s) |
| 103 |
} |
| 104 |
return uint16(c2), nil |
| 105 |
} |
| 106 |
|
| 107 |
// StringToOpcode converts the text representation or "OPCODE"+value to an opcode. |
| 108 |
// Inverse of [OpcodeToString]. |
| 109 |
func StringToOpcode(s string) (uint8, error) { |
| 110 |
s = strings.ToUpper(s) |
| 111 |
if o1, ok := dns.StringToOpcode[s]; ok { |
| 112 |
return o1, nil |
| 113 |
} |
| 114 |
if !strings.HasPrefix(s, "OPCODE") { |
| 115 |
return 0, fmt.Errorf("invalid opcode %q", s) |
| 116 |
} |
| 117 |
o2, err := strconv.ParseUint(s[6:], 10, 8) |
| 118 |
if err != nil { |
| 119 |
return 0, fmt.Errorf("invalid opcode %q", s) |
| 120 |
} |
| 121 |
return uint8(o2), nil |
| 122 |
} |
| 123 |
|
| 124 |
// StringToCode converts the text representation or "CODE"+value to an EDNS0 code. |
| 125 |
// Inverse of [CodeToString]. |
| 126 |
func StringToCode(s string) (uint16, error) { |
| 127 |
s = strings.ToUpper(s) |
| 128 |
if c1, ok := dns.StringToCode[s]; ok { |
| 129 |
return c1, nil |
| 130 |
} |
| 131 |
if !strings.HasPrefix(s, "CODE") { |
| 132 |
return 0, fmt.Errorf("invalid code %q", s) |
| 133 |
} |
| 134 |
c2, err := strconv.ParseUint(s[4:], 10, 16) |
| 135 |
if err != nil { |
| 136 |
return 0, fmt.Errorf("invalid code %q", s) |
| 137 |
} |
| 138 |
return uint16(c2), nil |
| 139 |
} |