From 314cc45e160cb0a2d6ccb51f95e40c42bed84c4e Mon Sep 17 00:00:00 2001 From: Nathan Straz Date: Wed, 29 Sep 2010 13:41:46 -0500 Subject: Cache last used port in bind_any This way we don't try to bind to ports we already checked --- sockutil.c | 10 ++++++++++ 1 file changed, 10 insertions(+) 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)); -- cgit