Make DestructorCheck::Safety no-copy, no-move
[folly.git] / folly / io / async / AsyncUDPSocket.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/io/async/AsyncUDPSocket.h>
18
19 #include <folly/io/async/EventBase.h>
20 #include <folly/Likely.h>
21 #include <folly/portability/Fcntl.h>
22 #include <folly/portability/Sockets.h>
23 #include <folly/portability/Unistd.h>
24
25 #include <errno.h>
26
27 // Due to the way kernel headers are included, this may or may not be defined.
28 // Number pulled from 3.10 kernel headers.
29 #ifndef SO_REUSEPORT
30 #define SO_REUSEPORT 15
31 #endif
32
33 namespace fsp = folly::portability::sockets;
34
35 namespace folly {
36
37 AsyncUDPSocket::AsyncUDPSocket(EventBase* evb)
38     : EventHandler(CHECK_NOTNULL(evb)),
39       eventBase_(evb),
40       fd_(-1),
41       readCallback_(nullptr) {
42   DCHECK(evb->isInEventBaseThread());
43 }
44
45 AsyncUDPSocket::~AsyncUDPSocket() {
46   if (fd_ != -1) {
47     close();
48   }
49 }
50
51 void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
52   int socket = fsp::socket(address.getFamily(), SOCK_DGRAM, IPPROTO_UDP);
53   if (socket == -1) {
54     throw AsyncSocketException(AsyncSocketException::NOT_OPEN,
55                               "error creating async udp socket",
56                               errno);
57   }
58
59   auto g = folly::makeGuard([&] { ::close(socket); });
60
61   // put the socket in non-blocking mode
62   int ret = fcntl(socket, F_SETFL, O_NONBLOCK);
63   if (ret != 0) {
64     throw AsyncSocketException(AsyncSocketException::NOT_OPEN,
65                               "failed to put socket in non-blocking mode",
66                               errno);
67   }
68
69   if (reuseAddr_) {
70     // put the socket in reuse mode
71     int value = 1;
72     if (setsockopt(socket,
73                   SOL_SOCKET,
74                   SO_REUSEADDR,
75                   &value,
76                   sizeof(value)) != 0) {
77       throw AsyncSocketException(AsyncSocketException::NOT_OPEN,
78                                 "failed to put socket in reuse mode",
79                                 errno);
80     }
81   }
82
83   if (reusePort_) {
84     // put the socket in port reuse mode
85     int value = 1;
86     if (setsockopt(socket,
87                    SOL_SOCKET,
88                    SO_REUSEPORT,
89                    &value,
90                    sizeof(value)) != 0) {
91       throw AsyncSocketException(AsyncSocketException::NOT_OPEN,
92                                 "failed to put socket in reuse_port mode",
93                                 errno);
94
95     }
96   }
97
98   // If we're using IPv6, make sure we don't accept V4-mapped connections
99   if (address.getFamily() == AF_INET6) {
100     int flag = 1;
101     if (setsockopt(socket, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag))) {
102       throw AsyncSocketException(
103         AsyncSocketException::NOT_OPEN,
104         "Failed to set IPV6_V6ONLY",
105         errno);
106     }
107   }
108
109   // bind to the address
110   sockaddr_storage addrStorage;
111   address.getAddress(&addrStorage);
112   sockaddr* saddr = reinterpret_cast<sockaddr*>(&addrStorage);
113   if (fsp::bind(socket, saddr, address.getActualSize()) != 0) {
114     throw AsyncSocketException(
115         AsyncSocketException::NOT_OPEN,
116         "failed to bind the async udp socket for:" + address.describe(),
117         errno);
118   }
119
120   // success
121   g.dismiss();
122   fd_ = socket;
123   ownership_ = FDOwnership::OWNS;
124
125   // attach to EventHandler
126   EventHandler::changeHandlerFD(fd_);
127
128   if (address.getPort() != 0) {
129     localAddress_ = address;
130   } else {
131     localAddress_.setFromLocalAddress(fd_);
132   }
133 }
134
135 void AsyncUDPSocket::setFD(int fd, FDOwnership ownership) {
136   CHECK_EQ(-1, fd_) << "Already bound to another FD";
137
138   fd_ = fd;
139   ownership_ = ownership;
140
141   EventHandler::changeHandlerFD(fd_);
142   localAddress_.setFromLocalAddress(fd_);
143 }
144
145 ssize_t AsyncUDPSocket::write(const folly::SocketAddress& address,
146                                const std::unique_ptr<folly::IOBuf>& buf) {
147   // UDP's typical MTU size is 1500, so high number of buffers
148   //   really do not make sense. Optimze for buffer chains with
149   //   buffers less than 16, which is the highest I can think of
150   //   for a real use case.
151   iovec vec[16];
152   size_t iovec_len = buf->fillIov(vec, sizeof(vec)/sizeof(vec[0]));
153   if (UNLIKELY(iovec_len == 0)) {
154     buf->coalesce();
155     vec[0].iov_base = const_cast<uint8_t*>(buf->data());
156     vec[0].iov_len = buf->length();
157     iovec_len = 1;
158   }
159
160   return writev(address, vec, iovec_len);
161 }
162
163 ssize_t AsyncUDPSocket::writev(const folly::SocketAddress& address,
164                                const struct iovec* vec, size_t iovec_len) {
165   CHECK_NE(-1, fd_) << "Socket not yet bound";
166
167   sockaddr_storage addrStorage;
168   address.getAddress(&addrStorage);
169
170   struct msghdr msg;
171   msg.msg_name = reinterpret_cast<void*>(&addrStorage);
172   msg.msg_namelen = address.getActualSize();
173   msg.msg_iov = const_cast<struct iovec*>(vec);
174   msg.msg_iovlen = iovec_len;
175   msg.msg_control = nullptr;
176   msg.msg_controllen = 0;
177   msg.msg_flags = 0;
178
179   return ::sendmsg(fd_, &msg, 0);
180 }
181
182 void AsyncUDPSocket::resumeRead(ReadCallback* cob) {
183   CHECK(!readCallback_) << "Another read callback already installed";
184   CHECK_NE(-1, fd_) << "UDP server socket not yet bind to an address";
185
186   readCallback_ = CHECK_NOTNULL(cob);
187   if (!updateRegistration()) {
188     AsyncSocketException ex(AsyncSocketException::NOT_OPEN,
189                            "failed to register for accept events");
190
191     readCallback_ = nullptr;
192     cob->onReadError(ex);
193     return;
194   }
195 }
196
197 void AsyncUDPSocket::pauseRead() {
198   // It is ok to pause an already paused socket
199   readCallback_ = nullptr;
200   updateRegistration();
201 }
202
203 void AsyncUDPSocket::close() {
204   DCHECK(eventBase_->isInEventBaseThread());
205
206   if (readCallback_) {
207     auto cob = readCallback_;
208     readCallback_ = nullptr;
209
210     cob->onReadClosed();
211   }
212
213   // Unregister any events we are registered for
214   unregisterHandler();
215
216   if (fd_ != -1 && ownership_ == FDOwnership::OWNS) {
217     ::close(fd_);
218   }
219
220   fd_ = -1;
221 }
222
223 void AsyncUDPSocket::handlerReady(uint16_t events) noexcept {
224   if (events & EventHandler::READ) {
225     DCHECK(readCallback_);
226     handleRead();
227   }
228 }
229
230 void AsyncUDPSocket::handleRead() noexcept {
231   void* buf{nullptr};
232   size_t len{0};
233
234   readCallback_->getReadBuffer(&buf, &len);
235   if (buf == nullptr || len == 0) {
236     AsyncSocketException ex(
237         AsyncSocketException::BAD_ARGS,
238         "AsyncUDPSocket::getReadBuffer() returned empty buffer");
239
240
241     auto cob = readCallback_;
242     readCallback_ = nullptr;
243
244     cob->onReadError(ex);
245     updateRegistration();
246     return;
247   }
248
249   struct sockaddr_storage addrStorage;
250   socklen_t addrLen = sizeof(addrStorage);
251   memset(&addrStorage, 0, size_t(addrLen));
252   struct sockaddr* rawAddr = reinterpret_cast<sockaddr*>(&addrStorage);
253   rawAddr->sa_family = localAddress_.getFamily();
254
255   ssize_t bytesRead = recvfrom(fd_, buf, len, MSG_TRUNC, rawAddr, &addrLen);
256   if (bytesRead >= 0) {
257     clientAddress_.setFromSockaddr(rawAddr, addrLen);
258
259     if (bytesRead > 0) {
260       bool truncated = false;
261       if ((size_t)bytesRead > len) {
262         truncated = true;
263         bytesRead = ssize_t(len);
264       }
265
266       readCallback_->onDataAvailable(
267           clientAddress_, size_t(bytesRead), truncated);
268     }
269   } else {
270     if (errno == EAGAIN || errno == EWOULDBLOCK) {
271       // No data could be read without blocking the socket
272       return;
273     }
274
275     AsyncSocketException ex(AsyncSocketException::INTERNAL_ERROR,
276                            "::recvfrom() failed",
277                            errno);
278
279     // In case of UDP we can continue reading from the socket
280     // even if the current request fails. We notify the user
281     // so that he can do some logging/stats collection if he wants.
282     auto cob = readCallback_;
283     readCallback_ = nullptr;
284
285     cob->onReadError(ex);
286     updateRegistration();
287   }
288 }
289
290 bool AsyncUDPSocket::updateRegistration() noexcept {
291   uint16_t flags = NONE;
292
293   if (readCallback_) {
294     flags |= READ;
295   }
296
297   return registerHandler(uint16_t(flags | PERSIST));
298 }
299
300 } // Namespace