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