X-Git-Url: https://git.decadent.org.uk/gitweb/?p=nfs-utils.git;a=blobdiff_plain;f=utils%2Fstatd%2Frmtcall.c;h=fd576d9158b6fa54dd530147401056d9b6b4419d;hp=0e52fe2b81b1f357637b0e824fe1d4da0cc3827f;hb=8239ec6587ce103d7bcb4b37c680c0c10ef5b37c;hpb=d16bd0400fc096ec2fed36d5dfa8620b2370e4d1 diff --git a/utils/statd/rmtcall.c b/utils/statd/rmtcall.c index 0e52fe2..fd576d9 100644 --- a/utils/statd/rmtcall.c +++ b/utils/statd/rmtcall.c @@ -68,24 +68,22 @@ statd_get_socket(void) { struct sockaddr_in sin; struct servent *se; - int loopcnt = 100; + const int loopcnt = 100; + int i, tmp_sockets[loopcnt]; if (sockfd >= 0) return sockfd; - while (loopcnt-- > 0) { - - if (sockfd >= 0) close(sockfd); + for (i = 0; i < loopcnt; ++i) { if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { xlog(L_ERROR, "%s: Can't create socket: %m", __func__); - return -1; + break; } - memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; - sin.sin_addr.s_addr = INADDR_ANY; + sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); if (bindresvport(sockfd, &sin) < 0) { xlog(D_GENERAL, "%s: can't bind to reserved port", @@ -96,7 +94,16 @@ statd_get_socket(void) if (se == NULL) break; /* rather not use that port, try again */ + + tmp_sockets[i] = sockfd; } + + while (--i >= 0) + close(tmp_sockets[i]); + + if (sockfd < 0) + return -1; + FD_SET(sockfd, &SVC_FDSET); return sockfd; }