summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorray <>2007-05-17 03:55:08 +0000
committerray <>2007-05-17 03:55:08 +0000
commit76e72c2cbf982bde55d62c7ec7b62f7cd6b53616 (patch)
treeb109276f19d89392c4c718d4035bf3a195462c39 /src
parent9a364ffb85cbe4c22d7e8895aeebced0acd0ed7a (diff)
downloadopenbsd-76e72c2cbf982bde55d62c7ec7b62f7cd6b53616.tar.gz
openbsd-76e72c2cbf982bde55d62c7ec7b62f7cd6b53616.tar.bz2
openbsd-76e72c2cbf982bde55d62c7ec7b62f7cd6b53616.zip
Improve reentrancy by not using global variable. Removes two
wrappers around fopen/fclose. From tbert. OK millert@, tedu@, and itojun@.
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/net/getaddrinfo.c42
1 files changed, 11 insertions, 31 deletions
diff --git a/src/lib/libc/net/getaddrinfo.c b/src/lib/libc/net/getaddrinfo.c
index 3f8521cb31..5e11ab00c3 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.64 2007/05/12 21:38:14 ray Exp $ */ 1/* $OpenBSD: getaddrinfo.c,v 1.65 2007/05/17 03:55:08 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/*
@@ -217,9 +217,8 @@ static const struct afd *find_afd(int);
217static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *); 217static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *);
218#endif 218#endif
219 219
220static void _sethtent(void); 220static struct addrinfo * _gethtent(const char *, const struct addrinfo *,
221static void _endhtent(void); 221 FILE *);
222static struct addrinfo * _gethtent(const char *, const struct addrinfo *);
223static struct addrinfo *_files_getaddrinfo(const char *, 222static struct addrinfo *_files_getaddrinfo(const char *,
224 const struct addrinfo *); 223 const struct addrinfo *);
225 224
@@ -949,7 +948,6 @@ ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid)
949 948
950static const char AskedForGot[] = 949static const char AskedForGot[] =
951 "gethostby*.getanswer: asked for \"%s\", got \"%s\""; 950 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
952static FILE *hostf = NULL;
953 951
954static struct addrinfo * 952static struct addrinfo *
955getanswer(const querybuf *answer, int anslen, const char *qname, int qtype, 953getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
@@ -1221,28 +1219,8 @@ _dns_getaddrinfo(const char *name, const struct addrinfo *pai)
1221 return sentinel.ai_next; 1219 return sentinel.ai_next;
1222} 1220}
1223 1221
1224static FILE *hostf;
1225
1226static void
1227_sethtent(void)
1228{
1229 if (!hostf)
1230 hostf = fopen(_PATH_HOSTS, "r" );
1231 else
1232 rewind(hostf);
1233}
1234
1235static void
1236_endhtent(void)
1237{
1238 if (hostf) {
1239 (void) fclose(hostf);
1240 hostf = NULL;
1241 }
1242}
1243
1244static struct addrinfo * 1222static struct addrinfo *
1245_gethtent(const char *name, const struct addrinfo *pai) 1223_gethtent(const char *name, const struct addrinfo *pai, FILE *hostf)
1246{ 1224{
1247 char *p; 1225 char *p;
1248 char *cp, *tname, *cname; 1226 char *cp, *tname, *cname;
@@ -1251,8 +1229,6 @@ _gethtent(const char *name, const struct addrinfo *pai)
1251 const char *addr; 1229 const char *addr;
1252 char hostbuf[8*1024]; 1230 char hostbuf[8*1024];
1253 1231
1254 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" )))
1255 return (NULL);
1256 again: 1232 again:
1257 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) 1233 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf)))
1258 return (NULL); 1234 return (NULL);
@@ -1308,17 +1284,21 @@ _files_getaddrinfo(const char *name, const struct addrinfo *pai)
1308{ 1284{
1309 struct addrinfo sentinel, *cur; 1285 struct addrinfo sentinel, *cur;
1310 struct addrinfo *p; 1286 struct addrinfo *p;
1287 FILE *hostf;
1288
1289 hostf = fopen(_PATH_HOSTS, "r");
1290 if (hostf == NULL)
1291 return NULL;
1311 1292
1312 memset(&sentinel, 0, sizeof(sentinel)); 1293 memset(&sentinel, 0, sizeof(sentinel));
1313 cur = &sentinel; 1294 cur = &sentinel;
1314 1295
1315 _sethtent(); 1296 while ((p = _gethtent(name, pai, hostf)) != NULL) {
1316 while ((p = _gethtent(name, pai)) != NULL) {
1317 cur->ai_next = p; 1297 cur->ai_next = p;
1318 while (cur && cur->ai_next) 1298 while (cur && cur->ai_next)
1319 cur = cur->ai_next; 1299 cur = cur->ai_next;
1320 } 1300 }
1321 _endhtent(); 1301 fclose(hostf);
1322 1302
1323 return sentinel.ai_next; 1303 return sentinel.ai_next;
1324} 1304}