]>
git.saurik.com Git - apple/libinfo.git/blob - dns.subproj/getnetnamadr.c
bd62e062ed19af3e89f787d0bf4643fb4c6a1ecc
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
25 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
27 * Permission to use, copy, modify, and distribute this software for any
28 * purpose with or without fee is hereby granted, provided that the above
29 * copyright notice and this permission notice appear in all copies.
32 * Copyright (c) 1983, 1993
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 #if defined(LIBC_SCCS) && !defined(lint)
65 static char sccsid
[] = "@(#)getnetbyaddr.c 8.1 (Berkeley) 6/4/93";
66 static char sccsid_
[] = "from getnetnamadr.c 1.4 (Coimbra) 93/06/03";
67 static char rcsid
[] = "$Id: getnetnamadr.c,v 1.5 2004/10/07 17:33:33 majka Exp $";
68 #endif /* LIBC_SCCS and not lint */
70 #include <sys/param.h>
71 #include <sys/socket.h>
72 #include <netinet/in.h>
73 #include <arpa/inet.h>
74 #include <arpa/nameser8_compat.h>
79 #include <resolv8_compat.h>
86 #if defined(mips) && defined(SYSTYPE_BSD43)
90 struct netent
*_getnetbyaddr
__P((long net
, int type
));
91 struct netent
*_getnetbyname
__P((const char *name
));
98 #define MAXPACKET PACKETSZ
100 #define MAXPACKET 1024
105 u_char buf
[MAXPACKET
];
113 static struct netent
*
114 getnetanswer(answer
, anslen
, net_i
)
124 int type
, class, buflen
, ancount
, qdcount
, haveanswer
, i
, nchar
;
125 char aux1
[30], aux2
[30], ans
[30], *in
, *st
, *pauxt
, *bp
, **ap
,
126 *paux1
= &aux1
[0], *paux2
= &aux2
[0], flag
= 0;
127 static struct netent net_entry
;
128 static char *net_aliases
[MAXALIASES
], *netbuf
= NULL
;
130 if (netbuf
== NULL
) {
131 netbuf
= malloc(BUFSIZ
+1);
138 * find first satisfactory answer
140 * answer --> +------------+ ( MESSAGE )
143 * | Question | the question for the name server
145 * | Answer | RRs answering the question
147 * | Authority | RRs pointing toward an authority
148 * | Additional | RRs holding additional information
151 eom
= answer
->buf
+ anslen
;
153 ancount
= ntohs(hp
->ancount
); /* #/records in the answer section */
154 qdcount
= ntohs(hp
->qdcount
); /* #/entries in the question section */
156 cp
= answer
->buf
+ HFIXEDSZ
;
159 h_errno
= HOST_NOT_FOUND
;
164 while (qdcount
-- > 0)
165 cp
+= __dn_skipname(cp
, eom
) + QFIXEDSZ
;
168 net_entry
.n_aliases
= net_aliases
;
170 while (--ancount
>= 0 && cp
< eom
) {
171 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
176 (void)strcpy(&ans
[0], bp
);
179 cp
+= INT32SZ
; /* TTL */
181 if (class == C_IN
&& type
== T_PTR
) {
182 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
189 bp
+= strlen(bp
) + 1;
190 net_entry
.n_addrtype
=
191 (class == C_IN
) ? AF_INET
: AF_UNSPEC
;
199 net_entry
.n_name
= *net_entry
.n_aliases
;
200 net_entry
.n_net
= 0L;
203 in
= *net_entry
.n_aliases
;
204 net_entry
.n_name
= &ans
[0];
206 for (i
= 0; i
< 4; i
++) {
207 for (st
= in
, nchar
= 0;
211 if (nchar
!= 1 || *in
!= '0' || flag
) {
215 (i
==0) ?nchar
: nchar
+1);
216 paux1
[(i
==0) ? nchar
: nchar
+1] = '\0';
218 paux2
= strcat(paux1
, paux2
);
223 net_entry
.n_net
= inet_network(paux2
);
226 net_entry
.n_aliases
++;
234 getnetbyaddr(net
, net_type
)
235 register uint32_t net
;
236 register int net_type
;
238 unsigned int netbr
[4];
243 struct netent
*net_entry
;
245 if (net_type
!= AF_INET
)
246 #if defined(__APPLE__)
249 return (_getnetbyaddr(net
, net_type
));
252 for (nn
= 4, net2
= net
; net2
; net2
>>= 8)
253 netbr
[--nn
] = net2
& 0xff;
255 case 3: /* Class A */
256 sprintf(qbuf
, "0.0.0.%u.in-addr.arpa", netbr
[3]);
258 case 2: /* Class B */
259 sprintf(qbuf
, "0.0.%u.%u.in-addr.arpa", netbr
[3], netbr
[2]);
261 case 1: /* Class C */
262 sprintf(qbuf
, "0.%u.%u.%u.in-addr.arpa", netbr
[3], netbr
[2],
265 case 0: /* Class D - E */
266 sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa", netbr
[3], netbr
[2],
270 anslen
= res_query(qbuf
, C_IN
, T_PTR
, (u_char
*)&buf
, sizeof(buf
));
273 if (_res
.options
& RES_DEBUG
)
274 printf("res_query failed\n");
276 #if !defined(__APPLE__)
277 if (errno
== ECONNREFUSED
)
278 return (_getnetbyaddr(net
, net_type
));
282 net_entry
= getnetanswer(&buf
, anslen
, BYADDR
);
284 unsigned u_net
= net
; /* maybe net should be unsigned ? */
286 /* Strip trailing zeros */
287 while ((u_net
& 0xff) == 0 && u_net
!= 0)
289 net_entry
->n_net
= u_net
;
292 #if defined(__APPLE__)
295 return (_getnetbyaddr(net
, net_type
));
301 register const char *net
;
306 struct netent
*net_entry
;
308 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
309 h_errno
= NETDB_INTERNAL
;
312 strcpy(&qbuf
[0], net
);
313 anslen
= res_search(qbuf
, C_IN
, T_PTR
, (u_char
*)&buf
, sizeof(buf
));
316 if (_res
.options
& RES_DEBUG
)
317 printf("res_query failed\n");
319 #if defined(__APPLE__)
322 if (errno
== ECONNREFUSED
)
323 return (_getnetbyname(net
));
324 return (_getnetbyname(net
));
327 net_entry
= getnetanswer(&buf
, anslen
, BYNAME
);
330 #if defined(__APPLE__)
333 return (_getnetbyname(net
));