From f6049fdd4722ff4a4a0cfc6498653ce101a7e646 Mon Sep 17 00:00:00 2001 Message-Id: From: Mark Wooding Date: Fri, 6 May 2005 15:02:59 +0000 Subject: [PATCH] Allow user control over autobinding. Also try ports at random to start off with, falling back to linear search. Organization: Straylight/Edgeware From: mdw --- noip.1 | 10 +++++++ noip.c | 83 ++++++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 73 insertions(+), 20 deletions(-) diff --git a/noip.1 b/noip.1 index 36b4cf0..d11ce7d 100644 --- a/noip.1 +++ b/noip.1 @@ -114,6 +114,16 @@ rather than the default. The environment variable .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 diff --git a/noip.c b/noip.c index 2f7fd14..61d4a62 100644 --- a/noip.c +++ b/noip.c @@ -42,6 +42,7 @@ static int n_local_ipaddrs; 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; @@ -218,6 +219,19 @@ static void dump_acl(aclnode *a) #endif +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) @@ -244,8 +258,14 @@ static int encode_inet_addr(struct sockaddr_un *sun, 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 (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); @@ -493,20 +513,15 @@ static void parse_ports(char **pp, unsigned short *min, unsigned short *max) int del; SKIPSPC; - if (*p != ':') { - *min = 0; - *max = 0xffff; - } else { + if (*p != ':') + { *min = 0; *max = 0xffff; } + else { p++; - NEXTNUMBER(q, del); - *min = atoi(q); - RESCAN(del); + NEXTNUMBER(q, del); *min = strtoul(q, 0, 0); RESCAN(del); SKIPSPC; - if (*p == '-') { - NEXTNUMBER(q, del); - *max = atoi(q); - RESCAN(del); - } else + if (*p == '-') + { NEXTNUMBER(q, del); *max = strtoul(q, 0, 0); RESCAN(del); } + else *max = *min; } *pp = p; @@ -576,7 +591,7 @@ static void parse_acl_line(char **pp, aclnode ***tail) if (inet_pton(AF_INET, q, &addr) <= 0) goto bad; mask = ntohl(addr.s_addr); } else { - n = atoi(q); + n = strtoul(q, 0, 0); mask = (~0ul << (32 - n)) & 0xffffffff; } RESCAN(del); @@ -600,13 +615,32 @@ bad: return; } +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) { - char *p; + char *p, *q; if ((p = getenv(var)) != 0) { - p = xstrdup(p); - parse_acl_line(&p, tail); + p = q = xstrdup(p); + parse_acl_line(&q, tail); free(p); } } @@ -616,16 +650,21 @@ static void readconfig(void) FILE *fp; char buf[1024]; size_t n; - char *p, *cmd; + char *p, *q, *cmd; 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); + 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; @@ -643,6 +682,8 @@ static void readconfig(void) 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 @@ -663,6 +704,8 @@ done: 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"); -- [mdw]