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 Aug 30, 2024
1 parent 0f12e6d commit adc753a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions doc/ping.xml
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ xml:id="man.ping">
<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.
See also <emphasis remap="I">IPUTILS_PING_PTR_LOOKUP</emphasis>
environment variable.
</para>
</listitem>
</varlistentry>
Expand Down Expand Up @@ -552,6 +554,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 +781,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
11 changes: 11 additions & 0 deletions ping/ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,17 @@ 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.
*/
char *env = getenv("IPUTILS_PING_PTR_LOOKUP");
if (env && !strcmp(env, "0")) {
rts.opt_numeric = 1;
if (rts.opt_verbose)
error(0, 0, _("WARNING: reverse DNS resolution (PTR lookup) disabled, enforce with -H"));
}

/* 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

0 comments on commit adc753a

Please sign in to comment.