#define UNUSED 0u /* No sign of anyone using it */
#define STALE 1u /* Socket exists, but is abandoned */
#define USED 16u /* Socket is in active use */
+#define LISTEN 2u /* Socket has an active listener */
enum { DENY, ALLOW }; /* ACL verdicts */
FILE *fp = 0;
size_t len, n;
int rc;
+ unsigned long f;
char buf[256];
/* If we can't find the socket node, then it's definitely not in use. If
if (n >= len + 2 && buf[n - len - 2] == ' ' && buf[n - 1] == '\n' &&
memcmp(buf + n - len - 1, sun->sun_path, len) == 0) {
rc |= USED;
+ if (sscanf(buf, "%*s %*x %*x %lx", &f) < 0 || (f&0x00010000))
+ rc |= LISTEN;
}
}
if (ferror(fp))
snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s", sockdir,
present_sockaddr(sa, 0, buf, sizeof(buf)));
- if ((rc = unix_socket_status(sun, quickp)) == USED) return (USED);
- else if (rc == STALE) unlink(sun->sun_path);
- return (UNUSED);
+ rc = unix_socket_status(sun, quickp);
+ if (rc == STALE) unlink(sun->sun_path);
+ return (rc);
}
/* Convert the IP address SA to a Unix-domain address SUN. Fail if the
* Returns zero on success; -1 on failure.
*/
#define ENCF_FRESH 1u
+#define ENCF_REUSEADDR 2u
static int encode_inet_addr(struct sockaddr_un *sun,
const struct sockaddr *sa,
unsigned f)
* want an existing socket, then we're done.
*/
rc = encode_single_inet_addr(sa, sun, 0);
+ if ((f&ENCF_REUSEADDR) && !(rc&LISTEN)) unlink(sun->sun_path);
if ((rc&USED) || (f&ENCF_FRESH)) goto found;
/* We're looking for a socket which already exists. This is
{
struct sockaddr_un sun;
int rc;
+ unsigned f;
+ int reusep;
+ socklen_t n;
Dpid;
D({ char buf[ADDRBUFSZ];
if (fixup_real_ip_socket(sk, sa->sa_family, 0))
return (-1);
} else {
- encode_inet_addr(&sun, sa, ENCF_FRESH);
+ f = ENCF_FRESH;
+ n = sizeof(reusep);
+ if (!getsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &reusep, &n) && reusep)
+ f |= ENCF_REUSEADDR;
+ encode_inet_addr(&sun, sa, f);
sa = SA(&sun);
len = SUN_LEN(&sun);
}
D( fprintf(stderr, "noip(%d): ... GETPEERNAME", pid); )
}
D( dump_addrresult(rc, sa, *len); )
- return (0);
+ return (rc);
}
int getsockopt(int sk, int lev, int opt, void *p, socklen_t *len)