yp_dnslookup.c revision 12891
1/*
2 * Copyright (c) 1995
3 *	Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by Bill Paul.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	$Id: yp_dnslookup.c,v 1.4 1995/12/07 05:01:34 wpaul Exp $
33 */
34
35/*
36 * Do standard and reverse DNS lookups using the resolver library.
37 * Take care of all the dirty work here so the main program only has to
38 * pass us a pointer to an array of characters.
39 *
40 * We have to use direct resolver calls here otherwise the YP server
41 * could end up looping by calling itself over and over again until
42 * it disappeared up its own belly button.
43 */
44
45#include <sys/types.h>
46#include <stdio.h>
47#include <string.h>
48#include <stdlib.h>
49#include <sys/param.h>
50#include <netdb.h>
51#include <netinet/in.h>
52#include <sys/socket.h>
53#include <arpa/inet.h>
54#include "yp_extern.h"
55
56extern struct hostent *_gethostbydnsname __P(( char * ));
57extern struct hostent *_gethostbydnsaddr __P(( const char *, int, int ));
58
59static char *parse(hp)
60	struct hostent *hp;
61{
62	static char result[MAXHOSTNAMELEN * 2];
63	int len,i;
64	struct in_addr addr;
65
66	len = 16 + strlen(hp->h_name);
67	for (i = 0; hp->h_aliases[i]; i++)
68		len += strlen(hp->h_aliases[i]) + 1;
69
70	bzero(result, sizeof(result));
71
72	bcopy(hp->h_addr, &addr, sizeof(struct in_addr));
73	snprintf(result, sizeof(result), "%s %s", inet_ntoa(addr), hp->h_name);
74
75	for (i = 0; hp->h_aliases[i]; i++) {
76		strcat(result, " ");
77		strcat(result, hp->h_aliases[i]);
78	}
79
80	return ((char *)&result);
81}
82
83char *yp_dnsname(address)
84	char *address;
85{
86	struct hostent *hp;
87
88	if (strchr(address, '@'))
89		return (NULL);
90	if ((hp = (struct hostent *)_gethostbydnsname(address)) == NULL)
91		return (NULL);
92
93	return(parse(hp));
94}
95
96char *yp_dnsaddr(address)
97	const char *address;
98{
99	struct hostent *hp;
100	struct in_addr addr;
101
102	if (strchr(address, '@'))
103		return (NULL);
104	if (!inet_aton(address, &addr))
105		return (NULL);
106	if ((hp = (struct hostent *)_gethostbydnsaddr((const char *)&addr,
107	     sizeof(unsigned long), AF_INET)) == NULL)
108		return (NULL);
109
110	return(parse(hp));
111}
112