copy wangle back into folly
[folly.git] / folly / wangle / acceptor / LoadShedConfiguration.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/LoadShedConfiguration.h>
11
12 #include <folly/Conv.h>
13 #include <openssl/ssl.h>
14
15 using std::string;
16
17 namespace folly {
18
19 void LoadShedConfiguration::addWhitelistAddr(folly::StringPiece input) {
20   auto addr = input.str();
21   size_t separator = addr.find_first_of('/');
22   if (separator == string::npos) {
23     whitelistAddrs_.insert(SocketAddress(addr, 0));
24   } else {
25     unsigned prefixLen = folly::to<unsigned>(addr.substr(separator + 1));
26     addr.erase(separator);
27     whitelistNetworks_.insert(NetworkAddress(SocketAddress(addr, 0), prefixLen));
28   }
29 }
30
31 bool LoadShedConfiguration::isWhitelisted(const SocketAddress& address) const {
32   if (whitelistAddrs_.find(address) != whitelistAddrs_.end()) {
33     return true;
34   }
35   for (auto& network : whitelistNetworks_) {
36     if (network.contains(address)) {
37       return true;
38     }
39   }
40   return false;
41 }
42
43 }