bashpingsymlink

How does ping know that it was called by ping6 symbolic link?


When I'm executing ping4 linux.com in a bash terminal on Linux Mint 22.1 Kernel 6.8.0.55 the URL is resolved to a IPv4 address, when executing ping6 linux.com the URL is resolved to a IPv6 adress as expected. As which shows, both, ping4 and ping6, are symbolic links in /usr/bin/ to ping (also in /usr/bin/). So where comes the information for ping from, that it is called via ping4 or ping6? Calling ping4 -6 linux.com or ping6 -4 linux.com both result in only one -4 or -6 option may be specified

I would expect that ping4 and ping6 behave the same way as ping, because they are only symbolic links to ping.


Solution

  • If it is the iputils version of ping:

        /* Support being called using `ping4` or `ping6` symlinks */
        if (argv[0][strlen(argv[0]) - 1] == '4')
            hints.ai_family = AF_INET;
        else if (argv[0][strlen(argv[0]) - 1] == '6')
            hints.ai_family = AF_INET6;
    

    and later:

            case '4':
                if (hints.ai_family == AF_INET6)
                    error(2, 0, _("only one -4 or -6 option may be specified"));
                hints.ai_family = AF_INET;
                break;
    

    Even if you don't know the language that a program is written in, skimming its source can give useful information.