getnameinfo fixes
[oweals/tinc.git] / lib / fake-getnameinfo.c
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 "config.h"
13
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <system.h>
17
18 /* RCSID("$Id: fake-getnameinfo.c,v 1.1.2.2 2002/06/09 15:50:11 zarq Exp $"); */
19
20 #ifndef HAVE_GETNAMEINFO
21
22 int getnameinfo(const struct sockaddr *sa, size_t salen, char *host, 
23                 size_t hostlen, char *serv, size_t servlen, int flags)
24 {
25         struct sockaddr_in *sin = (struct sockaddr_in *)sa;
26         struct hostent *hp;
27         char tmpserv[16];
28
29         if (serv) {
30                 snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port));
31                 if (strlen(tmpserv) >= servlen)
32                         return EAI_MEMORY;
33                 else
34                         strcpy(serv, tmpserv);
35         }
36
37         if (host) {
38                 if (flags & NI_NUMERICHOST) {
39                         if (strlen(inet_ntoa(sin->sin_addr)) >= hostlen)
40                                 return EAI_MEMORY;
41
42                         strcpy(host, inet_ntoa(sin->sin_addr));
43                         return 0;
44                 } else {
45                         hp = gethostbyaddr((char *)&sin->sin_addr, 
46                                 sizeof(struct in_addr), AF_INET);
47                         if (hp == NULL)
48                                 return EAI_NODATA;
49                         
50                         if (strlen(hp->h_name) >= hostlen)
51                                 return EAI_MEMORY;
52
53                         strcpy(host, hp->h_name);
54                         return 0;
55                 }
56         }
57         return 0;
58 }
59 #endif /* !HAVE_GETNAMEINFO */