Better error messages under Windows.
[tinc] / 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 "system.h"
13
14 #include "fake-getnameinfo.h"
15 #include "fake-getaddrinfo.h"
16
17 #ifndef HAVE_GETNAMEINFO
18
19 int getnameinfo(const struct sockaddr *sa, size_t salen, char *host, 
20                 size_t hostlen, char *serv, size_t servlen, int flags)
21 {
22         struct sockaddr_in *sin = (struct sockaddr_in *)sa;
23         struct hostent *hp;
24         char tmpserv[16];
25
26         if (serv) {
27                 snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port));
28                 if (strlen(tmpserv) >= servlen)
29                         return EAI_MEMORY;
30                 else
31                         strcpy(serv, tmpserv);
32         }
33
34         if (host) {
35                 if (flags & NI_NUMERICHOST) {
36                         if (strlen(inet_ntoa(sin->sin_addr)) >= hostlen)
37                                 return EAI_MEMORY;
38
39                         strcpy(host, inet_ntoa(sin->sin_addr));
40                         return 0;
41                 } else {
42                         hp = gethostbyaddr((char *)&sin->sin_addr, 
43                                 sizeof(struct in_addr), AF_INET);
44                         if (hp == NULL)
45                                 return EAI_NODATA;
46                         
47                         if (strlen(hp->h_name) >= hostlen)
48                                 return EAI_MEMORY;
49
50                         strcpy(host, hp->h_name);
51                         return 0;
52                 }
53         }
54         return 0;
55 }
56 #endif /* !HAVE_GETNAMEINFO */