From: Mark Wooding Date: Wed, 6 Jun 2018 19:10:14 +0000 (+0100) Subject: noip.c (encode_..._inet_addr: Abstract out checking a single address. X-Git-Tag: 1.2.0~22 X-Git-Url: https://git.distorted.org.uk/~mdw/preload-hacks/commitdiff_plain/81e3737d842818a305e23802405f4e15b5e8381d noip.c (encode_..._inet_addr: Abstract out checking a single address. There's this common pattern of building the Unix-domain socket address, probing its status, maybe unlinking the socket if it's stale, and returning the result. Let's only write that once. No functional change. --- diff --git a/noip.c b/noip.c index 8806d02..1d40850 100644 --- a/noip.c +++ b/noip.c @@ -679,6 +679,23 @@ done: return (rc); } +/* Encode SA as a Unix-domain address SUN, and return whether it's currently + * in use. + */ +static int encode_single_inet_addr(const struct sockaddr *sa, + struct sockaddr_un *sun, + int quickp) +{ + char buf[ADDRBUFSZ]; + int rc; + + 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); +} + /* Convert the IP address SA to a Unix-domain address SUN. Fail if the * address seems already taken. If DESPARATEP then try cleaning up stale old * sockets. @@ -689,28 +706,22 @@ static int encode_unused_inet_addr(struct sockaddr *sa, { address waddr; struct sockaddr_un wsun; - int rc; - char buf[ADDRBUFSZ]; int port = port_from_sockaddr(sa); /* First, look for an exact match. Only look quickly unless we're * desperate. If the socket is in use, we fail here. (This could get * racy. Let's not worry about that for now.) */ - snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s", sockdir, - present_sockaddr(sa, 0, buf, sizeof(buf))); - if ((rc = unix_socket_status(sun, !desperatep)) == USED) return (-1); - else if (rc == STALE) unlink(sun->sun_path); + if (encode_single_inet_addr(sa, sun, !desperatep) == USED) + return (-1); /* Next, check the corresponding wildcard address, so as to avoid * inadvertant collisions with listeners. Do this in the same way. */ wildcard_address(sa->sa_family, &waddr.sa); port_to_sockaddr(&waddr.sa, port); - snprintf(wsun.sun_path, sizeof(wsun.sun_path), "%s/%s", sockdir, - present_sockaddr(&waddr.sa, 0, buf, sizeof(buf))); - if ((rc = unix_socket_status(&wsun, !desperatep)) == USED) return (-1); - else if (rc == STALE) unlink(wsun.sun_path); + if (encode_single_inet_addr(&waddr.sa, &wsun, !desperatep) == USED) + return (-1); /* All is well. */ return (0); @@ -732,7 +743,6 @@ static int encode_inet_addr(struct sockaddr_un *sun, address addr; int port = port_from_sockaddr(sa); char buf[ADDRBUFSZ]; - int rc; D( fprintf(stderr, "noip(%d): encode %s (%s)", getpid(), present_sockaddr(sa, 0, buf, sizeof(buf)), @@ -746,20 +756,15 @@ static int encode_inet_addr(struct sockaddr_un *sun, /* Try the address as given. If it's in use, or we don't necessarily * want an existing socket, then we're done. */ - snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s", sockdir, - present_sockaddr(sa, 0, buf, sizeof(buf))); - rc = unix_socket_status(sun, 0); - if (rc == STALE) unlink(sun->sun_path); - if (rc == USED || (f&ENCF_FRESH)) goto found; + if (encode_single_inet_addr(sa, sun, 0) == USED || (f&ENCF_FRESH)) + goto found; /* We're looking for a socket which already exists. Try the * corresponding wildcard address. */ wildcard_address(sa->sa_family, &addr.sa); port_to_sockaddr(&addr.sa, port); - snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s", sockdir, - present_sockaddr(&addr.sa, 0, buf, sizeof(buf))); - if (unix_socket_status(sun, 0) == STALE) unlink(sun->sun_path); + encode_single_inet_addr(&addr.sa, sun, 0); } else { /* We want a fresh new socket. */