• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ProtectFromVpn(int32_t socketFd)53 int32_t FwmarkClient::ProtectFromVpn(int32_t socketFd)
54 {
55     if (socketFd < 0) {
56         return HandleError(-1, ERROR_CODE_SOCKETFD_INVALID);
57     }
58     FwmarkCommand command = {FwmarkCommand::PROTECT_FROM_VPN, 0};
59     return Send(&command, socketFd);
60 }
61 
Send(FwmarkCommand * data,int32_t fd)62 int32_t FwmarkClient::Send(FwmarkCommand *data, int32_t fd)
63 {
64     socketFd_ = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
65     if (socketFd_ == -1) {
66         return HandleError(-1, ERROR_CODE_SOCKETFD_INVALID);
67     }
68     NETNATIVE_LOGI("FwmarkClient: socketFd_: %{public}d", socketFd_);
69     if (connect(socketFd_, reinterpret_cast<const sockaddr *>(&FWMARK_SERVER_PATH), sizeof(FWMARK_SERVER_PATH)) == -1) {
70         return HandleError(-1, ERROR_CODE_CONNECT_FAILED);
71     }
72 
73     iovec iov;
74     iov.iov_base = data;
75     iov.iov_len = sizeof(*data);
76     msghdr message;
77     (void)memset_s(&message, sizeof(message), 0, sizeof(message));
78     message.msg_iov = &iov;
79     message.msg_iovlen = 1;
80     union {
81         cmsghdr cmh;
82         char cmsg[CMSG_SPACE(sizeof(fd))];
83     } cmsgu;
84 
85     (void)memset_s(cmsgu.cmsg, sizeof(cmsgu.cmsg), 0, sizeof(cmsgu.cmsg));
86     message.msg_control = cmsgu.cmsg;
87     message.msg_controllen = sizeof(cmsgu.cmsg);
88     cmsghdr *const cmsgh = CMSG_FIRSTHDR(&message);
89     cmsgh->cmsg_len = CMSG_LEN(sizeof(fd));
90     cmsgh->cmsg_level = SOL_SOCKET;
91     cmsgh->cmsg_type = SCM_RIGHTS;
92     (void)memcpy_s(CMSG_DATA(cmsgh), sizeof(fd), &fd, sizeof(fd));
93     int32_t ret = sendmsg(socketFd_, &message, 0);
94     if (ret < 0) {
95         return HandleError(ret, ERROR_CODE_SENDMSG_FAILED);
96     }
97     int32_t error = 0;
98     ret = read(socketFd_, &error, sizeof(error));
99     if (ret < 0) {
100         return HandleError(ret, ERROR_CODE_READ_FAILED);
101     }
102 
103     close(socketFd_);
104     socketFd_ = -1;
105     return NETMANAGER_SUCCESS;
106 }
107 
HandleError(int32_t ret,int32_t errorCode)108 int32_t FwmarkClient::HandleError(int32_t ret, int32_t errorCode)
109 {
110     switch (errorCode) {
111         case ERROR_CODE_SOCKETFD_INVALID:
112             NETNATIVE_LOGE("socketFd invalid, ret:%{public}d, errno: %{public}d", ret, errno);
113             break;
114         case ERROR_CODE_CONNECT_FAILED:
115             NETNATIVE_LOGE("connect failed, ret:%{public}d, errno: %{public}d", ret, errno);
116             break;
117         case ERROR_CODE_SENDMSG_FAILED:
118             NETNATIVE_LOGE("sendmsg failed, ret:%{public}d, errno: %{public}d", ret, errno);
119             break;
120         case ERROR_CODE_READ_FAILED:
121             NETNATIVE_LOGE("read failed, ret:%{public}d, errno: %{public}d", ret, errno);
122             break;
123         default:
124             break;
125     }
126     close(socketFd_);
127     socketFd_ = -1;
128     return NETMANAGER_ERROR;
129 }
130 
131 #ifdef __cplusplus
BindSocket(int32_t fd,uint32_t netId)132 extern int32_t BindSocket(int32_t fd, uint32_t netId)
133 {
134     FwmarkClient instance;
135     return instance.BindSocket(fd, netId);
136 }
137 #endif
138 } // namespace nmd
139 } // namespace OHOS
140