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
16 #include "le_socket.h"
17
18 #include <arpa/inet.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <netinet/in.h>
22 #include <stddef.h>
23 #include <string.h>
24 #include <sys/socket.h>
25 #include <sys/stat.h>
26 #include <sys/un.h>
27 #include <unistd.h>
28
29 #include "le_utils.h"
30
CreatePipeServerSocket_(const char * server,int maxClient)31 static int CreatePipeServerSocket_(const char *server, int maxClient)
32 {
33 int listenfd = socket(PF_UNIX, SOCK_STREAM, 0);
34 LE_CHECK(listenfd > 0, return listenfd, "Failed to create socket errno %d", errno);
35
36 unlink(server);
37 struct sockaddr_un serverAddr;
38 int ret = memset_s(&serverAddr, sizeof(serverAddr), 0, sizeof(serverAddr));
39 LE_CHECK(ret == 0, close(listenfd);
40 return ret, "Failed to memory set. error: %d", errno);
41 serverAddr.sun_family = AF_UNIX;
42 ret = strcpy_s(serverAddr.sun_path, sizeof(serverAddr.sun_path), server);
43 LE_CHECK(ret == 0, close(listenfd);
44 return ret, "Failed to copy. error: %d", errno);
45 uint32_t size = offsetof(struct sockaddr_un, sun_path) + strlen(server);
46 ret = bind(listenfd, (struct sockaddr *)&serverAddr, size);
47 LE_CHECK(ret >= 0, close(listenfd);
48 return ret, "Failed to bind socket. error: %d", errno);
49
50 SetNoBlock(listenfd);
51 ret = listen(listenfd, maxClient);
52 LE_CHECK(ret >= 0, close(listenfd);
53 return ret, "Failed to listen socket error: %d", errno);
54 ret = chmod(server, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
55 LE_CHECK(ret == 0, return -1, "Failed to chmod %s, err %d. ", server, errno);
56 LE_LOGV("CreatePipeSocket listen fd: %d server:%s ", listenfd, serverAddr.sun_path);
57 return listenfd;
58 }
59
CreatePipeSocket_(const char * server)60 static int CreatePipeSocket_(const char *server)
61 {
62 int fd = socket(PF_UNIX, SOCK_STREAM, 0);
63 LE_CHECK(fd > 0, return fd, "Failed to create socket");
64 SetNoBlock(fd);
65
66 int on = 1;
67 int ret = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
68 LE_CHECK(ret == 0, return ret, "Failed to set socket option");
69
70 struct sockaddr_un serverAddr;
71 ret = memset_s(&serverAddr, sizeof(serverAddr), 0, sizeof(serverAddr));
72 LE_CHECK(ret == 0, close(fd);
73 return ret, "Failed to memset_s serverAddr");
74 serverAddr.sun_family = AF_UNIX;
75 ret = strcpy_s(serverAddr.sun_path, sizeof(serverAddr.sun_path), server);
76 LE_CHECK(ret == 0, close(fd);
77 return ret, "Failed to strcpy_s sun_path");
78 uint32_t size = offsetof(struct sockaddr_un, sun_path) + strlen(serverAddr.sun_path);
79 ret = connect(fd, (struct sockaddr *)&serverAddr, size);
80 LE_CHECK(ret >= 0, close(fd);
81 return ret, "Failed to connect socket");
82 LE_LOGV("CreatePipeSocket connect fd: %d server: %s ", fd, serverAddr.sun_path);
83 return fd;
84 }
85
GetSockaddrFromServer_(const char * server,struct sockaddr_in * addr)86 static LE_STATUS GetSockaddrFromServer_(const char *server, struct sockaddr_in *addr)
87 {
88 int ret = memset_s(addr, sizeof(struct sockaddr_in), 0, sizeof(struct sockaddr_in));
89 LE_CHECK(ret == 0, return ret, "Failed to memory set. error: %s", strerror(errno));
90 addr->sin_family = AF_INET;
91 const char *portStr = strstr(server, ":");
92 LE_CHECK(portStr != NULL, return LE_FAILURE, "Failed to get addr %s", server);
93 uint16_t port = atoi(portStr + 1);
94 addr->sin_port = htons(port);
95 ret = inet_pton(AF_INET, server, &addr->sin_addr);
96 LE_CHECK(ret >= 0, return LE_FAILURE, "Failed to inet_pton addr %s", server);
97 return LE_SUCCESS;
98 }
99
CreateTcpServerSocket_(const char * server,int maxClient)100 static int CreateTcpServerSocket_(const char *server, int maxClient)
101 {
102 int listenfd = socket(AF_INET, SOCK_STREAM, 0);
103 LE_CHECK(listenfd > 0, return listenfd, "Failed to create socket");
104
105 struct sockaddr_in serverAddr;
106 GetSockaddrFromServer_(server, &serverAddr);
107 int ret = bind(listenfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
108 LE_CHECK(ret >= 0, close(listenfd);
109 return ret, "Failed to bind socket");
110 SetNoBlock(listenfd);
111
112 ret = listen(listenfd, maxClient);
113 LE_CHECK(ret >= 0, close(listenfd);
114 return ret, "Failed to listen socket");
115 return listenfd;
116 }
117
CreateTcpSocket_(const char * server)118 static int CreateTcpSocket_(const char *server)
119 {
120 int fd = socket(AF_INET, SOCK_STREAM, 0);
121 LE_CHECK(fd > 0, return fd, "Failed to create socket");
122 SetNoBlock(fd);
123
124 int on = 1;
125 int ret = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
126 LE_CHECK(ret == 0, return ret, "Failed to set socket option");
127 struct sockaddr_in serverAddr;
128 GetSockaddrFromServer_(server, &serverAddr);
129 LE_LOGV("CreateTcpSocket connect: %s ", server);
130 ret = connect(fd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
131 LE_CHECK(ret >= 0, close(fd);
132 return ret, "Failed to connect socket");
133 return fd;
134 }
135
AcceptPipeSocket_(int serverFd)136 static int AcceptPipeSocket_(int serverFd)
137 {
138 struct sockaddr_un clientAddr;
139 socklen_t addrlen = sizeof(clientAddr);
140 bzero(&clientAddr, addrlen);
141 int fd = accept(serverFd, (struct sockaddr *)&clientAddr, &addrlen);
142 LE_CHECK(fd >= 0, return fd, "Failed to accept socket");
143 LE_LOGV("AcceptPipeSocket client fd %d %s ", fd, clientAddr.sun_path);
144 return fd;
145 }
146
AcceptTcpSocket_(int serverFd)147 static int AcceptTcpSocket_(int serverFd)
148 {
149 struct sockaddr_in clientAddr;
150 socklen_t addrlen = sizeof(clientAddr);
151 bzero(&clientAddr, addrlen);
152 int fd = accept(serverFd, (struct sockaddr *)&clientAddr, &addrlen);
153 LE_CHECK(fd >= 0, return fd, "Failed to accept socket");
154 LE_LOGV("AcceptTcpSocket_ client: %s ", inet_ntoa(clientAddr.sin_addr));
155 return fd;
156 }
157 INIT_LOCAL_API
CreateSocket(int flags,const char * server)158 int CreateSocket(int flags, const char *server)
159 {
160 int fd = -1;
161 int type = flags & 0x0000ff00;
162 LE_LOGV("CreateSocket flags %x type %x server %s", flags, type, server);
163 if (type == TASK_TCP) {
164 if (LE_TEST_FLAGS(flags, TASK_SERVER)) {
165 fd = CreateTcpServerSocket_(server, LOOP_MAX_CLIENT);
166 } else if (LE_TEST_FLAGS(flags, TASK_CONNECT)) {
167 fd = CreateTcpSocket_(server);
168 }
169 } else if (type == TASK_PIPE) {
170 if (LE_TEST_FLAGS(flags, TASK_SERVER)) {
171 fd = CreatePipeServerSocket_(server, LOOP_MAX_CLIENT);
172 } else if (LE_TEST_FLAGS(flags, TASK_CONNECT)) {
173 fd = CreatePipeSocket_(server);
174 }
175 }
176 if (fd <= 0) {
177 LE_LOGE("Invalid flags 0x%08x for server %s", flags, server);
178 return -1;
179 }
180 return fd;
181 }
182 INIT_LOCAL_API
AcceptSocket(int fd,int flags)183 int AcceptSocket(int fd, int flags)
184 {
185 int clientFd = -1;
186 int type = flags & 0x0000ff00;
187 if (type == TASK_TCP) {
188 clientFd = AcceptTcpSocket_(fd);
189 } else if (type == TASK_PIPE) {
190 clientFd = AcceptPipeSocket_(fd);
191 } else {
192 LE_LOGE("AcceptSocket invalid flags %#8x ", flags);
193 return -1;
194 }
195 SetNoBlock(clientFd);
196 return clientFd;
197 }
198