]>
git.saurik.com Git - apple/libinfo.git/blob - dns.subproj/getnetnamadr.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
26 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
28 * Permission to use, copy, modify, and distribute this software for any
29 * purpose with or without fee is hereby granted, provided that the above
30 * copyright notice and this permission notice appear in all copies.
33 * Copyright (c) 1983, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 #if defined(LIBC_SCCS) && !defined(lint)
66 static char sccsid
[] = "@(#)getnetbyaddr.c 8.1 (Berkeley) 6/4/93";
67 static char sccsid_
[] = "from getnetnamadr.c 1.4 (Coimbra) 93/06/03";
68 static char rcsid
[] = "$Id: getnetnamadr.c,v 1.4 2003/02/18 17:29:23 majka Exp $";
69 #endif /* LIBC_SCCS and not lint */
71 #include <sys/param.h>
72 #include <sys/socket.h>
73 #include <netinet/in.h>
74 #include <arpa/inet.h>
75 #include <arpa/nameser8_compat.h>
80 #include <resolv8_compat.h>
87 #if defined(mips) && defined(SYSTYPE_BSD43)
91 struct netent
*_getnetbyaddr
__P((long net
, int type
));
92 struct netent
*_getnetbyname
__P((const char *name
));
99 #define MAXPACKET PACKETSZ
101 #define MAXPACKET 1024
106 u_char buf
[MAXPACKET
];
114 static struct netent
*
115 getnetanswer(answer
, anslen
, net_i
)
125 int type
, class, buflen
, ancount
, qdcount
, haveanswer
, i
, nchar
;
126 char aux1
[30], aux2
[30], ans
[30], *in
, *st
, *pauxt
, *bp
, **ap
,
127 *paux1
= &aux1
[0], *paux2
= &aux2
[0], flag
= 0;
128 static struct netent net_entry
;
129 static char *net_aliases
[MAXALIASES
], *netbuf
= NULL
;
131 if (netbuf
== NULL
) {
132 netbuf
= malloc(BUFSIZ
+1);
139 * find first satisfactory answer
141 * answer --> +------------+ ( MESSAGE )
144 * | Question | the question for the name server
146 * | Answer | RRs answering the question
148 * | Authority | RRs pointing toward an authority
149 * | Additional | RRs holding additional information
152 eom
= answer
->buf
+ anslen
;
154 ancount
= ntohs(hp
->ancount
); /* #/records in the answer section */
155 qdcount
= ntohs(hp
->qdcount
); /* #/entries in the question section */
157 cp
= answer
->buf
+ HFIXEDSZ
;
160 h_errno
= HOST_NOT_FOUND
;
165 while (qdcount
-- > 0)
166 cp
+= __dn_skipname(cp
, eom
) + QFIXEDSZ
;
169 net_entry
.n_aliases
= net_aliases
;
171 while (--ancount
>= 0 && cp
< eom
) {
172 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
177 (void)strcpy(&ans
[0], bp
);
180 cp
+= INT32SZ
; /* TTL */
182 if (class == C_IN
&& type
== T_PTR
) {
183 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
190 bp
+= strlen(bp
) + 1;
191 net_entry
.n_addrtype
=
192 (class == C_IN
) ? AF_INET
: AF_UNSPEC
;
200 net_entry
.n_name
= *net_entry
.n_aliases
;
201 net_entry
.n_net
= 0L;
204 in
= *net_entry
.n_aliases
;
205 net_entry
.n_name
= &ans
[0];
207 for (i
= 0; i
< 4; i
++) {
208 for (st
= in
, nchar
= 0;
212 if (nchar
!= 1 || *in
!= '0' || flag
) {
216 (i
==0) ?nchar
: nchar
+1);
217 paux1
[(i
==0) ? nchar
: nchar
+1] = '\0';
219 paux2
= strcat(paux1
, paux2
);
224 net_entry
.n_net
= inet_network(paux2
);
227 net_entry
.n_aliases
++;
235 getnetbyaddr(net
, net_type
)
237 register int net_type
;
239 unsigned int netbr
[4];
244 struct netent
*net_entry
;
246 if (net_type
!= AF_INET
)
247 #if defined(__APPLE__)
250 return (_getnetbyaddr(net
, net_type
));
253 for (nn
= 4, net2
= net
; net2
; net2
>>= 8)
254 netbr
[--nn
] = net2
& 0xff;
256 case 3: /* Class A */
257 sprintf(qbuf
, "0.0.0.%u.in-addr.arpa", netbr
[3]);
259 case 2: /* Class B */
260 sprintf(qbuf
, "0.0.%u.%u.in-addr.arpa", netbr
[3], netbr
[2]);
262 case 1: /* Class C */
263 sprintf(qbuf
, "0.%u.%u.%u.in-addr.arpa", netbr
[3], netbr
[2],
266 case 0: /* Class D - E */
267 sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa", netbr
[3], netbr
[2],
271 anslen
= res_query(qbuf
, C_IN
, T_PTR
, (u_char
*)&buf
, sizeof(buf
));
274 if (_res
.options
& RES_DEBUG
)
275 printf("res_query failed\n");
277 #if !defined(__APPLE__)
278 if (errno
== ECONNREFUSED
)
279 return (_getnetbyaddr(net
, net_type
));
283 net_entry
= getnetanswer(&buf
, anslen
, BYADDR
);
285 unsigned u_net
= net
; /* maybe net should be unsigned ? */
287 /* Strip trailing zeros */
288 while ((u_net
& 0xff) == 0 && u_net
!= 0)
290 net_entry
->n_net
= u_net
;
293 #if defined(__APPLE__)
296 return (_getnetbyaddr(net
, net_type
));
302 register const char *net
;
307 struct netent
*net_entry
;
309 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
310 h_errno
= NETDB_INTERNAL
;
313 strcpy(&qbuf
[0], net
);
314 anslen
= res_search(qbuf
, C_IN
, T_PTR
, (u_char
*)&buf
, sizeof(buf
));
317 if (_res
.options
& RES_DEBUG
)
318 printf("res_query failed\n");
320 #if defined(__APPLE__)
323 if (errno
== ECONNREFUSED
)
324 return (_getnetbyname(net
));
325 return (_getnetbyname(net
));
328 net_entry
= getnetanswer(&buf
, anslen
, BYNAME
);
331 #if defined(__APPLE__)
334 return (_getnetbyname(net
));