copy wangle back into folly
[folly.git] / folly / wangle / acceptor / SocketOptions.cpp
1 /*
2  *  Copyright (c) 2015, Facebook, Inc.
3  *  All rights reserved.
4  *
5  *  This source code is licensed under the BSD-style license found in the
6  *  LICENSE file in the root directory of this source tree. An additional grant
7  *  of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #include <folly/wangle/acceptor/SocketOptions.h>
11
12 #include <netinet/tcp.h>
13 #include <sys/socket.h>
14
15 namespace folly {
16
17 AsyncSocket::OptionMap filterIPSocketOptions(
18   const AsyncSocket::OptionMap& allOptions,
19   const int addrFamily) {
20   AsyncSocket::OptionMap opts;
21   int exclude;
22   if (addrFamily == AF_INET) {
23     exclude = IPPROTO_IPV6;
24   } else if (addrFamily == AF_INET6) {
25     exclude = IPPROTO_IP;
26   } else {
27     LOG(FATAL) << "Address family " << addrFamily << " was not IPv4 or IPv6";
28     return opts;
29   }
30   for (const auto& opt: allOptions) {
31     if (opt.first.level != exclude) {
32       opts[opt.first] = opt.second;
33     }
34   }
35   return opts;
36 }
37
38 }