go.d dnsmsasq_dhcp: improve parsing of dhcp ranges (#18394)
Ilya Mashchenko committed
Aug 22, 2024 at 12:51 UTC
b170da193c9f2dcd48887855533ca722712785a1
2 files changed
+79
-10
src/go/plugin/go.d/modules/dnsmasq_dhcp/dhcp_test.go
+60
@@ -147,3 +147,63 @@ func TestDnsmasqDHCP_CollectFailedToOpenLeasesPath(t *testing.T) {
147
job.LeasesPath = ""
148
assert.Nil(t, job.Collect())
149
}
150
+
151
+func TestDnsmasqDHCP_parseDHCPRangeValue(t *testing.T) {
152
+ tests := map[string]struct {
153
+ input string
154
+ wantFail bool
155
+ }{
156
+ "ipv4": {
157
+ input: "192.168.0.50,192.168.0.150,12h",
158
+ },
159
+ "ipv4 with netmask": {
160
+ input: "192.168.0.50,192.168.0.150,255.255.255.0,12h",
161
+ },
162
+ "ipv4 with netmask and tag": {
163
+ input: "set:red,1.1.1.50,1.1.2.150, 255.255.252.0",
164
+ },
165
+ "ipv4 with iface": {
166
+ input: "enp3s0, 172.16.1.2, 172.16.1.254, 1h",
167
+ },
168
+ "ipv4 with iface 2": {
169
+ input: "enp2s0.100, 192.168.100.2, 192.168.100.254, 1h",
170
+ },
171
+ "ipv4 static": {
172
+ wantFail: true,
173
+ input: "192.168.0.0,static",
174
+ },
175
+ "ipv6": {
176
+ input: "1234::2,1234::500",
177
+ },
178
+ "ipv6 slacc": {
179
+ input: "1234::2,1234::500, slaac",
180
+ },
181
+ "ipv6 with with prefix length and lease time": {
182
+ input: "1234::2,1234::500, 64, 12h",
183
+ },
184
+ "ipv6 ra-only": {
185
+ wantFail: true,
186
+ input: "1234::,ra-only",
187
+ },
188
+ "ipv6 ra-names": {
189
+ wantFail: true,
190
+ input: "1234::,ra-names",
191
+ },
192
+ "ipv6 ra-stateless": {
193
+ wantFail: true,
194
+ input: "1234::,ra-stateless",
195
+ },
196
+ }
197
+
198
+ for name, test := range tests {
199
+ t.Run(name, func(t *testing.T) {
200
+ v := parseDHCPRangeValue(test.input)
201
+
202
+ if test.wantFail {
203
+ assert.Emptyf(t, v, "parsing '%s' must fail", test.input)
204
+ } else {
205
+ assert.NotEmptyf(t, v, "parsing '%s' must not fail", test.input)
206
+ }
207
+ })
208
+ }
209
+}
src/go/plugin/go.d/modules/dnsmasq_dhcp/parse_configuration.go
+19
-10
@@ -97,24 +97,31 @@ Examples:
97
- 1234::,ra-names
98
- 1234::,ra-stateless
99
*/
100
-var reDHCPRange = regexp.MustCompile(`([0-9a-f.:]+),([0-9a-f.:]+)`)
100
101
func parseDHCPRangeValue(s string) (r string) {
102
if strings.Contains(s, "ra-stateless") {
104
- return
103
+ return ""
104
}
105
107
- match := reDHCPRange.FindStringSubmatch(s)
108
- if match == nil {
109
- return
110
- }
106
+ s = strings.ReplaceAll(s, " ", "")
107
+
108
+ var start, end net.IP
109
+ parts := strings.Split(s, ",")
110
112
- start, end := net.ParseIP(match[1]), net.ParseIP(match[2])
113
- if start == nil || end == nil {
114
- return
111
+ for i, v := range parts {
112
+ if start = net.ParseIP(strings.TrimSpace(v)); start == nil {
113
+ continue
114
+ }
115
+ if len(parts) < i+1 {
116
+ return ""
117
+ }
118
+ if end = net.ParseIP(parts[i+1]); end == nil || iprange.New(start, end) == nil {
119
+ return ""
120
+ }
121
+ return fmt.Sprintf("%s-%s", start, end)
122
}
123
117
- return fmt.Sprintf("%s-%s", start, end)
124
+ return ""
125
}
126
127
/*
@@ -134,6 +141,8 @@ var (
141
)
142
143
func parseDHCPHostValue(s string) (r string) {
144
+ s = strings.ReplaceAll(s, " ", "")
145
+
146
if strings.Contains(s, "[") {
147
return strings.Trim(reDHCPHostV6.FindString(s), "[]")
148
}