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 SOCKET_EXEC_COMMON_H
17 #define SOCKET_EXEC_COMMON_H
18
19 #include <cerrno>
20 #include <fcntl.h>
21 #include <sys/socket.h>
22 #include <unistd.h>
23
24 #include "netstack_log.h"
25
26 namespace OHOS::NetStack::Socket {
27 class ExecCommonUtils {
28 public:
MakeNonBlock(int sock)29 static bool MakeNonBlock(int sock)
30 {
31 int flags = fcntl(sock, F_GETFL, 0);
32 while (flags == -1 && errno == EINTR) {
33 flags = fcntl(sock, F_GETFL, 0);
34 }
35 if (flags == -1) {
36 NETSTACK_LOGE("make non block failed, socket is %{public}d, errno is %{public}d", sock, errno);
37 return false;
38 }
39 int ret = fcntl(sock, F_SETFL, flags | O_NONBLOCK);
40 while (ret == -1 && errno == EINTR) {
41 ret = fcntl(sock, F_SETFL, flags | O_NONBLOCK);
42 }
43 if (ret == -1) {
44 NETSTACK_LOGE("make non block failed, socket is %{public}d, errno is %{public}d", sock, errno);
45 return false;
46 }
47 return true;
48 }
49
50 static int MakeTcpSocket(sa_family_t family, bool needNonblock = true)
51 {
52 if (family != AF_INET && family != AF_INET6) {
53 return -1;
54 }
55 int sock = socket(family, SOCK_STREAM, IPPROTO_TCP);
56 NETSTACK_LOGI("new tcp socket is %{public}d", sock);
57 if (sock < 0) {
58 NETSTACK_LOGE("make tcp socket failed, errno is %{public}d", errno);
59 return -1;
60 }
61 if (needNonblock && !MakeNonBlock(sock)) {
62 close(sock);
63 return -1;
64 }
65 return sock;
66 }
67
MakeUdpSocket(sa_family_t family)68 static int MakeUdpSocket(sa_family_t family)
69 {
70 if (family != AF_INET && family != AF_INET6) {
71 return -1;
72 }
73 int sock = socket(family, SOCK_DGRAM, IPPROTO_UDP);
74 NETSTACK_LOGI("new udp socket is %{public}d", sock);
75 if (sock < 0) {
76 NETSTACK_LOGE("make udp socket failed, errno is %{public}d", errno);
77 return -1;
78 }
79 if (!MakeNonBlock(sock)) {
80 close(sock);
81 return -1;
82 }
83 return sock;
84 }
85
86 static int MakeLocalSocket(int socketType, bool needNonblock = true)
87 {
88 int sock = socket(AF_UNIX, socketType, 0);
89 NETSTACK_LOGI("new local socket is %{public}d", sock);
90 if (sock < 0) {
91 NETSTACK_LOGE("make local socket failed, errno is %{public}d", errno);
92 return -1;
93 }
94 if (needNonblock && !MakeNonBlock(sock)) {
95 close(sock);
96 return -1;
97 }
98 return sock;
99 }
100 };
101 }
102
103 std::string ConvertAddressToIp(const std::string &address, sa_family_t family);
104
105 bool IpMatchFamily(const std::string &address, sa_family_t family);
106
107 bool NonBlockConnect(int sock, sockaddr *addr, socklen_t addrLen, uint32_t timeoutMSec);
108
109 bool PollSendData(int sock, const char *data, size_t size, sockaddr *addr, socklen_t addrLen);
110
111 int ConfirmSocketTimeoutMs(int sock, int type, int defaultValue);
112
113 int ConfirmBufferSize(int sock);
114
115 #endif