• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_IPC_DBINDER_DATABUS_SOCKET_LISTENER_H
17 #define OHOS_IPC_DBINDER_DATABUS_SOCKET_LISTENER_H
18 
19 #include <list>
20 #include <unordered_map>
21 
22 #include "dbinder_softbus_client.h"
23 #include "ipc_types.h"
24 #include "singleton.h"
25 
26 namespace OHOS {
27 static constexpr QosTV QOS_TV[] = {
28     { .qos = QOS_TYPE_MIN_BW, .value = RPC_QOS_MIN_BW },
29     { .qos = QOS_TYPE_MAX_LATENCY, .value = RPC_QOS_MAX_LATENCY },
30     { .qos = QOS_TYPE_MIN_LATENCY, .value = RPC_QOS_MIN_LATENCY }
31 };
32 static constexpr uint32_t QOS_COUNT = static_cast<uint32_t>(sizeof(QOS_TV) / sizeof(QosTV));
33 
34 static const std::string DBINDER_PKG_NAME = "DBinderBus";
35 static const std::string DBINDER_SOCKET_NAME_PREFIX = "DBinder";
36 static const std::string DBINDER_UID_PID_SEPARATOR = "_";
37 
38 class DBinderSocketInfo {
39 public:
40     DBinderSocketInfo(const std::string &ownName, const std::string &peerName, const std::string &networkId);
41     DBinderSocketInfo() = default;
42     virtual ~DBinderSocketInfo() = default;
43     std::string GetOwnName() const;
44     std::string GetPeerName() const;
45     std::string GetNetworkId() const;
46 
47     bool operator==(const DBinderSocketInfo &info) const
48     {
49         return (info.GetOwnName().compare(ownName_) == 0 && info.GetPeerName().compare(peerName_) == 0 &&
50                 info.GetNetworkId().compare(networkId_) == 0);
51     }
52 
53 private:
54     std::string ownName_;
55     std::string peerName_;
56     std::string networkId_;
57 };
58 
59 struct SocketInfoHash {
operatorSocketInfoHash60     size_t operator()(const DBinderSocketInfo &info) const
61     {
62         return std::hash<std::string>()(info.GetOwnName()) ^ std::hash<std::string>()(info.GetPeerName()) ^
63                std::hash<std::string>()(info.GetNetworkId());
64     }
65 };
66 
67 class DatabusSocketListener {
68     DECLARE_DELAYED_SINGLETON(DatabusSocketListener)
69 public:
70     int32_t StartServerListener(const std::string &ownName);
71     int32_t CreateClientSocket(const std::string &ownName, const std::string &peerName, const std::string &networkId);
72     void ShutdownSocket(int32_t socket);
73 
74     static void ServerOnBind(int32_t socket, PeerSocketInfo info);
75     static void ServerOnShutdown(int32_t socket, ShutdownReason reason);
76     static void ClientOnBind(int32_t socket, PeerSocketInfo info);
77     static void ClientOnShutdown(int32_t socket, ShutdownReason reason);
78     static void OnBytesReceived(int32_t socket, const void *data, uint32_t dataLen);
79     static void EraseDeviceLock(DBinderSocketInfo info);
80     static void RemoveSessionName(void);
81 
82     static bool GetPidAndUidFromServiceName(const std::string &serviceName, int32_t &pid, int32_t &uid);
83 private:
84     std::shared_ptr<std::mutex> QueryOrNewInfoMutex(DBinderSocketInfo socketInfo);
85 
86     ISocketListener clientListener_{};
87     ISocketListener serverListener_{};
88 
89     static inline std::mutex socketInfoMutex_;
90     static inline std::mutex deviceMutex_;
91     static inline std::unordered_map<DBinderSocketInfo, std::shared_ptr<std::mutex>, SocketInfoHash> infoMutexMap_{};
92     static inline std::unordered_map<DBinderSocketInfo, int32_t, SocketInfoHash> socketInfoMap_{};
93 };
94 } // namespace OHOS
95 #endif // OHOS_IPC_DBINDER_DATABUS_SOCKET_LISTENER_H
96