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)
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)
{
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,
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)
{
{
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)
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!>");
case EAI_NONAME:
snprintf(errbuf, errbuflen,
- "%sThe host name %s couldn't be resolved",
+ "%sThe %s couldn't be resolved",
prefix, hostport);
break;
{
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)