| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#include <sys/types.h> |
| 30 |
#include <sys/socket.h> |
| 31 |
#include <sys/time.h> |
| 32 |
|
| 33 |
#include <netdb.h> |
| 34 |
#include <netinet/in.h> |
| 35 |
|
| 36 |
#include <stdlib.h> |
| 37 |
#include <unistd.h> |
| 38 |
#include <errno.h> |
| 39 |
#include <string.h> |
| 40 |
#include <syslog.h> |
| 41 |
|
| 42 |
#include <rpc/rpc.h> |
| 43 |
|
| 44 |
#include "reentrant.h" |
| 45 |
#include "rpc_com.h" |
| 46 |
|
| 47 |
extern pthread_mutex_t port_lock; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
enum { |
| 55 |
LOWPORT = 49152, |
| 56 |
ENDPORT = 65534, |
| 57 |
NPORTS = ENDPORT - LOWPORT + 1, |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
static int is_reserved(char *reserved_ports, int port) { |
| 70 |
port -= LOWPORT; |
| 71 |
if (port < 0 || port >= NPORTS) |
| 72 |
return 0; |
| 73 |
return reserved_ports[port/(8*sizeof(char))] & 1<<(port%(8*sizeof(char))); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
static void set_reserved(char *reserved_ports, int port) { |
| 85 |
port -= LOWPORT; |
| 86 |
if (port < 0 || port >= NPORTS) |
| 87 |
return; |
| 88 |
reserved_ports[port/(8*sizeof(char))] |= 1<<(port%(8*sizeof(char))); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
static int parse_reserved_ports(char *reserved_ports) { |
| 106 |
int from=0, to; |
| 107 |
char delimiter = ','; |
| 108 |
int res; |
| 109 |
FILE * file_ptr = fopen("/proc/sys/net/ipv4/ip_local_reserved_ports","r"); |
| 110 |
if (file_ptr == NULL) { |
| 111 |
(void) syslog(LOG_ERR, |
| 112 |
"Unable to open open /proc/sys/net/ipv4/ip_local_reserved_ports."); |
| 113 |
return -1; |
| 114 |
} |
| 115 |
do { |
| 116 |
if ((res = fscanf(file_ptr, "%d", &to)) != 1) { |
| 117 |
if (res == EOF) break; |
| 118 |
goto err; |
| 119 |
} |
| 120 |
if (delimiter != '-') { |
| 121 |
from = to; |
| 122 |
} |
| 123 |
for (int i = from; i <= to; ++i) { |
| 124 |
set_reserved(reserved_ports, i); |
| 125 |
} |
| 126 |
} while ((res = fscanf(file_ptr, "%c", &delimiter)) == 1); |
| 127 |
if (res != EOF) |
| 128 |
goto err; |
| 129 |
fclose(file_ptr); |
| 130 |
return 0; |
| 131 |
err: |
| 132 |
(void) syslog(LOG_ERR, |
| 133 |
"An error occurred while parsing ip_local_reserved_ports."); |
| 134 |
fclose(file_ptr); |
| 135 |
return -1; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
int __binddynport(int fd) |
| 153 |
{ |
| 154 |
struct sockaddr_storage ss; |
| 155 |
#ifdef INET6 |
| 156 |
struct sockaddr_in6 *sin6; |
| 157 |
#endif |
| 158 |
struct sockaddr_in *sin; |
| 159 |
static unsigned int seed; |
| 160 |
in_port_t port, *portp; |
| 161 |
struct sockaddr *sap; |
| 162 |
socklen_t salen; |
| 163 |
int i, res, array_size; |
| 164 |
char *reserved_ports = NULL; |
| 165 |
|
| 166 |
if (__rpc_sockisbound(fd)) |
| 167 |
return 0; |
| 168 |
|
| 169 |
res = -1; |
| 170 |
sap = (struct sockaddr *)(void *)&ss; |
| 171 |
salen = sizeof(ss); |
| 172 |
memset(sap, 0, salen); |
| 173 |
|
| 174 |
mutex_lock(&port_lock); |
| 175 |
|
| 176 |
if (getsockname(fd, sap, &salen) == -1) |
| 177 |
goto out; |
| 178 |
|
| 179 |
switch (ss.ss_family) { |
| 180 |
case AF_INET: |
| 181 |
sin = (struct sockaddr_in *)(void *)&ss; |
| 182 |
portp = &sin->sin_port; |
| 183 |
salen = sizeof(struct sockaddr_in); |
| 184 |
break; |
| 185 |
#ifdef INET6 |
| 186 |
case AF_INET6: |
| 187 |
sin6 = (struct sockaddr_in6 *)(void *)&ss; |
| 188 |
portp = &sin6->sin6_port; |
| 189 |
salen = sizeof(struct sockaddr_in6); |
| 190 |
break; |
| 191 |
#endif |
| 192 |
default: |
| 193 |
goto out; |
| 194 |
} |
| 195 |
|
| 196 |
if (!seed) { |
| 197 |
struct timeval tv; |
| 198 |
|
| 199 |
gettimeofday(&tv, NULL); |
| 200 |
seed = tv.tv_usec * getpid(); |
| 201 |
} |
| 202 |
array_size = NPORTS / (8*sizeof(char)) + 1; |
| 203 |
reserved_ports = malloc(array_size); |
| 204 |
if (!reserved_ports) { |
| 205 |
goto out; |
| 206 |
} |
| 207 |
memset(reserved_ports, 0, array_size); |
| 208 |
if (parse_reserved_ports(reserved_ports) < 0) |
| 209 |
goto out; |
| 210 |
|
| 211 |
port = (rand_r(&seed) % NPORTS) + LOWPORT; |
| 212 |
for (i = 0; i < NPORTS; ++i) { |
| 213 |
*portp = htons(port); |
| 214 |
if (!is_reserved(reserved_ports, port++)) { |
| 215 |
res = bind(fd, sap, salen); |
| 216 |
if (res >= 0) { |
| 217 |
res = 0; |
| 218 |
break; |
| 219 |
} |
| 220 |
if (errno != EADDRINUSE) |
| 221 |
break; |
| 222 |
} |
| 223 |
if (port > ENDPORT) |
| 224 |
port = LOWPORT; |
| 225 |
} |
| 226 |
|
| 227 |
out: |
| 228 |
free(reserved_ports); |
| 229 |
mutex_unlock(&port_lock); |
| 230 |
return res; |
| 231 |
} |