Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / minimal-mdns / AllInterfaceListener.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include <mdns/minimal/Server.h>
19
20 namespace MdnsExample {
21
22 /// Returns
23 ///   - NULL interface for IPv4
24 ///   - all interfaces for IPv6
25 class AllInterfaces : public mdns::Minimal::ListenIterator
26 {
27 public:
28     AllInterfaces(bool enableIpV4) : mState(enableIpV4 ? State::kIpV4 : State::kIpV6)
29     {
30         if (!enableIpV4)
31         {
32             SkipToFirstValidInterface();
33         }
34     }
35
36     bool Next(chip::Inet::InterfaceId * id, chip::Inet::IPAddressType * type) override
37     {
38         if (mState == State::kIpV4)
39         {
40             *id    = INET_NULL_INTERFACEID;
41             *type  = chip::Inet::kIPAddressType_IPv4;
42             mState = State::kIpV6;
43
44             SkipToFirstValidInterface();
45
46             return true;
47         }
48
49         if (!mIterator.HasCurrent())
50         {
51             return false;
52         }
53
54         *id   = mIterator.GetInterfaceId();
55         *type = chip::Inet::kIPAddressType_IPv6;
56
57         for (mIterator.Next(); SkipCurrentInterface(); mIterator.Next())
58         {
59         }
60         return true;
61     }
62
63 private:
64     enum class State
65     {
66         kIpV4,
67         kIpV6,
68     };
69     State mState;
70     chip::Inet::InterfaceIterator mIterator;
71
72     void SkipToFirstValidInterface()
73     {
74         if (SkipCurrentInterface())
75         {
76             while (mIterator.Next())
77             {
78                 if (!SkipCurrentInterface())
79                 {
80                     break;
81                 }
82             }
83         }
84     }
85
86     bool SkipCurrentInterface()
87     {
88         if (!mIterator.HasCurrent())
89         {
90             return false; // nothing to try.
91         }
92
93         if (!mIterator.IsUp() || !mIterator.SupportsMulticast())
94         {
95             return true; // not a usable interface
96         }
97
98         char name[chip::Inet::InterfaceIterator::kMaxIfNameLength];
99         if (mIterator.GetInterfaceName(name, sizeof(name)) != CHIP_NO_ERROR)
100         {
101             printf("!!!! FAILED TO GET INTERFACE NAME\n");
102             return true;
103         }
104
105         if (strncmp(name, "lo", 2) == 0)
106         {
107             printf("Skipping interface '%s' (assume local loopback)\n", name);
108             return true;
109         }
110
111         printf("Usable interface: %s (%d)\n", name, static_cast<int>(mIterator.GetInterfaceId()));
112
113         return false;
114     }
115 };
116
117 } // namespace MdnsExample