Source-based routing in Golang
Find a file
Dmitrii Stepanov 4a46c8c008
[#11] dialer: Use source IPs intead of interfaces
The use of network interfaces does not cover cases where it is necessary
to use network interfaces to access different subnets.

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
2024-10-09 16:22:45 +03:00
balancer.go [#11] dialer: Use source IPs intead of interfaces 2024-10-09 16:22:45 +03:00
dialer.go [#11] dialer: Use source IPs intead of interfaces 2024-10-09 16:22:45 +03:00
dialer_hostname_test.go [#11] dialer: Use source IPs intead of interfaces 2024-10-09 16:22:45 +03:00
dialer_test.go [#11] dialer: Use source IPs intead of interfaces 2024-10-09 16:22:45 +03:00
go.mod [#11] dialer: Use source IPs intead of interfaces 2024-10-09 16:22:45 +03:00
go.sum [#11] dialer: Use source IPs intead of interfaces 2024-10-09 16:22:45 +03:00
Makefile [#3] dialer: Add gopatchs patch. 2023-11-02 13:25:54 +03:00
README.md [#11] dialer: Use source IPs intead of interfaces 2024-10-09 16:22:45 +03:00

Source-based routing in Golang

Consider this routing table:

10.11.70.0/23 dev data0 proto kernel scope link src 10.11.70.42
10.11.70.0/23 dev data1 proto kernel scope link src 10.11.71.42
192.168.123.0/24 dev internal0 proto kernel scope link src 192.168.123.42
192.168.123.0/24 dev internal1 proto kernel scope link src 192.168.123.142

Simple net.Dial to either 10.11.70.42 or 10.11.71.42 will match the first subnet and be routed via data0. This problems is usually solved by bonds. But sometimes you need to invent a bicycle.

Usage

import (
    "context"
    "net"
    "net/netip"

    "git.frostfs.info/TrueCloudLab/multinet"
)

d, err := multinet.NewDialer(Config{
    Subnets:  []Subnet{
        {
            Prefix: netip.MustParsePrefix("10.11.70.0/23"),
            SourceIPs: []netip.Addr{
                netip.MustParseAddr("10.11.70.42"),
                netip.MustParseAddr("10.11.71.42"),
            },
        },
        {
            Prefix: netip.MustParsePrefix("192.168.123.0/24"),
            SourceIPs: []netip.Addr{
                netip.MustParseAddr("192.168.123.42"),
                netip.MustParseAddr("192.168.123.142"),
            },
        },
    },
    Balancer: multinet.BalancerTypeRoundRobin,
})
if err != nil {
    // handle error
}

conn, err := d.DialContext(ctx, "tcp", "10.11.70.42")
if err != nil {
    // handle error
}
// do stuff