Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020-2025 Project CHIP Authors
4 : * Copyright (c) 2018 Google LLC
5 : * Copyright (c) 2013-2017 Nest Labs, Inc.
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : /**
21 : * This file declares an implementation of Inet::UDPEndPoint using sockets.
22 : */
23 :
24 : #pragma once
25 :
26 : #include <inet/EndPointStateSockets.h>
27 : #include <inet/UDPEndPoint.h>
28 :
29 : namespace chip {
30 : namespace Inet {
31 :
32 : class UDPEndPointImplSockets : public UDPEndPoint, public EndPointStateSockets
33 : {
34 : public:
35 81 : UDPEndPointImplSockets(EndPointManager<UDPEndPoint> & endPointManager) :
36 81 : UDPEndPoint(endPointManager), mBoundIntfId(InterfaceId::Null())
37 81 : {}
38 :
39 : // UDPEndPoint overrides.
40 : CHIP_ERROR SetMulticastLoopback(IPVersion aIPVersion, bool aLoopback) override;
41 : InterfaceId GetBoundInterface() const override;
42 : uint16_t GetBoundPort() const override;
43 :
44 : private:
45 : // UDPEndPoint overrides.
46 : #if INET_CONFIG_ENABLE_IPV4
47 : CHIP_ERROR IPv4JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) override;
48 : #endif // INET_CONFIG_ENABLE_IPV4
49 : CHIP_ERROR IPv6JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) override;
50 : CHIP_ERROR BindImpl(IPAddressType addressType, const IPAddress & address, uint16_t port, InterfaceId interfaceId) override;
51 : CHIP_ERROR BindInterfaceImpl(IPAddressType addressType, InterfaceId interfaceId) override;
52 : CHIP_ERROR ListenImpl() override;
53 : CHIP_ERROR SendMsgImpl(const IPPacketInfo * pktInfo, chip::System::PacketBufferHandle && msg) override;
54 : void CloseImpl() override;
55 :
56 : CHIP_ERROR GetSocket(IPAddressType addressType);
57 : void HandlePendingIO(System::SocketEvents events);
58 : static void HandlePendingIO(System::SocketEvents events, intptr_t data);
59 :
60 : InterfaceId mBoundIntfId;
61 : uint16_t mBoundPort;
62 :
63 : #if CHIP_SYSTEM_CONFIG_USE_PLATFORM_MULTICAST_API
64 : public:
65 : enum class MulticastOperation
66 : {
67 : kJoin,
68 : kLeave
69 : };
70 :
71 : using MulticastGroupHandler = CHIP_ERROR (*)(InterfaceId, const IPAddress &, MulticastOperation operation);
72 :
73 : static void SetMulticastGroupHandler(MulticastGroupHandler handler) { sMulticastGroupHandler = handler; }
74 :
75 : private:
76 : static MulticastGroupHandler sMulticastGroupHandler;
77 : #endif // CHIP_SYSTEM_CONFIG_USE_PLATFORM_MULTICAST_API
78 : };
79 :
80 : using UDPEndPointImpl = UDPEndPointImplSockets;
81 :
82 : } // namespace Inet
83 : } // namespace chip
|