summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorray <>2006-04-14 03:16:02 +0000
committerray <>2006-04-14 03:16:02 +0000
commit04261074d345653277ced7adf044ea87ac7c93cc (patch)
tree0e33cadf3047477c60148a0615378ad61f346bc6
parentf96e7b7035ac868739059b815bcdf7eed259c208 (diff)
downloadopenbsd-04261074d345653277ced7adf044ea87ac7c93cc.tar.gz
openbsd-04261074d345653277ced7adf044ea87ac7c93cc.tar.bz2
openbsd-04261074d345653277ced7adf044ea87ac7c93cc.zip
Remove str2number(), use strtonum(3) instead.
Fix some type mismatches. Replace magic numbers. Remove superfluous strlen(3) calls. Earlier diff OK kjell@, OK deraadt@
-rw-r--r--src/lib/libc/net/getaddrinfo.c64
1 files changed, 20 insertions, 44 deletions
diff --git a/src/lib/libc/net/getaddrinfo.c b/src/lib/libc/net/getaddrinfo.c
index b7d13003d2..9113bfb39b 100644
--- a/src/lib/libc/net/getaddrinfo.c
+++ b/src/lib/libc/net/getaddrinfo.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: getaddrinfo.c,v 1.54 2006/03/22 13:29:26 ray Exp $ */ 1/* $OpenBSD: getaddrinfo.c,v 1.55 2006/04/14 03:16:02 ray Exp $ */
2/* $KAME: getaddrinfo.c,v 1.31 2000/08/31 17:36:43 itojun Exp $ */ 2/* $KAME: getaddrinfo.c,v 1.31 2000/08/31 17:36:43 itojun Exp $ */
3 3
4/* 4/*
@@ -91,6 +91,7 @@
91#include <netdb.h> 91#include <netdb.h>
92#include <resolv.h> 92#include <resolv.h>
93#include <string.h> 93#include <string.h>
94#include <stdint.h>
94#include <stdlib.h> 95#include <stdlib.h>
95#include <stddef.h> 96#include <stddef.h>
96#include <ctype.h> 97#include <ctype.h>
@@ -197,7 +198,6 @@ struct res_target {
197 int n; /* result length */ 198 int n; /* result length */
198}; 199};
199 200
200static int str2number(const char *);
201static int explore_fqdn(const struct addrinfo *, const char *, 201static int explore_fqdn(const struct addrinfo *, const char *,
202 const char *, struct addrinfo **); 202 const char *, struct addrinfo **);
203static int explore_null(const struct addrinfo *, 203static int explore_null(const struct addrinfo *,
@@ -278,23 +278,6 @@ do { \
278#define MATCH(x, y, w) \ 278#define MATCH(x, y, w) \
279 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY))) 279 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
280 280
281static int
282str2number(const char *p)
283{
284 char *ep;
285 unsigned long v;
286
287 if (*p == '\0')
288 return -1;
289 ep = NULL;
290 errno = 0;
291 v = strtoul(p, &ep, 10);
292 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
293 return v;
294 else
295 return -1;
296}
297
298int 281int
299getaddrinfo(const char *hostname, const char *servname, 282getaddrinfo(const char *hostname, const char *servname,
300 const struct addrinfo *hints, struct addrinfo **res) 283 const struct addrinfo *hints, struct addrinfo **res)
@@ -842,7 +825,7 @@ get_portmatch(const struct addrinfo *ai, const char *servname)
842static int 825static int
843get_port(struct addrinfo *ai, const char *servname, int matchonly) 826get_port(struct addrinfo *ai, const char *servname, int matchonly)
844{ 827{
845 const char *proto; 828 const char *errstr, *proto;
846 struct servent *sp; 829 struct servent *sp;
847 int port; 830 int port;
848 int allownumeric; 831 int allownumeric;
@@ -875,14 +858,14 @@ get_port(struct addrinfo *ai, const char *servname, int matchonly)
875 return EAI_SOCKTYPE; 858 return EAI_SOCKTYPE;
876 } 859 }
877 860
878 port = str2number(servname); 861 port = (int)strtonum(servname, 0, USHRT_MAX, &errstr);
879 if (port >= 0) { 862 if (!errstr) {
880 if (!allownumeric) 863 if (!allownumeric)
881 return EAI_SERVICE; 864 return EAI_SERVICE;
882 if (port < 0 || port > 65535)
883 return EAI_SERVICE;
884 port = htons(port); 865 port = htons(port);
885 } else { 866 } else {
867 if (errno == ERANGE)
868 return EAI_SERVICE;
886 if (ai->ai_flags & AI_NUMERICSERV) 869 if (ai->ai_flags & AI_NUMERICSERV)
887 return EAI_NONAME; 870 return EAI_NONAME;
888 871
@@ -943,9 +926,8 @@ find_afd(int af)
943static int 926static int
944ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid) 927ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid)
945{ 928{
946 u_long lscopeid;
947 struct in6_addr *a6 = &sin6->sin6_addr; 929 struct in6_addr *a6 = &sin6->sin6_addr;
948 char *ep; 930 const char *errstr;
949 931
950 /* empty scopeid portion is invalid */ 932 /* empty scopeid portion is invalid */
951 if (*scope == '\0') 933 if (*scope == '\0')
@@ -973,13 +955,10 @@ ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid)
973 955
974 /* try to convert to a numeric id as a last resort */ 956 /* try to convert to a numeric id as a last resort */
975 trynumeric: 957 trynumeric:
976 errno = 0; 958 *scopeid = (u_int32_t)strtonum(scope, 0, UINT32_MAX, &errstr);
977 lscopeid = strtoul(scope, &ep, 10); 959 if (errstr)
978 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL); 960 return (-1);
979 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid) 961 return (0);
980 return 0;
981 else
982 return -1;
983} 962}
984#endif 963#endif
985 964
@@ -1766,7 +1745,7 @@ res_querydomainN(const char *name, const char *domain,
1766 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); 1745 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
1767 char nbuf[MAXDNAME]; 1746 char nbuf[MAXDNAME];
1768 const char *longname = nbuf; 1747 const char *longname = nbuf;
1769 size_t n, d; 1748 size_t n;
1770 1749
1771 if (_res_init(0) == -1) { 1750 if (_res_init(0) == -1) {
1772 h_errno = NETDB_INTERNAL; 1751 h_errno = NETDB_INTERNAL;
@@ -1782,23 +1761,20 @@ res_querydomainN(const char *name, const char *domain,
1782 * Check for trailing '.'; 1761 * Check for trailing '.';
1783 * copy without '.' if present. 1762 * copy without '.' if present.
1784 */ 1763 */
1785 n = strlen(name); 1764 if ((n = strlcpy(nbuf, name, sizeof(nbuf))) >= sizeof(nbuf)) {
1786 if (n >= MAXDNAME) {
1787 h_errno = NO_RECOVERY; 1765 h_errno = NO_RECOVERY;
1788 return (-1); 1766 return (-1);
1789 } 1767 }
1790 if (n > 0 && name[--n] == '.') { 1768 if (n > 0 && nbuf[n - 1] == '.')
1791 strlcpy(nbuf, name, n + 1); 1769 nbuf[n - 1] = '\0';
1792 } else
1793 longname = name;
1794 } else { 1770 } else {
1795 n = strlen(name); 1771 int i;
1796 d = strlen(domain); 1772
1797 if (n + d + 1 >= MAXDNAME) { 1773 i = snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1774 if (i < 0 || i >= sizeof(nbuf)) {
1798 h_errno = NO_RECOVERY; 1775 h_errno = NO_RECOVERY;
1799 return (-1); 1776 return (-1);
1800 } 1777 }
1801 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1802 } 1778 }
1803 return (res_queryN(longname, target)); 1779 return (res_queryN(longname, target));
1804} 1780}