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