]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ip/NfMarkConfig.cc
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / ip / NfMarkConfig.cc
CommitLineData
244da4ad 1/*
f70aedc4 2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
244da4ad
AG
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9#include "squid.h"
10
11#include "ConfigParser.h"
12#include "ip/NfMarkConfig.h"
13#include "parser/Tokenizer.h"
14#include "sbuf/Stream.h"
15
16#include <limits>
17
18static nfmark_t
19getNfmark(Parser::Tokenizer &tokenizer, const SBuf &token)
20{
21 int64_t number;
22 if (!tokenizer.int64(number, 0, false))
23 throw TexcHere(ToSBuf("NfMarkConfig: invalid value '", tokenizer.buf(), "' in '", token, "'"));
24
25 if (number > std::numeric_limits<nfmark_t>::max())
26 throw TexcHere(ToSBuf("NfMarkConfig: number, ", number, "in '", token, "' is too big"));
27
28 return static_cast<nfmark_t>(number);
29}
30
31Ip::NfMarkConfig
32Ip::NfMarkConfig::Parse(const SBuf &token)
33{
34 Parser::Tokenizer tokenizer(token);
35
36 const nfmark_t mark = getNfmark(tokenizer, token);
37 const nfmark_t mask = tokenizer.skip('/') ? getNfmark(tokenizer, token) : 0xffffffff;
38
39 if (!tokenizer.atEnd())
40 throw TexcHere(ToSBuf("NfMarkConfig: trailing garbage in '", token, "'"));
41
42 return Ip::NfMarkConfig(mark, mask);
43}
44
45nfmark_t
46Ip::NfMarkConfig::applyToMark(nfmark_t m) const
47{
48 return (m & ~mask) | mark;
49}
50
51std::ostream &
52operator <<(std::ostream &os, const Ip::NfMarkConfig c)
53{
54 os << asHex(c.mark);
55
56 if (c.mask != 0xffffffff)
57 os << '/' << asHex(c.mask);
58
59 return os;
60}
45e49ce3 61