3 * Copyright (c) 2020 Project CHIP Authors
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * This file defines the CHIP Connection object that maintains a UDP connection.
22 * It binds to any avaiable local addr and port and begins listening.
30 #include <core/CHIPCore.h>
31 #include <inet/IPAddress.h>
32 #include <inet/IPEndPointBasis.h>
33 #include <inet/InetInterface.h>
34 #include <transport/raw/Base.h>
39 /** Defines listening parameters for setting up a UDP transport */
40 class UdpListenParameters
43 explicit UdpListenParameters(Inet::InetLayer * layer) : mLayer(layer) {}
44 UdpListenParameters(const UdpListenParameters &) = default;
45 UdpListenParameters(UdpListenParameters &&) = default;
47 Inet::InetLayer * GetInetLayer() { return mLayer; }
49 Inet::IPAddressType GetAddressType() const { return mAddressType; }
50 UdpListenParameters & SetAddressType(Inet::IPAddressType type)
57 uint16_t GetListenPort() const { return mListenPort; }
58 UdpListenParameters & SetListenPort(uint16_t port)
65 Inet::InterfaceId GetInterfaceId() const { return mInterfaceId; }
66 UdpListenParameters & SetInterfaceId(Inet::InterfaceId id)
74 Inet::InetLayer * mLayer = nullptr; ///< Associated inet layer
75 Inet::IPAddressType mAddressType = Inet::kIPAddressType_IPv6; ///< type of listening socket
76 uint16_t mListenPort = CHIP_PORT; ///< UDP listen port
77 Inet::InterfaceId mInterfaceId = INET_NULL_INTERFACEID; ///< Interface to listen on
80 /** Implements a transport using UDP. */
81 class DLL_EXPORT UDP : public Base
84 * The State of the UDP connection
89 kNotReady = 0, /**< State before initialization. */
90 kInitialized = 1, /**< State after class is listening and ready. */
97 * Initialize a UDP transport on a given port.
99 * @param params UDP configuration parameters for this transport
102 * Generally send and receive ports should be the same and equal to CHIP_PORT.
103 * The class allows separate definitions to allow local execution of several
106 CHIP_ERROR Init(UdpListenParameters & params);
109 * Close the open endpoint without destroying the object
111 void Close() override;
113 CHIP_ERROR SendMessage(const PacketHeader & header, const Transport::PeerAddress & address,
114 System::PacketBufferHandle msgBuf) override;
116 bool CanSendToPeer(const Transport::PeerAddress & address) override
118 return (mState == State::kInitialized) && (address.GetTransportType() == Type::kUdp) &&
119 (address.GetIPAddress().Type() == mUDPEndpointType);
123 // UDP message receive handler.
124 static void OnUdpReceive(Inet::IPEndPointBasis * endPoint, System::PacketBufferHandle buffer,
125 const Inet::IPPacketInfo * pktInfo);
127 Inet::UDPEndPoint * mUDPEndPoint = nullptr; ///< UDP socket used by the transport
128 Inet::IPAddressType mUDPEndpointType = Inet::IPAddressType::kIPAddressType_Unknown; ///< Socket listening type
129 State mState = State::kNotReady; ///< State of the UDP transport
132 } // namespace Transport