1 /*
2 * Copyright (c) 2021 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 <cerrno>
19 #include <iostream>
20 #include <sys/socket.h>
21
22 #include "hilog/log.h"
23 #include "securec.h"
24
25 namespace OHOS {
26 namespace AppSpawn {
27 using namespace OHOS::HiviewDFX;
28 static constexpr HiLogLabel LABEL = {LOG_CORE, 0, "ClientSocket"};
29
ClientSocket(const std::string & client)30 ClientSocket::ClientSocket(const std::string &client) : AppSpawnSocket(client)
31 {}
32
CreateClient()33 int ClientSocket::CreateClient()
34 {
35 if (socketFd_ < 0) {
36 socketFd_ = CreateSocket();
37 if (socketFd_ < 0) {
38 HiLog::Error(LABEL, "Client: Create socket failed");
39 return socketFd_;
40 }
41 }
42
43 HiLog::Debug(LABEL, "Client: CreateClient socket fd %d", socketFd_);
44 return 0;
45 }
46
CloseClient()47 void ClientSocket::CloseClient()
48 {
49 if (socketFd_ < 0) {
50 HiLog::Error(LABEL, "Client: Invalid connectFd %d", socketFd_);
51 return;
52 }
53
54 CloseSocket(socketFd_);
55 socketFd_ = -1;
56 }
57
ConnectSocket(int connectFd)58 int ClientSocket::ConnectSocket(int connectFd)
59 {
60 if (connectFd < 0) {
61 HiLog::Error(LABEL, "Client: Invalid socket fd: %d", connectFd);
62 return -1;
63 }
64
65 if (PackSocketAddr() != 0) {
66 CloseSocket(connectFd);
67 return -1;
68 }
69
70 if ((setsockopt(connectFd, SOL_SOCKET, SO_RCVTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) != 0) ||
71 (setsockopt(connectFd, SOL_SOCKET, SO_SNDTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) != 0)) {
72 HiLog::Warn(LABEL, "Client: Failed to set opt of socket %d, err %d", connectFd, errno);
73 CloseSocket(connectFd);
74 return -1;
75 }
76
77 if (connect(connectFd, reinterpret_cast<struct sockaddr *>(&socketAddr_), socketAddrLen_) < 0) {
78 HiLog::Warn(LABEL, "Client: Connect on socket fd %d, failed: %d", connectFd, errno);
79 CloseSocket(connectFd);
80 return -1;
81 }
82
83 HiLog::Debug(LABEL, "Client: Connected on socket fd %d, name '%s'", connectFd, socketAddr_.sun_path);
84 return 0;
85 }
86
ConnectSocket()87 int ClientSocket::ConnectSocket()
88 {
89 return ConnectSocket(socketFd_);
90 }
91
WriteSocketMessage(const void * buf,int len)92 int ClientSocket::WriteSocketMessage(const void *buf, int len)
93 {
94 return WriteSocketMessage(socketFd_, buf, len);
95 }
96
ReadSocketMessage(void * buf,int len)97 int ClientSocket::ReadSocketMessage(void *buf, int len)
98 {
99 return ReadSocketMessage(socketFd_, buf, len);
100 }
101 } // namespace AppSpawn
102 } // namespace OHOS
103