From 6f83934f592f50176ffd4ad76f2d4bb9e4c47e16 Mon Sep 17 00:00:00 2001 From: atomirex Date: Sat, 21 Dec 2024 17:25:14 -0500 Subject: [PATCH] Dealing with testing thinking it is a race in configuration --- config.go | 3 +++ conn.go | 21 ++++++++++----------- conn_test.go | 5 ++--- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/config.go b/config.go index 4659e06..799a680 100644 --- a/config.go +++ b/config.go @@ -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 + DoNotEchoQueryWithAnswer bool } diff --git a/conn.go b/conn.go index 9610312..5bccc4b 100644 --- a/conn.go +++ b/conn.go @@ -37,8 +37,6 @@ type Conn struct { queries []*query ifaces map[int]netInterface - echoQueryInAnswer bool - closed chan interface{} } @@ -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 == "" { @@ -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 @@ -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} } @@ -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 @@ -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) } } } diff --git a/conn_test.go b/conn_test.go index 21c8ae4..995388f 100644 --- a/conn_test.go +++ b/conn_test.go @@ -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)