xref: /aosp_15_r20/external/cronet/net/socket/udp_server_socket.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/socket/udp_server_socket.h"
6 
7 #include <utility>
8 
9 #include "build/build_config.h"
10 #include "net/base/net_errors.h"
11 
12 namespace net {
13 
UDPServerSocket(net::NetLog * net_log,const net::NetLogSource & source)14 UDPServerSocket::UDPServerSocket(net::NetLog* net_log,
15                                  const net::NetLogSource& source)
16     : socket_(DatagramSocket::DEFAULT_BIND, net_log, source) {}
17 
18 UDPServerSocket::~UDPServerSocket() = default;
19 
Listen(const IPEndPoint & address)20 int UDPServerSocket::Listen(const IPEndPoint& address) {
21   int rv = socket_.Open(address.GetFamily());
22   if (rv != OK)
23     return rv;
24 
25   if (allow_address_reuse_) {
26     rv = socket_.AllowAddressReuse();
27     if (rv != OK) {
28       socket_.Close();
29       return rv;
30     }
31   }
32 
33   if (allow_broadcast_) {
34     rv = socket_.SetBroadcast(true);
35     if (rv != OK) {
36       socket_.Close();
37       return rv;
38     }
39   }
40 
41   if (allow_address_sharing_for_multicast_) {
42     rv = socket_.AllowAddressSharingForMulticast();
43     if (rv != OK) {
44       socket_.Close();
45       return rv;
46     }
47   }
48 
49   return socket_.Bind(address);
50 }
51 
RecvFrom(IOBuffer * buf,int buf_len,IPEndPoint * address,CompletionOnceCallback callback)52 int UDPServerSocket::RecvFrom(IOBuffer* buf,
53                               int buf_len,
54                               IPEndPoint* address,
55                               CompletionOnceCallback callback) {
56   return socket_.RecvFrom(buf, buf_len, address, std::move(callback));
57 }
58 
SendTo(IOBuffer * buf,int buf_len,const IPEndPoint & address,CompletionOnceCallback callback)59 int UDPServerSocket::SendTo(IOBuffer* buf,
60                             int buf_len,
61                             const IPEndPoint& address,
62                             CompletionOnceCallback callback) {
63   return socket_.SendTo(buf, buf_len, address, std::move(callback));
64 }
65 
SetReceiveBufferSize(int32_t size)66 int UDPServerSocket::SetReceiveBufferSize(int32_t size) {
67   return socket_.SetReceiveBufferSize(size);
68 }
69 
SetSendBufferSize(int32_t size)70 int UDPServerSocket::SetSendBufferSize(int32_t size) {
71   return socket_.SetSendBufferSize(size);
72 }
73 
SetDoNotFragment()74 int UDPServerSocket::SetDoNotFragment() {
75   return socket_.SetDoNotFragment();
76 }
77 
SetRecvTos()78 int UDPServerSocket::SetRecvTos() {
79   return socket_.SetRecvTos();
80 }
81 
SetMsgConfirm(bool confirm)82 void UDPServerSocket::SetMsgConfirm(bool confirm) {
83   return socket_.SetMsgConfirm(confirm);
84 }
85 
Close()86 void UDPServerSocket::Close() {
87   socket_.Close();
88 }
89 
GetPeerAddress(IPEndPoint * address) const90 int UDPServerSocket::GetPeerAddress(IPEndPoint* address) const {
91   return socket_.GetPeerAddress(address);
92 }
93 
GetLocalAddress(IPEndPoint * address) const94 int UDPServerSocket::GetLocalAddress(IPEndPoint* address) const {
95   return socket_.GetLocalAddress(address);
96 }
97 
NetLog() const98 const NetLogWithSource& UDPServerSocket::NetLog() const {
99   return socket_.NetLog();
100 }
101 
AllowAddressReuse()102 void UDPServerSocket::AllowAddressReuse() {
103   allow_address_reuse_ = true;
104 }
105 
AllowBroadcast()106 void UDPServerSocket::AllowBroadcast() {
107   allow_broadcast_ = true;
108 }
109 
AllowAddressSharingForMulticast()110 void UDPServerSocket::AllowAddressSharingForMulticast() {
111   allow_address_sharing_for_multicast_ = true;
112 }
113 
JoinGroup(const IPAddress & group_address) const114 int UDPServerSocket::JoinGroup(const IPAddress& group_address) const {
115   return socket_.JoinGroup(group_address);
116 }
117 
LeaveGroup(const IPAddress & group_address) const118 int UDPServerSocket::LeaveGroup(const IPAddress& group_address) const {
119   return socket_.LeaveGroup(group_address);
120 }
121 
SetMulticastInterface(uint32_t interface_index)122 int UDPServerSocket::SetMulticastInterface(uint32_t interface_index) {
123   return socket_.SetMulticastInterface(interface_index);
124 }
125 
SetMulticastTimeToLive(int time_to_live)126 int UDPServerSocket::SetMulticastTimeToLive(int time_to_live) {
127   return socket_.SetMulticastTimeToLive(time_to_live);
128 }
129 
SetMulticastLoopbackMode(bool loopback)130 int UDPServerSocket::SetMulticastLoopbackMode(bool loopback) {
131   return socket_.SetMulticastLoopbackMode(loopback);
132 }
133 
SetDiffServCodePoint(DiffServCodePoint dscp)134 int UDPServerSocket::SetDiffServCodePoint(DiffServCodePoint dscp) {
135   return socket_.SetDiffServCodePoint(dscp);
136 }
137 
SetTos(DiffServCodePoint dscp,EcnCodePoint ecn)138 int UDPServerSocket::SetTos(DiffServCodePoint dscp, EcnCodePoint ecn) {
139   return socket_.SetTos(dscp, ecn);
140 }
141 
DetachFromThread()142 void UDPServerSocket::DetachFromThread() {
143   socket_.DetachFromThread();
144 }
145 
GetLastTos() const146 DscpAndEcn UDPServerSocket::GetLastTos() const {
147   return socket_.GetLastTos();
148 }
149 
UseNonBlockingIO()150 void UDPServerSocket::UseNonBlockingIO() {
151 #if BUILDFLAG(IS_WIN)
152   socket_.UseNonBlockingIO();
153 #endif
154 }
155 
156 }  // namespace net
157