Skip to content

Commit

Permalink
Make minmdns use a distinct policy for endpoint and IP choices (proje…
Browse files Browse the repository at this point in the history
…ct-chip#22168)

* Use AllInterfacesListenIterator header for minmdns.

Removes the minmdns sever implementation for logic regarding
interface selection, use the central header for this.

* Start adding some policies and a default policy impl

* Restyle

* Compile in the default minmdns policy

* Use policies for IP address iteration

* Switch interface iteration for Advertiser_ImplMinimalMdns

* Completely remove the use of AllInterfaceListenIterator

* Add file to header dependency

* make testipresponder initialize memory

* Switch address policy to be a virtual base class that can be overrider. Address policy default implementation is optional

* Disable ipv4 flag in minmdns example: build flags should control server capabilities

* Address review comment: address iterator type filtering happens in the policy

* Remove FIXME text in code

* Fix TestIPResponder
  • Loading branch information
andy31415 authored and isiu-apple committed Sep 16, 2022
1 parent e730606 commit 102a8bb
Show file tree
Hide file tree
Showing 19 changed files with 464 additions and 364 deletions.
122 changes: 0 additions & 122 deletions examples/minimal-mdns/AllInterfaceListener.h

This file was deleted.

2 changes: 1 addition & 1 deletion examples/minimal-mdns/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import("${chip_root}/build/chip/tools.gni")

static_library("minimal-mdns-example-common") {
sources = [
"AllInterfaceListener.h",
"PacketReporter.cpp",
"PacketReporter.h",
]
Expand Down Expand Up @@ -53,6 +52,7 @@ executable("minimal-mdns-server") {
":minimal-mdns-example-common",
"${chip_root}/src/lib",
"${chip_root}/src/lib/dnssd/minimal_mdns",
"${chip_root}/src/lib/dnssd/minimal_mdns:default_policy",
"${chip_root}/src/lib/dnssd/minimal_mdns/responders",
]

Expand Down
23 changes: 7 additions & 16 deletions examples/minimal-mdns/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include <inet/InetInterface.h>
#include <inet/UDPEndPoint.h>
#include <lib/dnssd/minimal_mdns/AddressPolicy.h>
#include <lib/dnssd/minimal_mdns/AddressPolicy_DefaultImpl.h>
#include <lib/dnssd/minimal_mdns/QueryBuilder.h>
#include <lib/dnssd/minimal_mdns/Server.h>
#include <lib/dnssd/minimal_mdns/core/QName.h>
Expand All @@ -30,7 +32,6 @@
#include <platform/CHIPDeviceLayer.h>
#include <system/SystemPacketBuffer.h>

#include "AllInterfaceListener.h"
#include "PacketReporter.h"

using namespace chip;
Expand All @@ -39,7 +40,6 @@ namespace {

struct Options
{
bool enableIpV4 = false;
bool unicastAnswers = true;
uint32_t runtimeMs = 500;
uint16_t querySendPort = 5353;
Expand All @@ -53,9 +53,8 @@ constexpr size_t kMdnsMaxPacketSize = 1'024;

using namespace chip::ArgParser;

constexpr uint16_t kOptionEnableIpV4 = '4';
constexpr uint16_t kOptionQuery = 'q';
constexpr uint16_t kOptionType = 't';
constexpr uint16_t kOptionQuery = 'q';
constexpr uint16_t kOptionType = 't';

// non-ascii options have no short option version
constexpr uint16_t kOptionListenPort = 0x100;
Expand All @@ -67,10 +66,6 @@ bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier,
{
switch (aIdentifier)
{
case kOptionEnableIpV4:
gOptions.enableIpV4 = true;
return true;

case kOptionListenPort:
if (!ParseInt(aValue, gOptions.listenPort))
{
Expand Down Expand Up @@ -145,7 +140,6 @@ bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier,

OptionDef cmdLineOptionsDef[] = {
{ "listen-port", kArgumentRequired, kOptionListenPort },
{ "enable-ip-v4", kNoArgument, kOptionEnableIpV4 },
{ "query", kArgumentRequired, kOptionQuery },
{ "type", kArgumentRequired, kOptionType },
{ "query-port", kArgumentRequired, kOptionQueryPort },
Expand All @@ -157,9 +151,6 @@ OptionDef cmdLineOptionsDef[] = {
OptionSet cmdLineOptions = { HandleOptions, cmdLineOptionsDef, "PROGRAM OPTIONS",
" --listen-port <number>\n"
" The port number to listen on\n"
" -4\n"
" --enable-ip-v4\n"
" enable listening on IPv4\n"
" -q\n"
" --query\n"
" The query to send\n"
Expand Down Expand Up @@ -324,13 +315,13 @@ int main(int argc, char ** args)
ReportDelegate reporter;
CHIP_ERROR err;

mdns::Minimal::SetDefaultAddressPolicy();
gMdnsServer.SetDelegate(&reporter);

{
auto endpoints = mdns::Minimal::GetAddressPolicy()->GetListenEndpoints();

MdnsExample::AllInterfaces allInterfaces(gOptions.enableIpV4);

err = gMdnsServer.Listen(chip::DeviceLayer::UDPEndPointManager(), &allInterfaces, gOptions.listenPort);
err = gMdnsServer.Listen(chip::DeviceLayer::UDPEndPointManager(), endpoints.get(), gOptions.listenPort);
if (err != CHIP_NO_ERROR)
{
printf("Server failed to listen on all interfaces: %s\n", chip::ErrorStr(err));
Expand Down
9 changes: 6 additions & 3 deletions examples/minimal-mdns/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <inet/InetInterface.h>
#include <inet/UDPEndPoint.h>
#include <lib/dnssd/ServiceNaming.h>
#include <lib/dnssd/minimal_mdns/AddressPolicy.h>
#include <lib/dnssd/minimal_mdns/AddressPolicy_DefaultImpl.h>
#include <lib/dnssd/minimal_mdns/QueryBuilder.h>
#include <lib/dnssd/minimal_mdns/ResponseSender.h>
#include <lib/dnssd/minimal_mdns/Server.h>
Expand All @@ -36,7 +38,6 @@
#include <platform/CHIPDeviceLayer.h>
#include <system/SystemPacketBuffer.h>

#include "AllInterfaceListener.h"
#include "PacketReporter.h"

using namespace chip;
Expand Down Expand Up @@ -198,6 +199,8 @@ int main(int argc, char ** args)
return 1;
}

mdns::Minimal::SetDefaultAddressPolicy();

printf("Running on port %d using %s...\n", gOptions.listenPort, gOptions.enableIpV4 ? "IPv4 AND IPv6" : "IPv6 ONLY");

mdns::Minimal::QueryResponder<16 /* maxRecords */> queryResponder;
Expand Down Expand Up @@ -265,9 +268,9 @@ int main(int argc, char ** args)
gMdnsServer.SetDelegate(&delegate);

{
MdnsExample::AllInterfaces allInterfaces(gOptions.enableIpV4);
auto endpoints = mdns::Minimal::GetAddressPolicy()->GetListenEndpoints();

if (gMdnsServer.Listen(DeviceLayer::UDPEndPointManager(), &allInterfaces, gOptions.listenPort) != CHIP_NO_ERROR)
if (gMdnsServer.Listen(DeviceLayer::UDPEndPointManager(), endpoints.get(), gOptions.listenPort) != CHIP_NO_ERROR)
{
printf("Server failed to listen on all interfaces\n");
return 1;
Expand Down
Loading

0 comments on commit 102a8bb

Please sign in to comment.