Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Kevin Schoedel | f7c68a3 | 2021-06-18 15:39:06 -0400 | [diff] [blame] | 3 | * Copyright (c) 2020-2021 Project CHIP Authors |
Rob Walker | e812e67 | 2020-03-31 17:51:57 -0700 | [diff] [blame] | 4 | * Copyright (c) 2015-2017 Nest Labs, Inc. |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 5 | * |
| 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 Litvin | 1873e8c | 2020-10-12 10:52:26 -0400 | [diff] [blame] | 25 | #pragma once |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 26 | |
Pankaj Garg | d4aa1df | 2020-03-24 08:56:20 -0700 | [diff] [blame] | 27 | #include <inet/InetConfig.h> |
Martin Turon | 7802654 | 2020-03-17 17:24:59 -0700 | [diff] [blame] | 28 | |
Kevin Schoedel | 579935b | 2021-10-05 19:18:07 -0400 | [diff] [blame] | 29 | #include <inet/IPAddress.h> |
Zang MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 30 | #include <lib/support/DLLUtil.h> |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 31 | |
Kevin Schoedel | f7c68a3 | 2021-06-18 15:39:06 -0400 | [diff] [blame] | 32 | #if CHIP_SYSTEM_CONFIG_USE_SOCKETS |
Kevin Schoedel | 82cf1b1 | 2021-08-23 09:20:13 -0400 | [diff] [blame] | 33 | #include <system/SocketEvents.h> |
Kevin Schoedel | f7c68a3 | 2021-06-18 15:39:06 -0400 | [diff] [blame] | 34 | #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS |
| 35 | |
Vivien Nicolas | 7962596 | 2020-06-19 00:28:26 +0200 | [diff] [blame] | 36 | #if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK |
| 37 | #include <Network/Network.h> |
| 38 | #endif // CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK |
| 39 | |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 40 | #if CHIP_SYSTEM_CONFIG_USE_LWIP |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 41 | struct udp_pcb; |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 42 | struct tcp_pcb; |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 43 | #endif // CHIP_SYSTEM_CONFIG_USE_LWIP |
| 44 | |
| 45 | namespace chip { |
| 46 | namespace Inet { |
| 47 | |
Kevin Schoedel | 05200d6 | 2021-11-02 10:52:45 -0400 | [diff] [blame] | 48 | class InetLayer; |
| 49 | |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 50 | /** |
Kevin Schoedel | 579935b | 2021-10-05 19:18:07 -0400 | [diff] [blame] | 51 | * Basis of internet transport endpoint classes. |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 52 | */ |
Zang MingJie | 5c73522 | 2021-11-09 02:26:48 +0800 | [diff] [blame^] | 53 | class DLL_EXPORT EndPointBasis |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 54 | { |
Kevin Schoedel | 05200d6 | 2021-11-02 10:52:45 -0400 | [diff] [blame] | 55 | public: |
| 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 MingJie | 5c73522 | 2021-11-09 02:26:48 +0800 | [diff] [blame^] | 70 | void * AppState; |
| 71 | |
Kevin Schoedel | 05200d6 | 2021-11-02 10:52:45 -0400 | [diff] [blame] | 72 | private: |
| 73 | InetLayer * mInetLayer; /**< Pointer to the InetLayer object that owns this object. */ |
| 74 | |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 75 | protected: |
Kevin Schoedel | 579935b | 2021-10-05 19:18:07 -0400 | [diff] [blame] | 76 | void InitEndPointBasis(InetLayer & aInetLayer, void * aAppState = nullptr); |
| 77 | |
Vivien Nicolas | 7962596 | 2020-06-19 00:28:26 +0200 | [diff] [blame] | 78 | #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 Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 83 | #if CHIP_SYSTEM_CONFIG_USE_SOCKETS |
Kevin Schoedel | 579935b | 2021-10-05 19:18:07 -0400 | [diff] [blame] | 84 | static constexpr int kInvalidSocketFd = -1; |
Kevin Schoedel | 82cf1b1 | 2021-08-23 09:20:13 -0400 | [diff] [blame] | 85 | int mSocket; /**< Encapsulated socket descriptor. */ |
Kevin Schoedel | f7c68a3 | 2021-06-18 15:39:06 -0400 | [diff] [blame] | 86 | IPAddressType mAddrType; /**< Protocol family, i.e. IPv4 or IPv6. */ |
Kevin Schoedel | 82cf1b1 | 2021-08-23 09:20:13 -0400 | [diff] [blame] | 87 | System::SocketWatchToken mWatch; /**< Socket event watcher */ |
Kevin Schoedel | f7c68a3 | 2021-06-18 15:39:06 -0400 | [diff] [blame] | 88 | #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 89 | |
| 90 | #if CHIP_SYSTEM_CONFIG_USE_LWIP |
| 91 | /** Encapsulated LwIP protocol control block */ |
| 92 | union |
| 93 | { |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 94 | #if INET_CONFIG_ENABLE_UDP_ENDPOINT |
Martin Turon | cd17ad4 | 2020-03-18 20:03:53 -0700 | [diff] [blame] | 95 | udp_pcb * mUDP; /**< User datagram protocol (UDP) control */ |
| 96 | #endif // INET_CONFIG_ENABLE_UDP_ENDPOINT |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 97 | #if INET_CONFIG_ENABLE_TCP_ENDPOINT |
Martin Turon | cd17ad4 | 2020-03-18 20:03:53 -0700 | [diff] [blame] | 98 | tcp_pcb * mTCP; /**< Transmission control protocol (TCP) control */ |
| 99 | #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
Kevin Schoedel | 579935b | 2021-10-05 19:18:07 -0400 | [diff] [blame] | 102 | enum class LwIPEndPointType : uint8_t |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 103 | { |
Kevin Schoedel | 579935b | 2021-10-05 19:18:07 -0400 | [diff] [blame] | 104 | Unknown = 0, |
| 105 | Raw = 1, |
| 106 | UDP = 2, |
| 107 | UCP = 3, |
| 108 | TCP = 4 |
| 109 | } mLwIPEndPointType; |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 110 | #endif // CHIP_SYSTEM_CONFIG_USE_LWIP |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
Pankaj Garg | 2efbcce | 2020-03-10 12:05:08 -0700 | [diff] [blame] | 113 | } // namespace Inet |
| 114 | } // namespace chip |