summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathan Straz <nstraz@redhat.com>2010-09-29 13:41:46 -0500
committerNathan Straz <nstraz@redhat.com>2010-09-29 13:41:46 -0500
commit314cc45e160cb0a2d6ccb51f95e40c42bed84c4e (patch)
treee1c1d72e946e14a2d54cb72c8518bf5de7f3d2b3
parenta9b1f95507791f34422084fd47dbf74956902ee1 (diff)
downloadqarsh-314cc45e160cb0a2d6ccb51f95e40c42bed84c4e.tar.gz
qarsh-314cc45e160cb0a2d6ccb51f95e40c42bed84c4e.tar.xz
qarsh-314cc45e160cb0a2d6ccb51f95e40c42bed84c4e.zip
Cache last used port in bind_any
This way we don't try to bind to ports we already checked
-rw-r--r--sockutil.c10
1 files changed, 10 insertions, 0 deletions
diff --git a/sockutil.c b/sockutil.c
index 05097b2..f13bcaf 100644
--- a/sockutil.c
+++ b/sockutil.c
@@ -49,8 +49,16 @@ getsockport(int sd)
int
bind_any(int minport)
{
+ static int lastminport = 0;
+ static int nextport = 0;
int sd;
struct sockaddr_in addr;
+
+ if (minport == lastminport) {
+ minport = nextport;
+ } else {
+ lastminport = minport;
+ }
sd = socket(AF_INET, SOCK_STREAM, 0);
if (sd == -1) return -1;
@@ -59,6 +67,8 @@ bind_any(int minport)
do {
addr.sin_port = htons(minport++);
} while (bind(sd, (struct sockaddr *)&addr, sizeof addr) != 0);
+ nextport = minport;
+
if (listen(sd, 0) == -1) {
syslog(LOG_WARNING, "listen error %d, %s", errno,
strerror(errno));