getaddrinfo.c (4c6867a867ad07908c0b7f43788412b49cc26447) getaddrinfo.c (33dee819339974eaf7dbf69068001771cbdcd548)
1/* $KAME: getaddrinfo.c,v 1.15 2000/07/09 04:37:24 itojun Exp $ */
2
3/*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 291 unchanged lines hidden (view full) ---

300 { "Resolved protocol is unknown", EAI_PROTOCOL, },
301 /* backward compatibility with userland code prior to 2553bis-02 */
302 { "Address family for hostname not supported", 1, },
303 { "No address associated with hostname", 7, },
304 { NULL, -1, },
305};
306
307/*
1/* $KAME: getaddrinfo.c,v 1.15 2000/07/09 04:37:24 itojun Exp $ */
2
3/*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 291 unchanged lines hidden (view full) ---

300 { "Resolved protocol is unknown", EAI_PROTOCOL, },
301 /* backward compatibility with userland code prior to 2553bis-02 */
302 { "Address family for hostname not supported", 1, },
303 { "No address associated with hostname", 7, },
304 { NULL, -1, },
305};
306
307/*
308 * XXX: Our res_*() is not thread-safe. So, we share lock between
308 * XXX: Many dependencies are not thread-safe. So, we share lock between
309 * getaddrinfo() and getipnodeby*(). Still, we cannot use
310 * getaddrinfo() and getipnodeby*() in conjunction with other
309 * getaddrinfo() and getipnodeby*(). Still, we cannot use
310 * getaddrinfo() and getipnodeby*() in conjunction with other
311 * functions which call res_*().
311 * functions which call them.
312 */
313pthread_mutex_t __getaddrinfo_thread_lock = PTHREAD_MUTEX_INITIALIZER;
314#define THREAD_LOCK() \
315 if (__isthreaded) _pthread_mutex_lock(&__getaddrinfo_thread_lock);
316#define THREAD_UNLOCK() \
317 if (__isthreaded) _pthread_mutex_unlock(&__getaddrinfo_thread_lock);
318
319/* XXX macros that make external reference is BAD. */

--- 1023 unchanged lines hidden (view full) ---

1343 case SOCK_STREAM:
1344 proto = "tcp";
1345 break;
1346 default:
1347 proto = NULL;
1348 break;
1349 }
1350
312 */
313pthread_mutex_t __getaddrinfo_thread_lock = PTHREAD_MUTEX_INITIALIZER;
314#define THREAD_LOCK() \
315 if (__isthreaded) _pthread_mutex_lock(&__getaddrinfo_thread_lock);
316#define THREAD_UNLOCK() \
317 if (__isthreaded) _pthread_mutex_unlock(&__getaddrinfo_thread_lock);
318
319/* XXX macros that make external reference is BAD. */

--- 1023 unchanged lines hidden (view full) ---

1343 case SOCK_STREAM:
1344 proto = "tcp";
1345 break;
1346 default:
1347 proto = NULL;
1348 break;
1349 }
1350
1351 if ((sp = getservbyname(servname, proto)) == NULL)
1351 THREAD_LOCK();
1352 if ((sp = getservbyname(servname, proto)) == NULL) {
1353 THREAD_UNLOCK();
1352 return EAI_SERVICE;
1354 return EAI_SERVICE;
1355 }
1353 port = sp->s_port;
1356 port = sp->s_port;
1357 THREAD_UNLOCK();
1354 }
1355
1356 if (!matchonly) {
1357 switch (ai->ai_family) {
1358 case AF_INET:
1359 ((struct sockaddr_in *)(void *)
1360 ai->ai_addr)->sin_port = port;
1361 break;

--- 134 unchanged lines hidden (view full) ---

1496 NS_FILES_CB(_files_getaddrinfo, NULL)
1497 { NSSRC_DNS, _dns_getaddrinfo, NULL }, /* force -DHESIOD */
1498 NS_NIS_CB(_yp_getaddrinfo, NULL)
1499 { 0 }
1500 };
1501
1502 result = NULL;
1503
1358 }
1359
1360 if (!matchonly) {
1361 switch (ai->ai_family) {
1362 case AF_INET:
1363 ((struct sockaddr_in *)(void *)
1364 ai->ai_addr)->sin_port = port;
1365 break;

--- 134 unchanged lines hidden (view full) ---

1500 NS_FILES_CB(_files_getaddrinfo, NULL)
1501 { NSSRC_DNS, _dns_getaddrinfo, NULL }, /* force -DHESIOD */
1502 NS_NIS_CB(_yp_getaddrinfo, NULL)
1503 { 0 }
1504 };
1505
1506 result = NULL;
1507
1504 THREAD_LOCK();
1505
1506 /*
1507 * if the servname does not match socktype/protocol, ignore it.
1508 */
1508 /*
1509 * if the servname does not match socktype/protocol, ignore it.
1510 */
1509 if (get_portmatch(pai, servname) != 0) {
1510 THREAD_UNLOCK();
1511 if (get_portmatch(pai, servname) != 0)
1511 return 0;
1512 return 0;
1512 }
1513
1514 switch (_nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo",
1515 default_dns_files, hostname, pai)) {
1516 case NS_TRYAGAIN:
1517 error = EAI_AGAIN;
1518 goto free;
1519 case NS_UNAVAIL:
1520 error = EAI_FAIL;

--- 4 unchanged lines hidden (view full) ---

1525 case NS_SUCCESS:
1526 error = 0;
1527 for (cur = result; cur; cur = cur->ai_next) {
1528 GET_PORT(cur, servname);
1529 /* canonname should be filled already */
1530 }
1531 break;
1532 }
1513
1514 switch (_nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo",
1515 default_dns_files, hostname, pai)) {
1516 case NS_TRYAGAIN:
1517 error = EAI_AGAIN;
1518 goto free;
1519 case NS_UNAVAIL:
1520 error = EAI_FAIL;

--- 4 unchanged lines hidden (view full) ---

1525 case NS_SUCCESS:
1526 error = 0;
1527 for (cur = result; cur; cur = cur->ai_next) {
1528 GET_PORT(cur, servname);
1529 /* canonname should be filled already */
1530 }
1531 break;
1532 }
1533 THREAD_UNLOCK();
1534
1535 *res = result;
1536
1537 return 0;
1538
1539free:
1533
1534 *res = result;
1535
1536 return 0;
1537
1538free:
1540 THREAD_UNLOCK();
1541 if (result)
1542 freeaddrinfo(result);
1543 return error;
1544}
1545
1546#ifdef DEBUG
1547static const char AskedForGot[] =
1548 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";

--- 483 unchanged lines hidden (view full) ---

2032 struct addrinfo *p;
2033
2034 name = va_arg(ap, char *);
2035 pai = va_arg(ap, struct addrinfo *);
2036
2037 memset(&sentinel, 0, sizeof(sentinel));
2038 cur = &sentinel;
2039
1539 if (result)
1540 freeaddrinfo(result);
1541 return error;
1542}
1543
1544#ifdef DEBUG
1545static const char AskedForGot[] =
1546 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";

--- 483 unchanged lines hidden (view full) ---

2030 struct addrinfo *p;
2031
2032 name = va_arg(ap, char *);
2033 pai = va_arg(ap, struct addrinfo *);
2034
2035 memset(&sentinel, 0, sizeof(sentinel));
2036 cur = &sentinel;
2037
2038 THREAD_LOCK();
2040 _sethtent();
2041 while ((p = _gethtent(name, pai)) != NULL) {
2042 cur->ai_next = p;
2043 while (cur && cur->ai_next)
2044 cur = cur->ai_next;
2045 }
2046 _endhtent();
2039 _sethtent();
2040 while ((p = _gethtent(name, pai)) != NULL) {
2041 cur->ai_next = p;
2042 while (cur && cur->ai_next)
2043 cur = cur->ai_next;
2044 }
2045 _endhtent();
2046 THREAD_UNLOCK();
2047
2048 *((struct addrinfo **)rv) = sentinel.ai_next;
2049 if (sentinel.ai_next == NULL)
2050 return NS_NOTFOUND;
2051 return NS_SUCCESS;
2052}
2053
2054#ifdef YP

--- 92 unchanged lines hidden (view full) ---

2147 const struct addrinfo *pai;
2148
2149 name = va_arg(ap, char *);
2150 pai = va_arg(ap, const struct addrinfo *);
2151
2152 memset(&sentinel, 0, sizeof(sentinel));
2153 cur = &sentinel;
2154
2047
2048 *((struct addrinfo **)rv) = sentinel.ai_next;
2049 if (sentinel.ai_next == NULL)
2050 return NS_NOTFOUND;
2051 return NS_SUCCESS;
2052}
2053
2054#ifdef YP

--- 92 unchanged lines hidden (view full) ---

2147 const struct addrinfo *pai;
2148
2149 name = va_arg(ap, char *);
2150 pai = va_arg(ap, const struct addrinfo *);
2151
2152 memset(&sentinel, 0, sizeof(sentinel));
2153 cur = &sentinel;
2154
2155 THREAD_LOCK();
2155 if (!__ypdomain) {
2156 if (!__ypdomain) {
2156 if (_yp_check(&__ypdomain) == 0)
2157 if (_yp_check(&__ypdomain) == 0) {
2158 THREAD_UNLOCK();
2157 return NS_UNAVAIL;
2159 return NS_UNAVAIL;
2160 }
2158 }
2159 if (__ypcurrent)
2160 free(__ypcurrent);
2161 __ypcurrent = NULL;
2162
2163 /* hosts.byname is only for IPv4 (Solaris8) */
2164 if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) {
2165 r = yp_match(__ypdomain, "hosts.byname", name,

--- 18 unchanged lines hidden (view full) ---

2184 if (r == 0) {
2185 ai = _yphostent(__ypcurrent, pai);
2186 if (ai) {
2187 cur->ai_next = ai;
2188 while (cur && cur->ai_next)
2189 cur = cur->ai_next;
2190 }
2191 }
2161 }
2162 if (__ypcurrent)
2163 free(__ypcurrent);
2164 __ypcurrent = NULL;
2165
2166 /* hosts.byname is only for IPv4 (Solaris8) */
2167 if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) {
2168 r = yp_match(__ypdomain, "hosts.byname", name,

--- 18 unchanged lines hidden (view full) ---

2187 if (r == 0) {
2188 ai = _yphostent(__ypcurrent, pai);
2189 if (ai) {
2190 cur->ai_next = ai;
2191 while (cur && cur->ai_next)
2192 cur = cur->ai_next;
2193 }
2194 }
2195 THREAD_UNLOCK();
2192
2193 if (sentinel.ai_next == NULL) {
2194 h_errno = HOST_NOT_FOUND;
2195 return NS_NOTFOUND;
2196 }
2197 *((struct addrinfo **)rv) = sentinel.ai_next;
2198 return NS_SUCCESS;
2199}
2200#endif
2201
2202/* resolver logic */
2203
2204extern const char *__hostalias(const char *);
2196
2197 if (sentinel.ai_next == NULL) {
2198 h_errno = HOST_NOT_FOUND;
2199 return NS_NOTFOUND;
2200 }
2201 *((struct addrinfo **)rv) = sentinel.ai_next;
2202 return NS_SUCCESS;
2203}
2204#endif
2205
2206/* resolver logic */
2207
2208extern const char *__hostalias(const char *);
2205extern int h_errno;
2206
2207/*
2208 * Formulate a normal query, send, and await answer.
2209 * Returned answer is placed in supplied buffer "answer".
2210 * Perform preliminary check of answer, returning success only
2211 * if no error is indicated and the answer count is nonzero.
2212 * Return the size of the response on success, -1 on error.
2213 * Error number is left in h_errno.

--- 304 unchanged lines hidden ---
2209
2210/*
2211 * Formulate a normal query, send, and await answer.
2212 * Returned answer is placed in supplied buffer "answer".
2213 * Perform preliminary check of answer, returning success only
2214 * if no error is indicated and the answer count is nonzero.
2215 * Return the size of the response on success, -1 on error.
2216 * Error number is left in h_errno.

--- 304 unchanged lines hidden ---