Skip to content

ENT-14276: Refactor Addr lib - #6485

Open
victormlg wants to merge 3 commits into
cfengine:masterfrom
victormlg:addr_lib
Open

victormlg wants to merge 3 commits into
cfengine:masterfrom
victormlg:addr_lib

Conversation

@victormlg

Copy link
Copy Markdown
Contributor

No description provided.

FuzzySetMatch() handled every pattern notation and address family in
one long function. It now determines the notation (prefix, CIDR or
range) and calls one helper per notation and address family.
ParseHostPort() now splits host and port first and determines the
address type afterwards.

Also removed the declaration of IsLoopbackAddress(), which was never
defined, and made the parameter names in addr_lib.h match the
definitions.

No change in behaviour, except that FuzzySetMatch() returns -1 instead
of the strncmp() result when an address prefix does not match. All
callers only compare the result to 0.

Ticket: ENT-14276
Signed-off-by: Victor Moene <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@victormlg victormlg changed the title ENT-14276_ Refactor Addr lib ENT-14276: Refactor Addr lib Nov 9, 2026
Comment thread cf-serverd/server_access.c Fixed
Comment thread cf-serverd/server_access.c Fixed
Comment thread libcfnet/addr_lib.c Fixed
Comment thread libcfnet/addr_lib.c Fixed
Comment thread libcfnet/addr_lib.c Fixed
Comment thread libcfnet/addr_lib.c Fixed
Comment thread libcfnet/addr_lib.c Fixed
Comment thread libcfnet/addr_lib.c Fixed
Comment thread libcfnet/addr_lib.c Fixed
victormlg and others added 2 commits November 9, 2026 16:21
- IPv6 range patterns compared groups without a range as decimal
  numbers, so groups such as "fe80" failed to parse and stale values
  from the previous group were compared instead. For example,
  "fe80:1-3:0:0:0:0:0:1" matched "fe81:2:0:0:0:0:0:1". This affects
  admit_ips/deny_ips in cf-serverd.
- IPv6 range patterns excluded the lower bound of a range.
- IPv6 range patterns did not support "::", neither in the pattern nor
  in the address, so "2001:db8:1-5::1" never matched "2001:db8:3::1".
- IPv4 range patterns reused values from the previous octet when an
  octet failed to parse.
- The address in an IPv4 CIDR pattern was never validated, so an
  uninitialized address was compared, and e.g. "bogus/0" matched every
  address.
- FuzzyMatchParse() (used by iprange() and isipinsubnet()) always
  returned true, since its check for an address stopped at the first
  '.' or ':'. It now validates addresses, CIDR masks (0-32 for IPv4,
  0-128 for IPv6) and ranges, using the same parser as FuzzySetMatch().
- FuzzyHostMatch() (used by hostrange()) read before the start of its
  buffer when the host name was empty or only digits.
- ParseHostPort() classified any host name starting with a digit as an
  IPv4 address, e.g. "1password.com". It now uses inet_pton().

Signed-off-by: Victor Moene <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There is nothing fuzzy about them. They now share a prefix with the
policy functions they implement, iprange() and hostrange():

  FuzzySetMatch()   -> IPRangeMatch()
  FuzzyMatchParse() -> IPRangeIsValid()
  FuzzyHostMatch()  -> HostRangeMatch()
  FuzzyHostParse()  -> HostRangeIsValid()

IPRangeMatch() and HostRangeMatch() now return true on match, like
StringMatch(), instead of 0.

Signed-off-by: Victor Moene <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@victormlg
victormlg marked this pull request as ready for review November 9, 2026 16:17

@larsewi larsewi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please separate each bug fix into their own commits with a changelog.

Comment thread libcfnet/addr_lib.c
Comment on lines +48 to +49
const bool is_cidr = (strchr(pattern, '/') != NULL);
const bool is_range = (strchr(pattern, '-') != NULL);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you've already touched all of this code, you can replace this and other strchr() with StringContainsChar(). Similar with strstr() and StringEqual() in a separate commit. If you want to.

Comment thread libcfnet/addr_lib.c
/* TODO rename to AddrSubnetMatch() */
int FuzzySetMatch(const char *s1, const char *s2)
/**
* Determines the notation of an address pattern.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should mention what notation is, and what it means for notation to be true / false

Comment thread libcfnet/addr_lib.c
{
isCIDR = true;
}
*notation = is_cidr ? PATTERN_CIDR : (is_range ? PATTERN_RANGE : PATTERN_PREFIX);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be easier to read if this was an actual if statement, rather than nesting two ternary operators?

Comment thread libcfnet/addr_lib.c
#include <string_lib.h>

#define CF_ADDRSIZE 128
#define IPV4_OCTETS 4

@larsewi larsewi Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each of these bullets (in the commit message) deserve their own commit, and their own changelog entry.

const char *ipaddr, const char *hostname,
const char *key, const char *username)
{
assert(acl != NULL);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this assert should go into a separate commit. Because the commit message only mentions a rename if a function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants