Skip to content

Conversation

lixmal
Copy link
Collaborator

@lixmal lixmal commented Aug 10, 2025

Describe your changes

  • Move util/grpc and util/net to client so internal packages can be accessed
  • Add methods to return the next best interface after the NetBird interface.
  • Use IP_UNICAST_IF sock opt to force the outgoing interface for the NetBird net.Dialer and net.ListenerConfig to avoid routing loops. The interface is picked by the new route lookup method.
  • Some refactoring to avoid import cycles
  • Old behavior is available through NB_USE_LEGACY_ROUTING=true env var

Issue ticket number and link

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)

Interal routing change, no user facing code change

Docs PR URL (required if "docs added" is checked)

Paste the PR link from https://github.com/netbirdio/docs here:

https://github.com/netbirdio/docs/pull/__

@Copilot Copilot AI review requested due to automatic review settings August 10, 2025 18:53
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 refactors networking code by moving core networking utilities from util/net to client/net and implements Windows-specific socket options to prevent routing loops. The refactor consolidates networking functionality within the client package structure while adding native Windows support for advanced routing.

  • Moves networking utilities from util/net to client/net for better organization
  • Implements Windows-specific socket options using IP_UNICAST_IF and IPV6_UNICAST_IF
  • Extracts hook functionality into a separate hooks package for better modularity

Reviewed Changes

Copilot reviewed 55 out of 70 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
client/net/net_windows.go Implements Windows-specific socket options and interface selection logic
client/net/env_windows.go Adds Windows environment configuration and VPN interface management
client/net/hooks/hooks.go Extracts hook functionality into dedicated package
util/net/*.go Removes old networking utility files
Multiple files Updates import paths from util/net to client/net
Comments suppressed due to low confidence (1)

Copy link

// 1. Longest prefix match (most specific route)
// 2. Lowest route metric
// 3. Lowest interface metric
func GetBestInterface(dest netip.Addr, vpnIntf string) (*net.Interface, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Do we call this on every network change (e.g Wifi-> Ethernet) ? If we don't refresh the best iface we might keep pinning to a non-best interface as i understand.

return &interfaceSelection{iface4: iface}, nil
}

func setIPv4UnicastIF(fd uintptr, iface *net.Interface) error {
Copy link
Member

Choose a reason for hiding this comment

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

do we need to fallback NB_USE_LEGACY_ROUTING if sth goes wrong here?

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