Can bind() socket to remote address?

后端 未结 2 1554
长发绾君心
长发绾君心 2021-01-21 21:44

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

相关标签:
2条回答
  • 2021-01-21 21:57

    I agree the Coursera material is probably wrong or at least misleading for introduction level learning about network programming.

    However, I can not suppress a pedantic comment. It is possible (on Linux) to bind to a non-local IP address. Please see option IP_TRANSPARENT at http://man7.org/linux/man-pages/man7/ip.7.html. This probably has very little to do with the issue at hand, though. Also i appreciate the question is tagged bsd.

    More on the point, bind() does not have anything to do with listening. It simply associates the fd with an address. It is the listen() call that enables "listening". It is possible to bind() a socket used for outgoing connections.

    0 讨论(0)
  • 2021-01-21 22:04

    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).

    0 讨论(0)
提交回复
热议问题