Skip to content

Commit

Permalink
Fix ifcIndex usage in Windows
Browse files Browse the repository at this point in the history
We were not doing the ifcIndex check correctly which is a bug
everywhere but only shows on Windows right now due to not seeing
Control Messages.
  • Loading branch information
Eric Daniels committed Apr 2, 2024
1 parent 1f6facc commit a4cb377
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,22 +319,22 @@ func Server(
// further out into the network stack.
if multicastPktConnV4 != nil {
if err := multicastPktConnV4.SetMulticastLoopback(true); err != nil {
c.log.Warnf("[%s] failed to SetMulticastLoopback(true) on multicast IPv4 PacketConn %v; this may cause inefficient network path c.name,communications", err)
c.log.Warnf("[%s] failed to SetMulticastLoopback(true) on multicast IPv4 PacketConn %v; this may cause inefficient network path c.name,communications", c.name, err)

Check warning on line 322 in conn.go

View check run for this annotation

Codecov / codecov/patch

conn.go#L322

Added line #L322 was not covered by tests
}
}
if multicastPktConnV6 != nil {
if err := multicastPktConnV6.SetMulticastLoopback(true); err != nil {
c.log.Warnf("[%s] failed to SetMulticastLoopback(true) on multicast IPv6 PacketConn %v; this may cause inefficient network path c.name,communications", err)
c.log.Warnf("[%s] failed to SetMulticastLoopback(true) on multicast IPv6 PacketConn %v; this may cause inefficient network path c.name,communications", c.name, err)

Check warning on line 327 in conn.go

View check run for this annotation

Codecov / codecov/patch

conn.go#L327

Added line #L327 was not covered by tests
}
}
if unicastPktConnV4 != nil {
if err := unicastPktConnV4.SetMulticastLoopback(true); err != nil {
c.log.Warnf("[%s] failed to SetMulticastLoopback(true) on unicast IPv4 PacketConn %v; this may cause inefficient network path c.name,communications", err)
c.log.Warnf("[%s] failed to SetMulticastLoopback(true) on unicast IPv4 PacketConn %v; this may cause inefficient network path c.name,communications", c.name, err)

Check warning on line 332 in conn.go

View check run for this annotation

Codecov / codecov/patch

conn.go#L332

Added line #L332 was not covered by tests
}
}
if unicastPktConnV6 != nil {
if err := unicastPktConnV6.SetMulticastLoopback(true); err != nil {
c.log.Warnf("[%s] failed to SetMulticastLoopback(true) on unicast IPv6 PacketConn %v; this may cause inefficient network path c.name,communications", err)
c.log.Warnf("[%s] failed to SetMulticastLoopback(true) on unicast IPv6 PacketConn %v; this may cause inefficient network path c.name,communications", c.name, err)

Check warning on line 337 in conn.go

View check run for this annotation

Codecov / codecov/patch

conn.go#L337

Added line #L337 was not covered by tests
}
}
}
Expand Down Expand Up @@ -590,7 +590,7 @@ func (c *Conn) sendQuestion(name string) {
return
}

c.writeToSocket(0, rawQuery, false, false, writeTypeQuestion, nil)
c.writeToSocket(-1, rawQuery, false, false, writeTypeQuestion, nil)
}

//nolint:gocognit
Expand All @@ -616,7 +616,7 @@ func (c *Conn) writeToSocket(
}
}

if ifIndex != 0 {
if ifIndex != -1 {
if wType == writeTypeQuestion {
c.log.Errorf("[%s] Unexpected question using specific interface index %d; dropping question", c.name, ifIndex)
return
Expand Down Expand Up @@ -690,7 +690,7 @@ func (c *Conn) writeToSocket(
}
}
} else {
c.log.Debugf("[%s] writing answer to IPv4: %s, IPv6: %s", c.name, dst4, dst6)
c.log.Debugf("[%s] writing answer to IPv4: %v, IPv6: %v", c.name, dst4, dst6)

Check warning on line 693 in conn.go

View check run for this annotation

Codecov / codecov/patch

conn.go#L693

Added line #L693 was not covered by tests

if ifc.supportsV4 && c.multicastPktConnV4 != nil && dst4 != nil {
if !hasIPv6Zone {
Expand Down Expand Up @@ -972,15 +972,15 @@ func (c *Conn) readLoop(name string, pktConn ipPacketConn, inboundBufferSize int
continue
}
if !isSupportedIPv6(addrCopy, c.multicastPktConnV4 == nil) {
c.log.Debugf("[%s] interface %d address not a supported IPv6 address %s", ifIndex, c.name, &addrCopy)
c.log.Debugf("[%s] interface %d address not a supported IPv6 address %s", c.name, ifIndex, &addrCopy)

Check warning on line 975 in conn.go

View check run for this annotation

Codecov / codecov/patch

conn.go#L975

Added line #L975 was not covered by tests
continue
}
}

selectedAddrs = append(selectedAddrs, addrCopy)
}
if len(selectedAddrs) == 0 {
c.log.Debugf("[%s] failed to find suitable IP for interface %d; deriving address from source address c.name,instead", ifIndex)
c.log.Debugf("[%s] failed to find suitable IP for interface %d; deriving address from source address c.name,instead", c.name, ifIndex)
} else {
// choose the best match
var choice *netip.Addr
Expand Down Expand Up @@ -1017,12 +1017,12 @@ func (c *Conn) readLoop(name string, pktConn ipPacketConn, inboundBufferSize int
}
if queryWantsV4 {
if !localAddress.Is4() {
c.log.Debugf("[%s] have IPv6 address %s to respond with but not question is for A not c.name,AAAA", localAddress)
c.log.Debugf("[%s] have IPv6 address %s to respond with but question is for A not c.name,AAAA", c.name, localAddress)

Check warning on line 1020 in conn.go

View check run for this annotation

Codecov / codecov/patch

conn.go#L1020

Added line #L1020 was not covered by tests
continue
}
} else {
if !localAddress.Is6() {
c.log.Debugf("[%s] have IPv4 address %s to respond with but not question is for AAAA not c.name,A", localAddress)
c.log.Debugf("[%s] have IPv4 address %s to respond with but question is for AAAA not c.name,A", c.name, localAddress)
continue
}
if !isSupportedIPv6(*localAddress, c.multicastPktConnV4 == nil) {
Expand All @@ -1042,6 +1042,7 @@ func (c *Conn) readLoop(name string, pktConn ipPacketConn, inboundBufferSize int
c.log.Debugf("[%s] refusing to send link-local address %s to an IPv4 destination %s", c.name, localAddress, dst)
continue
}
c.log.Debugf("[%s] sending response for %s on ifc %d of %s to %s", c.name, q.Name, ifIndex, *localAddress, dst)
c.sendAnswer(header.ID, q.Name.String(), ifIndex, *localAddress, dst)
}
}
Expand Down

0 comments on commit a4cb377

Please sign in to comment.