]>
Commit | Line | Data |
---|---|---|
595b6a5e JR |
1 | diff --git a/src/bindresvport.c b/src/bindresvport.c |
2 | index b197efa..bc75d29 100644 | |
3 | --- a/src/bindresvport.c | |
4 | +++ b/src/bindresvport.c | |
5 | @@ -62,6 +62,7 @@ bindresvport(sd, sin) | |
6 | #ifdef __linux__ | |
7 | ||
8 | #define STARTPORT 600 | |
9 | +#define LOWPORT 512 | |
10 | #define ENDPORT (IPPORT_RESERVED - 1) | |
11 | #define NPORTS (ENDPORT - STARTPORT + 1) | |
12 | ||
13 | @@ -76,10 +77,13 @@ bindresvport_sa(sd, sa) | |
14 | #ifdef INET6 | |
15 | struct sockaddr_in6 *sin6; | |
16 | #endif | |
17 | - u_int16_t port; | |
18 | u_int16_t *portp; | |
19 | + static u_int16_t port; | |
20 | + static short startport = STARTPORT; | |
21 | socklen_t salen; | |
22 | - int i; | |
23 | + int nports = ENDPORT - startport + 1; | |
24 | + int endport = ENDPORT; | |
25 | + int i; | |
26 | ||
27 | if (sa == NULL) { | |
28 | salen = sizeof(myaddr); | |
29 | @@ -119,13 +123,22 @@ bindresvport_sa(sd, sa) | |
30 | } | |
31 | res = -1; | |
32 | errno = EADDRINUSE; | |
33 | - for (i = 0; i < NPORTS && res < 0 && errno == EADDRINUSE; i++) { | |
34 | + again: | |
35 | + for (i = 0; i < nports; ++i) { | |
36 | *portp = htons(port++); | |
37 | - if (port > ENDPORT) { | |
38 | - port = STARTPORT; | |
39 | - } | |
40 | + if (port > endport) | |
41 | + port = startport; | |
42 | res = bind(sd, sa, salen); | |
43 | + if (res >= 0 || errno != EADDRINUSE) | |
44 | + break; | |
45 | } | |
46 | + if (i == nports && startport != LOWPORT) { | |
47 | + startport = LOWPORT; | |
48 | + endport = STARTPORT - 1; | |
49 | + nports = STARTPORT - LOWPORT; | |
50 | + port = LOWPORT + port % (STARTPORT - LOWPORT); | |
51 | + goto again; | |
52 | + } | |
53 | return (res); | |
54 | } | |
55 | #else |