Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@DL6ER
Copy link
Member

@DL6ER DL6ER commented May 15, 2025

What does this implement/fix?

Follow-up on similar PRs across Pi-hole's repositories, see #2448 (review)

You can use

pihole-FTL --default-gateway

to get which interface would be used on your particular machine


Related issue or feature (if applicable): #2448 (review)

Pull request in docs with documentation (if applicable): N/A


By submitting this pull request, I confirm the following:

  1. I have read and understood the contributors guide, as well as this entire template. I understand which branch to base my commits and Pull Requests against.
  2. I have commented my proposed changes within the code.
  3. I am willing to help maintain this change if there are issues with it later.
  4. It is compatible with the EUPL 1.2 license
  5. I have squashed any insignificant commits. (git rebase)

Checklist:

  • The code change is tested and works locally.
  • I based my code and PRs against the repositories developmental branch.
  • I signed off all commits. Pi-hole enforces the DCO for all contributions
  • I signed all my commits. Pi-hole requires signatures to verify authorship
  • I have read the above and my PR is ready for review.

@DL6ER DL6ER requested a review from a team as a code owner May 15, 2025 17:39
Copy link
Member

@yubiuser yubiuser left a comment

Choose a reason for hiding this comment

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

Leave empty for auto-detection.

I'm not sure if I like that. That makes it unpredictable. I think we should have this auto detecting code only for the migration and then write it into pihole.toml. If the user wants to change it, they happily can do this afterwards.

@DL6ER
Copy link
Member Author

DL6ER commented May 15, 2025

I thought about this as well. In the end, it is not unpredictable. You have the Ethernet cable only in one of the sockets so the automatic detection should always return the same. If you happen to use another one (for whatever reason), it may not be that bad that the automatism automatically changes the interface for you.

Writing it statically into the file would also make the tests harder because this would make the toml be different based on the machine the CI tests are running one. This wouldn't be a show-stopper and could be coded around, but still, on second and third thought, I still think the "empty is auto" is the best approach here. And I think we do this elsewhere, too.

@rdwebdesign
Copy link
Member

rdwebdesign commented May 20, 2025

Will the API still return an empty string (just like the pihole.toml file) or it will return the interface FTL is actually using?

In this case, the Web Interface will show eth0.

@DL6ER
Copy link
Member Author

DL6ER commented May 21, 2025

Yes. And this is a very good point. So we need to add an additional AJAX call to get the interface in this case from the API (instead of blindly substituting with "eth0")

@rdwebdesign
Copy link
Member

rdwebdesign commented May 21, 2025

So we need to add an additional AJAX call to get the interface in this case from the API

We don't need another AJAX call.
The admin/settings/dns page already calls the correct API endpoint, but the API sends an empty string:

image

The API needs to be changed to send the interface FTL is actually using, but I don't know how to change that without messing the other settings pages and pihole.toml.

Maybe we need another API "virtual value" that doesn't need to be written from (or read to) the config file, like dns.realInterface, but with a better name.

@DL6ER
Copy link
Member Author

DL6ER commented May 21, 2025

We don't need another AJAX call.

Yes, we do need one. This field you are mentioning has to stay empty when it is empty in pihole.toml. Otherwise, when the user hits "save & apply", the empty value would be replaced by the substituted one.

We can use network/gateway here and just take gateway.0.interface. This is guaranteed to be the same FTL will by pick by default for an empty sting.

@rdwebdesign
Copy link
Member

We can use network/gateway here and just take gateway.0.interface. This is guaranteed to be the same FTL will by pick by default for an empty sting.

OK.
Then we need a new API call to get gateway.0.interface and adjust the code in scripts/js/settings-dns.js.

I will try to open a new PR later.

@rdwebdesign
Copy link
Member

rdwebdesign commented May 22, 2025

Done.

I think "empty is auto" approach is fine.

  • Core repo will use the selected interface, on new installs;
  • FTL will use the interface set in pihole.toml, or automatically detect the interface if empty;
  • Web will replicate FTL (use the interface set in pihole.toml, or use the interface automatically detected by FTL).

rdwebdesign
rdwebdesign previously approved these changes May 22, 2025
Copy link
Member

@rdwebdesign rdwebdesign left a comment

Choose a reason for hiding this comment

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

From my point of view this PR is approved, unless yubiuser has something else to add.

@DL6ER
Copy link
Member Author

DL6ER commented May 22, 2025

@yubiuser any objections?

Copy link
Member

@yubiuser yubiuser left a comment

Choose a reason for hiding this comment

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

No objections to the proposed idea.
Just for the implementation:

We can use network/gateway here and just take gateway.0.interface.

If we have code already at an other place, why not use this directly, but instead create a new function here?

@pralor-bot
Copy link

This pull request has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/interfqces-not-shown-can-not-select-wg0/80185/2

@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions
Copy link

github-actions bot commented Jul 7, 2025

Conflicts have been resolved.

@DL6ER DL6ER requested a review from a team July 7, 2025 17:42
@yubiuser
Copy link
Member

@DL6ER

Did you see my question above?

If we have code already at an other place, why not use this directly, but instead create a new function here?

@DL6ER
Copy link
Member Author

DL6ER commented Jul 22, 2025

Well, that's basically what we do: get_gateway_name() calls the existing nlroutes(routes, false); function and what follows is basically extracting said gateway.0.oif. A bit more because we iterate over each of the array elements and pick the interface name (oif) where gateway.X.dst == 'default'. I see now that my explanation above was too oversimplified.

@yubiuser
Copy link
Member

I was just asking because this also what get_gateway() does

FTL/src/api/network.c

Lines 47 to 70 in 632a90d

cJSON *gateway = JSON_NEW_ARRAY();
// Search through routes for the default gateway
// They are the ones with "dst" == "default"
cJSON *route = NULL;
cJSON_ArrayForEach(route, routes)
{
cJSON *dst = cJSON_GetObjectItem(route, "dst");
if(dst != NULL &&
cJSON_IsString(dst) &&
strcmp(cJSON_GetStringValue(dst), "default") == 0)
{
cJSON *gwobj = JSON_NEW_OBJECT();
// Extract and add family
const char *family = cJSON_GetStringValue(cJSON_GetObjectItem(route, "family"));
JSON_REF_STR_IN_OBJECT(gwobj, "family", family);
// Extract and add interface name
const char *iface_name = cJSON_GetStringValue(cJSON_GetObjectItem(route, "oif"));
JSON_COPY_STR_TO_OBJECT(gwobj, "interface", iface_name);
// Extract and add gateway address
const char *gw_addr = cJSON_GetStringValue(cJSON_GetObjectItem(route, "gateway"));
JSON_COPY_STR_TO_OBJECT(gwobj, "address", gw_addr);

But it might be more code to extract part of the information from get_gateway() instead of just doing what we really need in get_gateway_name()

@DL6ER
Copy link
Member Author

DL6ER commented Jul 22, 2025

Yes

@DL6ER DL6ER merged commit 151a343 into development Jul 22, 2025
19 checks passed
@DL6ER DL6ER deleted the tweak/auto_interface branch July 22, 2025 19:26
@PromoFaux PromoFaux mentioned this pull request Oct 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants