Fix potential double close() on exception
[folly.git] / folly / io / async / AsyncUDPSocket.cpp
index 1364ed3fac93deb003a2c75ef07896e22d150838..082f7994cb0af8f2ddc0318cb195853d5f04e18a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 #include <folly/io/async/EventBase.h>
 #include <folly/Likely.h>
+#include <folly/portability/Fcntl.h>
+#include <folly/portability/Sockets.h>
+#include <folly/portability/Unistd.h>
 
 #include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
 
 // Due to the way kernel headers are included, this may or may not be defined.
 // Number pulled from 3.10 kernel headers.
@@ -29,6 +30,8 @@
 #define SO_REUSEPORT 15
 #endif
 
+namespace fsp = folly::portability::sockets;
+
 namespace folly {
 
 AsyncUDPSocket::AsyncUDPSocket(EventBase* evb)
@@ -46,7 +49,7 @@ AsyncUDPSocket::~AsyncUDPSocket() {
 }
 
 void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
-  int socket = ::socket(address.getFamily(), SOCK_DGRAM, IPPROTO_UDP);
+  int socket = fsp::socket(address.getFamily(), SOCK_DGRAM, IPPROTO_UDP);
   if (socket == -1) {
     throw AsyncSocketException(AsyncSocketException::NOT_OPEN,
                               "error creating async udp socket",
@@ -63,16 +66,18 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
                               errno);
   }
 
-  // put the socket in reuse mode
-  int value = 1;
-  if (setsockopt(socket,
-                 SOL_SOCKET,
-                 SO_REUSEADDR,
-                 &value,
-                 sizeof(value)) != 0) {
-    throw AsyncSocketException(AsyncSocketException::NOT_OPEN,
-                              "failed to put socket in reuse mode",
-                              errno);
+  if (reuseAddr_) {
+    // put the socket in reuse mode
+    int value = 1;
+    if (setsockopt(socket,
+                  SOL_SOCKET,
+                  SO_REUSEADDR,
+                  &value,
+                  sizeof(value)) != 0) {
+      throw AsyncSocketException(AsyncSocketException::NOT_OPEN,
+                                "failed to put socket in reuse mode",
+                                errno);
+    }
   }
 
   if (reusePort_) {
@@ -83,7 +88,6 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
                    SO_REUSEPORT,
                    &value,
                    sizeof(value)) != 0) {
-      ::close(socket);
       throw AsyncSocketException(AsyncSocketException::NOT_OPEN,
                                 "failed to put socket in reuse_port mode",
                                 errno);
@@ -94,8 +98,7 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
   // If we're using IPv6, make sure we don't accept V4-mapped connections
   if (address.getFamily() == AF_INET6) {
     int flag = 1;
-    if (::setsockopt(socket, IPPROTO_IPV6, IPV6_V6ONLY,
-                     &flag, sizeof(flag))) {
+    if (setsockopt(socket, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag))) {
       throw AsyncSocketException(
         AsyncSocketException::NOT_OPEN,
         "Failed to set IPV6_V6ONLY",
@@ -107,7 +110,7 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
   sockaddr_storage addrStorage;
   address.getAddress(&addrStorage);
   sockaddr* saddr = reinterpret_cast<sockaddr*>(&addrStorage);
-  if (::bind(socket, saddr, address.getActualSize()) != 0) {
+  if (fsp::bind(socket, saddr, address.getActualSize()) != 0) {
     throw AsyncSocketException(
         AsyncSocketException::NOT_OPEN,
         "failed to bind the async udp socket for:" + address.describe(),
@@ -249,7 +252,7 @@ void AsyncUDPSocket::handleRead() noexcept {
   struct sockaddr* rawAddr = reinterpret_cast<sockaddr*>(&addrStorage);
   rawAddr->sa_family = localAddress_.getFamily();
 
-  ssize_t bytesRead = ::recvfrom(fd_, buf, len, MSG_TRUNC, rawAddr, &addrLen);
+  ssize_t bytesRead = recvfrom(fd_, buf, len, MSG_TRUNC, rawAddr, &addrLen);
   if (bytesRead >= 0) {
     clientAddress_.setFromSockaddr(rawAddr, addrLen);