Socket connect to a ports bound to IP vs localhost

bindsocket

It is my understanding that sockets bound to ports using localhost will not be visible to the subnet because the binding is not with the nic IP address. Local processes connecting to such port will create a unix pipe instead of an IP socket. However if bound to the nic IP address the port is visible to the subnet. I want to learn the types of connections made when connecting to ports bound to localhost and the nic IP.

** Port bound to localhost **

  • local process connect using localhost creates a unix pipe
  • local process connect using nic IP creates a unix pipe
  • foreign process connect using nic IP cannot connect

** Port bound to nic IP **

  • local process connect using localhost creates a unix pipe
  • local process connect using nic IP creates an IP socket
  • foreign process connect using nic IP connects and creates an IP socket

Are the above statements correct? I am reviewing network programming from this site:

http://beej.us/guide/bgipc/output/html/multipage/unixsock.html

in hopes of more insight.

Best Answer

Local processes connecting to such port will create a unix pipe instead of an IP socket.

Whether a process uses a pipe or a socket does not depend on the interface per se. It depends on which system services it calls.

To create a named pipe, the program calls mkfifo(2). To get a descriptor on that file, it calls open(2). ls -l shows a "p" in the status bits of a fifo. Anonymous pipes, created with pipe(2), have no name and are invisible to processes without a common ancestor.

To create a TCP socket, it calls socket(2) and then bind(2) to assign it a name for other processes to connect to. If the argument to bind is an external address, it will be visible to the network, else not.

You may be thinking of the loopback address 127.0.0.1. If the program binds to that, it won't be visible or accessible from the network. Only processes running on the host machine can bind or connect to the loopback address.

It is also possible to create a TCP connection over something that looks a bit like a file, known as a UNIX-domain socket. In that case the argument to bind (and connect) has a filename. Because the name appears in the filesystem, it looks a little like a pipe, but ls -l shows an "s" among the status bits.

If you're interested in how all these strange distinctions came to be, NetBSD distributes two papers that date from their invention and are still relevant today. Look for An Advanced 4.4BSD Interprocess Communication Tutorial and An Introductory 4.4BSD Interprocess Communication Tutorial on the web.

Related Question