off with, falling back to linear search.
.B NOIP_SOCKETDIR
can also be used to control which directory is used for sockets.
.TP
.B NOIP_SOCKETDIR
can also be used to control which directory is used for sockets.
.TP
+.BI "autoports " min "\-" max
+Select which ports are used for implicit binding. Allocating ports can
+be a bit slow, since checking whether a Unix domain socket is in use is
+difficult. A wide range makes things easier, because
+.B noip
+starts by trying ports at random from the given range. The environment
+variable
+.B NOIP_AUTOPORTS
+can also be used to control which ports are assigned automatically.
+.TP
.BI "realbind " acl-entry
Add an entry to the
.B realbind
.BI "realbind " acl-entry
Add an entry to the
.B realbind
static uid_t uid;
static char *sockdir = 0;
static int debug = 0;
static uid_t uid;
static char *sockdir = 0;
static int debug = 0;
+static unsigned minautoport = 16384, maxautoport = 65536;
static aclnode *bind_real, **bind_tail = &bind_real;
static aclnode *connect_real, **connect_tail = &connect_real;
static aclnode *bind_real, **bind_tail = &bind_real;
static aclnode *connect_real, **connect_tail = &connect_real;
+static unsigned randrange(unsigned min, unsigned max)
+{
+ unsigned mask, i;
+
+ /* It's so nice not to have to care about the quality of the generator
+ much! */
+ max -= min;
+ for (mask = 1; mask < max; mask = (mask << 1) | 1)
+ ;
+ do i = rand() & mask; while (i > max);
+ return (i + min);
+}
+
static int encode_inet_addr(struct sockaddr_un *sun,
const struct sockaddr_in *sin,
int want)
static int encode_inet_addr(struct sockaddr_un *sun,
const struct sockaddr_in *sin,
int want)
if (unix_socket_status(sun, 0) == STALE) unlink(sun->sun_path);
}
} else {
if (unix_socket_status(sun, 0) == STALE) unlink(sun->sun_path);
}
} else {
+ for (i = 0; i < 10; i++) {
+ snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s:%u", sockdir,
+ inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)),
+ randrange(minautoport, maxautoport));
+ if (unix_socket_status(sun, 1) == UNUSED) goto found;
+ }
for (desperate_p = 0; desperate_p < 2; desperate_p++) {
for (desperate_p = 0; desperate_p < 2; desperate_p++) {
- for (i = 16384; i < 65536; i++) {
+ for (i = minautoport; i <= maxautoport; i++) {
snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s:%u", sockdir,
inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)),
(unsigned)i);
snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s:%u", sockdir,
inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)),
(unsigned)i);
- if (*p != ':') {
- *min = 0;
- *max = 0xffff;
- } else {
+ if (*p != ':')
+ { *min = 0; *max = 0xffff; }
+ else {
- NEXTNUMBER(q, del);
- *min = atoi(q);
- RESCAN(del);
+ NEXTNUMBER(q, del); *min = strtoul(q, 0, 0); RESCAN(del);
- if (*p == '-') {
- NEXTNUMBER(q, del);
- *max = atoi(q);
- RESCAN(del);
- } else
+ if (*p == '-')
+ { NEXTNUMBER(q, del); *max = strtoul(q, 0, 0); RESCAN(del); }
+ else
if (inet_pton(AF_INET, q, &addr) <= 0) goto bad;
mask = ntohl(addr.s_addr);
} else {
if (inet_pton(AF_INET, q, &addr) <= 0) goto bad;
mask = ntohl(addr.s_addr);
} else {
mask = (~0ul << (32 - n)) & 0xffffffff;
}
RESCAN(del);
mask = (~0ul << (32 - n)) & 0xffffffff;
}
RESCAN(del);
+static void parse_autoports(char **pp)
+{
+ char *p = *pp, *q;
+ unsigned x, y;
+ int del;
+
+ SKIPSPC;
+ NEXTNUMBER(q, del); x = strtoul(q, 0, 0); RESCAN(del);
+ SKIPSPC;
+ if (*p != '-') goto bad; p++;
+ NEXTNUMBER(q, del); y = strtoul(q, 0, 0); RESCAN(del);
+ minautoport = x; maxautoport = y;
+ return;
+
+bad:
+ D( fprintf(stderr, "bad port range (ignored)\n"); )
+ return;
+}
+
static void parse_acl_env(const char *var, aclnode ***tail)
{
static void parse_acl_env(const char *var, aclnode ***tail)
{
if ((p = getenv(var)) != 0) {
if ((p = getenv(var)) != 0) {
- p = xstrdup(p);
- parse_acl_line(&p, tail);
+ p = q = xstrdup(p);
+ parse_acl_line(&q, tail);
FILE *fp;
char buf[1024];
size_t n;
FILE *fp;
char buf[1024];
size_t n;
if ((p = getenv("NOIP_CONFIG")) == 0)
snprintf(p = buf, sizeof(buf), "%s/.noip", home());
D( fprintf(stderr, "noip: config file: %s\n", p); )
if ((p = getenv("NOIP_CONFIG")) == 0)
snprintf(p = buf, sizeof(buf), "%s/.noip", home());
D( fprintf(stderr, "noip: config file: %s\n", p); )
- if ((fp = fopen(p, "r")) == 0)
- goto done;
parse_acl_env("NOIP_REALBIND_BEFORE", &bind_tail);
parse_acl_env("NOIP_REALCONNECT_BEFORE", &connect_tail);
parse_acl_env("NOIP_REALBIND_BEFORE", &bind_tail);
parse_acl_env("NOIP_REALCONNECT_BEFORE", &connect_tail);
+ if ((p = getenv("NOIP_AUTOPORTS")) != 0) {
+ p = q = xstrdup(p);
+ parse_autoports(&q);
+ free(p);
+ }
+ if ((fp = fopen(p, "r")) == 0)
+ goto done;
while (fgets(buf, sizeof(buf), fp)) {
n = strlen(buf);
p = buf;
while (fgets(buf, sizeof(buf), fp)) {
n = strlen(buf);
p = buf;
parse_acl_line(&p, &bind_tail);
else if (strcmp(cmd, "realconnect") == 0)
parse_acl_line(&p, &connect_tail);
parse_acl_line(&p, &bind_tail);
else if (strcmp(cmd, "realconnect") == 0)
parse_acl_line(&p, &connect_tail);
+ else if (strcmp(cmd, "autoports") == 0)
+ parse_autoports(&p);
else if (strcmp(cmd, "debug") == 0)
debug = *p ? atoi(p) : 1;
else
else if (strcmp(cmd, "debug") == 0)
debug = *p ? atoi(p) : 1;
else
sockdir = xstrdup(buf);
}
D( fprintf(stderr, "noip: sockdir: %s\n", sockdir);
sockdir = xstrdup(buf);
}
D( fprintf(stderr, "noip: sockdir: %s\n", sockdir);
+ fprintf(stderr, "noip: autoports: %u-%u\n",
+ minautoport, maxautoport);
fprintf(stderr, "noip: realbind acl:\n");
dump_acl(bind_real);
fprintf(stderr, "noip: realconnect acl:\n");
fprintf(stderr, "noip: realbind acl:\n");
dump_acl(bind_real);
fprintf(stderr, "noip: realconnect acl:\n");