Skip to content

Commit

Permalink
ping: Allow to disable with environment variable
Browse files Browse the repository at this point in the history
Allow to disable reverse DNS resolution (PTR lookup) with
IPUTILS_PING_PTR_LOOKUP environment variable set to 0:

    $ IPUTILS_PING_PTR_LOOKUP=0 ./builddir/ping/ping -c1 google.com
    PING google.com (142.251.37.110) 56(84) bytes of data.
    64 bytes from 142.251.37.110: icmp_seq=1 ttl=116 time=11.1 ms

It's off by default (we are conservative, most of the users does not
have problem thus why to loose the functionality):

    $ ./builddir/ping/ping -c1 google.com
    PING google.com (142.251.37.110) 56(84) bytes of data.
    64 bytes from prg03s13-in-f14.1e100.net (142.251.37.110): icmp_seq=1 ttl=116 time=18.6 ms

-H/-n override the variable:

    $ IPUTILS_PING_PTR_LOOKUP=0 ./builddir/ping/ping -c1 -H google.com
    PING google.com (142.251.37.110) 56(84) bytes of data.
    64 bytes from prg03s13-in-f14.1e100.net (142.251.37.110): icmp_seq=1 ttl=116 time=17.1 ms

    $ IPUTILS_PING_PTR_LOOKUP= ./builddir/ping/ping -c1 -n google.com
    PING google.com (142.251.37.110) 56(84) bytes of data.
    64 bytes from 142.251.36.142: icmp_seq=1 ttl=116 time=15.8 ms

This help users to easier disable reverse DNS resolution than alias ping='ping -H'
which would not work in scripts.

Update man page.

Implements: iputils#531
Closes: iputils#553
Signed-off-by: Petr Vorel <[email protected]>
  • Loading branch information
pevik committed Sep 2, 2024
1 parent 0f12e6d commit e0b31a5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
16 changes: 15 additions & 1 deletion doc/ping.xml
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,10 @@ xml:id="man.ping">
<listitem>
<para>Force DNS name resolution for the output. Useful for numeric
destination, or <option>-f</option> option, which by default do not
perform it. Override previously defined <option>-n</option> option.
perform it. It can also help to workaround DNS resolution problems.
Override previously defined <option>-n</option> option.
See also <emphasis remap="I">IPUTILS_PING_PTR_LOOKUP</emphasis>
environment variable.
</para>
</listitem>
</varlistentry>
Expand Down Expand Up @@ -552,6 +555,8 @@ xml:id="man.ping">
symbolic names for host addresses (no reverse DNS resolution).
This is the default for numeric destination or <option>-f</option>
option. Override previously defined <option>-H</option> option.
See also <emphasis remap="I">IPUTILS_PING_PTR_LOOKUP</emphasis>
environment variable.
</para>
</listitem>
</varlistentry>
Expand Down Expand Up @@ -777,6 +782,15 @@ xml:id="man.ping">
automated scripts.</para>
</refsection>

<refsection xml:id="environment">
<info>
<title>ENVIRONMENT</title>
</info>
<emphasis remap="I">IPUTILS_PING_PTR_LOOKUP</emphasis> environment
variable set to 0 disable reverse DNS resolution (PTR lookup) by default.
It will be overrided by <option>-H</option> or <option>-n</option> option.
</refsection>

<refsection xml:id="exit_status">
<info>
<title>EXIT STATUS</title>
Expand Down
15 changes: 15 additions & 0 deletions ping/ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,18 @@ main(int argc, char **argv)
else if (argv[0][strlen(argv[0]) - 1] == '6')
hints.ai_family = AF_INET6;

/*
* Optionally disable reverse DNS resolution (PTR lookup) by default.
* -n/-H override the variable.
* NOTE: warn below if disabled due this.
*/
char *env = getenv("IPUTILS_PING_PTR_LOOKUP");
int force_numeric = 0;
if (env && !strcmp(env, "0")) {
rts.opt_numeric = 1;
force_numeric = 1;
}

/* Parse command line options */
while ((ch = getopt(argc, argv, "h?" "4bRT:" "6F:N:" "3aABc:CdDe:fHi:I:l:Lm:M:nOp:qQ:rs:S:t:UvVw:W:")) != EOF) {
switch(ch) {
Expand Down Expand Up @@ -570,6 +582,9 @@ main(int argc, char **argv)
}
}

if (rts.opt_numeric && force_numeric && rts.opt_verbose)
error(0, 0, _("WARNING: reverse DNS resolution (PTR lookup) disabled, enforce with -H"));

argc -= optind;
argv += optind;

Expand Down

0 comments on commit e0b31a5

Please sign in to comment.