Line data Source code
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 : /**
19 : * @brief
20 : * File contains definitions on how a connection to a peer can be defined.
21 : *
22 : */
23 :
24 : #pragma once
25 :
26 : #include <inet/IPAddress.h>
27 : #include <inet/InetInterface.h>
28 : #include <lib/core/CHIPConfig.h>
29 : #include <lib/core/DataModelTypes.h>
30 : #include <lib/support/CHIPMemString.h>
31 :
32 : namespace chip {
33 : namespace Transport {
34 :
35 : /**
36 : * Communication path defines how two peers communicate.
37 : *
38 : * When a peer contacts another peer, it defines how the peers communicate.
39 : *
40 : * Once communication between two peers is established, the same transport
41 : * path should be used: a peer contacting another peer over UDP will receive
42 : * messages back over UDP. A communication channel established over TCP
43 : * will keep the same TCP channel.
44 : *
45 : */
46 :
47 : /**
48 : * Here we specified Type to be uint8_t, so the PeerAddress can be serialized easily.
49 : */
50 : enum class Type : uint8_t
51 : {
52 : kUndefined,
53 : kUdp,
54 : kBle,
55 : kTcp,
56 : kWiFiPAF,
57 : kNfc,
58 : kThreadMeshcop,
59 : kLast = kThreadMeshcop, // This is not an actual transport type, it just refers to the last transport type
60 : };
61 :
62 : /**
63 : * Describes how a peer on a CHIP network can be addressed.
64 : */
65 : class PeerAddress
66 : {
67 : public:
68 922776 : constexpr PeerAddress() : mTransportType(Type::kUndefined), mId{ .mRemoteId = kUndefinedNodeId } {}
69 1035 : constexpr PeerAddress(const Inet::IPAddress & addr, Type type) :
70 1035 : mIPAddress(addr), mTransportType(type), mId{ .mRemoteId = kUndefinedNodeId }
71 1035 : {}
72 5780 : constexpr PeerAddress(Type type) : mTransportType(type), mId{ .mRemoteId = kUndefinedNodeId } {}
73 0 : constexpr PeerAddress(Type type, NodeId remoteId) : mTransportType(type), mId{ .mRemoteId = remoteId } {}
74 :
75 0 : constexpr PeerAddress(PeerAddress &&) = default;
76 92189 : constexpr PeerAddress(const PeerAddress &) = default;
77 18383 : PeerAddress & operator=(const PeerAddress &) = default;
78 235 : PeerAddress & operator=(PeerAddress &&) = default;
79 :
80 15249 : const Inet::IPAddress & GetIPAddress() const { return mIPAddress; }
81 1 : PeerAddress & SetIPAddress(const Inet::IPAddress & addr)
82 : {
83 1 : mIPAddress = addr;
84 1 : return *this;
85 : }
86 :
87 0 : NodeId GetRemoteId() const { return mId.mRemoteId; }
88 :
89 : // NB: 0xFFFF is not allowed for NFC ShortId.
90 : uint16_t GetNFCShortId() const { return mId.mNFCShortId; }
91 :
92 134413 : Type GetTransportType() const { return mTransportType; }
93 0 : PeerAddress & SetTransportType(Type type)
94 : {
95 0 : mTransportType = type;
96 0 : return *this;
97 : }
98 :
99 15919 : uint16_t GetPort() const { return mPort; }
100 16897 : PeerAddress & SetPort(uint16_t port)
101 : {
102 16897 : mPort = port;
103 16897 : return *this;
104 : }
105 :
106 170 : Inet::InterfaceId GetInterface() const { return mInterface; }
107 15173 : PeerAddress & SetInterface(Inet::InterfaceId interface)
108 : {
109 15173 : mInterface = interface;
110 15173 : return *this;
111 : }
112 :
113 : bool IsInitialized() const { return mTransportType != Type::kUndefined; }
114 :
115 1 : bool IsMulticast() { return Type::kUdp == mTransportType && mIPAddress.IsIPv6Multicast(); }
116 :
117 : bool operator==(const PeerAddress & other) const;
118 14895 : bool operator!=(const PeerAddress & other) const { return !(*this == other); }
119 :
120 : /// Maximum size of the string outputes by ToString. Format is of the form:
121 : /// "UDP:<ip>:<port>"
122 : static constexpr size_t kMaxToStringSize = 3 // type: UDP/TCP/BLE
123 : + 1 // splitter :
124 : + 2 // brackets around address
125 : + Inet::IPAddress::kMaxStringLength // address
126 : + 1 // splitter %
127 : + Inet::InterfaceId::kMaxIfNameLength // interface
128 : + 1 // splitter :
129 : + 5 // port: 16 bit interger
130 : + 1; // NullTerminator
131 :
132 : template <size_t N>
133 15263 : inline void ToString(char (&buf)[N]) const
134 : {
135 15263 : ToString(buf, N);
136 15263 : }
137 :
138 15263 : void ToString(char * buf, size_t bufSize) const
139 : {
140 : char ip_addr[Inet::IPAddress::kMaxStringLength];
141 :
142 15263 : char interface[Inet::InterfaceId::kMaxIfNameLength + 1] = {}; // +1 to prepend '%'
143 15263 : if (mInterface.IsPresent())
144 : {
145 0 : interface[0] = '%';
146 0 : interface[1] = 0;
147 0 : CHIP_ERROR err = mInterface.GetInterfaceName(interface + 1, sizeof(interface) - 1);
148 0 : if (err != CHIP_NO_ERROR)
149 : {
150 0 : Platform::CopyString(interface, sizeof(interface), "%(err)");
151 : }
152 : }
153 :
154 15263 : switch (mTransportType)
155 : {
156 47 : case Type::kUndefined:
157 47 : snprintf(buf, bufSize, "UNDEFINED");
158 47 : break;
159 15105 : case Type::kUdp:
160 15105 : mIPAddress.ToString(ip_addr);
161 : #if INET_CONFIG_ENABLE_IPV4
162 15105 : if (mIPAddress.IsIPv4())
163 0 : snprintf(buf, bufSize, "UDP:%s%s:%d", ip_addr, interface, mPort);
164 : else
165 : #endif
166 15105 : snprintf(buf, bufSize, "UDP:[%s%s]:%d", ip_addr, interface, mPort);
167 15105 : break;
168 110 : case Type::kTcp:
169 110 : mIPAddress.ToString(ip_addr);
170 : #if INET_CONFIG_ENABLE_IPV4
171 110 : if (mIPAddress.IsIPv4())
172 32 : snprintf(buf, bufSize, "TCP:%s%s:%d", ip_addr, interface, mPort);
173 : else
174 : #endif
175 78 : snprintf(buf, bufSize, "TCP:[%s%s]:%d", ip_addr, interface, mPort);
176 110 : break;
177 0 : case Type::kWiFiPAF:
178 0 : snprintf(buf, bufSize, "Wi-Fi PAF");
179 0 : break;
180 1 : case Type::kBle:
181 : // Note that BLE does not currently use any specific address.
182 1 : snprintf(buf, bufSize, "BLE");
183 1 : break;
184 0 : case Type::kNfc:
185 0 : snprintf(buf, bufSize, "NFC:%d", mId.mNFCShortId);
186 0 : break;
187 0 : case Type::kThreadMeshcop:
188 0 : mIPAddress.ToString(ip_addr);
189 : #if INET_CONFIG_ENABLE_IPV4
190 0 : if (mIPAddress.IsIPv4())
191 0 : snprintf(buf, bufSize, "ThreadMeshcop:%s:%d", ip_addr, mPort);
192 : else
193 : #endif
194 0 : snprintf(buf, bufSize, "ThreadMeshcop:[%s]:%d", ip_addr, mPort);
195 0 : break;
196 0 : default:
197 0 : snprintf(buf, bufSize, "ERROR");
198 0 : break;
199 : }
200 15263 : }
201 :
202 : /****** Factory methods for convenience ******/
203 :
204 1248 : static constexpr PeerAddress Uninitialized() { return PeerAddress(Type::kUndefined); }
205 :
206 1 : static constexpr PeerAddress BLE() { return PeerAddress(Type::kBle); }
207 :
208 : // NB: 0xFFFF is not allowed for NFC ShortId.
209 : static constexpr PeerAddress NFC() { return PeerAddress(kUndefinedNFCShortId()); }
210 : static constexpr PeerAddress NFC(const uint16_t shortId) { return PeerAddress(shortId); }
211 :
212 : static PeerAddress ThreadMeshcop(const Inet::IPAddress & addr, uint16_t port)
213 : {
214 : return PeerAddress(Type::kThreadMeshcop).SetIPAddress(addr).SetPort(port);
215 : }
216 :
217 962 : static PeerAddress UDP(const Inet::IPAddress & addr) { return PeerAddress(addr, Type::kUdp); }
218 881 : static PeerAddress UDP(const Inet::IPAddress & addr, uint16_t port) { return UDP(addr).SetPort(port); }
219 :
220 : /**
221 : * Parses a PeerAddress from the given IP address string with UDP type. For example,
222 : * "192.168.1.4", "fe80::2", "fe80::1%wlan0". Notably this will also include the network scope
223 : * ID in either index or name form (e.g. %wlan0, %14).
224 : */
225 : static PeerAddress UDP(char * addrStr, uint16_t port) { return PeerAddress::FromString(addrStr, port, Type::kUdp); }
226 61 : static PeerAddress UDP(const Inet::IPAddress & addr, uint16_t port, Inet::InterfaceId interface)
227 : {
228 61 : return UDP(addr).SetPort(port).SetInterface(interface);
229 : }
230 73 : static PeerAddress TCP(const Inet::IPAddress & addr) { return PeerAddress(addr, Type::kTcp); }
231 36 : static PeerAddress TCP(const Inet::IPAddress & addr, uint16_t port) { return TCP(addr).SetPort(port); }
232 :
233 : /**
234 : * Parses a PeerAddress from the given IP address string with TCP type. For example,
235 : * "192.168.1.4", "fe80::2", "fe80::1%wlan0". Notably this will also include the network scope
236 : * ID in either index or name form (e.g. %wlan0, %14).
237 : */
238 : static PeerAddress TCP(char * addrStr, uint16_t port) { return PeerAddress::FromString(addrStr, port, Type::kTcp); }
239 37 : static PeerAddress TCP(const Inet::IPAddress & addr, uint16_t port, Inet::InterfaceId interface)
240 : {
241 37 : return TCP(addr).SetPort(port).SetInterface(interface);
242 : }
243 :
244 0 : static constexpr PeerAddress WiFiPAF(NodeId remoteId) { return PeerAddress(Type::kWiFiPAF, remoteId); }
245 :
246 7 : static PeerAddress Multicast(chip::FabricId fabric, chip::GroupId group)
247 : {
248 7 : constexpr uint8_t scope = 0x05; // Site-Local
249 7 : constexpr uint8_t prefixLength = 0x40; // 64-bit long network prefix field
250 : // The network prefix portion of the Multicast Address is the 64-bit bitstring formed by concatenating:
251 : // * 0xFD to designate a locally assigned ULA prefix
252 : // * The upper 56-bits of the Fabric ID for the network in big-endian order
253 7 : const uint64_t prefix = 0xfd00000000000000 | ((fabric >> 8) & 0x00ffffffffffffff);
254 : // The 32-bit group identifier portion of the Multicast Address is the 32-bits formed by:
255 : // * The lower 8-bits of the Fabric ID
256 : // * 0x00
257 : // * The 16-bits Group Identifier in big-endian order
258 7 : uint32_t groupId = static_cast<uint32_t>((fabric << 24) & 0xff000000) | group;
259 7 : return UDP(Inet::IPAddress::MakeIPv6PrefixMulticast(scope, prefixLength, prefix, groupId));
260 : }
261 :
262 0 : static PeerAddress Groupcast()
263 : {
264 0 : constexpr uint8_t scope = 0x05; // Site-Local
265 0 : constexpr uint8_t prefixLength = 0x40; // 64-bit long network prefix field
266 : // IANA assigned address
267 0 : return UDP(Inet::IPAddress::MakeIPv6PrefixMulticast(scope, prefixLength, 0xff05000000000000, 0xfa));
268 : }
269 :
270 : private:
271 : constexpr PeerAddress(uint16_t shortId) : mTransportType(Type::kNfc), mId{ .mNFCShortId = shortId } {}
272 :
273 : static PeerAddress FromString(char * addrStr, uint16_t port, Type type)
274 : {
275 : Inet::IPAddress addr;
276 : Inet::InterfaceId interfaceId;
277 : Inet::IPAddress::FromString(addrStr, addr, interfaceId);
278 : return PeerAddress(addr, type).SetPort(port).SetInterface(interfaceId);
279 : }
280 :
281 : static constexpr uint16_t kUndefinedNFCShortId() { return 0xFFFF; }
282 :
283 : Inet::IPAddress mIPAddress = {};
284 : Type mTransportType = Type::kUndefined;
285 : uint16_t mPort = CHIP_PORT; ///< Relevant for UDP data sending.
286 : Inet::InterfaceId mInterface = Inet::InterfaceId::Null();
287 :
288 : union Id
289 : {
290 : NodeId mRemoteId;
291 : uint16_t mNFCShortId;
292 : } mId;
293 : };
294 :
295 : } // namespace Transport
296 : } // namespace chip
|