public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
* Re: [PATCH] Add-on to gethostbyname2
@ 2015-01-27 14:57 Pierre A. Humblet
  2015-01-27 15:00 ` Corinna Vinschen
  0 siblings, 1 reply; 6+ messages in thread
From: Pierre A. Humblet @ 2015-01-27 14:57 UTC (permalink / raw)
  To: cygwin-patches


>-----Original Message-----
>From: Pierre A Humblet
>Sent: Friday, January 23, 2015 9:30 AM
>
> > From: Corinna Vinschen
> > Sent: Friday, January 23, 2015 5:48 AM
> >
> > On Jan 22 21:05, Pierre A. Humblet wrote:
> > > Add-on to gethostbyname2, as discussed previously on main list.
> > > The diff is also attached.
> > >
> >
> > Do you have some wording for the release info in the docs, please?
> >
>Make gethostbyname2 handle numerical host addresses as well as the 
>reserved domain names "localhost" and "invalid".

Actually it should be "numeric host addresses"

Pierre

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Add-on to gethostbyname2
  2015-01-27 14:57 [PATCH] Add-on to gethostbyname2 Pierre A. Humblet
@ 2015-01-27 15:00 ` Corinna Vinschen
  0 siblings, 0 replies; 6+ messages in thread
From: Corinna Vinschen @ 2015-01-27 15:00 UTC (permalink / raw)
  To: cygwin-patches

[-- Attachment #1: Type: text/plain, Size: 841 bytes --]

On Jan 27 09:56, Pierre A. Humblet wrote:
> 
> >-----Original Message-----
> >From: Pierre A Humblet
> >Sent: Friday, January 23, 2015 9:30 AM
> >
> >> From: Corinna Vinschen
> >> Sent: Friday, January 23, 2015 5:48 AM
> >>
> >> On Jan 22 21:05, Pierre A. Humblet wrote:
> >> > Add-on to gethostbyname2, as discussed previously on main list.
> >> > The diff is also attached.
> >> >
> >>
> >> Do you have some wording for the release info in the docs, please?
> >>
> >Make gethostbyname2 handle numerical host addresses as well as the
> >reserved domain names "localhost" and "invalid".
> 
> Actually it should be "numeric host addresses"

Fixed.


Thanks,
Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Add-on to gethostbyname2
  2015-01-23 14:30   ` phumblet
@ 2015-01-23 15:00     ` Corinna Vinschen
  0 siblings, 0 replies; 6+ messages in thread
From: Corinna Vinschen @ 2015-01-23 15:00 UTC (permalink / raw)
  To: cygwin-patches

[-- Attachment #1: Type: text/plain, Size: 668 bytes --]

On Jan 23 09:30, phumblet@phumblet.no-ip.org wrote:
> > From: Corinna Vinschen
> > Sent: Friday, January 23, 2015 5:48 AM
> > 	
> > On Jan 22 21:05, Pierre A. Humblet wrote:
> > > Add-on to gethostbyname2, as discussed previously on main list.
> > > The diff is also attached.
> > >
> > 
> > Do you have some wording for the release info in the docs, please?
> > 
> Make gethostbyname2 handle numerical host addresses as well as the reserved domain names "localhost" and "invalid".

Thanks!


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [PATCH] Add-on to gethostbyname2
  2015-01-23 10:48 ` Corinna Vinschen
@ 2015-01-23 14:30   ` phumblet
  2015-01-23 15:00     ` Corinna Vinschen
  0 siblings, 1 reply; 6+ messages in thread
From: phumblet @ 2015-01-23 14:30 UTC (permalink / raw)
  To: cygwin-patches

> From: Corinna Vinschen
> Sent: Friday, January 23, 2015 5:48 AM
> 	
> On Jan 22 21:05, Pierre A. Humblet wrote:
> > Add-on to gethostbyname2, as discussed previously on main list.
> > The diff is also attached.
> >
> 
> Do you have some wording for the release info in the docs, please?
> 
Make gethostbyname2 handle numerical host addresses as well as the reserved domain names "localhost" and "invalid".

Pierre


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Add-on to gethostbyname2
  2015-01-23  2:06 Pierre A. Humblet
@ 2015-01-23 10:48 ` Corinna Vinschen
  2015-01-23 14:30   ` phumblet
  0 siblings, 1 reply; 6+ messages in thread
From: Corinna Vinschen @ 2015-01-23 10:48 UTC (permalink / raw)
  To: cygwin-patches

[-- Attachment #1: Type: text/plain, Size: 805 bytes --]

On Jan 22 21:05, Pierre A. Humblet wrote:
> Add-on to gethostbyname2, as discussed previously on main list.
> The diff is also attached.
> 
> 
> Pierre
> 
> 2015-01-22  Pierre A. Humblet <...>
> 
>         * net.cc (cygwin_inet_pton): Declare.
>         (gethostby_specials): New function.
>         (gethostby_helper): Change returned addrtype in 4-to-6 case.
>         (gethostbyname2): Call gethostby_specials.

Patch applied with just fixing the coding style a bit:

  if () {
    [...]
  }

  ==>

  if ()
    {
      [...]
    }

Do you have some wording for the release info in the docs, please?


Thanks,
Corinna


-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH] Add-on to gethostbyname2
@ 2015-01-23  2:06 Pierre A. Humblet
  2015-01-23 10:48 ` Corinna Vinschen
  0 siblings, 1 reply; 6+ messages in thread
From: Pierre A. Humblet @ 2015-01-23  2:06 UTC (permalink / raw)
  To: cygwin-patches

[-- Attachment #1: Type: text/plain, Size: 5031 bytes --]

Add-on to gethostbyname2, as discussed previously on main list.
The diff is also attached.


Pierre

2015-01-22  Pierre A. Humblet <pierre@phumblet.no-ip.org>

         * net.cc (cygwin_inet_pton): Declare.
         (gethostby_specials): New function.
         (gethostby_helper): Change returned addrtype in 4-to-6 case.
         (gethostbyname2): Call gethostby_specials.



cvs diff -up net.cc
Index: net.cc
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/net.cc,v
retrieving revision 1.322
diff -u -p -r1.322 net.cc
--- net.cc      20 Jan 2015 18:23:19 -0000      1.322
+++ net.cc      23 Jan 2015 00:02:22 -0000
@@ -72,6 +72,7 @@ extern "C"
    int __stdcall rcmd (char **ahost, unsigned short inport, char *locuser,
                       char *remuser, char *cmd, SOCKET * fd2p);
    int sscanf (const char *, const char *, ...);
+  int cygwin_inet_pton(int, const char *, void *);
    int cygwin_inet_aton(const char *, struct in_addr *);
    const char *cygwin_inet_ntop (int, const void *, char *, socklen_t);
    int dn_length1(const unsigned char *, const unsigned char *,
@@ -1168,6 +1169,97 @@ memcpy4to6 (char *dst, const u_char *src
    memcpy (dst + 12, src, NS_INADDRSZ);
  }

+/* gethostby_specials: RFC 6761
+   Handles numerical addresses and special names for gethostbyname2 */
+static hostent *
+gethostby_specials (const char *name, const int af,
+                   const int addrsize_in, const int addrsize_out)
+{
+  int namelen = strlen (name);
+  /* Ignore a final '.' */
+  if ((namelen == 0) || ((namelen -= (name[namelen - 1] == '.')) == 0))  {
+    set_errno (EINVAL);
+    h_errno = NETDB_INTERNAL;
+    return NULL;
+  }
+
+  int res;
+  u_char address[NS_IN6ADDRSZ];
+  /* Test for numerical addresses */
+  res = cygwin_inet_pton(af, name, address);
+  /* Test for special domain names */
+  if (res != 1) {
+    {
+      char const match[] = "invalid";
+      int const matchlen = sizeof(match) - 1;
+      int start = namelen - matchlen;
+      if ((start >= 0) && ((start == 0) || (name[start-1] == '.'))
+         && (strncasecmp (&name[start], match , matchlen) == 0)) {
+       h_errno = HOST_NOT_FOUND;
+       return NULL;
+      }
+    }
+    {
+      char const match[] = "localhost";
+      int const matchlen = sizeof(match) - 1;
+      int start = namelen - matchlen;
+      if ((start >= 0) && ((start == 0) || (name[start-1] == '.'))
+         && (strncasecmp (&name[start], match , matchlen) == 0)) {
+       res = 1;
+       if (af == AF_INET) {
+         address[0] = 127;
+         address[1] = address[2] = 0;
+         address[3] = 1;
+       }
+       else {
+         memset (address, 0, NS_IN6ADDRSZ);
+         address[NS_IN6ADDRSZ-1] = 1;
+       }
+      }
+    }
+  }
+  if (res != 1)
+    return NULL;
+
+  int const alias_count = 0, address_count = 1;
+  char * string_ptr;
+  int sz = DWORD_round (sizeof(hostent))
+    + sizeof (char *) * (alias_count + address_count + 2)
+    + namelen + 1
+    + address_count * addrsize_out;
+  hostent *ret = realloc_ent (sz,  (hostent *) NULL);
+  if (!ret)
+    {
+      /* errno is already set */
+      h_errno = NETDB_INTERNAL;
+      return NULL;
+    }
+
+  ret->h_addrtype = af;
+  ret->h_length = addrsize_out;
+  ret->h_aliases = (char **) (((char *) ret) + DWORD_round (sizeof(hostent)));
+  ret->h_addr_list = ret->h_aliases + alias_count + 1;
+  string_ptr = (char *) (ret->h_addr_list + address_count + 1);
+  ret->h_name = string_ptr;
+
+  memcpy (string_ptr, name, namelen);
+  string_ptr[namelen] = 0;
+  string_ptr += namelen + 1;
+
+  ret->h_addr_list[0] = string_ptr;
+  if (addrsize_in != addrsize_out) {
+    memcpy4to6 (string_ptr, address);
+    ret->h_addrtype = AF_INET6;
+  }
+  else
+    memcpy (string_ptr, address, addrsize_out);
+
+  ret->h_aliases[alias_count] = NULL;
+  ret->h_addr_list[address_count] = NULL;
+
+  return ret;
+}
+
  static hostent *
  gethostby_helper (const char *name, const int af, const int type,
                   const int addrsize_in, const int addrsize_out)
@@ -1352,8 +1444,10 @@ gethostby_helper (const char *name, cons
               string_ptr += curptr->namelen1;
             }
           ret->h_addr_list[address_count++] = string_ptr;
-         if (addrsize_in != addrsize_out)
+         if (addrsize_in != addrsize_out) {
             memcpy4to6 (string_ptr, curptr->data);
+           ret->h_addrtype =  AF_INET6;
+         }
           else
             memcpy (string_ptr, curptr->data, addrsize_in);
           string_ptr += addrsize_out;
@@ -1405,7 +1499,10 @@ gethostbyname2 (const char *name, int af
           __leave;
         }

-      res = gethostby_helper (name, af, type, addrsize_in, addrsize_out);
+      h_errno = NETDB_SUCCESS;
+      res = gethostby_specials (name, af, addrsize_in, addrsize_out);
+      if ((res == NULL) && (h_errno == NETDB_SUCCESS))
+         res = gethostby_helper (name, af, type, addrsize_in, addrsize_out);
      }
    __except (EFAULT) {}
    __endtry

[-- Attachment #2: net.cc.diff --]
[-- Type: application/octet-stream, Size: 4395 bytes --]

Index: net.cc
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/net.cc,v
retrieving revision 1.322
diff -u -p -r1.322 net.cc
--- net.cc	20 Jan 2015 18:23:19 -0000	1.322
+++ net.cc	23 Jan 2015 00:03:45 -0000
@@ -72,6 +72,7 @@ extern "C"
   int __stdcall rcmd (char **ahost, unsigned short inport, char *locuser,
 		      char *remuser, char *cmd, SOCKET * fd2p);
   int sscanf (const char *, const char *, ...);
+  int cygwin_inet_pton(int, const char *, void *);
   int cygwin_inet_aton(const char *, struct in_addr *);
   const char *cygwin_inet_ntop (int, const void *, char *, socklen_t);
   int dn_length1(const unsigned char *, const unsigned char *,
@@ -1168,6 +1169,97 @@ memcpy4to6 (char *dst, const u_char *src
   memcpy (dst + 12, src, NS_INADDRSZ);
 }
 
+/* gethostby_specials: RFC 6761 
+   Handles numerical addresses and special names for gethostbyname2 */ 
+static hostent *
+gethostby_specials (const char *name, const int af,
+		    const int addrsize_in, const int addrsize_out)
+{
+  int namelen = strlen (name);
+  /* Ignore a final '.' */
+  if ((namelen == 0) || ((namelen -= (name[namelen - 1] == '.')) == 0))  {
+    set_errno (EINVAL);
+    h_errno = NETDB_INTERNAL;
+    return NULL;
+  }
+
+  int res;
+  u_char address[NS_IN6ADDRSZ];
+  /* Test for numerical addresses */
+  res = cygwin_inet_pton(af, name, address);
+  /* Test for special domain names */
+  if (res != 1) {
+    {
+      char const match[] = "invalid";
+      int const matchlen = sizeof(match) - 1;
+      int start = namelen - matchlen;
+      if ((start >= 0) && ((start == 0) || (name[start-1] == '.'))
+	  && (strncasecmp (&name[start], match , matchlen) == 0)) {
+	h_errno = HOST_NOT_FOUND;
+	return NULL;
+      }
+    }
+    {
+      char const match[] = "localhost";
+      int const matchlen = sizeof(match) - 1;
+      int start = namelen - matchlen;
+      if ((start >= 0) && ((start == 0) || (name[start-1] == '.'))
+	  && (strncasecmp (&name[start], match , matchlen) == 0)) {
+	res = 1;
+	if (af == AF_INET) {
+	  address[0] = 127;
+	  address[1] = address[2] = 0;
+	  address[3] = 1;
+	}
+	else {
+	  memset (address, 0, NS_IN6ADDRSZ);
+	  address[NS_IN6ADDRSZ-1] = 1;
+	}
+      }
+    }
+  }
+  if (res != 1)
+    return NULL;  
+
+  int const alias_count = 0, address_count = 1;
+  char * string_ptr;
+  int sz = DWORD_round (sizeof(hostent))
+    + sizeof (char *) * (alias_count + address_count + 2)
+    + namelen + 1
+    + address_count * addrsize_out;
+  hostent *ret = realloc_ent (sz,  (hostent *) NULL);
+  if (!ret)
+    {
+      /* errno is already set */
+      h_errno = NETDB_INTERNAL;
+      return NULL;
+    }
+
+  ret->h_addrtype = af;
+  ret->h_length = addrsize_out;
+  ret->h_aliases = (char **) (((char *) ret) + DWORD_round (sizeof(hostent)));
+  ret->h_addr_list = ret->h_aliases + alias_count + 1;
+  string_ptr = (char *) (ret->h_addr_list + address_count + 1);
+  ret->h_name = string_ptr;
+
+  memcpy (string_ptr, name, namelen);
+  string_ptr[namelen] = 0;
+  string_ptr += namelen + 1;
+
+  ret->h_addr_list[0] = string_ptr;
+  if (addrsize_in != addrsize_out) {
+    memcpy4to6 (string_ptr, address);
+    ret->h_addrtype = AF_INET6;
+  }
+  else
+    memcpy (string_ptr, address, addrsize_out);
+
+  ret->h_aliases[alias_count] = NULL;
+  ret->h_addr_list[address_count] = NULL;
+
+  return ret;
+}
+
 static hostent *
 gethostby_helper (const char *name, const int af, const int type,
 		  const int addrsize_in, const int addrsize_out)
@@ -1352,8 +1444,10 @@ gethostby_helper (const char *name, cons
 	      string_ptr += curptr->namelen1;
 	    }
 	  ret->h_addr_list[address_count++] = string_ptr;
-	  if (addrsize_in != addrsize_out)
+	  if (addrsize_in != addrsize_out) {
 	    memcpy4to6 (string_ptr, curptr->data);
+	    ret->h_addrtype =  AF_INET6;
+	  }
 	  else
 	    memcpy (string_ptr, curptr->data, addrsize_in);
 	  string_ptr += addrsize_out;
@@ -1405,7 +1499,10 @@ gethostbyname2 (const char *name, int af
 	  __leave;
 	}
 
-      res = gethostby_helper (name, af, type, addrsize_in, addrsize_out);
+      h_errno = NETDB_SUCCESS;
+      res = gethostby_specials (name, af, addrsize_in, addrsize_out);
+      if ((res == NULL) && (h_errno == NETDB_SUCCESS))
+	  res = gethostby_helper (name, af, type, addrsize_in, addrsize_out);
     }
   __except (EFAULT) {}
   __endtry

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-01-27 15:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-27 14:57 [PATCH] Add-on to gethostbyname2 Pierre A. Humblet
2015-01-27 15:00 ` Corinna Vinschen
  -- strict thread matches above, loose matches on Subject: below --
2015-01-23  2:06 Pierre A. Humblet
2015-01-23 10:48 ` Corinna Vinschen
2015-01-23 14:30   ` phumblet
2015-01-23 15:00     ` Corinna Vinschen

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).