blob: 0a68bf5c29924a508461595e36926e99614351d8 [file] [log] [blame]
Pankaj Garg2efbcce2020-03-10 12:05:08 -07001/*
2 *
Kevin Schoedelf7c68a32021-06-18 15:39:06 -04003 * Copyright (c) 2020-2021 Project CHIP Authors
Rob Walkere812e672020-03-31 17:51:57 -07004 * Copyright (c) 2015-2017 Nest Labs, Inc.
Pankaj Garg2efbcce2020-03-10 12:05:08 -07005 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 */
18
19/**
20 * @file
21 * This file contains the basis class for all the various transport
22 * endpoint classes in the Inet layer, i.e. TCP, UDP, Raw and Tun.
23 */
24
Andrei Litvin1873e8c2020-10-12 10:52:26 -040025#pragma once
Pankaj Garg2efbcce2020-03-10 12:05:08 -070026
Pankaj Gargd4aa1df2020-03-24 08:56:20 -070027#include <inet/InetConfig.h>
Martin Turon78026542020-03-17 17:24:59 -070028
Kevin Schoedel579935b2021-10-05 19:18:07 -040029#include <inet/IPAddress.h>
Zang MingJie53dd5832021-09-03 03:05:16 +080030#include <lib/support/DLLUtil.h>
Pankaj Garg2efbcce2020-03-10 12:05:08 -070031
Kevin Schoedelf7c68a32021-06-18 15:39:06 -040032#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
Kevin Schoedel82cf1b12021-08-23 09:20:13 -040033#include <system/SocketEvents.h>
Kevin Schoedelf7c68a32021-06-18 15:39:06 -040034#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
35
Vivien Nicolas79625962020-06-19 00:28:26 +020036#if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
37#include <Network/Network.h>
38#endif // CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
39
Pankaj Garg2efbcce2020-03-10 12:05:08 -070040#if CHIP_SYSTEM_CONFIG_USE_LWIP
Pankaj Garg2efbcce2020-03-10 12:05:08 -070041struct udp_pcb;
Pankaj Garg2efbcce2020-03-10 12:05:08 -070042struct tcp_pcb;
Pankaj Garg2efbcce2020-03-10 12:05:08 -070043#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
44
45namespace chip {
46namespace Inet {
47
Kevin Schoedel05200d62021-11-02 10:52:45 -040048class InetLayer;
49
Pankaj Garg2efbcce2020-03-10 12:05:08 -070050/**
Kevin Schoedel579935b2021-10-05 19:18:07 -040051 * Basis of internet transport endpoint classes.
Pankaj Garg2efbcce2020-03-10 12:05:08 -070052 */
Zang MingJie5c735222021-11-09 02:26:48 +080053class DLL_EXPORT EndPointBasis
Pankaj Garg2efbcce2020-03-10 12:05:08 -070054{
Kevin Schoedel05200d62021-11-02 10:52:45 -040055public:
56 /**
57 * Returns a reference to the Inet layer object that owns this basis object.
58 */
59 InetLayer & Layer() const { return *mInetLayer; }
60
61 /**
62 * Returns \c true if the basis object was obtained by the specified Inet layer instance.
63 *
64 * @note
65 * Does not check whether the object is actually obtained by the system layer instance associated with the Inet layer
66 * instance. It merely tests whether \c aInetLayer is the Inet layer instance that was provided to \c InitInetLayerBasis.
67 */
68 bool IsCreatedByInetLayer(const InetLayer & aInetLayer) const { return mInetLayer == &aInetLayer; }
69
Zang MingJie5c735222021-11-09 02:26:48 +080070 void * AppState;
71
Kevin Schoedel05200d62021-11-02 10:52:45 -040072private:
73 InetLayer * mInetLayer; /**< Pointer to the InetLayer object that owns this object. */
74
Pankaj Garg2efbcce2020-03-10 12:05:08 -070075protected:
Kevin Schoedel579935b2021-10-05 19:18:07 -040076 void InitEndPointBasis(InetLayer & aInetLayer, void * aAppState = nullptr);
77
Vivien Nicolas79625962020-06-19 00:28:26 +020078#if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
79 nw_parameters_t mParameters;
80 IPAddressType mAddrType; /**< Protocol family, i.e. IPv4 or IPv6. */
81#endif
82
Pankaj Garg2efbcce2020-03-10 12:05:08 -070083#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
Kevin Schoedel579935b2021-10-05 19:18:07 -040084 static constexpr int kInvalidSocketFd = -1;
Kevin Schoedel82cf1b12021-08-23 09:20:13 -040085 int mSocket; /**< Encapsulated socket descriptor. */
Kevin Schoedelf7c68a32021-06-18 15:39:06 -040086 IPAddressType mAddrType; /**< Protocol family, i.e. IPv4 or IPv6. */
Kevin Schoedel82cf1b12021-08-23 09:20:13 -040087 System::SocketWatchToken mWatch; /**< Socket event watcher */
Kevin Schoedelf7c68a32021-06-18 15:39:06 -040088#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
Pankaj Garg2efbcce2020-03-10 12:05:08 -070089
90#if CHIP_SYSTEM_CONFIG_USE_LWIP
91 /** Encapsulated LwIP protocol control block */
92 union
93 {
Pankaj Garg2efbcce2020-03-10 12:05:08 -070094#if INET_CONFIG_ENABLE_UDP_ENDPOINT
Martin Turoncd17ad42020-03-18 20:03:53 -070095 udp_pcb * mUDP; /**< User datagram protocol (UDP) control */
96#endif // INET_CONFIG_ENABLE_UDP_ENDPOINT
Pankaj Garg2efbcce2020-03-10 12:05:08 -070097#if INET_CONFIG_ENABLE_TCP_ENDPOINT
Martin Turoncd17ad42020-03-18 20:03:53 -070098 tcp_pcb * mTCP; /**< Transmission control protocol (TCP) control */
99#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700100 };
101
Kevin Schoedel579935b2021-10-05 19:18:07 -0400102 enum class LwIPEndPointType : uint8_t
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700103 {
Kevin Schoedel579935b2021-10-05 19:18:07 -0400104 Unknown = 0,
105 Raw = 1,
106 UDP = 2,
107 UCP = 3,
108 TCP = 4
109 } mLwIPEndPointType;
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700110#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700111};
112
Pankaj Garg2efbcce2020-03-10 12:05:08 -0700113} // namespace Inet
114} // namespace chip