1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 "tcp_socket.h"
17 #include "common/media_log.h"
18 #include "network/socket/socket_utils.h"
19 #include "utils.h"
20 
21 namespace OHOS {
22 namespace Sharing {
TcpSocket()23 TcpSocket::TcpSocket()
24 {
25     SHARING_LOGD("trace.");
26 }
27 
~TcpSocket()28 TcpSocket::~TcpSocket()
29 {
30     SHARING_LOGD("trace.");
31 }
32 
Bind(const uint16_t port, const std::string &host, bool enableReuse, uint32_t backlog)33 bool TcpSocket::Bind(const uint16_t port, const std::string &host, bool enableReuse, uint32_t backlog)
34 {
35     SHARING_LOGD("trace.");
36     int32_t fd = -1;
37     if (!SocketUtils::CreateSocket(SOCK_STREAM, fd)) {
38         SHARING_LOGE("bind CreateSocket Failed!");
39         return false;
40     }
41     socketLocalFd_ = fd;
42 
43     if (enableReuse) {
44         SocketUtils::SetReuseAddr(fd, enableReuse);
45         SocketUtils::SetReusePort(fd, enableReuse);
46     }
47 
48     SocketUtils::SetCloseWait(fd);
49     SocketUtils::SetCloExec(fd, true);
50 
51     if (!SocketUtils::BindSocket(fd, host, port)) {
52         SHARING_LOGE("bind BindSocket Failed!");
53         SocketUtils::ShutDownSocket(fd);
54         return false;
55     }
56 
57     if (!SocketUtils::ListenSocket(fd, backlog)) {
58         SHARING_LOGE("bind ListenSocket Failed!");
59         SocketUtils::ShutDownSocket(fd);
60         return false;
61     }
62 
63     localIp_ = host;
64     localPort_ = port;
65     socketType_ = SOCKET_TYPE_TCP;
66     SetSocketState(SOCKET_STATE_LISTENING);
67     return true;
68 }
69 
Connect(const std::string &peerIp, uint16_t peerPort, int32_t &retCode, bool isAsync, bool enableReuse, const std::string &localIp, uint16_t localPort)70 bool TcpSocket::Connect(const std::string &peerIp, uint16_t peerPort, int32_t &retCode, bool isAsync, bool enableReuse,
71                         const std::string &localIp, uint16_t localPort)
72 {
73     SHARING_LOGD("trace.");
74     int32_t fd = -1;
75     if (!SocketUtils::CreateSocket(SOCK_STREAM, fd)) {
76         SHARING_LOGE("create socket failed!");
77         return false;
78     }
79 
80     socketLocalFd_ = fd;
81     if (enableReuse) {
82         SocketUtils::SetReuseAddr(fd, enableReuse);
83         SocketUtils::SetReusePort(fd, enableReuse);
84     }
85     SocketUtils::SetNoDelay(fd, true);
86     SocketUtils::SetSendBuf(fd);
87     SocketUtils::SetRecvBuf(fd);
88     SocketUtils::SetCloseWait(fd);
89     SocketUtils::SetCloExec(fd, true);
90     SocketUtils::SetKeepAlive(fd);
91     SocketUtils::SetNonBlocking(fd, isAsync);
92 
93     int32_t value = 0xD0;
94     setsockopt(fd, IPPROTO_IP, IP_TOS, &value, sizeof(value));
95 
96     if (!SocketUtils::ConnectSocket(fd, isAsync, peerIp, peerPort, retCode)) {
97         SHARING_LOGE("connect socket failed, ip: %{public}s, port: %{public}d.", GetAnonyString(peerIp).c_str(),
98                      peerPort);
99         return false;
100     } else {
101         SHARING_LOGI("connect socket success, ip: %{public}s, port: %{public}d.", GetAnonyString(peerIp).c_str(),
102                      peerPort);
103     }
104 
105     localIp_ = localIp;
106     localPort_ = localPort;
107     peerIp_ = peerIp;
108     peerPort_ = peerPort;
109     socketType_ = SOCKET_TYPE_TCP;
110     socketState_ = SOCKET_STATE_CONNECTED;
111     return true;
112 }
113 } // namespace Sharing
114 } // namespace OHOS