3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <mdns/minimal/Server.h>
20 namespace MdnsExample {
23 /// - NULL interface for IPv4
24 /// - all interfaces for IPv6
25 class AllInterfaces : public mdns::Minimal::ListenIterator
28 AllInterfaces(bool enableIpV4) : mState(enableIpV4 ? State::kIpV4 : State::kIpV6)
32 SkipToFirstValidInterface();
36 bool Next(chip::Inet::InterfaceId * id, chip::Inet::IPAddressType * type) override
38 if (mState == State::kIpV4)
40 *id = INET_NULL_INTERFACEID;
41 *type = chip::Inet::kIPAddressType_IPv4;
42 mState = State::kIpV6;
44 SkipToFirstValidInterface();
49 if (!mIterator.HasCurrent())
54 *id = mIterator.GetInterfaceId();
55 *type = chip::Inet::kIPAddressType_IPv6;
57 for (mIterator.Next(); SkipCurrentInterface(); mIterator.Next())
70 chip::Inet::InterfaceIterator mIterator;
72 void SkipToFirstValidInterface()
74 if (SkipCurrentInterface())
76 while (mIterator.Next())
78 if (!SkipCurrentInterface())
86 bool SkipCurrentInterface()
88 if (!mIterator.HasCurrent())
90 return false; // nothing to try.
93 if (!mIterator.IsUp() || !mIterator.SupportsMulticast())
95 return true; // not a usable interface
98 char name[chip::Inet::InterfaceIterator::kMaxIfNameLength];
99 if (mIterator.GetInterfaceName(name, sizeof(name)) != CHIP_NO_ERROR)
101 printf("!!!! FAILED TO GET INTERFACE NAME\n");
105 if (strncmp(name, "lo", 2) == 0)
107 printf("Skipping interface '%s' (assume local loopback)\n", name);
111 printf("Usable interface: %s (%d)\n", name, static_cast<int>(mIterator.GetInterfaceId()));
117 } // namespace MdnsExample