Is it a bad idea to bind() to a particular port in a client program?
It’s occasionally justifiable, but most of the time it’s a very bad idea. I’ve only heard of two good uses of this feature. The first is when your program needs to bind to a port in a particular range. Some implementations of the Berkeley “r commands” (e.g. rlogin, rsh, rcp, etc.) do this for security purposes. Because only the superuser on a Unix system can bind to a low-numbered port (1-1023), such an r command tries, sequentially, to bind to one of the ports in this range until it succeeds. This allows the remote server to surmise that if the connection is coming from a low-numbered port, the remote user must be a superuser. (This port range limit also applies on Windows NT derivatives, but not on Windows 95 derivatives.) The second justifiable example is FTP in its “active” mode: the client binds to a random port and then tells the server to connect to that port for the next data transfer (whether it is an upload, download, or a file listing). This is justifiable because it arguabl
It’s occasionally justifiable, but most of the time it’s a very bad idea. I’ve only heard of two good uses of this feature: • Some protocols demand that the client connection come in from a port in a particular range. Some implementations of the Berkeley “r-commands” (e.g. rlogin, rsh, rcp, etc.) do this for security purposes. Because only privileged users can bind to a low-numbered port (1-1023) on modern operating systems, a connection coming from such a port implies that the remote user is a privileged user. This is one of the very tiny nods to security in the r-command scheme, in that the server program only believes a remote user claiming to be root is who they say they are if the connection comes in on a low-numbered port. (These protocols are otherwise horribly insecure, and thus no longer used on any system that has a clueful sysadmin.) These commands achieve this by attempting to bind, one by one, to each port in this range until it succeeds. This is a Unix-centric view, thoug