With the following snapshot of C code, I understand that, the address that bind()
call binds to listfd
, is the logical address of the local machine wh
You cannot bind()
to a remote address, at least not in the AF_INET
family. According to the man page of bind, you will get a EADDRNOTAVAIL
error, saying that the address you wanted to bind to is not local.
Edit: bind()
may work for remote addresses but it certainly does not in the AF_INET
family. Please note that there is more than this. There are probably some families that do indeed support binding to remote addresses, probably some clustering protocols. Even if there are not, bind()
may work on those theoretically in case some protocols emerge where this makes sense at all.
Edit2: As thuovila pointed out, there actually is a case where binding on remote addresses in AF_INET
works. That is, setting the IP_TRANSPARENT
socket option before binding. The man page of ip(7) tells us:
IP_TRANSPARENT (since Linux 2.6.24)
Setting this boolean option enables transparent proxying on
this socket. This socket option allows the calling
application to bind to a nonlocal IP address and operate both
as a client and a server with the foreign address as the local
endpoint. NOTE: this requires that routing be set up in a way
that packets going to the foreign address are routed through
the TProxy box (i.e., the system hosting the application that
employs the IP_TRANSPARENT socket option). Enabling this
socket option requires superuser privileges (the CAP_NET_ADMIN
capability).
TProxy redirection with the iptables TPROXY target also
requires that this option be set on the redirected socket.
So, with a lot of extra work, you can build a transparent proxy by tieing a local and a remote socket together with that socket option set (if I understand correctly).