]> The Tcpdump Group git mirrors - libpcap/commitdiff
rpcap: improve error messages for host and port resolution errors.
authorGuy Harris <[email protected]>
Sun, 31 Jul 2022 18:30:43 +0000 (11:30 -0700)
committerGuy Harris <[email protected]>
Sun, 28 Aug 2022 00:16:34 +0000 (17:16 -0700)
If we don't want a particular port nuber in a sock_initaddress() call,
pass NULL rather than "0".  If the service name parameter passsed to
sock_initaddress() is NULL, pass "0" as the service name parameter to
getaddrinfo().

Have get_gai_errstring() precede the host/port name information with an
indication as to whethe it's a host name, port name, or host name and
port name.  Don't say "host name" for EAI_NONAME; rely on the
description get_gai_errstring() provides.  If there's only a port
number, don't preceded it with ":" in get_gai_errstring().

This makes the error message reported if a host and port are provided
not say that the host name couldn't be resolved, because it could be a
problem with the port name (sadly, getaddinfo() doesn't indicate which
is the one with the problem).

It also makes the error message reported if only a port is provided not
say that it's a problem with the host name or show the "host name" as
":<port>".

(cherry picked from commit 33cf6fb70a13a982d70f6a5e5e63aa765073c8e8)

pcap-rpcap.c
rpcapd/daemon.c
sockutils.c

index 889ade32f69233172ebfd5dff572ca0c49278667..b68af65d52c1ead7b149a4269a9925b8765e0068 100644 (file)
@@ -1020,7 +1020,7 @@ rpcap_remoteact_getsock(const char *host, int *error, char *errbuf)
        hints.ai_family = PF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
 
-       retval = sock_initaddress(host, "0", &hints, &addrinfo, errbuf,
+       retval = sock_initaddress(host, NULL, &hints, &addrinfo, errbuf,
            PCAP_ERRBUF_SIZE);
        if (retval != 0)
        {
@@ -1172,7 +1172,7 @@ static int pcap_startcapture_remote(pcap_t *fp)
                hints.ai_flags = AI_PASSIVE;    /* Data connection is opened by the server toward the client */
 
                /* Let's the server pick up a free network port for us */
-               if (sock_initaddress(NULL, "0", &hints, &addrinfo, fp->errbuf, PCAP_ERRBUF_SIZE) == -1)
+               if (sock_initaddress(NULL, NULL, &hints, &addrinfo, fp->errbuf, PCAP_ERRBUF_SIZE) == -1)
                        goto error_nodiscard;
 
                if ((sockdata = sock_open(addrinfo, SOCKOPEN_SERVER,
@@ -3024,7 +3024,7 @@ int pcap_remoteact_close(const char *host, char *errbuf)
        hints.ai_family = PF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
 
-       retval = sock_initaddress(host, "0", &hints, &addrinfo, errbuf,
+       retval = sock_initaddress(host, NULL, &hints, &addrinfo, errbuf,
            PCAP_ERRBUF_SIZE);
        if (retval != 0)
        {
index 362f4b9bb060f5c6548d305ab4fe89a9f97cfa27..4b91a4324227e2ea1d26fe98dd1970cea135201b 100644 (file)
@@ -2085,8 +2085,8 @@ daemon_msg_startcap_req(uint8 ver, struct daemon_slpars *pars, uint32 plen,
        {
                hints.ai_flags = AI_PASSIVE;
 
-               // Let's the server socket pick up a free network port for us
-               if (sock_initaddress(NULL, "0", &hints, &addrinfo, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
+               // Make the server socket pick up a free network port for us
+               if (sock_initaddress(NULL, NULL, &hints, &addrinfo, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
                        goto error;
 
                if ((session->sockdata = sock_open(addrinfo, SOCKOPEN_SERVER, 1 /* max 1 connection in queue */, errmsgbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
index a34f0d17382213b7a55dc1bd14a81c3f1cdbb311..ca5b683720d9f0b3e40281be3a165060eb435288 100644 (file)
@@ -548,13 +548,13 @@ get_gai_errstring(char *errbuf, int errbuflen, const char *prefix, int err,
        char hostport[PCAP_ERRBUF_SIZE];
 
        if (hostname != NULL && portname != NULL)
-               snprintf(hostport, PCAP_ERRBUF_SIZE, "%s:%s",
+               snprintf(hostport, PCAP_ERRBUF_SIZE, "host and port %s:%s",
                    hostname, portname);
        else if (hostname != NULL)
-               snprintf(hostport, PCAP_ERRBUF_SIZE, "%s",
+               snprintf(hostport, PCAP_ERRBUF_SIZE, "host %s",
                    hostname);
        else if (portname != NULL)
-               snprintf(hostport, PCAP_ERRBUF_SIZE, ":%s",
+               snprintf(hostport, PCAP_ERRBUF_SIZE, "port %s",
                    portname);
        else
                snprintf(hostport, PCAP_ERRBUF_SIZE, "<no host or port!>");
@@ -618,7 +618,7 @@ get_gai_errstring(char *errbuf, int errbuflen, const char *prefix, int err,
 
                case EAI_NONAME:
                        snprintf(errbuf, errbuflen,
-                           "%sThe host name %s couldn't be resolved",
+                           "%sThe %s couldn't be resolved",
                            prefix, hostport);
                        break;
 
@@ -720,7 +720,16 @@ int sock_initaddress(const char *host, const char *port,
 {
        int retval;
 
-       retval = getaddrinfo(host, port, hints, addrinfo);
+       /*
+        * We allow both the host and port to be null, but getaddrinfo()
+        * is not guaranteed to do so; to handle that, if port is null,
+        * we provide "0" as the port number.
+        *
+        * This results in better error messages from get_gai_errstring(),
+        * as those messages won't talk about a problem with the port if
+        * no port was specified.
+        */
+       retval = getaddrinfo(host, port == NULL ? "0" : port, hints, addrinfo);
        if (retval != 0)
        {
                if (errbuf)