|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011-2019 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +#if !defined (_HTTPSERVER_HPP_INSIDE_) && !defined (HTTPSERVER_COMPILATION) |
| 22 | +#error "Only <httpserver.hpp> or <httpserverpp> can be included directly." |
| 23 | +#endif |
| 24 | + |
| 25 | +#ifndef SRC_HTTPSERVER_IP_REPRESENTATION_HPP_ |
| 26 | +#define SRC_HTTPSERVER_IP_REPRESENTATION_HPP_ |
| 27 | + |
| 28 | +#include <stdint.h> |
| 29 | + |
| 30 | +#include <algorithm> |
| 31 | +#include <string> |
| 32 | +#include <vector> |
| 33 | + |
| 34 | +#include "httpserver/constants.hpp" |
| 35 | +#include "httpserver/http_utils.hpp" |
| 36 | + |
| 37 | +// Forward-declared at file scope; the parsing path in src/http_utils.cpp |
| 38 | +// pulls in <sys/socket.h> directly so the sockaddr ctor can read the |
| 39 | +// address family. Same rationale as http_utils.hpp: backend headers must |
| 40 | +// not leak through the umbrella to downstream consumers. |
| 41 | +struct sockaddr; |
| 42 | + |
| 43 | +namespace httpserver { |
| 44 | +namespace http { |
| 45 | + |
| 46 | +struct ip_representation { |
| 47 | + http_utils::IP_version_T ip_version; |
| 48 | + uint16_t pieces[16]; |
| 49 | + uint16_t mask; |
| 50 | + |
| 51 | + explicit ip_representation(http_utils::IP_version_T ip_version) : |
| 52 | + ip_version(ip_version) { |
| 53 | + mask = constants::DEFAULT_MASK_VALUE; |
| 54 | + std::fill(pieces, pieces + 16, 0); |
| 55 | + } |
| 56 | + |
| 57 | + explicit ip_representation(const std::string& ip); |
| 58 | + explicit ip_representation(const struct sockaddr* ip); |
| 59 | + |
| 60 | + bool operator<(const ip_representation& b) const; |
| 61 | + |
| 62 | + int weight() const { |
| 63 | + // variable-precision SWAR algorithm |
| 64 | + uint16_t x = mask; |
| 65 | + x = x - ((x >> 1) & 0x55555555); |
| 66 | + x = (x & 0x33333333) + ((x >> 2) & 0x33333333); |
| 67 | + return (((x + (x >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; |
| 68 | + } |
| 69 | + |
| 70 | + private: |
| 71 | + // Helpers carved out of the string-ctor to keep each function under |
| 72 | + // the project cyclomatic-complexity bar. parse_ipv6 / parse_ipv4 |
| 73 | + // do the top-level dispatch; the rest serve parse_ipv6. |
| 74 | + void parse_ipv4(const std::string& ip); |
| 75 | + void parse_ipv6(const std::string& ip); |
| 76 | + static unsigned int compute_ipv6_omitted_segments(std::vector<std::string>& parts); |
| 77 | + void apply_ipv6_part(std::vector<std::string>& parts, unsigned int i, |
| 78 | + int& y, unsigned int omitted); |
| 79 | + void parse_nested_ipv4(const std::vector<std::string>& parts, |
| 80 | + unsigned int i, int y); |
| 81 | +}; |
| 82 | + |
| 83 | +} // namespace http |
| 84 | +} // namespace httpserver |
| 85 | + |
| 86 | +#endif // SRC_HTTPSERVER_IP_REPRESENTATION_HPP_ |
0 commit comments