Skip to content

Commit

Permalink
Refactor addrFromAnswerHeader logic with a switch
Browse files Browse the repository at this point in the history
Replaced the if-else structure with a switch statement for better
readability and future extensibility, Added a default case to handle
unsupported record types explicitly (Never happens)
  • Loading branch information
JoeTurki committed Jan 27, 2025
1 parent 3a20496 commit 2bf28f4
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,8 @@ func (c *Conn) start(started chan<- struct{}, inboundBufferSize int, config *Con
}

func addrFromAnswerHeader(a dnsmessage.ResourceHeader, p dnsmessage.Parser) (addr *netip.Addr, err error) {
if a.Type == dnsmessage.TypeA {
switch a.Type {
case dnsmessage.TypeA:
resource, err := p.AResource()
if err != nil {
return nil, err
Expand All @@ -1181,8 +1182,9 @@ func addrFromAnswerHeader(a dnsmessage.ResourceHeader, p dnsmessage.Parser) (add
return nil, fmt.Errorf("failed to convert A record: %w", ipToAddrError{resource.A[:]})
}
ipAddr = ipAddr.Unmap() // do not want 4-in-6
addr = &ipAddr
} else {

return &ipAddr, nil
case dnsmessage.TypeAAAA:
resource, err := p.AAAAResource()
if err != nil {
return nil, err
Expand All @@ -1191,10 +1193,12 @@ func addrFromAnswerHeader(a dnsmessage.ResourceHeader, p dnsmessage.Parser) (add
if !ok {
return nil, fmt.Errorf("failed to convert AAAA record: %w", ipToAddrError{resource.AAAA[:]})
}
addr = &ipAddr
}

return
return &ipAddr, nil
default:
//nolint:err113 // Never happens, unless the code changes, the caller will filter out the unsupported types.
return nil, fmt.Errorf("unsupported record type %d", a.Type)

Check warning on line 1200 in conn.go

View check run for this annotation

Codecov / codecov/patch

conn.go#L1198-L1200

Added lines #L1198 - L1200 were not covered by tests
}
}

func isSupportedIPv6(addr netip.Addr, ipv6Only bool) bool {
Expand Down

0 comments on commit 2bf28f4

Please sign in to comment.