Skip to content

Commit

Permalink
Dealing with testing thinking it is a race in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
atomirex committed Dec 21, 2024
1 parent 4e2fcb4 commit 0d118e9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ type Config struct {

// Interfaces will override the interfaces used for queries and answers.
Interfaces []net.Interface

// Override the default behaviour of echoing the query with the answer

Check failure on line 50 in config.go

View workflow job for this annotation

GitHub Actions / lint / Go

`behaviour` is a misspelling of `behavior` (misspell)
DoNotEchoQueryWithAnswer bool
}
21 changes: 10 additions & 11 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ type Conn struct {
queries []*query
ifaces map[int]netInterface

echoQueryInAnswer bool

closed chan interface{}
}

Expand Down Expand Up @@ -104,10 +102,9 @@ func Server(
log := loggerFactory.NewLogger("mdns")

c := &Conn{
queryInterval: defaultQueryInterval,
log: log,
closed: make(chan interface{}),
echoQueryInAnswer: true,
queryInterval: defaultQueryInterval,
log: log,
closed: make(chan interface{}),
}
c.name = config.Name
if c.name == "" {
Expand Down Expand Up @@ -713,7 +710,7 @@ func (c *Conn) writeToSocket(
}
}

func (c *Conn) createAnswer(id uint16, q dnsmessage.Question, addr netip.Addr) (dnsmessage.Message, error) {
func (c *Conn) createAnswer(id uint16, q dnsmessage.Question, addr netip.Addr, config *Config) (dnsmessage.Message, error) {
packedName, err := dnsmessage.NewName(q.Name.String())
if err != nil {
return dnsmessage.Message{}, err
Expand All @@ -736,7 +733,9 @@ func (c *Conn) createAnswer(id uint16, q dnsmessage.Question, addr netip.Addr) (
},
}

if c.echoQueryInAnswer {
// This is a negative because we want to default to echoing the query with an answer
// The main use of turning it off is in testing
if !config.DoNotEchoQueryWithAnswer {
msg.Questions = []dnsmessage.Question{q}
}

Expand Down Expand Up @@ -764,8 +763,8 @@ func (c *Conn) createAnswer(id uint16, q dnsmessage.Question, addr netip.Addr) (
return msg, nil
}

func (c *Conn) sendAnswer(queryID uint16, q dnsmessage.Question, ifIndex int, result netip.Addr, dst *net.UDPAddr) {
answer, err := c.createAnswer(queryID, q, result)
func (c *Conn) sendAnswer(queryID uint16, q dnsmessage.Question, ifIndex int, result netip.Addr, dst *net.UDPAddr, config *Config) {
answer, err := c.createAnswer(queryID, q, result, config)
if err != nil {
c.log.Warnf("[%s] failed to create mDNS answer %v", c.name, err)
return
Expand Down Expand Up @@ -1045,7 +1044,7 @@ func (c *Conn) readLoop(name string, pktConn ipPacketConn, inboundBufferSize int
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(msg.Header.ID, q, ifIndex, *localAddress, dst)
c.sendAnswer(msg.Header.ID, q, ifIndex, *localAddress, dst, config)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,12 +732,11 @@ func testAnswerHandlingWithQueryEchoed(t *testing.T, echoQuery bool) {
bSock := createListener4(t)

aServer, err := Server(ipv4.NewPacketConn(aSock), nil, &Config{
LocalNames: []string{"pion-mdns-1.local", "pion-mdns-2.local"},
LocalNames: []string{"pion-mdns-1.local", "pion-mdns-2.local"},
DoNotEchoQueryWithAnswer: !echoQuery,
})
check(err, t)

aServer.echoQueryInAnswer = echoQuery

bServer, err := Server(ipv4.NewPacketConn(bSock), nil, &Config{})
check(err, t)

Expand Down

0 comments on commit 0d118e9

Please sign in to comment.