blob: 203621f0d7477fb1aad6d74791e62c37699319f3 [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
15#ifndef HAVE_GETNAMEINFO
Damien Miller2f6a0ad2000-05-31 11:20:11 +100016int getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
17 size_t hostlen, char *serv, size_t servlen, int flags)
Damien Miller34132e52000-01-14 15:45:46 +110018{
Damien Miller2f6a0ad2000-05-31 11:20:11 +100019 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
20 struct hostent *hp;
21 char tmpserv[16];
22
23 if (serv) {
24 snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port));
Damien Millere9edd7c2000-09-29 10:59:14 +110025 if (strlen(tmpserv) >= servlen)
Damien Miller2f6a0ad2000-05-31 11:20:11 +100026 return EAI_MEMORY;
27 else
Damien Millerecf58302000-09-26 13:13:26 +110028 strcpy(serv, tmpserv);
Damien Miller34132e52000-01-14 15:45:46 +110029 }
Damien Miller2f6a0ad2000-05-31 11:20:11 +100030
31 if (host) {
32 if (flags & NI_NUMERICHOST) {
Damien Millere772b682000-09-26 13:10:37 +110033 if (strlen(inet_ntoa(sin->sin_addr)) >= hostlen)
Damien Miller2f6a0ad2000-05-31 11:20:11 +100034 return EAI_MEMORY;
35
Damien Millerecf58302000-09-26 13:13:26 +110036 strcpy(host, inet_ntoa(sin->sin_addr));
Damien Miller2f6a0ad2000-05-31 11:20:11 +100037 return 0;
38 } else {
39 hp = gethostbyaddr((char *)&sin->sin_addr,
40 sizeof(struct in_addr), AF_INET);
41 if (hp == NULL)
42 return EAI_NODATA;
43
Damien Millere772b682000-09-26 13:10:37 +110044 if (strlen(hp->h_name) >= hostlen)
Damien Miller2f6a0ad2000-05-31 11:20:11 +100045 return EAI_MEMORY;
46
Damien Millerecf58302000-09-26 13:13:26 +110047 strcpy(host, hp->h_name);
Damien Miller2f6a0ad2000-05-31 11:20:11 +100048 return 0;
49 }
50 }
51 return 0;
Damien Miller34132e52000-01-14 15:45:46 +110052}
53#endif /* !HAVE_GETNAMEINFO */