-
Notifications
You must be signed in to change notification settings - Fork 514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: implement Ip::Address::asText() #1739
base: master
Are you sure you want to change the base?
Changes from all commits
c673207
b97f3ed
49b1423
8979a90
7e6b316
1ab9f10
5f2ee67
0a15787
95bd8d1
ba29475
1b46e26
6105648
468faa7
bf14fcc
a422967
06644aa
343f04b
6007f79
beee7cb
96489ce
899724a
794cad1
c8f7293
c73c17f
a38723a
4de23d0
abf7d4d
9a19f31
15ca319
e29fa69
ec14be4
88402b1
80dd4bf
8545ac0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
#define SQUID_SRC_IP_ADDRESS_H | ||
|
||
#include "ip/forward.h" | ||
#include "sbuf/SBuf.h" | ||
|
||
#include <iosfwd> | ||
#include <ostream> | ||
|
@@ -34,6 +35,49 @@ | |
namespace Ip | ||
{ | ||
|
||
/** | ||
* Formats an Address for stream output | ||
* | ||
* Normally instantiated via factory method Ip::Address::asText(). | ||
* Methods manipulate flags to determine output format | ||
*/ | ||
class AddressText | ||
{ | ||
public: | ||
explicit AddressText(const Address &ip); | ||
|
||
/// if the Address has a port, output it (default: no) | ||
/// if set to true, also print brackets for IPv6 addresses | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The "if set to true..." feature introduces a method call order dependency (e.g., the second XXX below) and other problems (e.g., the third XXX below). Please avoid that. Eventually, it would be nice to address both problems:
|
||
AddressText &withPort(bool b = true) | ||
{ | ||
printPort_ = b; | ||
if (b) | ||
printBrackets_ = true; | ||
return *this; | ||
}; | ||
|
||
/// whether to print brackets surrounding the IP portion of IPv6 addresses (default: yes) | ||
/// IPv4 addresses never have brackets | ||
AddressText &bracketed(bool b = true) | ||
rousskov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
printBrackets_ = b; | ||
return *this; | ||
}; | ||
/// output to ostream. Normally used through operator << | ||
std::ostream &print(std::ostream &) const; | ||
|
||
private: | ||
const Address &ip_; | ||
bool printPort_ = false; | ||
bool printBrackets_ = true; | ||
}; | ||
|
||
inline std::ostream & | ||
operator<<(std::ostream &os, const AddressText &at) | ||
{ | ||
return at.print(os); | ||
} | ||
|
||
/** | ||
* Holds and manipulates IPv4, IPv6, and Socket Addresses. | ||
*/ | ||
|
@@ -212,6 +256,9 @@ class Address | |
*/ | ||
char* toStr(char *buf, const unsigned int blen, int force = AF_UNSPEC) const; | ||
|
||
/// Return object textually representing an Address for stream output | ||
AddressText asText() const { return AddressText(*this); } | ||
|
||
/** Return the ASCII equivalent of the address:port combination | ||
* Provides a URL formatted version of the content. | ||
* If buffer is not large enough the data is truncated silently. | ||
|
@@ -354,6 +401,8 @@ class Address | |
static const struct in6_addr v4_anyaddr; | ||
static const struct in6_addr v4_noaddr; | ||
static const struct in6_addr v6_noaddr; | ||
|
||
friend class AddressText; | ||
}; | ||
|
||
inline std::ostream & | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,15 +52,13 @@ ProxyProtocol::Header::getValues(const uint32_t headerType, const char sep) cons | |
return SBuf(); | ||
auto logAddr = sourceAddress; | ||
logAddr.applyClientMask(Config.Addrs.client_netmask); | ||
char ipBuf[MAX_IPSTRLEN]; | ||
return SBuf(logAddr.toStr(ipBuf, sizeof(ipBuf))); | ||
return ToSBuf(logAddr.asText()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both new and the old code suffer from the same problem that this PR should address: A reader cannot easily tell whether this code formats the address correctly. For example, I cannot easily tell whether this code prints the port number. The same problem is present in other converted code as well. To fix this, I suggest adjusting AddressText API so that callers must use
Any better ideas? |
||
} | ||
|
||
case Two::htPseudoDstAddr: { | ||
if (!hasAddresses()) | ||
return SBuf(); | ||
char ipBuf[MAX_IPSTRLEN]; | ||
return SBuf(destinationAddress.toStr(ipBuf, sizeof(ipBuf))); | ||
return ToSBuf(destinationAddress.asText()); | ||
} | ||
|
||
case Two::htPseudoSrcPort: { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we check for errors here? Legacy code doesn't
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should, eventually.
This PR may not have to check, but it is too early to tell. For example, if this code does not fix any of the (many) other problems with legacy printing code, then it does not have to fix this problem either.
I cannot offer a formula that will tell us which legacy problems should be solved in this PR, but it feels like this PR should solve most if not all of them (eventually). For now, I recommend adding an XXX comment to mark this problem in the new code and focus on this code callers/users/API. We will polish the implementation details later (but probably in this PR).