Squid Web Cache master
Loading...
Searching...
No Matches
NfMarkConfig.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
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#include "base/IoManip.h"
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
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
47{
48 return (m & ~mask) | mark;
49}
50
51std::ostream &
52Ip::operator <<(std::ostream &os, const NfMarkConfig c)
53{
54 os << asHex(c.mark);
55
56 if (c.mask != 0xffffffff)
57 os << '/' << asHex(c.mask);
58
59 return os;
60}
61
AsHex< Integer > asHex(const Integer n)
a helper to ease AsHex object creation
Definition IoManip.h:169
static nfmark_t getNfmark(Parser::Tokenizer &tokenizer, const SBuf &token)
#define TexcHere(msg)
legacy convenience macro; it is not difficult to type Here() now
a netfilter mark/mask pair
static NfMarkConfig Parse(const SBuf &token)
parses a token and returns an object, expects a "mark[/mask]" format
nfmark_t applyToMark(nfmark_t m) const
bool int64(int64_t &result, int base=0, bool allowSign=true, SBuf::size_type limit=SBuf::npos)
Definition Tokenizer.cc:238
SBuf buf() const
yet unparsed data
Definition Tokenizer.h:35
bool atEnd() const
whether the end of the buffer has been reached
Definition Tokenizer.h:41
bool skip(const SBuf &tokenToSkip)
Definition Tokenizer.cc:189
Definition SBuf.h:94
uint32_t nfmark_t
Definition forward.h:26
std::ostream & operator<<(std::ostream &os, const std::optional< Address > &optional)
Definition Xaction.cc:140
SBuf ToSBuf(Args &&... args)
slowly stream-prints all arguments into a freshly allocated SBuf
Definition Stream.h:63
number