refactor(filter): name a channel's index instead of pointer arithmetic

Two small things in the channel store:

- `filter get`-style status recounted the channels with a non-empty name, but
  every slot below num_channels always has one (addChannel only ever writes the
  slot it is about to count, and delChannel zeroes the tail), so the count was
  always num_channels. The dead loop is gone; the rest of the file already
  treated the store as dense, except searchChannelsByHash()'s null-key guard,
  which stays as the corruption check it is.

- `chan - filter.getChannel(0)` appeared twice as a way to ask "which index is
  this channel?". indexOfChannel() now answers that directly and findChannel()
  is a thin wrapper over it, so `filter add chan=` does one lookup instead of
  find-then-subtract.

No reply changes (StatusLineFormat pins the exact line).
This commit is contained in:
Jason Huebert authored and github-actions[bot] committed 2026-10-09 18:28:44 +00:00
1 parent 40174d652d
commit 0412fc56cc
2 files changed
+19 -15

No files matched your search

+16 -15
View File
@@ -144,10 +144,15 @@ void FilterRules::moveRule(int from, int to) {
// ---------------------------------------------------------------- channel store
FilterChannel* FilterRules::findChannel(const char* name) {
int i = indexOfChannel(name);
return i < 0 ? NULL : &channels[i];
}
int FilterRules::indexOfChannel(const char* name) const {
for (int i = 0; i < num_channels; i++) {
if (strcmp(channels[i].name, name) == 0) return &channels[i];
if (strcmp(channels[i].name, name) == 0) return i; // every slot below the count is named
}
return NULL;
return -1;
}
FilterChannel* FilterRules::addChannel(const char* name, const char* psk_hex) {
@@ -775,11 +780,12 @@ static bool addRuleParam(FilterRules& filter, FilterRule* r, RegionMap* regions,
char* nm;
while ((nm = strsep(&np, ",")) != NULL) {
if (nm[0] == 0 || (nm[0] == '#' && nm[1] == 0)) { strcpy(reply, "Err - empty chan name"); return false; }
FilterChannel* ch = filter.findChannel(nm);
if (ch == NULL && nm[0] == '#') {
ch = filter.addChannel(nm, NULL); // auto-provision '#' names with derived PSK
int idx = filter.indexOfChannel(nm);
if (idx < 0 && nm[0] == '#') {
filter.addChannel(nm, NULL); // auto-provision '#' names with derived PSK
idx = filter.indexOfChannel(nm);
}
if (ch == NULL) {
if (idx < 0) {
if (nm[0] == '#' && filter.getNumChannels() >= FILTER_MAX_CHANNELS) {
strcpy(reply, "Err - chan store full");
} else {
@@ -787,7 +793,6 @@ static bool addRuleParam(FilterRules& filter, FilterRule* r, RegionMap* regions,
}
return false;
}
int idx = ch - filter.getChannel(0);
r->chan_mask |= (1 << idx);
r->chan_flags |= FILTER_CHANFLG_MASK_SET;
}
@@ -917,15 +922,11 @@ static bool addRuleParam(FilterRules& filter, FilterRule* r, RegionMap* regions,
// ---------------------------------------------------------------- CLI commands
static void cliStatus(FilterRules& filter, char* reply) {
int used = 0;
for (int i = 0; i < filter.getNumChannels(); i++) {
if (filter.getChannel(i)->name[0] != 0) used++;
}
char* out = reply;
int remain = CLI_REPLY_MAX;
radd(&out, &remain, "%s; rules %d/%d; chans %d/%d; ratelimit advert %uh; cache %d/%d",
filter.isEnabled() ? "on" : "off", filter.getNumRules(), FILTER_MAX_RULES,
used, FILTER_MAX_CHANNELS, filter.getAdvertRatelimit(),
filter.getNumChannels(), FILTER_MAX_CHANNELS, filter.getAdvertRatelimit(),
filter.getAdvertRatelimit() ? filter.getAdvertCacheCount() : 0, FILTER_ADVERT_CACHE_SIZE);
radd(&out, &remain, "; limiter %lu; aborted %lu", (unsigned long)filter.getLimiterDrops(),
(unsigned long)filter.getBudgetAborts());
@@ -966,9 +967,9 @@ static void cliChanDel(FilterRules& filter, char* params, char* reply) {
char* p = params;
char* name = nextToken(&p);
if (name == NULL) { strcpy(reply, "Err - usage: filter chan del <name>"); return; }
auto ch = filter.findChannel(name);
if (ch == NULL) { strcpy(reply, "Err - unknown channel"); return; }
filter.delChannel(ch - filter.getChannel(0));
int idx = filter.indexOfChannel(name);
if (idx < 0) { strcpy(reply, "Err - unknown channel"); return; }
filter.delChannel(idx);
snprintf(reply, CLI_REPLY_MAX, "OK - chan %s deleted", name);
}
+3
View File
@@ -204,6 +204,9 @@ public:
int getNumChannels() const { return num_channels; }
FilterChannel* getChannel(int idx) { return &channels[idx]; }
FilterChannel* findChannel(const char* name);
// index of a stored channel, or -1; the store is dense, so the index is also
// the bit a rule's chan_mask uses
int indexOfChannel(const char* name) const;
// addChannel: psk_hex required for non-'#' names; NULL/empty for '#name'
// derives secret = sha256(name)[0..15] per the companion protocol.
FilterChannel* addChannel(const char* name, const char* psk_hex);