add support for reverse name lookups from hosts file to getnameinfo
[musl] / src / network / getnameinfo.c
1 #include <netdb.h>
2 #include <limits.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7 #include <arpa/inet.h>
8 #include <net/if.h>
9 #include <ctype.h>
10 #include "lookup.h"
11 #include "stdio_impl.h"
12
13 int __dns_parse(const unsigned char *, int, int (*)(void *, int, const void *, int, const void *), void *);
14 int __dn_expand(const unsigned char *, const unsigned char *, const unsigned char *, char *, int);
15 int __res_mkquery(int, const char *, int, int, const unsigned char *, int, const unsigned char*, unsigned char *, int);
16 int __res_send(const unsigned char *, int, unsigned char *, int);
17
18 #define PTR_MAX (64 + sizeof ".in-addr.arpa")
19 #define RR_PTR 12
20
21 static char *itoa(char *p, unsigned x) {
22         p += 3*sizeof(int);
23         *--p = 0;
24         do {
25                 *--p = '0' + x % 10;
26                 x /= 10;
27         } while (x);
28         return p;
29 }
30
31 static void mkptr4(char *s, const unsigned char *ip)
32 {
33         sprintf(s, "%d.%d.%d.%d.in-addr.arpa",
34                 ip[3], ip[2], ip[1], ip[0]);
35 }
36
37 static void mkptr6(char *s, const unsigned char *ip)
38 {
39         static const char xdigits[] = "0123456789abcdef";
40         int i;
41         for (i=15; i>=0; i--) {
42                 *s++ = xdigits[ip[i]&15]; *s++ = '.';
43                 *s++ = xdigits[ip[i]>>4]; *s++ = '.';
44         }
45         strcpy(s, "ip6.arpa");
46 }
47
48 static char *reverse_hosts(char *buf, const unsigned char *a, unsigned scopeid, int family)
49 {
50         char line[512], *p, *z;
51         unsigned char _buf[1032], atmp[16];
52         struct address iplit;
53         FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
54         if (!f) return 0;
55         if (family == AF_INET) {
56                 memcpy(atmp+12, a, 4);
57                 memcpy(atmp, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
58                 a = atmp;
59         }
60         while (fgets(line, sizeof line, f)) {
61                 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
62
63                 for (p=line; *p && !isspace(*p); p++);
64                 *p++ = 0;
65                 if (__lookup_ipliteral(&iplit, line, AF_UNSPEC)<=0)
66                         continue;
67
68                 if (iplit.family == AF_INET) {
69                         memcpy(iplit.addr+12, iplit.addr, 4);
70                         memcpy(iplit.addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
71                         iplit.scopeid = 0;
72                 }
73
74                 if (memcmp(a, iplit.addr, 16) || iplit.scopeid != scopeid)
75                         continue;
76                         
77                 for (; *p && isspace(*p); p++);
78                 for (z=p; *z && !isspace(*z); z++);
79                 *z = 0;
80                 if (z-p < 256) {
81                         memcpy(buf, p, z-p+1);
82                         break;
83                 }
84         }
85         __fclose_ca(f);
86         return 0;
87 }
88
89 static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet)
90 {
91         char tmp[256];
92         if (rr != RR_PTR) return 0;
93         if (__dn_expand(packet, (const unsigned char *)packet + 512,
94             data, tmp, sizeof tmp) > 0)
95                 strcpy(c, tmp);
96         return 0;
97         
98 }
99
100 int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl,
101         char *restrict node, socklen_t nodelen,
102         char *restrict serv, socklen_t servlen,
103         int flags)
104 {
105         char ptr[PTR_MAX];
106         char buf[256], num[3*sizeof(int)+1];
107         int af = sa->sa_family;
108         unsigned char *a;
109         unsigned scopeid;
110
111         switch (af) {
112         case AF_INET:
113                 a = (void *)&((struct sockaddr_in *)sa)->sin_addr;
114                 if (sl != sizeof(struct sockaddr_in)) return EAI_FAMILY;
115                 mkptr4(ptr, a);
116                 scopeid = 0;
117                 break;
118         case AF_INET6:
119                 a = (void *)&((struct sockaddr_in6 *)sa)->sin6_addr;
120                 if (sl != sizeof(struct sockaddr_in6)) return EAI_FAMILY;
121                 if (memcmp(a, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12))
122                         mkptr6(ptr, a);
123                 else
124                         mkptr4(ptr, a+12);
125                 scopeid = ((struct sockaddr_in6 *)sa)->sin6_scope_id;
126                 break;
127         default:
128                 return EAI_FAMILY;
129         }
130
131         if (node && nodelen) {
132                 buf[0] = 0;
133                 if (!(flags & NI_NUMERICHOST)) {
134                         reverse_hosts(buf, a, scopeid, af);
135                 }
136                 if (!*buf && !(flags & NI_NUMERICHOST)) {
137                         unsigned char query[18+PTR_MAX], reply[512];
138                         int qlen = __res_mkquery(0, ptr, 1, RR_PTR,
139                                 0, 0, 0, query, sizeof query);
140                         int rlen = __res_send(query, qlen, reply, sizeof reply);
141                         buf[0] = 0;
142                         if (rlen > 0)
143                                 __dns_parse(reply, rlen, dns_parse_callback, buf);
144                 }
145                 if (!*buf) {
146                         if (flags & NI_NAMEREQD) return EAI_NONAME;
147                         inet_ntop(af, a, buf, sizeof buf);
148                         if (scopeid) {
149                                 char *p = 0, tmp[IF_NAMESIZE+1];
150                                 if (!(flags & NI_NUMERICSCOPE) &&
151                                     (IN6_IS_ADDR_LINKLOCAL(a) ||
152                                      IN6_IS_ADDR_MC_LINKLOCAL(a)))
153                                         p = if_indextoname(scopeid, tmp+1);
154                                 if (!p)
155                                         p = itoa(num, scopeid);
156                                 *--p = '%';
157                                 strcat(buf, p);
158                         }
159                 }
160                 if (strlen(buf) >= nodelen) return EAI_OVERFLOW;
161                 strcpy(node, buf);
162         }
163
164         if (serv && servlen) {
165                 char *p = itoa(num, ntohs(((struct sockaddr_in *)sa)->sin_port));
166                 if (strlen(p) >= servlen)
167                         return EAI_OVERFLOW;
168                 strcpy(serv, p);
169         }
170
171         return 0;
172 }