[PATCH 21/29] libnetworking: Update gethostent_r API.

Christian Mauderer christian.mauderer at embedded-brains.de
Mon Apr 25 15:06:55 UTC 2016


From: Christian Mauderer <Christian.Mauderer at embedded-brains.de>

Linux and FreeBSD use a common API now. Adapt the RTEMS one to provide
the same one.
---
 cpukit/libnetworking/libc/gethostbyht.c   | 52 ++++++++++++++++++++++++-------
 cpukit/libnetworking/libc/gethostnamadr.c |  7 ++++-
 2 files changed, 46 insertions(+), 13 deletions(-)

diff --git a/cpukit/libnetworking/libc/gethostbyht.c b/cpukit/libnetworking/libc/gethostbyht.c
index 2278e70..279514e 100644
--- a/cpukit/libnetworking/libc/gethostbyht.c
+++ b/cpukit/libnetworking/libc/gethostbyht.c
@@ -214,17 +214,36 @@ _gethostbyhtaddr(
 
 
 #ifdef _THREAD_SAFE
-struct hostent* gethostent_r(char* buf, int len) 
+int
+gethostent_r(
+	struct hostent *pe,
+	char *buf,
+	size_t len,
+	struct hostent **result,
+	int *h_errnop)
 {
   char  *dest;
-  struct hostent* pe=(struct hostent*)buf;
   char*  last;
   char*  max=buf+len;
   int    aliasidx;
   int    curlen;
-  
-   
-  if (!hostf) return 0;
+  int    rv;
+
+  if (pe == NULL || buf == NULL || result == NULL || h_errnop == NULL) {
+    if (h_errnop != NULL) {
+      *h_errnop = NETDB_INTERNAL;
+    }
+    return EINVAL;
+  }
+
+  *result = NULL;
+  *h_errnop = NETDB_INTERNAL;
+  rv = -1;
+
+  if (!hostf) {
+    rv = ENOENT;
+    return rv;
+  }
   fseek(hostf,0,SEEK_END);
   curlen=ftell(hostf);
   fseek(hostf,0,SEEK_SET);
@@ -248,16 +267,22 @@ struct hostent* gethostent_r(char* buf, int len)
   last=hostmap+hostlen;
 again:
   if ((size_t)len<sizeof(struct hostent)+11*sizeof(char*)) goto nospace;
-  dest=buf+sizeof(struct hostent);
+  dest=buf;
   pe->h_name=0;
   pe->h_aliases=(char**)dest; pe->h_aliases[0]=0; dest+=10*sizeof(char*);
   pe->h_addr_list=(char**)dest; dest+=2*sizeof(char**);
-  if (cur>=last) return 0;
+  if (cur>=last) {
+    rv = ERANGE;
+    return rv;
+  }
   if (*cur=='#' || *cur=='\n') goto parseerror;
   /* first, the ip number */
   pe->h_name=cur;
   while (cur<last && !isspace((unsigned char)*cur)) cur++;
-  if (cur>=last) return 0;
+  if (cur>=last) {
+    rv = ERANGE;
+    return rv;
+  }
   if (*cur=='\n') goto parseerror;
   {
     char save=*cur;
@@ -301,19 +326,22 @@ again:
   pe->h_aliases[aliasidx]=0;
   pe->h_name=pe->h_aliases[0];
   pe->h_aliases++;
-  return pe;
+  *result = pe;
+  *h_errnop = 0;
+  rv = 0;
+  return rv;
 parseerror:
   while (cur<last && *cur!='\n') cur++;
   cur++;
   goto again;
 nospace:
-  errno=ERANGE;
+  rv=ERANGE;
   goto __error;
 error:
-  errno=ENOMEM;
+  rv=ENOMEM;
 __error:
   if (hostmap!=NULL) free(hostmap);
   hostmap=NULL;
-  return 0;
+  return rv;
 }
 #endif
diff --git a/cpukit/libnetworking/libc/gethostnamadr.c b/cpukit/libnetworking/libc/gethostnamadr.c
index ccf2f96..a718820 100644
--- a/cpukit/libnetworking/libc/gethostnamadr.c
+++ b/cpukit/libnetworking/libc/gethostnamadr.c
@@ -414,8 +414,13 @@ int gethostbyname_r(const char*      name,
 
   {
     struct hostent* r;
+    struct hostent* he_buf = buf;
+    char *work_buf = buf + sizeof(struct hostent);
+    size_t remain_len = buflen - sizeof(struct hostent);
+    int he_errno;
+
     sethostent(0);
-    while ((r=gethostent_r(buf,buflen))) {
+    while (gethostent_r(he_buf, buf, remain_len, &r, &he_errno) == 0) {
       int i;
       if (r->h_addrtype==AF_INET && !strcasecmp(r->h_name,name)) {  /* found it! */
 found:
-- 
1.8.4.5




More information about the devel mailing list