Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion documentation/advanced-features/debugging-with-dlv.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Debugger
# Debugging Tips

- [Debugger](#debugger)
- [Debug a particular function](#debug-a-particular-function)
Expand Down Expand Up @@ -80,3 +80,31 @@ VSCode equivalent configuration is:

]
```

## Debug `helpers.js`

Develop a function:

```
node -e "
function IP(dot) {
var d = dot.split('.');
return ((((((+d[0]) * 256) + (+d[1])) * 256) + (+d[2])) * 256) + (+d[3]);
}
console.log(IP('135.181.247.240'));
"
```

Debug a function within helpers.js:

```
$ node -e "
const fs = require('fs');
const vm = require('vm');
const code = fs.readFileSync('/Users/tlimoncelli/gitthings/dnscontrol/pkg/js/helpers.js', 'utf8');
const sandbox = {};
vm.createContext(sandbox);
vm.runInContext(code, sandbox);
console.log(vm.runInContext(\" IP('135.181.247.240') \", sandbox));
"
```
3 changes: 3 additions & 0 deletions pkg/js/parse_tests/004-ips.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ D("foo.com", REG, DnsProvider(CF, 0),
A("@", BASE),
A("p1", BASE + 1),
A("p255", BASE + 255),

// An IP address with the highest bit set, to catch uint/int bugs.
A("yyy", IP("190.2.3.4"))
);
12 changes: 12 additions & 0 deletions pkg/js/parse_tests/004-ips.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@
"ttl": 300,
"type": "A",
"typenum": 1
},
{
"comparablev3": "190.2.3.4",
"filepos": "[line:12:5]",
"name": "yyy",
"name_unicode": "yyy",
"rdata": {
"Addr": "190.2.3.4"
},
"ttl": 300,
"type": "A",
"typenum": 1
}
],
"registrar": "Third-Party",
Expand Down
1 change: 1 addition & 0 deletions pkg/js/parse_tests/004-ips/foo.com.zone
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ $TTL 300
@ IN A 1.2.3.4
p1 IN A 1.2.3.5
p255 IN A 1.2.4.3
yyy IN A 190.2.3.4
16 changes: 7 additions & 9 deletions pkg/mustbe/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ import (
func IPv4(a any) (netip.Addr, error) {
switch v := a.(type) {
case float64:
i := int32(v)
a := (i >> 24) % 256
b := (i >> 16) % 256
c := (i >> 8) % 256
d := i % 256
// IP addresses expressed as a number (e.g. via the IP() helper in
// dnsconfig.js) range from 0 to 4294967295 (2^32-1)
i := uint32(v)
x := netip.AddrFrom4([4]byte{
byte(a),
byte(b),
byte(c),
byte(d),
byte(i >> 24), // NB(tlim): byte() silently truncates the upper bits.
byte(i >> 16),
byte(i >> 8),
byte(i),
})
return x, nil
case string:
Expand Down
5 changes: 5 additions & 0 deletions pkg/mustbe/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func TestIPv4_Parse(t *testing.T) {
{"a", "1.2.3.4", netip.MustParseAddr("1.2.3.4")},
{"b", float64((2 << 24) + (3 << 16) + (4 << 8) + 5), netip.MustParseAddr("2.3.4.5")},
{"c", netip.MustParseAddr("3.4.5.6"), netip.MustParseAddr("3.4.5.6")},
// Regression test for https://github.com/DNSControl/dnscontrol/issues/4825:
// addresses with a first octet >= 128 produce a number greater than
// math.MaxInt32, which used to overflow when converted via int32.
{"d", float64((135 << 24) + (181 << 16) + (247 << 8) + 240), netip.MustParseAddr("135.181.247.240")},
{"e", float64((255 << 24) + (255 << 16) + (255 << 8) + 255), netip.MustParseAddr("255.255.255.255")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading