1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * fake library for ssh
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * This file includes getnameinfo().
5*0Sstevel@tonic-gate  * These funtions are defined in rfc2133.
6*0Sstevel@tonic-gate  *
7*0Sstevel@tonic-gate  * But these functions are not implemented correctly. The minimum subset
8*0Sstevel@tonic-gate  * is implemented for ssh use only. For exapmle, this routine assumes
9*0Sstevel@tonic-gate  * that ai_family is AF_INET. Don't use it for another purpose.
10*0Sstevel@tonic-gate  */
11*0Sstevel@tonic-gate 
12*0Sstevel@tonic-gate #include "includes.h"
13*0Sstevel@tonic-gate #include "ssh.h"
14*0Sstevel@tonic-gate 
15*0Sstevel@tonic-gate RCSID("$Id: fake-getnameinfo.c,v 1.2 2001/02/09 01:55:36 djm Exp $");
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
18*0Sstevel@tonic-gate 
19*0Sstevel@tonic-gate #ifndef HAVE_GETNAMEINFO
getnameinfo(const struct sockaddr * sa,size_t salen,char * host,size_t hostlen,char * serv,size_t servlen,int flags)20*0Sstevel@tonic-gate int getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
21*0Sstevel@tonic-gate                 size_t hostlen, char *serv, size_t servlen, int flags)
22*0Sstevel@tonic-gate {
23*0Sstevel@tonic-gate 	struct sockaddr_in *sin = (struct sockaddr_in *)sa;
24*0Sstevel@tonic-gate 	struct hostent *hp;
25*0Sstevel@tonic-gate 	char tmpserv[16];
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate 	if (serv) {
28*0Sstevel@tonic-gate 		snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port));
29*0Sstevel@tonic-gate 		if (strlen(tmpserv) >= servlen)
30*0Sstevel@tonic-gate 			return EAI_MEMORY;
31*0Sstevel@tonic-gate 		else
32*0Sstevel@tonic-gate 			strcpy(serv, tmpserv);
33*0Sstevel@tonic-gate 	}
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate 	if (host) {
36*0Sstevel@tonic-gate 		if (flags & NI_NUMERICHOST) {
37*0Sstevel@tonic-gate 			if (strlen(inet_ntoa(sin->sin_addr)) >= hostlen)
38*0Sstevel@tonic-gate 				return EAI_MEMORY;
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate 			strcpy(host, inet_ntoa(sin->sin_addr));
41*0Sstevel@tonic-gate 			return 0;
42*0Sstevel@tonic-gate 		} else {
43*0Sstevel@tonic-gate 			hp = gethostbyaddr((char *)&sin->sin_addr,
44*0Sstevel@tonic-gate 				sizeof(struct in_addr), AF_INET);
45*0Sstevel@tonic-gate 			if (hp == NULL)
46*0Sstevel@tonic-gate 				return EAI_NODATA;
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate 			if (strlen(hp->h_name) >= hostlen)
49*0Sstevel@tonic-gate 				return EAI_MEMORY;
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate 			strcpy(host, hp->h_name);
52*0Sstevel@tonic-gate 			return 0;
53*0Sstevel@tonic-gate 		}
54*0Sstevel@tonic-gate 	}
55*0Sstevel@tonic-gate 	return 0;
56*0Sstevel@tonic-gate }
57*0Sstevel@tonic-gate #endif /* !HAVE_GETNAMEINFO */
58