diff options
author | Nathan Straz <nstraz@redhat.com> | 2010-09-29 13:41:46 -0500 |
---|---|---|
committer | Nathan Straz <nstraz@redhat.com> | 2010-09-29 13:41:46 -0500 |
commit | 314cc45e160cb0a2d6ccb51f95e40c42bed84c4e (patch) | |
tree | e1c1d72e946e14a2d54cb72c8518bf5de7f3d2b3 /sockutil.c | |
parent | a9b1f95507791f34422084fd47dbf74956902ee1 (diff) | |
download | qarsh-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
Diffstat (limited to 'sockutil.c')
-rw-r--r-- | sockutil.c | 10 |
1 files changed, 10 insertions, 0 deletions
@@ -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)); |