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