public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Ken Brown <kbrown@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: getifaddrs: don't return a zero IPv4 address
Date: Wed, 28 Jul 2021 11:45:38 +0000 (GMT)	[thread overview]
Message-ID: <20210728114538.956B4399203E@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=b88d686a68d7bf2ff22d857a28d37fe4fdbafdf2

commit b88d686a68d7bf2ff22d857a28d37fe4fdbafdf2
Author: Ken Brown <kbrown@cornell.edu>
Date:   Mon Jul 26 10:27:53 2021 -0400

    Cygwin: getifaddrs: don't return a zero IPv4 address
    
    If an interface is disconnected, net.cc:get_ifs tries to fetch IPv4
    addresses from the registry.  If it fails, it currently returns
    pointers to sockaddr structs with zero address.  Return a NULL pointer
    instead, to signal the caller of getifaddrs that we do not have a
    valid struct sockaddr.
    
    Partially addresses: https://cygwin.com/pipermail/cygwin/2021-July/248970.html

Diff:
---
 winsup/cygwin/net.cc | 29 +++++++++++++++++++++++------
 1 file changed, 23 insertions(+), 6 deletions(-)

diff --git a/winsup/cygwin/net.cc b/winsup/cygwin/net.cc
index 67dd7fc04..5bdc8709f 100644
--- a/winsup/cygwin/net.cc
+++ b/winsup/cygwin/net.cc
@@ -1595,6 +1595,7 @@ static void
 get_ipv4fromreg (struct ifall *ifp, const char *name, DWORD idx)
 {
   WCHAR regkey[256], *c;
+  bool got_addr = false, got_mask = false;
 
   c = wcpcpy (regkey, L"Tcpip\\Parameters\\Interfaces\\");
   sys_mbstowcs (c, 220, name);
@@ -1637,9 +1638,15 @@ get_ipv4fromreg (struct ifall *ifp, const char *name, DWORD idx)
       if (dhcp)
 	{
 	  if (udipa.Buffer)
-	    inet_uton (udipa.Buffer, &addr->sin_addr);
+	    {
+	      inet_uton (udipa.Buffer, &addr->sin_addr);
+	      got_addr = true;
+	    }
 	  if (udsub.Buffer)
-	    inet_uton (udsub.Buffer, &mask->sin_addr);
+	    {
+	      inet_uton (udsub.Buffer, &mask->sin_addr);
+	      got_mask = true;
+	    }
 	}
       else
 	{
@@ -1649,7 +1656,10 @@ get_ipv4fromreg (struct ifall *ifp, const char *name, DWORD idx)
 		   c += wcslen (c) + 1)
 		ifs++;
 	      if (*c)
-		inet_uton (c, &addr->sin_addr);
+		{
+		  inet_uton (c, &addr->sin_addr);
+		  got_addr = true;
+		}
 	    }
 	  if (usub.Buffer)
 	    {
@@ -1657,9 +1667,16 @@ get_ipv4fromreg (struct ifall *ifp, const char *name, DWORD idx)
 		   c += wcslen (c) + 1)
 		ifs++;
 	      if (*c)
-		inet_uton (c, &mask->sin_addr);
+		{
+		  inet_uton (c, &mask->sin_addr);
+		  got_mask = true;
+		}
 	    }
 	}
+      if (got_addr)
+	ifp->ifa_ifa.ifa_addr = (struct sockaddr *) addr;
+      if (got_mask)
+	ifp->ifa_ifa.ifa_netmask = (struct sockaddr *) mask;
       if (ifp->ifa_ifa.ifa_flags & IFF_BROADCAST)
 	brdc->sin_addr.s_addr = (addr->sin_addr.s_addr
 				 & mask->sin_addr.s_addr)
@@ -1800,13 +1817,13 @@ get_ifs (ULONG family)
 	      ifp->ifa_ifa.ifa_flags |= IFF_BROADCAST;
 	    /* Address */
 	    ifp->ifa_addr.ss_family = AF_INET;
-	    ifp->ifa_ifa.ifa_addr = (struct sockaddr *) &ifp->ifa_addr;
+	    ifp->ifa_ifa.ifa_addr = NULL;
 	    /* Broadcast/Destination address */
 	    ifp->ifa_brddstaddr.ss_family = AF_INET;
 	    ifp->ifa_ifa.ifa_dstaddr = NULL;
 	    /* Netmask */
 	    ifp->ifa_netmask.ss_family = AF_INET;
-	    ifp->ifa_ifa.ifa_netmask = (struct sockaddr *) &ifp->ifa_netmask;
+	    ifp->ifa_ifa.ifa_netmask = NULL;
 	    /* Try to fetch real IPv4 address information from registry. */
 	    get_ipv4fromreg (ifp, pap->AdapterName, idx);
 	    /* Hardware address */


                 reply	other threads:[~2021-07-28 11:45 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210728114538.956B4399203E@sourceware.org \
    --to=kbrown@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).