capnproto

FORK: Cap'n Proto serialization/RPC system - core tools and C++ library
git clone https://git.neptards.moe/neptards/capnproto.git
Log | Files | Refs | README | LICENSE

async-io-internal.h (2980B)


      1 // Copyright (c) 2017 Sandstorm Development Group, Inc. and contributors
      2 // Licensed under the MIT License:
      3 //
      4 // Permission is hereby granted, free of charge, to any person obtaining a copy
      5 // of this software and associated documentation files (the "Software"), to deal
      6 // in the Software without restriction, including without limitation the rights
      7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      8 // copies of the Software, and to permit persons to whom the Software is
      9 // furnished to do so, subject to the following conditions:
     10 //
     11 // The above copyright notice and this permission notice shall be included in
     12 // all copies or substantial portions of the Software.
     13 //
     14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     20 // THE SOFTWARE.
     21 
     22 #pragma once
     23 
     24 #include "string.h"
     25 #include "vector.h"
     26 #include "async-io.h"
     27 #include <stdint.h>
     28 
     29 struct sockaddr;
     30 struct sockaddr_un;
     31 
     32 namespace kj {
     33 namespace _ {  // private
     34 
     35 // =======================================================================================
     36 
     37 #if !_WIN32
     38 kj::ArrayPtr<const char> safeUnixPath(const struct sockaddr_un* addr, uint addrlen);
     39 // sockaddr_un::sun_path is not required to have a NUL terminator! Thus to be safe unix address
     40 // paths MUST be read using this function.
     41 #endif
     42 
     43 class CidrRange {
     44 public:
     45   CidrRange(StringPtr pattern);
     46 
     47   static CidrRange inet4(ArrayPtr<const byte> bits, uint bitCount);
     48   static CidrRange inet6(ArrayPtr<const uint16_t> prefix, ArrayPtr<const uint16_t> suffix,
     49                          uint bitCount);
     50   // Zeros are inserted between `prefix` and `suffix` to extend the address to 128 bits.
     51 
     52   uint getSpecificity() const { return bitCount; }
     53 
     54   bool matches(const struct sockaddr* addr) const;
     55   bool matchesFamily(int family) const;
     56 
     57   String toString() const;
     58 
     59 private:
     60   int family;
     61   byte bits[16];
     62   uint bitCount;    // how many bits in `bits` need to match
     63 
     64   CidrRange(int family, ArrayPtr<const byte> bits, uint bitCount);
     65 
     66   void zeroIrrelevantBits();
     67 };
     68 
     69 class NetworkFilter: public LowLevelAsyncIoProvider::NetworkFilter {
     70 public:
     71   NetworkFilter();
     72   NetworkFilter(ArrayPtr<const StringPtr> allow, ArrayPtr<const StringPtr> deny,
     73                 NetworkFilter& next);
     74 
     75   bool shouldAllow(const struct sockaddr* addr, uint addrlen) override;
     76   bool shouldAllowParse(const struct sockaddr* addr, uint addrlen);
     77 
     78 private:
     79   Vector<CidrRange> allowCidrs;
     80   Vector<CidrRange> denyCidrs;
     81   bool allowUnix;
     82   bool allowAbstractUnix;
     83 
     84   kj::Maybe<NetworkFilter&> next;
     85 };
     86 
     87 }  // namespace _ (private)
     88 }  // namespace kj