diff options
-rw-r--r-- | src/network/getservbyname_r.c | 26 | ||||
-rw-r--r-- | src/network/getservbyport_r.c | 28 |
2 files changed, 35 insertions, 19 deletions
diff --git a/src/network/getservbyname_r.c b/src/network/getservbyname_r.c index efa5d910..811c174c 100644 --- a/src/network/getservbyname_r.c +++ b/src/network/getservbyname_r.c @@ -12,25 +12,33 @@ int getservbyname_r(const char *name, const char *prots, struct addrinfo *ai, hint = { .ai_family = AF_INET }; int i; - if (!prots) return -( - getservbyname_r(name, "tcp", se, buf, buflen, res) - && getservbyname_r(name, "udp", se, buf, buflen, res) ); + if (!prots) { + int r = getservbyname_r(name, "tcp", se, buf, buflen, res); + if (r) r = getservbyname_r(name, "udp", se, buf, buflen, res); + return r; + } /* Align buffer */ i = (uintptr_t)buf & sizeof(char *)-1; if (!i) i = sizeof(char *); - if (buflen < 3*sizeof(char *)-i) { - errno = ERANGE; - return -1; - } + if (buflen < 3*sizeof(char *)-i) + return ERANGE; buf += sizeof(char *)-i; buflen -= sizeof(char *)-i; if (!strcmp(prots, "tcp")) hint.ai_protocol = IPPROTO_TCP; else if (!strcmp(prots, "udp")) hint.ai_protocol = IPPROTO_UDP; - else return -1; + else return EINVAL; - if (getaddrinfo(0, name, &hint, &ai) < 0) return -1; + switch (getaddrinfo(0, name, &hint, &ai)) { + case EAI_MEMORY: + case EAI_SYSTEM: + return ENOMEM; + default: + return ENOENT; + case 0: + break; + } se->s_name = (char *)name; se->s_aliases = (void *)buf; diff --git a/src/network/getservbyport_r.c b/src/network/getservbyport_r.c index 821afae9..a0a7caec 100644 --- a/src/network/getservbyport_r.c +++ b/src/network/getservbyport_r.c @@ -15,21 +15,21 @@ int getservbyport_r(int port, const char *prots, .sin_port = port, }; - if (!prots) return -( - getservbyport_r(port, "tcp", se, buf, buflen, res) - && getservbyport_r(port, "udp", se, buf, buflen, res) ); + if (!prots) { + int r = getservbyport_r(port, "tcp", se, buf, buflen, res); + if (r) r = getservbyport_r(port, "udp", se, buf, buflen, res); + return r; + } /* Align buffer */ i = (uintptr_t)buf & sizeof(char *)-1; if (!i) i = sizeof(char *); - if (buflen < 3*sizeof(char *)-i) { - errno = ERANGE; - return -1; - } + if (buflen < 3*sizeof(char *)-i) + return ERANGE; buf += sizeof(char *)-i; buflen -= sizeof(char *)-i; - if (strcmp(prots, "tcp") && strcmp(prots, "udp")) return -1; + if (strcmp(prots, "tcp") && strcmp(prots, "udp")) return EINVAL; se->s_port = port; se->s_proto = (char *)prots; @@ -39,8 +39,16 @@ int getservbyport_r(int port, const char *prots, se->s_aliases[1] = 0; se->s_aliases[0] = se->s_name = buf; - if (getnameinfo((void *)&sin, sizeof sin, 0, 0, buf, buflen, - strcmp(prots, "udp") ? 0 : NI_DGRAM) < 0) return -1; + switch (getnameinfo((void *)&sin, sizeof sin, 0, 0, buf, buflen, + strcmp(prots, "udp") ? 0 : NI_DGRAM)) { + case EAI_MEMORY: + case EAI_SYSTEM: + return ENOMEM; + default: + return ENOENT; + case 0: + break; + } *res = se; return 0; |