Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ DIALECT_NEUTRAL_TESTS = tests/case-00-hello.bash \
tests/case-22-empty-process-name.bash \
tests/case-23-multi-address-hostname.bash \
tests/case-24-dual-stack-socket.bash \
tests/case-25-hostname-port-families.bash \
tests/case-30-json-output.bash \
tests/case-31-jsonl-output.bash
TESTS = $(DIALECT_NEUTRAL_TESTS)
Expand Down
7 changes: 6 additions & 1 deletion lib/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,12 @@ extern int optind;
# endif /* defined(HASIPv6) */

# define MAXDCPATH 4 /* paths in DCpath[] */
# define MAXNWAD 100 /* maximum network addresses */
/*
* A host name with a comma-separated port list needs at least one network
* address entry per port for each address family. Allow for the historical
* 100-port capacity when a name has one address per family.
*/
# define MAXNWAD 200 /* maximum network addresses */

# if !defined(MEMMOVE)
# define MEMMOVE memmove
Expand Down
149 changes: 138 additions & 11 deletions src/arg.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ static int enter_nwad(struct lsof_context *ctx, struct nwad *n, int sp, int ep,
static struct hostent *lkup_hostnm(char *hn, struct nwad *n);
static char *isIPv4addr(char *hn, unsigned char *a, int al);
#if defined(HASIPv6)
static int clone_hostent(struct hostent *src, struct hostent *dst,
char **store);
static void set_nwad_address(struct nwad *n, struct hostent *he);
static int nwad_has_explicit_family(struct lsof_context *ctx, int af);
#endif /* defined(HASIPv6) */

Expand Down Expand Up @@ -1260,6 +1263,13 @@ int enter_network_address(struct lsof_context *ctx, /* context */
#if defined(HASIPv6)
char *cp;
int host_af = 0;
struct hostent he_v6;
struct hostent he_v4;
char *he_v6_store = (char *)NULL;
char *he_v4_store = (char *)NULL;
int have_v6 = 0;
int have_v4 = 0;
int checked_v4 = 0;
#endif /* defined(HASIPv6) */

if (!na) {
Expand Down Expand Up @@ -1352,6 +1362,12 @@ int enter_network_address(struct lsof_context *ctx, /* context */
"%s: no space for protocol name from: -i ", Pn);
safestrprt(na, stderr, 1);
nwad_exit:
#if defined(HASIPv6)
if (he_v6_store)
(void)free((FREE_P *)he_v6_store);
if (he_v4_store)
(void)free((FREE_P *)he_v4_store);
#endif /* defined(HASIPv6) */
if (n.proto)
(void)free((FREE_P *)n.proto);
if (hn)
Expand Down Expand Up @@ -1492,6 +1508,25 @@ int enter_network_address(struct lsof_context *ctx, /* context */
safestrprt(na, stderr, 1);
goto nwad_exit;
}
#if defined(HASIPv6)
/*
* A host name can be followed by a comma-separated port list.
* gethostbyname2() returns storage that the next lookup
* overwrites, so keep a private copy of the IPv6 addresses
* for the later ports.
*/
if (!ft && (n.af == AF_INET6)) {
if (clone_hostent(he, &he_v6, &he_v6_store)) {
(void)fprintf(stderr,
"%s: no space for host addresses: -i ",
Pn);
safestrprt(na, stderr, 1);
goto nwad_exit;
}
have_v6 = 1;
he = &he_v6;
}
#endif /* defined(HASIPv6) */
}
wa = p;
}
Expand Down Expand Up @@ -1640,11 +1675,15 @@ int enter_network_address(struct lsof_context *ctx, /* context */
nwad_enter:

#if defined(HASIPv6)
/* Restore the first hostname family after the previous port's lookup. */
if (hn && n.af != host_af) {
/*
* Reuse the cached host name addresses for every port. The IPv6
* addresses were copied when the name was resolved; the IPv4
* fallback is copied when it is first looked up.
*/
if (have_v6) {
n.af = host_af;
if (!(he = lkup_hostnm(hn, &n)))
goto unacc_address;
he = &he_v6;
set_nwad_address(&n, he);
}
#endif /* defined(HASIPv6) */

Expand All @@ -1654,15 +1693,41 @@ int enter_network_address(struct lsof_context *ctx, /* context */

#if defined(HASIPv6)
/*
* If IPv6 is enabled, a host name was specified, and the
* associated * address is for the AF_INET6 address family,
* try to get and address for the AF_INET family, too, unless
* IPv4 is prohibited.
* If a host name resolved to IPv6, also search the IPv4 family,
* unless the lookups are restricted to IPv6. The IPv4 hostent
* is copied on first use so that later ports don't have to
* repeat the lookup.
*/
if (hn && (n.af == AF_INET6) && (ft != 6)) {
n.af = AF_INET;
if ((he = lkup_hostnm(hn, &n)))
if (have_v6 && (n.af == AF_INET6)) {
if (have_v4) {
n.af = AF_INET;
he = &he_v4;
set_nwad_address(&n, he);
continue;
}
if (!checked_v4) {
/*
* Try the IPv4 lookup once. If the name has no IPv4
* addresses, later ports don't retry it.
*/
n.af = AF_INET;
he = lkup_hostnm(hn, &n);
checked_v4 = 1;
if (he) {
if (clone_hostent(he, &he_v4,
&he_v4_store)) {
(void)fprintf(
stderr,
"%s: no space for host addresses: -i ", Pn);
safestrprt(na, stderr, 1);
goto nwad_exit;
}
have_v4 = 1;
he = &he_v4;
set_nwad_address(&n, he);
continue;
}
}
}
#endif /* defined(HASIPv6) */

Expand All @@ -1677,6 +1742,12 @@ int enter_network_address(struct lsof_context *ctx, /* context */
(void)free((FREE_P *)n.proto);
if (hn)
(void)free((FREE_P *)hn);
#if defined(HASIPv6)
if (he_v6_store)
(void)free((FREE_P *)he_v6_store);
if (he_v4_store)
(void)free((FREE_P *)he_v4_store);
#endif /* defined(HASIPv6) */
return (0);
}

Expand Down Expand Up @@ -2237,6 +2308,62 @@ static char *isIPv4addr(char *hn, /* host name */
return (hn);
}

#if defined(HASIPv6)
/*
* set_nwad_address() - copy a hostent's first address into an nwad
*
* enter_nwad() uses the address already in the nwad for its first entry, so
* the cached hostent's first address must be restored before each use.
*/

static void
set_nwad_address(struct nwad *n, /* network address destination */
struct hostent *he) /* cached hostent */
{
zeromem((char *)n->a, sizeof(n->a));
(void)memcpy((void *)n->a, (void *)he->h_addr_list[0],
(size_t)he->h_length);
}

/*
* clone_hostent() - copy a hostent's address list into private storage
*
* gethostbyname2() returns a pointer to static storage that the next lookup
* overwrites. Save the addresses so a host name's family can be reused for
* every port in a comma-separated list.
*/

static int
clone_hostent(struct hostent *src, /* hostent to copy */
struct hostent *dst, /* destination hostent */
char **store) /* returned malloc'd storage */
{
char *data, **list;
size_t alen, i, n;
if (!src || !src->h_addr_list || !src->h_addr_list[0])
return (1);
for (n = 0; src->h_addr_list[n]; n++)
;
alen = ((n + 1) * sizeof(char *)) + (n * (size_t)src->h_length);
if ((data = (char *)malloc(alen)) == NULL)
return (1);
list = (char **)data;
data += (n + 1) * sizeof(char *);
for (i = 0; i < n; i++) {
(void)memcpy(data + (i * (size_t)src->h_length), src->h_addr_list[i],
(size_t)src->h_length);
list[i] = data + (i * (size_t)src->h_length);
}
list[n] = (char *)NULL;
*dst = *src;
dst->h_name = (char *)NULL;
dst->h_aliases = (char **)NULL;
dst->h_addr_list = list;
*store = (char *)list;
return (0);
}
#endif /* defined(HASIPv6) */

/*
* lkup_hostnm() - look up host name
*/
Expand Down
75 changes: 31 additions & 44 deletions tests/case-23-multi-address-hostname.bash
Original file line number Diff line number Diff line change
Expand Up @@ -12,66 +12,53 @@ if ! command -v python3 >/dev/null 2>&1; then
exit 77
fi

# Every port in a hostname filter must search both address families.
python3 - "$lsof" <<'PY' >> "$report" 2>&1 || exit 1
import os
import socket
import subprocess
import sys

try:
sockets = []
for family, address in ((socket.AF_INET, "127.0.0.1"),
(socket.AF_INET6, "::1")):
if address not in {x[4][0] for x in socket.getaddrinfo(
"localhost", None, family, socket.SOCK_STREAM)}:
raise OSError(f"localhost does not resolve to {address}")
s = socket.socket(family, socket.SOCK_STREAM)
s.bind((address, 0))
s.listen(1)
sockets.append(s)
except OSError as e:
print(f"SKIP: dual-family localhost unavailable: {e}")
else:
ports = [str(s.getsockname()[1]) for s in sockets]
expected = {str(s.fileno()) for s in sockets}
for order in (["0"] + ports, ports[::-1]):
result = subprocess.run(
[sys.argv[1], "-nP", "-a", "-p", str(os.getpid()), "-Ff",
"-iTCP@localhost:" + ",".join(order)],
capture_output=True, text=True, timeout=10)
found = {line[1:] for line in result.stdout.splitlines()
if line.startswith("f")}
assert result.returncode == 0 and found == expected, (
order, result.returncode, found, expected, result.stderr)
print("PASS: hostname port lists search both families in either order")
PY

host=$(hostname)
naddr=$(python3 - "$host" <<'PY'
count_addresses() {
python3 - "$1" <<'PY'
import socket
import sys

host = sys.argv[1]
count = 0
addresses = set()
for family in (socket.AF_INET, socket.AF_INET6):
try:
infos = socket.getaddrinfo(host, None, family, socket.SOCK_STREAM)
except OSError:
continue
count = max(count, len({info[4][0] for info in infos}))
print(count)
for info in infos:
address = info[4][0]
if family == socket.AF_INET6:
try:
packed = socket.inet_pton(socket.AF_INET6, address)
except OSError:
continue
# Ignore v4-mapped IPv6 addresses; the IPv4 lookup counts them.
if packed[:12] == b"\0" * 10 + b"\xff\xff":
continue
addresses.add(address)
print(len(addresses))
PY
)
}

host=$(hostname)
naddr=$(count_addresses "$host")
if [ "${naddr:-0}" -lt 2 ]; then
# Prefer localhost: most systems resolve it to one IPv4 and one IPv6
# loopback address, which is enough to exercise the multiple-entry path
# without requiring additional privileges.
localhost_naddr=$(count_addresses localhost)
if [ "${localhost_naddr:-0}" -ge 2 ]; then
host=localhost
naddr=$localhost_naddr
fi
fi

if [ "${naddr:-0}" -lt 2 ]; then
echo "SKIP: $host resolves to fewer than two addresses in one family" \
>> "$report"
echo "SKIP: $host resolves to fewer than two addresses" >> "$report"
exit 77
fi

{
echo "host name: $host ($naddr addresses in one address family)"
echo "host name: $host ($naddr addresses)"
$lsof -a -i@"$host" -p $$ >/dev/null 2>&1
status=$?
case $status in
Expand Down
Loading
Loading