I am tried to understand the passive socket by tracing the code
http://merkez.ube.ege.edu.tr/~erciyes/ube528/passivesock.c
And I found that the following code
/* Map service name to port number */
if(pse = getservbyname(service, protocol)){
sin.sin_port = htons(ntohs((u_short)pse->s_port) + portbase);
}
else if((sin.sin_port = htons((u_short)atoi(service))) == 0){
errexit("can't get \"%s\" service entry\n", service);
}
I have some question about the htons(ntohs((u_short)pse->s_port) + portbase);
What is the portbase, I found the comment u_short portbase = 0; /* port base, for non-root servers */
but still know what it means?
I have googled it by 'passive socket portbase' but cannot find useful information.
Thx in advance.
Ports below 1024 are system ports and require superuser privileges to access (i.e. root) [1]. As such when running this program as a non root user you may want to remap that port numbers to start at a different base from 0. e.g. by starting at port 1024 no ports will be in the superuser restricted zone (e.g. our well known port 80 will now be 1104). You could choose to map these anywhere.
The comment is the give away:/* port base, for non-root servers */
, combined with the fact that this variable is NEVER set anywhere in that file. Only ever is it read. So when using this C library, you are free to set port base before calling any of the functions in order to remap the ports.