HomePhabricator

net: Add netif utility

Description

net: Add netif utility

Summary:
This adds an utility header with two functions that will be needed for
PCP, QueryDefaultGateway and GetLocalAddresses.

Co-authored-by: Vasil Dimov <vd@FreeBSD.org>

Follow-up backports:

move to common/ (core#31011)

Use GetLocalAddresses in Discover.

net: Add optional length checking to CService::SetSockAddr

In almost all cases (the only exception is getifaddrs), we know the
size of the data passed into SetSockAddr, so we can check this to be
what is expected.

Use GetAdaptersAddresses to get local addresses on Windows

Instead of a gethostname hack, use the official way of calling
GetAdaptersAddresses to get local network addresses on Windows.

As additional cleanup, move out FromSockAddr from MacOS and use it
everywhere appropriate.

Suggested by Ava Chow.

core#32159:

net, pcp: handle multi-part responses and filter for default route while querying default gateway for default route in pcp pinholing.

Currently we only make a single recv call, which trucates results from large routing tables, or in the case the kernel may split the message into multiple responses (which may happen with NLM_F_DUMP).

We also do not filter on the default route. For IPv6, this led to selecting the first route with an RTA_GATEWAY attribute, often a non-default route instead of the actual default. This caused PCP port mapping failures because the wrong gateway was used.

Fix both issues by adding multi-part handling of responses and filter for the default route.

This is a partial backport of core#30043, core#31011, core#31022, core#31014 and core#32159
https://github.com/bitcoin/bitcoin/pull/30043/changes/e02030432b77abbf27bb4f67d879d3ad6d6366e6
https://github.com/bitcoin/bitcoin/pull/30043/changes/d72df63d16941576b3523cfeaa49985cf3cd4d42
https://github.com/bitcoin/bitcoin/pull/31022/changes/ab1d3ece026844e682676673b8a461964a5b3ce4
https://github.com/bitcoin/bitcoin/pull/31014/changes/b9d4d5f66a5a35c47e7abc9ec6ef5ab242b3f1e1

Depends on D19756

Test Plan: ninja all check-all

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D19759

Details

Provenance
laanwj <126646+laanwj@users.noreply.github.com>Authored on May 16 2024, 08:16
PiRKCommitted on Apr 1 2026, 18:35
PiRKPushed on Apr 1 2026, 18:35
Reviewer
Restricted Project
Differential Revision
D19759: net: Add netif utility
Parents
rABC7e4c53721dc8: [e.cash] Remove Kousha from team list
Branches
Unknown
Tags
Unknown

Event Timeline

PiRK committed rABCbf1607e2fd17: net: Add netif utility (authored by laanwj <126646+laanwj@users.noreply.github.com>).Apr 1 2026, 18:35