2 * Copyright (c) 2015, Facebook, Inc.
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.
12 #include <folly/SocketAddress.h>
17 * A simple wrapper around SocketAddress that represents
18 * a network in CIDR notation
20 class NetworkAddress {
23 * Create a NetworkAddress for an addr/prefixLen
24 * @param addr IPv4 or IPv6 address of the network
25 * @param prefixLen Prefix length, in bits
27 NetworkAddress(const folly::SocketAddress& addr,
29 addr_(addr), prefixLen_(prefixLen) {}
31 /** Get the network address */
32 const folly::SocketAddress& getAddress() const {
36 /** Get the prefix length in bits */
37 unsigned getPrefixLength() const { return prefixLen_; }
39 /** Check whether a given address lies within the network */
40 bool contains(const folly::SocketAddress& addr) const {
41 return addr_.prefixMatch(addr, prefixLen_);
44 /** Comparison operator to enable use in ordered collections */
45 bool operator<(const NetworkAddress& other) const {
46 if (addr_ < other.addr_) {
48 } else if (other.addr_ < addr_) {
51 return (prefixLen_ < other.prefixLen_);
56 folly::SocketAddress addr_;