]>
git.ipfire.org Git - thirdparty/glibc.git/blob - nss/nss_dns/dns-network.c
a2d9d280a5bc27f44104a61a3eea3eca6a5a8268
1 /* Copyright (C) 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Extended from original form by Ulrich Drepper <drepper@cygnus.com>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 /* Parts of this file are plain copies of the file `getnetnamadr.c' from
21 the bind package and it has the following copyright. */
23 /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
24 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
26 * Permission to use, copy, modify, and distribute this software for any
27 * purpose with or without fee is hereby granted, provided that the above
28 * copyright notice and this permission notice appear in all copies.
31 * Copyright (c) 1983, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 #include <arpa/inet.h>
73 /* Maximum number of aliases we allow. */
74 #define MAX_NR_ALIASES 48
78 #define MAXPACKET PACKETSZ
80 #define MAXPACKET 1024
91 /* We need this time later. */
92 typedef union querybuf
95 u_char buf
[MAXPACKET
];
99 /* Protortypes for local functions. */
100 static int getanswer_r (const querybuf
*answer
, int anslen
,
101 struct netent
*result
, char *buffer
, int buflen
,
102 lookup_method net_i
);
106 _nss_dns_getnetbyname_r (const char *name
, struct netent
*result
,
107 char *buffer
, int buflen
)
109 /* Return entry for network with NAME. */
114 qbuf
= strdupa (name
);
115 anslen
= res_search (qbuf
, C_IN
, T_PTR
, (u_char
*) &net_buffer
,
119 return (errno
== ECONNREFUSED
120 || errno
== EPFNOSUPPORT
121 || errno
== EAFNOSUPPORT
)
122 ? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
;
124 return getanswer_r (&net_buffer
, anslen
, result
, buffer
, buflen
, BYNAME
);
129 _nss_dns_getnetbyaddr_r (long net
, int type
, struct netent
*result
,
130 char *buffer
, int buflen
)
132 /* Return entry for network with NAME. */
135 unsigned int net_bytes
[4];
140 /* No net address lookup for IPv6 yet. */
142 return NSS_STATUS_UNAVAIL
;
144 net2
= (u_int32_t
) net
;
145 for (cnt
= 4; net2
!= 0; net2
>>= 8)
146 net_bytes
[--cnt
] = net2
& 0xff;
151 /* Class A network. */
152 sprintf (qbuf
, "0.0.0.%u.in-addr.arpa", net_bytes
[3]);
155 /* Class B network. */
156 sprintf (qbuf
, "0.0.%u.%u.in-addr.arpa", net_bytes
[3], net_bytes
[2]);
159 /* Class C network. */
160 sprintf (qbuf
, "0.%u.%u.%u.in-addr.arpa", net_bytes
[3], net_bytes
[2],
164 /* Class D - E network. */
165 sprintf (qbuf
, "%u.%u.%u.%u.in-addr.arpa", net_bytes
[3], net_bytes
[2],
166 net_bytes
[1], net_bytes
[0]);
170 anslen
= res_query (qbuf
, C_IN
, T_PTR
, (u_char
*) &net_buffer
,
174 return (errno
== ECONNREFUSED
175 || errno
== EPFNOSUPPORT
176 || errno
== EAFNOSUPPORT
)
177 ? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
;
179 status
= getanswer_r (&net_buffer
, anslen
, result
, buffer
, buflen
, BYADDR
);
180 if (status
== NSS_STATUS_SUCCESS
)
182 /* Strip trailing zeros. */
183 unsigned int u_net
= net
; /* Maybe net should be unsigned? */
185 while ((u_net
& 0xff) == 0 && u_net
!= 0)
187 result
->n_net
= u_net
;
195 #define offsetof(Type, Member) ((size_t) &((Type *) NULL)->Member)
198 getanswer_r (const querybuf
*answer
, int anslen
, struct netent
*result
,
199 char *buffer
, int buflen
, lookup_method net_i
)
202 * Find first satisfactory answer
204 * answer --> +------------+ ( MESSAGE )
207 * | Question | the question for the name server
209 * | Answer | RRs answering the question
211 * | Authority | RRs pointing toward an authority
212 * | Additional | RRs holding additional information
217 char *aliases
[MAX_NR_ALIASES
];
219 } *net_data
= (struct net_data
*) buffer
;
220 int linebuflen
= buflen
- offsetof (struct net_data
, linebuffer
);
221 const char *end_of_message
= &answer
->buf
[anslen
];
222 const HEADER
*header_pointer
= &answer
->hdr
;
223 /* #/records in the answer section. */
224 int answer_count
= ntohs (header_pointer
->ancount
);
225 /* #/entries in the question section. */
226 int question_count
= ntohs (header_pointer
->qdcount
);
227 char *bp
= net_data
->linebuffer
;
228 const char *cp
= &answer
->buf
[HFIXEDSZ
];
229 char **alias_pointer
;
233 if (question_count
== 0)
234 /* FIXME: the Sun version uses for host name lookup an additional
235 parameter for pointing to h_errno. this is missing here.
236 OSF/1 has a per-thread h_errno variable. */
237 if (header_pointer
->aa
!= 0)
239 __set_h_errno (HOST_NOT_FOUND
);
240 return NSS_STATUS_NOTFOUND
;
244 __set_h_errno (TRY_AGAIN
);
245 return NSS_STATUS_TRYAGAIN
;
248 /* Skip the question part. */
249 while (question_count
-- > 0)
250 cp
+= __dn_skipname (cp
, end_of_message
) + QFIXEDSZ
;
252 alias_pointer
= result
->n_aliases
= &net_data
->aliases
[0];
253 *alias_pointer
= NULL
;
257 while (--answer_count
>= 0 && cp
< end_of_message
)
259 int n
= dn_expand (answer
->buf
, end_of_message
, cp
, bp
, linebuflen
);
262 if (n
< 0 || res_dnok (bp
) == 0)
267 GETSHORT (class, cp
);
268 cp
+= INT32SZ
; /* TTL */
271 if (class == C_IN
&& type
== T_PTR
)
273 n
= dn_expand (answer
->buf
, end_of_message
, cp
, bp
, linebuflen
);
274 if (n
< 0 || !res_hnok (bp
))
276 /* XXX What does this mean? The original form from bind
277 returns NULL. Incrementing cp has no effect in any case.
278 What should I return here. ??? */
280 return NSS_STATUS_UNAVAIL
;
283 *alias_pointer
++ = bp
;
284 bp
+= strlen (bp
) + 1;
285 result
->n_addrtype
= class == C_IN
? AF_INET
: AF_UNSPEC
;
294 char *in
, *cp
, *rp
, *wp
;
297 *alias_pointer
= NULL
;
301 result
->n_name
= result
->n_aliases
[0];
305 len
= strlen (result
->n_aliases
[0]);
306 tmp
= (char *) alloca (len
+ 1);
310 rp
= in
= result
->n_aliases
[0];
311 result
->n_name
= ans
;
314 for (cnt
= 0; cnt
< 4; ++cnt
)
321 if (rp
- startp
> 1 || *startp
!= '0' || !first_flag
)
333 result
->n_net
= inet_network (wp
);
338 return NSS_STATUS_SUCCESS
;
341 __set_h_errno (TRY_AGAIN
);
342 return NSS_STATUS_TRYAGAIN
;