1 /*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16 #include <sys/time.h>
17 #include "channel_res.h"
18 #include "logger.h"
19 #include "securec.h"
20
21 #define SUCCESS 0
22 #define ERROR (-1)
23
ControlChannelInit(ControlChannelRes * channelInfo)24 int ControlChannelInit(ControlChannelRes *channelInfo)
25 {
26 int len;
27 int sockFd;
28 struct timeval timeOut;
29
30 unlink(channelInfo->srcDomainPath);
31 // Create a socket.
32 sockFd = socket(AF_UNIX, SOCK_DGRAM, 0);
33 if (sockFd < 0) {
34 LOG_ERROR("Get SockFd Error");
35 return ERROR;
36 }
37 // Set the non-blocking mode.
38 timeOut.tv_sec = 0; // Second
39 timeOut.tv_usec = 10000; // 10000 microseconds
40 if (setsockopt(sockFd, SOL_SOCKET, SO_RCVTIMEO, &timeOut, sizeof(timeOut)) == -1) {
41 LOG_ERROR("Setsockopt Fail");
42 return ERROR;
43 }
44 // Binding ports.
45 len = offsetof(struct sockaddr_un, sun_path) + strlen(channelInfo->srcDomainPath) + 1;
46 if (bind(sockFd, (struct sockaddr *)&(channelInfo->srcAddr), len) < 0) {
47 LOG_ERROR("Bind Error\n");
48 return ERROR;
49 }
50 channelInfo->sockFd = sockFd;
51 return 0;
52 }
53
ControlChannelAcept(ControlChannelRes * channelInfo)54 int ControlChannelAcept(ControlChannelRes *channelInfo)
55 {
56 (void)channelInfo;
57 return SUCCESS;
58 }
59
ControlChannelConnect(ControlChannelRes * channelInfo)60 int ControlChannelConnect(ControlChannelRes *channelInfo)
61 {
62 (void)channelInfo;
63 return SUCCESS;
64 }
65
ControlChannelWrite(int32_t sockFd,char * peerDomainPath,ControlChannelBuf * dataBuf)66 int ControlChannelWrite(int32_t sockFd, char *peerDomainPath, ControlChannelBuf *dataBuf)
67 {
68 int ret;
69 uint32_t dataLen;
70 uint32_t addrLen;
71 struct sockaddr_un peerAddr;
72
73 peerAddr.sun_family = AF_UNIX;
74 ret = strcpy_s(peerAddr.sun_path, strlen(peerDomainPath) + 1, peerDomainPath);
75 if (ret != EOK) {
76 LOG_ERROR("strcpy_s Error");
77 return ERROR;
78 }
79 addrLen = offsetof(struct sockaddr_un, sun_path) + strlen(peerDomainPath) + 1;
80 dataLen = sendto(sockFd, dataBuf->data, dataBuf->dataLen, 0, (struct sockaddr *)&peerAddr, addrLen);
81 if (dataLen != dataBuf->dataLen) {
82 LOG_ERROR("Send Msg Error: %s\n", dataBuf->data);
83 return ERROR;
84 }
85 return SUCCESS;
86 }
87
ControlChannelRead(int32_t sockFd,ControlChannelBuf * dataBuf)88 int ControlChannelRead(int32_t sockFd, ControlChannelBuf *dataBuf)
89 {
90 struct sockaddr_un peerAddr;
91 int dataLen;
92 socklen_t addrLen = sizeof(struct sockaddr_un);
93 (void)memset_s(dataBuf->data, CONTROL_CHANNEL_MAX_MSG_LEN, 0, CONTROL_CHANNEL_MAX_MSG_LEN);
94
95 dataLen = recvfrom(sockFd, dataBuf->data, CONTROL_CHANNEL_MAX_MSG_LEN, 0,
96 (struct sockaddr *)(&peerAddr), &(addrLen));
97 if (dataLen < 0) {
98 return ERROR;
99 }
100 dataBuf->dataLen = dataLen;
101 return SUCCESS;
102 }