diff --git a/include/netdb.h b/include/netdb.h index 4278794af072..57cd70b0b023 100644 --- a/include/netdb.h +++ b/include/netdb.h @@ -247,7 +247,7 @@ int getaddrinfo(const char *, const char *, int getnameinfo(const struct sockaddr *, socklen_t, char *, size_t, char *, size_t, int); void freeaddrinfo(struct addrinfo *); -char *gai_strerror(int); +const char *gai_strerror(int); void setnetgrent(const char *); void setservent(int); diff --git a/lib/libc/net/getaddrinfo.c b/lib/libc/net/getaddrinfo.c index 29b1f6d73cc1..eb811cbad2ad 100644 --- a/lib/libc/net/getaddrinfo.c +++ b/lib/libc/net/getaddrinfo.c @@ -285,26 +285,23 @@ static int res_searchN(const char *, struct res_target *); static int res_querydomainN(const char *, const char *, struct res_target *); -static struct ai_errlist { - const char *str; - int code; -} ai_errlist[] = { - { "Success", 0, }, - { "Temporary failure in name resolution", EAI_AGAIN, }, - { "Invalid value for ai_flags", EAI_BADFLAGS, }, - { "Non-recoverable failure in name resolution", EAI_FAIL, }, - { "ai_family not supported", EAI_FAMILY, }, - { "Memory allocation failure", EAI_MEMORY, }, - { "hostname nor servname provided, or not known", EAI_NONAME, }, - { "servname not supported for ai_socktype", EAI_SERVICE, }, - { "ai_socktype not supported", EAI_SOCKTYPE, }, - { "System error returned in errno", EAI_SYSTEM, }, - { "Invalid value for hints", EAI_BADHINTS, }, - { "Resolved protocol is unknown", EAI_PROTOCOL, }, - /* backward compatibility with userland code prior to 2553bis-02 */ - { "Address family for hostname not supported", 1, }, - { "No address associated with hostname", 7, }, - { NULL, -1, }, +/* Entries EAI_ADDRFAMILY (1) and EAI_NODATA (7) are obsoleted, but left */ +/* for backward compatibility with userland code prior to 2553bis-02 */ +static const char *ai_errlist[] = { + "Success", /* 0 */ + "Address family for hostname not supported", /* 1 */ + "Temporary failure in name resolution", /* EAI_AGAIN */ + "Invalid value for ai_flags", /* EAI_BADFLAGS */ + "Non-recoverable failure in name resolution", /* EAI_FAIL */ + "ai_family not supported", /* EAI_FAMILY */ + "Memory allocation failure", /* EAI_MEMORY */ + "No address associated with hostname", /* 7 */ + "hostname nor servname provided, or not known", /* EAI_NONAME */ + "servname not supported for ai_socktype", /* EAI_SERVICE */ + "ai_socktype not supported", /* EAI_SOCKTYPE */ + "System error returned in errno", /* EAI_SYSTEM */ + "Invalid value for hints", /* EAI_BADHINTS */ + "Resolved protocol is unknown" /* EAI_PROTOCOL */ }; /* @@ -360,16 +357,11 @@ do { \ #define MATCH(x, y, w) \ ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY))) -char * -gai_strerror(ecode) - int ecode; +const char * +gai_strerror(int ecode) { - struct ai_errlist *p; - - for (p = ai_errlist; p->str; p++) { - if (p->code == ecode) - return (char *)p->str; - } + if (ecode >= 0 && ecode < EAI_MAX) + return ai_errlist[ecode]; return "Unknown error"; }