Mageia Bugzilla – Attachment 1778 Details for
Bug 4056
Installer stage1 cannot resolve host names
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
New Account
|
Forgot Password
[patch]
use getaddrinfo()
getaddrinfo2.diff (text/plain), 2.56 KB, created by
Thierry Vignaud
on 2012-03-16 13:55:55 CET
(
hide
)
Description:
use getaddrinfo()
Filename:
MIME Type:
Creator:
Thierry Vignaud
Created:
2012-03-16 13:55:55 CET
Size:
2.56 KB
patch
obsolete
>diff --git a/mdk-stage1/dns.c b/mdk-stage1/dns.c >index 63d801c..7f99ba5 100644 >--- a/mdk-stage1/dns.c >+++ b/mdk-stage1/dns.c >@@ -39,46 +39,66 @@ > > int mygethostbyname(char * name, struct in_addr * addr) > { >- struct hostent * h; >+ struct addrinfo hints, *res, *p; >+ int status; >+ char ipstr[INET6_ADDRSTRLEN]; >+ >+ memset(&hints, 0, sizeof hints); >+ hints.ai_family = AF_INET; //AF_UNSPEC for both IPv4 & IPv6 >+ hints.ai_socktype = SOCK_STREAM; > > /* prevent from timeouts */ > if (_res.nscount == 0) > return -1; > >- h = gethostbyname(name); >- >- if (!h && domain) { >- // gethostbyname from dietlibc doesn't support domain handling >- char fully_qualified[500]; >- sprintf(fully_qualified, "%s.%s", name, domain); >- h = gethostbyname(fully_qualified); >+ if ((status = getaddrinfo(name, NULL, &hints, &res)) != 0) { >+ fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status)); >+ return -1; > } >- >- if (h && h->h_addr_list && (h->h_addr_list)[0]) { >- memcpy(addr, (h->h_addr_list)[0], sizeof(*addr)); >- log_message("is-at: %s", inet_ntoa(*addr)); >- return 0; >+ /* >+ if (!h && domain) { >+ // gethostbyname from dietlibc doesn't support domain handling >+ char fully_qualified[500]; >+ sprintf(fully_qualified, "%s.%s", name, domain); >+ h = gethostbyname(fully_qualified); >+ } >+ */ >+ >+ for (p = res;p != NULL; p = p->ai_next) { >+ void *tmp_addr; >+ >+ struct sockaddr_in *ipv = (struct sockaddr_in *)p->ai_addr; >+ tmp_addr = &(ipv->sin_addr); >+ >+ /* convert the IP to a string and print it: */ >+ inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr); >+ >+ memcpy(addr, tmp_addr, sizeof(*addr)); >+ printf("is-at: %s\n", inet_ntoa(*addr)); > } > >- log_message("unknown host %s", name); >- return -1; >+ freeaddrinfo(res); // free the linked list >+ return 0; > } > > char * mygethostbyaddr(char * ipnum) > { >- struct in_addr in; >- struct hostent * host; >+ struct sockaddr_in sa; >+ char hbuf[NI_MAXHOST]; > > /* prevent from timeouts */ > if (_res.nscount == 0) > return NULL; >+ >+ memset(&sa, 0, sizeof sa); >+ sa.sin_family = AF_INET; > >- if (!inet_aton(ipnum, &in)) >- return NULL; >- host = gethostbyaddr(&(in.s_addr), sizeof(in.s_addr) /* INADDRSZ */, AF_INET); >- if (host && host->h_name) >- return host->h_name; >- return NULL; >+ if (inet_pton(AF_INET, ipnum, &sa.sin_addr) != 1) >+ return NULL; >+ >+ if (getnameinfo((struct sockaddr*)&sa, sizeof(sa), hbuf, sizeof(hbuf), NULL, 0, 0 |NI_NAMEREQD) == 0) //NI_NUMERICHOST NI_NAMEREQD >+ return strdup(hbuf); >+ else return NULL; > } > > #elif defined(__GLIBC__)
diff --git a/mdk-stage1/dns.c b/mdk-stage1/dns.c index 63d801c..7f99ba5 100644 --- a/mdk-stage1/dns.c +++ b/mdk-stage1/dns.c @@ -39,46 +39,66 @@ int mygethostbyname(char * name, struct in_addr * addr) { - struct hostent * h; + struct addrinfo hints, *res, *p; + int status; + char ipstr[INET6_ADDRSTRLEN]; + + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_INET; //AF_UNSPEC for both IPv4 & IPv6 + hints.ai_socktype = SOCK_STREAM; /* prevent from timeouts */ if (_res.nscount == 0) return -1; - h = gethostbyname(name); - - if (!h && domain) { - // gethostbyname from dietlibc doesn't support domain handling - char fully_qualified[500]; - sprintf(fully_qualified, "%s.%s", name, domain); - h = gethostbyname(fully_qualified); + if ((status = getaddrinfo(name, NULL, &hints, &res)) != 0) { + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status)); + return -1; } - - if (h && h->h_addr_list && (h->h_addr_list)[0]) { - memcpy(addr, (h->h_addr_list)[0], sizeof(*addr)); - log_message("is-at: %s", inet_ntoa(*addr)); - return 0; + /* + if (!h && domain) { + // gethostbyname from dietlibc doesn't support domain handling + char fully_qualified[500]; + sprintf(fully_qualified, "%s.%s", name, domain); + h = gethostbyname(fully_qualified); + } + */ + + for (p = res;p != NULL; p = p->ai_next) { + void *tmp_addr; + + struct sockaddr_in *ipv = (struct sockaddr_in *)p->ai_addr; + tmp_addr = &(ipv->sin_addr); + + /* convert the IP to a string and print it: */ + inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr); + + memcpy(addr, tmp_addr, sizeof(*addr)); + printf("is-at: %s\n", inet_ntoa(*addr)); } - log_message("unknown host %s", name); - return -1; + freeaddrinfo(res); // free the linked list + return 0; } char * mygethostbyaddr(char * ipnum) { - struct in_addr in; - struct hostent * host; + struct sockaddr_in sa; + char hbuf[NI_MAXHOST]; /* prevent from timeouts */ if (_res.nscount == 0) return NULL; + + memset(&sa, 0, sizeof sa); + sa.sin_family = AF_INET; - if (!inet_aton(ipnum, &in)) - return NULL; - host = gethostbyaddr(&(in.s_addr), sizeof(in.s_addr) /* INADDRSZ */, AF_INET); - if (host && host->h_name) - return host->h_name; - return NULL; + if (inet_pton(AF_INET, ipnum, &sa.sin_addr) != 1) + return NULL; + + if (getnameinfo((struct sockaddr*)&sa, sizeof(sa), hbuf, sizeof(hbuf), NULL, 0, 0 |NI_NAMEREQD) == 0) //NI_NUMERICHOST NI_NAMEREQD + return strdup(hbuf); + else return NULL; } #elif defined(__GLIBC__)
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 4056
:
1348
| 1778