• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include "client_socket.h"
17 
18 #include <sys/socket.h>
19 #include <cerrno>
20 
21 #include "appspawn_server.h"
22 #include "parameters.h"
23 
24 namespace OHOS {
25 #ifdef APPSPAWN_ASAN
26 static constexpr int TIMEOUT_DEF = 5;
27 #else
28 static constexpr int TIMEOUT_DEF = 2;
29 #endif
30 namespace AppSpawn {
ClientSocket(const std::string & client)31 ClientSocket::ClientSocket(const std::string &client) : AppSpawnSocket(client)
32 {}
33 
CreateClient()34 int ClientSocket::CreateClient()
35 {
36     if (socketFd_ < 0) {
37         socketFd_ = CreateSocket();
38         APPSPAWN_CHECK(socketFd_ >= 0, return socketFd_, "Client: Create socket failed");
39     }
40 
41     int opt = 1;
42     int ret = setsockopt(socketFd_, SOL_SOCKET, SO_PASSCRED, &opt, sizeof(opt));
43     if (ret < 0) {
44         APPSPAWN_LOGE("Client: setsockopt failed!");
45         return -1;
46     }
47 
48     APPSPAWN_LOGV("Client: CreateClient socket fd %d", socketFd_);
49     return 0;
50 }
51 
CloseClient()52 void ClientSocket::CloseClient()
53 {
54     if (socketFd_ < 0) {
55         APPSPAWN_LOGE("Client: Invalid connectFd %d", socketFd_);
56         return;
57     }
58 
59     CloseSocket(socketFd_);
60     socketFd_ = -1;
61 }
62 
ConnectSocket(int connectFd)63 int ClientSocket::ConnectSocket(int connectFd)
64 {
65     if (connectFd < 0) {
66         APPSPAWN_LOGE("Client: Invalid socket fd: %d", connectFd);
67         return -1;
68     }
69 
70     APPSPAWN_CHECK(PackSocketAddr() == 0, return -1,  "pack socket failed");
71 
72     struct timeval timeout = {TIMEOUT_DEF, 0};
73     int value = OHOS::system::GetIntParameter("persist.appspawn.client.timeout", TIMEOUT_DEF);
74     if (value != TIMEOUT_DEF && value != 0) {
75         timeout.tv_sec = value;
76     }
77     APPSPAWN_LOGI("Client: Connected on socket fd %d value %d", connectFd, value);
78     bool isRet = (setsockopt(connectFd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) != 0) ||
79         (setsockopt(connectFd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) != 0);
80     APPSPAWN_CHECK(!isRet, return (-1), "Client: Failed to set opt of socket %d, err %d", connectFd, errno);
81 
82     if (connect(connectFd, reinterpret_cast<struct sockaddr *>(&socketAddr_), socketAddrLen_) < 0) {
83         APPSPAWN_LOGW("Client: Connect on socket fd %d, failed: %d", connectFd, errno);
84         return -1;
85     }
86     return 0;
87 }
88 
ConnectSocket()89 int ClientSocket::ConnectSocket()
90 {
91     int ret = ConnectSocket(socketFd_);
92     if (ret != 0) {
93         CloseClient();
94     }
95     return ret;
96 }
97 
WriteSocketMessage(const void * buf,int len)98 int ClientSocket::WriteSocketMessage(const void *buf, int len)
99 {
100     return WriteSocketMessage(socketFd_, buf, len);
101 }
102 
ReadSocketMessage(void * buf,int len)103 int ClientSocket::ReadSocketMessage(void *buf, int len)
104 {
105     return ReadSocketMessage(socketFd_, buf, len);
106 }
107 }  // namespace AppSpawn
108 }  // namespace OHOS
109