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