1 /*
2 * Copyright (c) 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 "fwmark_client.h"
17
18 #include <cerrno>
19 #include <sys/socket.h>
20 #include <sys/un.h>
21 #include <unistd.h>
22
23 #include "fwmark.h"
24 #include "fwmark_command.h"
25 #include "net_manager_constants.h"
26 #include "netnative_log_wrapper.h"
27 #include "securec.h"
28
29 namespace OHOS {
30 namespace nmd {
31 using namespace NetManagerStandard;
32 static constexpr const int32_t ERROR_CODE_SOCKETFD_INVALID = -1;
33 static constexpr const int32_t ERROR_CODE_CONNECT_FAILED = -2;
34 static constexpr const int32_t ERROR_CODE_SENDMSG_FAILED = -3;
35 static constexpr const int32_t ERROR_CODE_READ_FAILED = -4;
36
FwmarkClient()37 FwmarkClient::FwmarkClient() : socketFd_(-1) {}
38
~FwmarkClient()39 FwmarkClient::~FwmarkClient()
40 {
41 if (socketFd_ >= 0) {
42 close(socketFd_);
43 socketFd_ = -1;
44 }
45 }
46
BindSocket(int32_t fd,uint32_t netId)47 int32_t FwmarkClient::BindSocket(int32_t fd, uint32_t netId)
48 {
49 FwmarkCommand command = {FwmarkCommand::SELECT_NETWORK, netId};
50 return Send(&command, fd);
51 }
52
Send(FwmarkCommand * data,int32_t fd)53 int32_t FwmarkClient::Send(FwmarkCommand *data, int32_t fd)
54 {
55 socketFd_ = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
56 if (socketFd_ == -1) {
57 return HandleError(-1, ERROR_CODE_SOCKETFD_INVALID);
58 }
59 NETNATIVE_LOGI("FwmarkClient: socketFd_: %{public}d", socketFd_);
60 if (connect(socketFd_, reinterpret_cast<const sockaddr *>(&FWMARK_SERVER_PATH), sizeof(FWMARK_SERVER_PATH)) == -1) {
61 return HandleError(-1, ERROR_CODE_CONNECT_FAILED);
62 }
63
64 iovec iov;
65 iov.iov_base = data;
66 iov.iov_len = sizeof(*data);
67 msghdr message;
68 (void)memset_s(&message, sizeof(message), 0, sizeof(message));
69 message.msg_iov = &iov;
70 message.msg_iovlen = 1;
71 union {
72 cmsghdr cmh;
73 char cmsg[CMSG_SPACE(sizeof(fd))];
74 } cmsgu;
75
76 (void)memset_s(cmsgu.cmsg, sizeof(cmsgu.cmsg), 0, sizeof(cmsgu.cmsg));
77 message.msg_control = cmsgu.cmsg;
78 message.msg_controllen = sizeof(cmsgu.cmsg);
79 cmsghdr *const cmsgh = CMSG_FIRSTHDR(&message);
80 cmsgh->cmsg_len = CMSG_LEN(sizeof(fd));
81 cmsgh->cmsg_level = SOL_SOCKET;
82 cmsgh->cmsg_type = SCM_RIGHTS;
83 (void)memcpy_s(CMSG_DATA(cmsgh), sizeof(fd), &fd, sizeof(fd));
84 int32_t ret = sendmsg(socketFd_, &message, 0);
85 if (ret < 0) {
86 return HandleError(ret, ERROR_CODE_SENDMSG_FAILED);
87 }
88 int32_t error = 0;
89 ret = read(socketFd_, &error, sizeof(error));
90 if (ret < 0) {
91 return HandleError(ret, ERROR_CODE_READ_FAILED);
92 }
93
94 close(socketFd_);
95 socketFd_ = -1;
96 return NETMANAGER_SUCCESS;
97 }
98
HandleError(int32_t ret,int32_t errorCode)99 int32_t FwmarkClient::HandleError(int32_t ret, int32_t errorCode)
100 {
101 switch (errorCode) {
102 case ERROR_CODE_SOCKETFD_INVALID:
103 NETNATIVE_LOGE("socketFd invalid, ret:%{public}d, errno: %{public}d", ret, errno);
104 break;
105 case ERROR_CODE_CONNECT_FAILED:
106 NETNATIVE_LOGE("connect failed, ret:%{public}d, errno: %{public}d", ret, errno);
107 break;
108 case ERROR_CODE_SENDMSG_FAILED:
109 NETNATIVE_LOGE("sendmsg failed, ret:%{public}d, errno: %{public}d", ret, errno);
110 break;
111 case ERROR_CODE_READ_FAILED:
112 NETNATIVE_LOGE("read failed, ret:%{public}d, errno: %{public}d", ret, errno);
113 break;
114 default:
115 break;
116 }
117 close(socketFd_);
118 socketFd_ = -1;
119 return NETMANAGER_ERROR;
120 }
121 } // namespace nmd
122 } // namespace OHOS
123