Skip to content

Does not use bind to local address for outgoing connections #6476

Open

Description

@kroeckx

I have several IPv4 / IPv6 addresses on a host and I wish to use a specific one. So I set up the bind so it would use that. It seems to be using that for the bind() in the listen socket but not for the outgoing connections.

I've tried settings externalip. That only seems to change the "localaddresses" returned by getnetworkinfo. I'm guessing that's mostly useful for NAT.

Activity

  1. laanwj commented on Jul 27, 2015

    @laanwj
    Member

    Right, there is no option to specify binding for outgoing connections.
    I'd be OK with a per-network (IPv4/IPv6) setting that specifies this.
    Be aware that enforcing this at application level is, in practice, impossible to make watertight - e.g. impossible for DNS lookups. So if this is for security or privacy reasons I'd strongly recommend to do this at the OS level (eg by giving a certain user only access to certain network interface).

  2. kroeckx commented on Jul 31, 2015

    @kroeckx
    Author

    I don't care about things like DNS, I would just like the bitcoin traffic itself to go over the addresses I select.

  3. sipa commented on Jul 31, 2015

    @sipa
    Member
  4. phatkiller commented on May 2, 2016

    @phatkiller

    Has this been implemented ? If not is there a timeframe for it ?

  5. jonasschnelli commented on May 2, 2016

    @jonasschnelli
    Contributor

    @phatkiller: The issue is still open that's a sign that nobody has implemented it right now. We don't have a timeframe for features. Bitcoin-Core is an open source project, it will only be implemented if someone invests time to implement a such feature (which mostly is a sign that someone requires that feature).

  6. laanwj commented on May 4, 2016

    @laanwj
    Member

    If you need this, you need to make sure it gets implemented yourself. This is not a place to get free (as in beer) development resources.

  7. mrobinton commented on Oct 2, 2019

    @mrobinton

    What is required is to bind the socket to the -bind address prior to connecting to the remote site, not the usual way this would be done. I've looked at the code in net.cpp and netbase.cpp and boost appears to be in use and the location of the -bind ip constant is not obvious to me. I'm not skilled with c++. Here's a rough outline of what needs to be done but I don't have the skill to do it in the cpp environment. Perhaps someone else will pick up this thread.

    Found this doing research on the issue.
    No error checking.

    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    // Bind to a specific network interface (and optionally a specific local port)
    struct sockaddr_in localaddr;
    localaddr.sin_family = AF_INET;
    localaddr.sin_addr.s_addr = inet_addr("192.168.1.100");
    localaddr.sin_port = 0; // Any local port will do
    bind(sockfd, (struct sockaddr *)&localaddr, sizeof(localaddr));

  8. ryanofsky commented on Oct 3, 2019

    @ryanofsky
    Contributor

    This seems like an unusual request. Do you need bitcoin to use different outgoing IP address than other applications on the system? Or to connect from a different IP address than it it listens on? If not, it seems like you could be better off just fixing the routing table: https://serverfault.com/questions/182550/specifing-ip-address-for-outbound-connections-on-a-multi-ip-host

  9. infernix commented on Oct 4, 2019

    @infernix

    This can't be fixed through a routing table because it's not about return packets for incoming connections but for packets that are for connections initiated by the process itself, which if not bindable to a specific IP (as per this request) will just use the hosts' default IP (which may not be the IP where incoming packets arrive). So not that unusual of a request, I had to dockerize and apply some iptables magic to make this work right on a node with multiple IPs within the same subnet.

  10. Nyanraltotlapun commented on Nov 16, 2021

    @Nyanraltotlapun

    This seems like an unusual request. Do you need bitcoin to use different outgoing IP address than other applications on the system?

    I feel that this is needed for avoiding mess up with IPv6 privacy randomized addresses.

  11. vasild commented on Aug 5, 2023

    @vasild
    Contributor

    This request looks reasonable. Is there still interest in it?

    Something like this should do it (and pass the proper argument to ConnectSocketDirectly() untested):

    -bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nTimeout, bool manual_connection)
    +bool ConnectSocketDirectly(const std::optional<CNetAddr>& addr_from, const CService &addrConnect, const Sock& sock, int nTimeout, bool manual_connection)
     {
         // Create a sockaddr from the specified service.
         struct sockaddr_storage sockaddr;
         socklen_t len = sizeof(sockaddr);
         if (sock.Get() == INVALID_SOCKET) {
             LogPrintf("Cannot connect to %s: invalid socket\n", addrConnect.ToStringAddrPort());
             return false;
         }
         if (!addrConnect.GetSockAddr((struct sockaddr*)&sockaddr, &len)) {
             LogPrintf("Cannot connect to %s: unsupported network\n", addrConnect.ToStringAddrPort());
             return false;
         }
     
    +    if (addr_from.has_value()) {
    +        struct sockaddr_storage storage;
    +        struct sockaddr* saddr_from = reinterpret_cast<struct sockaddr*>(&storage);
    +        socklen_t saddr_from_len = sizeof(storage);
    +        if (!CService{addr_from.value(), /*port=*/0}.GetSockAddr(saddr_from, &saddr_from_len)) {
    +            LogPrintLevel(
    +                BCLog::NET,
    +                BCLog::Level::Error,
    +                "Cannot connect to %s: unsupported network of the specified outbound address %s\n",
    +                addrConnect.ToStringAddrPort(),
    +                addr_from->ToStringAddr());
    +            return false;
    +        }
    +        if (sock.Bind(saddr_from, saddr_from_len) == SOCKET_ERROR) {
    +            LogPrintLevel(
    +                BCLog::NET,
    +                BCLog::Level::Error,
    +                "Cannot connect to %s: unable to bind to the specified outbound address %s: %s\n",
    +                addrConnect.ToStringAddrPort(),
    +                addr_from->ToStringAddr(),
    +                NetworkErrorString(WSAGetLastError()));
    +            return false;
    +        }
    +    }
    +
         // Connect to the addrConnect service on the hSocket socket.
         if (sock.Connect(reinterpret_cast<struct sockaddr*>(&sockaddr), len) == SOCKET_ERROR) {
  12. Nyanraltotlapun commented on Aug 6, 2023

    @Nyanraltotlapun

    Well, I still interested in it. It is a logical thing to do in general.
    I think people uses some workarounds for this when they need this, but have native behavior is right thing to do.

  13. mahdibx commented on Apr 16, 2025

    @mahdibx

    Also, as the score is set by address, having multiple outgoing addresses means that the score of your node will be all over the place depending on which address other nodes know you by. I think it's reasonable to add an option to set the outgoing ipv6 address.

  14. 6 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions