Skip to content

Commit

Permalink
fix: properly reverse ip address
Browse files Browse the repository at this point in the history
  • Loading branch information
strideynet committed Mar 14, 2022
1 parent 3a624ae commit fc033fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
3 changes: 2 additions & 1 deletion backend/api_v1/rdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ func rdns(g group, log *zap.Logger, dnsServer string) {
return
}

ipAddr := net.ParseIP(ip)
result, err := dnsLib.LookupRDNS(
log, ip, dnsServer,
log, ipAddr, dnsServer,
)
if err != nil {
ctx.Error(&gin.Error{
Expand Down
19 changes: 12 additions & 7 deletions backend/dns/dns_resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math/rand"
"net"
"reflect"
"strings"
"sync"
Expand Down Expand Up @@ -427,16 +428,20 @@ func Lookup(log *zap.Logger, dnsServer, recordType, hostname string, fullTrace b
return recursiveQuery(log, dnsServer, recordType, hostname)
}

func reverseString(s string) string {
chars := []rune(s)
for i, j := 0, len(chars)-1; i < j; i, j = i+1, j-1 {
chars[i], chars[j] = chars[j], chars[i]
func reverseIP(ip net.IP) string {
addressParts := strings.Split(ip.String(), ".")
reversed := []string{}

for i := len(addressParts) - 1; i >= 0; i-- {
octet := addressParts[i]
reversed = append(reversed, octet)
}
return string(chars)

return strings.Join(reversed, ".")
}

func LookupRDNS(log *zap.Logger, ip, dnsServer string) (RecordType, error) {
hostname := reverseString(ip) + ".in-addr.arpa."
func LookupRDNS(log *zap.Logger, ip net.IP, dnsServer string) (RecordType, error) {
hostname := reverseIP(ip) + ".in-addr.arpa."
resp, err := traceQuery(log, dnsServer, "PTR", hostname)
if err != nil {
return nil, err
Expand Down

0 comments on commit fc033fd

Please sign in to comment.