aboutsummaryrefslogtreecommitdiff
blob: b0be882afbf49f274e166d19ff1ffd4c87499c79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Copyright (C) 1999 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>

#include "nsswitch.h"


/* We avoid using a too large buffer in case the user is accumulating the
   results and there is lots of unused space in the allocated buffer.  */
#define BUFLEN	512


/* Internal function to test whether IPv4 and/or IPv6 are available.  */
extern void __protocol_available (int *have_inet, int *have_inet6)
     internal_function;


/* Type of the lookup function we need here.  */
typedef enum nss_status (*lookup_function) (const char *, int, int,
					    struct hostent *, char *,
					    size_t, int *, int *);

/* The lookup function for the first entry of this service.  */
extern int __nss_hosts_lookup (service_user **nip, const char *name,
			       void **fctp);


struct hostent *
getipnodebyname (const char *name, int type, int flags, int *h_errnop)
{
  static service_user *startp;
  static lookup_function start_fct;
  service_user *nip;
  lookup_function fct;
  int no_more;
  size_t buffer_size;
  char *buffer;
  struct hostent *result;
  int save;
  int have_inet = 1;
  int have_inet6 = 1;

  /* First determine whether we have the appropriate interfaces.
     XXX I don't think we can cache the result since the system
     can be reconfigured.  */
  if ((flags & AI_ADDRCONFIG) != 0)
    __protocol_available (&have_inet, &have_inet6);

  /* Handle impossible requests first.  */
  if ((type == AF_INET && have_inet == 0)
      || (type == AF_INET6 && have_inet6 == 0 && ((flags & AI_V4MAPPED) == 0
						  || have_inet == 0))
      || (type != AF_INET && type != AF_INET6))
    {
      *h_errnop = NO_DATA;
      return NULL;
    }

  buffer_size = BUFLEN;
  buffer = (char *) malloc (buffer_size);

  result = NULL;
  if (buffer != NULL)
    {
#define HAVE_TYPE
#define resbuf (*((struct hostent *) buffer))
#include "../nss/digits_dots.c"
    }

  if (startp == NULL)
    {
      no_more = __nss_hosts_lookup (&nip, "getipnodebyname_r", (void **) &fct);
      if (no_more)
	startp = (service_user *) -1l;
      else
	{
	  startp = nip;
	  start_fct = fct;
	}
    }
  else
    {
      fct = start_fct;
      no_more = (nip = startp) == (service_user *) -1l;
    }

  /* First do a lookup with the original protocol type.  */
  while (no_more == 0 && buffer != NULL)
    {
      enum nss_status status;

      status = DL_CALL_FCT (fct, (name, type,
				  ((flags & AI_ALL)
				   ? flags : (flags & ~AI_V4MAPPED)),
				  (struct hostent *) buffer,
				  (char *) ((struct hostent *) buffer + 1),
				  buffer_size - sizeof (struct hostent),
				  &errno, h_errnop));

      if (status == NSS_STATUS_SUCCESS)
	{
	  result = (struct hostent *) buffer;
	  break;
	}

      if (status == NSS_STATUS_TRYAGAIN && *h_errnop == NETDB_INTERNAL
	  && errno == ERANGE)
	{
	  /* Resize the buffer, it's too small.  */
	  buffer_size += BUFLEN;
	  buffer = (char *) realloc (buffer, buffer_size);
	  continue;
	}

      no_more = __nss_next (&nip, "getipnodebyname_r",
			    (void **) &fct, status, 0);
    }

  /* If we are looking for an IPv6 address but haven't found any and
     do not have the AI_ALL but the AI_V4MAPPED flag set, now try
     looking up an IPv4 address and map it.  */
  if (buffer != NULL && result == NULL
      && type == AF_INET6 && (flags & AI_V4MAPPED) != 0
      && (no_more = (nip = startp) == (service_user *) -1l) == 0)
    {
      /* We have to use a new buffer if there is already a result.  */
      fct = start_fct;

      do
	{
	  enum nss_status status;

	  status = DL_CALL_FCT (fct, (name, type, flags,
				      (struct hostent *) buffer,
				      (char *) ((struct hostent *) buffer + 1),
				      buffer_size - sizeof (struct hostent),
				      &errno, h_errnop));

	  if (status == NSS_STATUS_SUCCESS)
	    {
	      result = (struct hostent *) buffer;
	      break;
	    }

	  if (status == NSS_STATUS_TRYAGAIN && *h_errnop == NETDB_INTERNAL
	      && errno == ERANGE)
	    {
	      /* Resize the buffer, it's too small.  */
	      buffer_size += BUFLEN;
	      buffer = (char *) realloc (buffer, buffer_size);
	      continue;
	    }

	  no_more = __nss_next (&nip, "getipnodebyname_r",
				(void **) &fct, status, 0);
	}
      while (no_more == 0 && buffer != NULL);
    }

 done:
  if (buffer == NULL)
    {
      /* We are out of memory.  */
      *h_errnop = TRY_AGAIN;
      assert (result == NULL);
    }
  else if (result == NULL && buffer != NULL)
    free (buffer);

  return result;
}