]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/getnetbydns.c
2 * Copyright (c) 1985, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Portions copyright (c) 1999, 2000
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley, Intel Corporation, and its contributors.
26 * 4. Neither the name of University, Intel Corporation, or their respective
27 * contributors may be used to endorse or promote products derived from
28 * this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND
31 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
32 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS,
34 * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
44 * Permission to use, copy, modify, and distribute this software for any
45 * purpose with or without fee is hereby granted, provided that the above
46 * copyright notice and this permission notice appear in all copies, and that
47 * the name of Digital Equipment Corporation not be used in advertising or
48 * publicity pertaining to distribution of the document or software without
49 * specific, written prior permission.
51 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
52 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
53 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
54 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
55 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
56 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
57 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
62 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
63 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
65 * Permission to use, copy, modify, and distribute this software for any
66 * purpose with or without fee is hereby granted, provided that the above
67 * copyright notice and this permission notice appear in all copies.
70 #if defined(LIBC_SCCS) && !defined(lint)
71 static char sccsid
[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
72 static char rcsid
[] = "$Id: getnetbydns.c,v 1.1.1.1 2003/11/19 01:51:27 kyu3 Exp $";
73 #endif /* LIBC_SCCS and not lint */
75 #include <sys/param.h>
76 #include <sys/socket.h>
77 #include <netinet/in.h>
78 #include <arpa/inet.h>
79 #include <arpa/nameser.h>
92 #include "res_config.h"
93 #include "Socklib_internals.h"
102 #define MAXPACKET PACKETSZ
104 #define MAXPACKET 1024
109 u_char buf
[MAXPACKET
];
117 static struct netent
*
118 getnetanswer(querybuf
*answer
, int anslen
, int net_i
)
125 int type
, class, buflen
, ancount
, qdcount
, haveanswer
, i
, nchar
;
126 char aux1
[MAXHOSTNAMELEN
], aux2
[MAXHOSTNAMELEN
], ans
[MAXHOSTNAMELEN
];
127 char *in
, *st
, *pauxt
, *bp
, **ap
;
128 char *paux1
= &aux1
[0], *paux2
= &aux2
[0], flag
= 0;
129 static struct netent net_entry
;
130 static char *net_aliases
[MAXALIASES
], netbuf
[PACKETSZ
];
133 * find first satisfactory answer
135 * answer --> +------------+ ( MESSAGE )
138 * | Question | the question for the name server
140 * | Answer | RRs answering the question
142 * | Authority | RRs pointing toward an authority
143 * | Additional | RRs holding additional information
146 eom
= answer
->buf
+ anslen
;
148 ancount
= ntohs(hp
->ancount
); /* #/records in the answer section */
149 qdcount
= ntohs(hp
->qdcount
); /* #/entries in the question section */
151 buflen
= sizeof(netbuf
);
152 cp
= answer
->buf
+ HFIXEDSZ
;
155 h_errno
= HOST_NOT_FOUND
;
160 while (qdcount
-- > 0)
161 cp
+= __dn_skipname(cp
, eom
) + QFIXEDSZ
;
164 net_entry
.n_aliases
= net_aliases
;
166 while (--ancount
>= 0 && cp
< eom
) {
167 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
168 if ((n
< 0) || !res_dnok(bp
))
172 (void)strncpy(&ans
[0], bp
, sizeof(ans
) - 1);
173 ans
[sizeof(ans
) - 1] = '\0';
176 cp
+= INT32SZ
; /* TTL */
178 if (class == C_IN
&& type
== T_PTR
) {
179 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
180 if ((n
< 0) || !res_hnok(bp
)) {
186 bp
+= strlen(bp
) + 1;
187 net_entry
.n_addrtype
=
188 (class == C_IN
) ? AF_INET
: AF_UNSPEC
;
196 net_entry
.n_name
= *net_entry
.n_aliases
;
197 net_entry
.n_net
= 0L;
200 in
= *net_entry
.n_aliases
;
201 net_entry
.n_name
= &ans
[0];
203 for (i
= 0; i
< 4; i
++) {
204 for (st
= in
, nchar
= 0;
208 if (nchar
!= 1 || *in
!= '0' || flag
) {
212 (i
==0) ?nchar
: nchar
+1);
213 paux1
[(i
==0) ? nchar
: nchar
+1] = '\0';
215 paux2
= strcat(paux1
, paux2
);
220 net_entry
.n_net
= inet_network(paux2
);
223 net_entry
.n_aliases
++;
231 _getnetbydnsaddr(register unsigned long net
, register int net_type
)
233 unsigned int netbr
[4];
238 struct netent
*net_entry
;
240 if (net_type
!= AF_INET
)
243 for (nn
= 4, net2
= net
; net2
; net2
>>= 8)
244 netbr
[--nn
] = net2
& 0xff;
246 case 3: /* Class A */
247 sprintf(qbuf
, "0.0.0.%u.in-addr.arpa", netbr
[3]);
249 case 2: /* Class B */
250 sprintf(qbuf
, "0.0.%u.%u.in-addr.arpa", netbr
[3], netbr
[2]);
252 case 1: /* Class C */
253 sprintf(qbuf
, "0.%u.%u.%u.in-addr.arpa", netbr
[3], netbr
[2],
256 case 0: /* Class D - E */
257 sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa", netbr
[3], netbr
[2],
261 anslen
= res_query(qbuf
, C_IN
, T_PTR
, (u_char
*)&buf
, sizeof(buf
));
264 if (_res
.options
& RES_DEBUG
)
265 printf("res_query failed\n");
269 net_entry
= getnetanswer(&buf
, anslen
, BYADDR
);
271 unsigned u_net
= net
; /* maybe net should be unsigned ? */
273 /* Strip trailing zeros */
274 while ((u_net
& 0xff) == 0 && u_net
!= 0)
276 net_entry
->n_net
= u_net
;
283 _getnetbydnsname(register const char *net
)
289 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
290 h_errno
= NETDB_INTERNAL
;
293 strncpy(qbuf
, net
, sizeof(qbuf
) - 1);
294 qbuf
[sizeof(qbuf
) - 1] = '\0';
295 anslen
= res_search(qbuf
, C_IN
, T_PTR
, (u_char
*)&buf
, sizeof(buf
));
298 if (_res
.options
& RES_DEBUG
)
299 printf("res_query failed\n");
303 return getnetanswer(&buf
, anslen
, BYNAME
);
307 _setnetdnsent(int stayopen
)
310 _res
.options
|= RES_STAYOPEN
| RES_USEVC
;
316 _res
.options
&= ~(RES_STAYOPEN
| RES_USEVC
);