ntirpc
ntirpc copied to clipboard
Q:Why does the bindresvport_sa function use ports 600 ~ 1023?
When ganesha tries to connect to other services, such as rpcbind, the ports 600~1023 are used when calling the bindresvport_sa interface(assuming that AF_LOCAL is not used), is there any particular reason for this, can't we just have the system assign a random source port?
code see:
#ifdef __linux__
#define STARTPORT 600
#define LOWPORT 512
#define ENDPORT (IPPORT_RESERVED - 1)
#define NPORTS (ENDPORT - STARTPORT + 1)
int
bindresvport_sa(int sd, struct sockaddr *sa)
{
............
u_int16_t *portp;
static u_int16_t port;
static short startport = STARTPORT;
socklen_t salen;
int nports = ENDPORT - startport + 1;
int endport = ENDPORT;
int i;
............
if (port == 0)
port = (getpid() % NPORTS) + STARTPORT;
res = -1;
errno = EADDRINUSE;
again:
for (i = 0; i < nports; ++i) {
*portp = htons(port++);
if (port > endport)
port = startport;
res = bind(sd, sa, salen);
if (res >= 0 || errno != EADDRINUSE)
break;
}
if (i == nports && startport != LOWPORT) {
startport = LOWPORT;
endport = STARTPORT - 1;
nports = STARTPORT - LOWPORT;
port = LOWPORT + port % (STARTPORT - LOWPORT);
goto again;
}
return (res);
}
Ganesha uses a lot of ports in this segment when it starts up, binds one for every request, and then releases it, but there is a time_wait time, and the next time it picks a new port, all of which occupy a lot of ports. This time may cause the bind port to fail for application services that use this port.
So can just set port to 0 and let the system assign a port, like this:
sin->sin_port = 0;
sa->sa_family = AF_INET;
res = bind(sd, sa, salen);
Or is there some kind of restriction that the ports here must be 600~1023?