Reformat all code using astyle.
[oweals/tinc.git] / src / 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 #if !HAVE_DECL_GETNAMEINFO
18
19 int getnameinfo(const struct sockaddr *sa, size_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags) {
20         struct sockaddr_in *sin = (struct sockaddr_in *)sa;
21         struct hostent *hp;
22         int len;
23
24         if(sa->sa_family != AF_INET) {
25                 return EAI_FAMILY;
26         }
27
28         if(serv && servlen) {
29                 len = snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
30
31                 if(len < 0 || len >= servlen) {
32                         return EAI_MEMORY;
33                 }
34         }
35
36         if(!host || !hostlen) {
37                 return 0;
38         }
39
40         if(flags & NI_NUMERICHOST) {
41                 len = snprintf(host, hostlen, "%s", inet_ntoa(sin->sin_addr));
42
43                 if(len < 0 || len >= hostlen) {
44                         return EAI_MEMORY;
45                 }
46
47                 return 0;
48         }
49
50         hp = gethostbyaddr((char *)&sin->sin_addr, sizeof(struct in_addr), AF_INET);
51
52         if(!hp || !hp->h_name || !hp->h_name[0]) {
53                 return EAI_NODATA;
54         }
55
56         len = snprintf(host, hostlen, "%s", hp->h_name);
57
58         if(len < 0 || len >= hostlen) {
59                 return EAI_MEMORY;
60         }
61
62         return 0;
63 }
64 #endif /* !HAVE_GETNAMEINFO */