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