blob: e255ed333eba94e733602f786b07796f12c73950 [file] [log] [blame]
Damien Miller34132e52000-01-14 15:45:46 +11001/*
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.
Damien Miller34132e52000-01-14 15:45:46 +110010 */
11
12#include "includes.h"
13#include "ssh.h"
14
Damien Millere9cf3572001-02-09 12:55:35 +110015RCSID("$Id: fake-getnameinfo.c,v 1.2 2001/02/09 01:55:36 djm Exp $");
16
Damien Miller34132e52000-01-14 15:45:46 +110017#ifndef HAVE_GETNAMEINFO
Damien Miller2f6a0ad2000-05-31 11:20:11 +100018int getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
19 size_t hostlen, char *serv, size_t servlen, int flags)
Damien Miller34132e52000-01-14 15:45:46 +110020{
Damien Miller2f6a0ad2000-05-31 11:20:11 +100021 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
22 struct hostent *hp;
23 char tmpserv[16];
24
25 if (serv) {
26 snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port));
Damien Millere9edd7c2000-09-29 10:59:14 +110027 if (strlen(tmpserv) >= servlen)
Damien Miller2f6a0ad2000-05-31 11:20:11 +100028 return EAI_MEMORY;
29 else
Damien Millerecf58302000-09-26 13:13:26 +110030 strcpy(serv, tmpserv);
Damien Miller34132e52000-01-14 15:45:46 +110031 }
Damien Miller2f6a0ad2000-05-31 11:20:11 +100032
33 if (host) {
34 if (flags & NI_NUMERICHOST) {
Damien Millere772b682000-09-26 13:10:37 +110035 if (strlen(inet_ntoa(sin->sin_addr)) >= hostlen)
Damien Miller2f6a0ad2000-05-31 11:20:11 +100036 return EAI_MEMORY;
37
Damien Millerecf58302000-09-26 13:13:26 +110038 strcpy(host, inet_ntoa(sin->sin_addr));
Damien Miller2f6a0ad2000-05-31 11:20:11 +100039 return 0;
40 } else {
41 hp = gethostbyaddr((char *)&sin->sin_addr,
42 sizeof(struct in_addr), AF_INET);
43 if (hp == NULL)
44 return EAI_NODATA;
45
Damien Millere772b682000-09-26 13:10:37 +110046 if (strlen(hp->h_name) >= hostlen)
Damien Miller2f6a0ad2000-05-31 11:20:11 +100047 return EAI_MEMORY;
48
Damien Millerecf58302000-09-26 13:13:26 +110049 strcpy(host, hp->h_name);
Damien Miller2f6a0ad2000-05-31 11:20:11 +100050 return 0;
51 }
52 }
53 return 0;
Damien Miller34132e52000-01-14 15:45:46 +110054}
55#endif /* !HAVE_GETNAMEINFO */