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 #include "net.h"
16 #include "log.h"
17 #undef LOG_TAG
18 #define LOG_TAG "WifiRpcNet"
19
SetNonBlock(int fd,int type)20 int SetNonBlock(int fd, int type)
21 {
22 int flags = fcntl(fd, F_GETFL, 0);
23 if (flags < 0) {
24 LOGE("get socket flags failed!");
25 return -1;
26 }
27 if (type == 1) {
28 flags |= O_NONBLOCK;
29 } else {
30 flags &= ~O_NONBLOCK;
31 }
32 if (fcntl(fd, F_SETFL, flags) < 0) {
33 LOGE("set socket O_NONBLOCK failed!");
34 return -1;
35 }
36 return 0;
37 }
38
MyRead(int fd,char * buf,int count)39 int MyRead(int fd, char *buf, int count)
40 {
41 int pos = 0;
42 while (count > 0) {
43 int ret = read(fd, buf + pos, count);
44 if (ret == 0) {
45 buf[pos] = 0;
46 return SOCK_CLOSE;
47 } else if (ret < 0) {
48 if (errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN) {
49 break;
50 } else {
51 LOGE("read failed! error is %{public}d", errno);
52 return SOCK_ERR;
53 }
54 } else {
55 pos += ret;
56 count -= ret;
57 }
58 }
59 buf[pos] = 0;
60 #ifdef DEBUG
61 LOGD("read: %{private}s", buf);
62 #endif
63 return pos;
64 }
65
MyWrite(int fd,const char * buf,int count)66 int MyWrite(int fd, const char *buf, int count)
67 {
68 int pos = 0;
69 while (count > 0) {
70 int ret = write(fd, buf + pos, count);
71 if (ret == 0) {
72 return SOCK_CLOSE;
73 } else if (ret < 0) {
74 if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN) {
75 break;
76 } else {
77 LOGE("write failed! error is %{public}d", errno);
78 return SOCK_ERR;
79 }
80 }
81 #ifdef DEBUG
82 int tmpSize = ret + 1;
83 char *szTmp = (char *)calloc(tmpSize, sizeof(char));
84 if (szTmp != NULL) {
85 if (strncpy_s(szTmp, tmpSize, buf + pos, ret) == EOK) {
86 LOGD("write: %{private}s", szTmp);
87 }
88 free(szTmp);
89 }
90 #endif
91 pos += ret;
92 count -= ret;
93 }
94 return pos;
95 }
96
CreateSocket(int domain)97 static int CreateSocket(int domain)
98 {
99 int sock = socket(domain, SOCK_STREAM, 0);
100 if (sock < 0) {
101 LOGE("create socket failed!");
102 return -1;
103 }
104 return sock;
105 }
106
CreateUnixServer(const char * path,int backlog)107 int CreateUnixServer(const char *path, int backlog)
108 {
109 struct sockaddr_un sockAddr;
110 if (memset_s(&sockAddr, sizeof(sockAddr), 0, sizeof(sockAddr)) != EOK) {
111 return -1;
112 }
113 sockAddr.sun_family = AF_LOCAL;
114 if (strncpy_s(sockAddr.sun_path, sizeof(sockAddr.sun_path), path, strlen(path)) != EOK) {
115 return -1;
116 }
117 int sock = CreateSocket(AF_LOCAL);
118 if (sock < 0) {
119 return -1;
120 }
121 int keepAlive = 1;
122 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepAlive, sizeof(keepAlive));
123 int reuseaddr = 1;
124 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&reuseaddr, sizeof(reuseaddr));
125 if (bind(sock, (struct sockaddr *)&sockAddr, sizeof(sockAddr)) < 0) {
126 LOGE("bind failed!");
127 close(sock);
128 return -1;
129 }
130 if (SetNonBlock(sock, 1) != 0) {
131 LOGE("set socket non block failed!");
132 close(sock);
133 return -1;
134 }
135 fcntl(sock, F_SETFD, FD_CLOEXEC);
136 if (listen(sock, backlog) < 0) {
137 LOGE("listen failed!");
138 close(sock);
139 return -1;
140 }
141 return sock;
142 }
143
ConnectUnixServer(const char * path)144 int ConnectUnixServer(const char *path)
145 {
146 struct sockaddr_un sockAddr;
147 if (memset_s(&sockAddr, sizeof(sockAddr), 0, sizeof(sockAddr)) != EOK) {
148 return -1;
149 }
150 sockAddr.sun_family = AF_LOCAL;
151 if (strncpy_s(sockAddr.sun_path, sizeof(sockAddr.sun_path), path, strlen(path)) != EOK) {
152 return -1;
153 }
154 int sock = CreateSocket(AF_LOCAL);
155 if (sock < 0) {
156 return -1;
157 }
158 if (connect(sock, (struct sockaddr *)&sockAddr, sizeof(sockAddr)) < 0) {
159 LOGE("connect failed!");
160 close(sock);
161 return -1;
162 }
163 return sock;
164 }
165
WaitFdEvent(int fd,unsigned int mask,int milliseconds)166 int WaitFdEvent(int fd, unsigned int mask, int milliseconds)
167 {
168 struct pollfd pFd = {0};
169 pFd.fd = fd;
170 if (mask & READ_EVENT) {
171 pFd.events |= POLLIN;
172 }
173 if (mask & WRIT_EVENT) {
174 pFd.events |= POLLOUT;
175 }
176 int ret = poll(&pFd, 1, milliseconds);
177 if (ret < 0) {
178 LOGE("poll failed!");
179 return -1;
180 } else if (ret == 0) {
181 return 0;
182 } else {
183 return 1;
184 }
185 }
186