Skip to content

LocalIP: consider preferred source address when retrieving default route #1883

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

Merged
merged 5 commits into from
Aug 5, 2025

Conversation

jannispl
Copy link
Contributor

@jannispl jannispl commented Aug 5, 2025

On my system, the default route's interface has more than one IP address:

# ip address show host-net | grep inet
    inet 10.0.0.9/32 scope global noprefixroute host-net
    inet 10.0.0.10/32 scope global noprefixroute host-net
    inet 10.0.0.13/32 scope global noprefixroute host-net
    inet 10.0.0.8/24 brd 10.0.0.255 scope global dynamic noprefixroute host-net

# ip -4 route
default via 10.0.0.1 dev host-net proto dhcp src 10.0.0.8 metric 10 
10.0.0.0/24 dev host-net proto kernel scope link src 10.0.0.8 metric 10 

In its default configuration, fastfetch appears to retrieve the first IP address it finds on the default route's interface:

Local IP (host-net): 10.0.0.9/32

I expected it to show the default route's preferred source address (10.0.0.8) instead. Unfortunately, this information is not present in /proc/net/route so we have to resort to communicating with the kernel directly - much like ip route does.

@CarterLi
Copy link
Member

CarterLi commented Aug 5, 2025

We do support multiple IPs on one interface (as it is very common for IPv6). We only use the default route to get the preferred interface. As for multiple IPs on the same interface, we dont have preferences.

Try --localip-show-all-ips

@jannispl
Copy link
Contributor Author

jannispl commented Aug 5, 2025

We do support multiple IPs on one interface (as it is very common for IPv6). We only use the default route to get the preferred interface. As for multiple IPs on the same interface, we dont have preferences.

Try --localip-show-all-ips

I know fastfetch can display multiple IP addresses, but I want to keep the default behavior of showing one main IP address which corresponds to the default gateway (as suggested by the relevant option --localip-default-route-only). This is currently not the case - instead, an IP address unrelated to the default route is shown.

@CarterLi
Copy link
Member

CarterLi commented Aug 5, 2025

  • --localip-default-route-only false shows interfaces that is not used for default route.
  • --localip-show-all-ips true show all IPs that belong to one interface. By default, if one interface has multiple IPs, only first one is shown
  • --localip-default-route-only true --localip-show-all-ips true shows all IPs that belongs to the interface used for the default route.

@jannispl
Copy link
Contributor Author

jannispl commented Aug 5, 2025

Are you saying the current behavior of --localip-default-route-only true --localip-show-all-ips false showing the first address present on the default route's interface is expected, rather than the one explicitly set as preferred source on the default route?

@CarterLi
Copy link
Member

CarterLi commented Aug 5, 2025

showing the first address present on the default route's interface is expected

It is. But if we can find which IP the kernel prefers, it would be of course better.

@CarterLi
Copy link
Member

CarterLi commented Aug 5, 2025

I'm not familar with how netlink works. Does it work with IPv6?

@CarterLi CarterLi requested a review from Copilot August 5, 2025 11:12
@jannispl
Copy link
Contributor Author

jannispl commented Aug 5, 2025

I'm not familar with how netlink works. Does it work with IPv6?

Yes it does. I didn't implement it for IPv6 just yet, but it should be a matter of swapping AF_INET for AF_INET6 and parsing route addresses differently.

Copilot

This comment was marked as outdated.

@CarterLi
Copy link
Member

CarterLi commented Aug 5, 2025

I'm not familar with how netlink works. Does it work with IPv6?

Yes it does. I didn't implement it for IPv6 just yet, but it should be a matter of swapping AF_INET for AF_INET6 and parsing route addresses differently.

Does it mean it will show GUA after it gets implemented?

@CarterLi
Copy link
Member

CarterLi commented Aug 5, 2025

@jannispl After applied your patch, no IP is displayed.

@jannispl
Copy link
Contributor Author

jannispl commented Aug 5, 2025

I'm not familar with how netlink works. Does it work with IPv6?

Yes it does. I didn't implement it for IPv6 just yet, but it should be a matter of swapping AF_INET for AF_INET6 and parsing route addresses differently.

Does it mean it will show GUA after it gets implemented?

I'm not sure how IPv6 should be handled. The default route will most likely be a link-local address. Also, IPv6 routes do not seem to have the concept of a "preferred source address". Additionally, interfaces very often have even multiple GUA, for instance when using privacy extensions.

Additionally, the way IPv6 is handled currently (as a fallback for when no IPv4 is present) seems kinda odd to me. It could make sense to split the localip module into one module for each address family with individual detections - this way you could get both v4 and v6 in the output.


@jannispl After applied your patch, no IP is displayed.

Hmm, this would indicate that you have a default route with a non-zero preferred source address that is not part of the interface's addresses. What do you get for ip -4 addr show <iface> and ip -4 route?

@CarterLi
Copy link
Member

CarterLi commented Aug 5, 2025

image

@CarterLi
Copy link
Member

CarterLi commented Aug 5, 2025

image

preferredSourceAddr is 0

@CarterLi
Copy link
Member

CarterLi commented Aug 5, 2025

The default route will most likely be a link-local address.

I guess no?

Also, IPv6 routes do not seem to have the concept of a "preferred source address". Additionally, interfaces very often have even multiple GUA, for instance when using privacy extensions.

There are temp addresses. I think it's ok to report them if the kernel use them for connection by default

@jannispl
Copy link
Contributor Author

jannispl commented Aug 5, 2025

The default route will most likely be a link-local address.

I guess no?

I don't have many systems with ipv6 properly configured, but for those that have it, I have a link-local gateway in the default route.

Also, IPv6 routes do not seem to have the concept of a "preferred source address". Additionally, interfaces very often have even multiple GUA, for instance when using privacy extensions.

There are temp addresses. I think it's ok to report them if the kernel use them for connection by default

Not sure how to accurately tell which address the kernel is going to use. Maybe we should implement something like ip route get <internet destination>?

@jannispl
Copy link
Contributor Author

jannispl commented Aug 5, 2025

preferredSourceAddr is 0

I made a logic mistake when checking for preferredSourceAddr being 0. What I meant to do is consider all interface addresses to be part of the default route when there is no preferred source. Should be fixed now.

@CarterLi
Copy link
Member

CarterLi commented Aug 5, 2025

Please use FF_AUTO_CLOSE_FD and FF_AUTO_FREE (macros of __attribute__((cleanup())) to cleanup your code. I'll do the IPv6 part.

@CarterLi
Copy link
Member

CarterLi commented Aug 5, 2025

It could make sense to split the localip module into one module for each address family with individual detections - this way you could get both v4 and v6 in the output.

Although LocalIP module is called LocalIP, it detects some info about the interfaces themselves like MTU and mac address. Split it would make the information duplicate.

@CarterLi CarterLi requested a review from Copilot August 5, 2025 14:35
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enhances the local IP detection functionality to prioritize the preferred source address from the default route instead of simply using the first IP address found on the default route interface. The implementation adds netlink socket communication on Linux to retrieve routing information directly from the kernel, similar to how the ip route command works.

Key changes:

  • Replaces /proc/net/route parsing with netlink socket communication for more complete routing information
  • Adds preferred source address retrieval functionality across all supported platforms
  • Updates IP filtering logic to prefer the default route's source address when available

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/detection/localip/localip_linux.c Updates IP filtering logic to use preferred source address for default route determination
src/common/netif/netif_linux.c Replaces proc filesystem parsing with comprehensive netlink-based route lookup
src/common/netif/netif_windows.c Updates function signature to support preferred source address parameter
src/common/netif/netif_haiku.c Updates function signature to support preferred source address parameter
src/common/netif/netif_bsd.c Adds preferred source address extraction from routing messages
src/common/netif/netif.h Adds new function declaration for preferred source address retrieval
src/common/netif/netif.c Implements caching and initialization for preferred source address functionality

@CarterLi
Copy link
Member

CarterLi commented Aug 5, 2025

@jannispl Please address ( or close ) the feedback from AI.

@jannispl jannispl force-pushed the localip-default-route branch from 6f80039 to bf5a55c Compare August 5, 2025 15:02
@CarterLi CarterLi merged commit 5e770dc into fastfetch-cli:dev Aug 5, 2025
@CarterLi
Copy link
Member

CarterLi commented Aug 5, 2025

Thanks

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.

2 participants