add support for reverse port lookups from services 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 void 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;
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 }
87
88 static void reverse_services(char *buf, int port, int dgram)
89 {
90         unsigned long svport;
91         char line[128], *p, *z;
92         unsigned char _buf[1032];
93         FILE _f, *f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf);
94         if (!f) return;
95         while (fgets(line, sizeof line, f)) {
96                 if ((p=strchr(line, '#'))) *p++='\n', *p=0;
97
98                 for (p=line; *p && !isspace(*p); p++);
99                 if (!p) continue;
100                 *p++ = 0;
101                 svport = strtoul(p, &z, 10);
102
103                 if (svport != port || z==p) continue;
104                 if (dgram && strncmp(z, "/udp", 4)) continue;
105                 if (!dgram && strncmp(z, "/tcp", 4)) continue;
106                 if (p-line > 32) continue;
107
108                 memcpy(buf, line, p-line);
109                 break;
110         }
111         __fclose_ca(f);
112 }
113
114 static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet)
115 {
116         char tmp[256];
117         if (rr != RR_PTR) return 0;
118         if (__dn_expand(packet, (const unsigned char *)packet + 512,
119             data, tmp, sizeof tmp) > 0)
120                 strcpy(c, tmp);
121         return 0;
122         
123 }
124
125 int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl,
126         char *restrict node, socklen_t nodelen,
127         char *restrict serv, socklen_t servlen,
128         int flags)
129 {
130         char ptr[PTR_MAX];
131         char buf[256], num[3*sizeof(int)+1];
132         int af = sa->sa_family;
133         unsigned char *a;
134         unsigned scopeid;
135
136         switch (af) {
137         case AF_INET:
138                 a = (void *)&((struct sockaddr_in *)sa)->sin_addr;
139                 if (sl != sizeof(struct sockaddr_in)) return EAI_FAMILY;
140                 mkptr4(ptr, a);
141                 scopeid = 0;
142                 break;
143         case AF_INET6:
144                 a = (void *)&((struct sockaddr_in6 *)sa)->sin6_addr;
145                 if (sl != sizeof(struct sockaddr_in6)) return EAI_FAMILY;
146                 if (memcmp(a, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12))
147                         mkptr6(ptr, a);
148                 else
149                         mkptr4(ptr, a+12);
150                 scopeid = ((struct sockaddr_in6 *)sa)->sin6_scope_id;
151                 break;
152         default:
153                 return EAI_FAMILY;
154         }
155
156         if (node && nodelen) {
157                 buf[0] = 0;
158                 if (!(flags & NI_NUMERICHOST)) {
159                         reverse_hosts(buf, a, scopeid, af);
160                 }
161                 if (!*buf && !(flags & NI_NUMERICHOST)) {
162                         unsigned char query[18+PTR_MAX], reply[512];
163                         int qlen = __res_mkquery(0, ptr, 1, RR_PTR,
164                                 0, 0, 0, query, sizeof query);
165                         int rlen = __res_send(query, qlen, reply, sizeof reply);
166                         buf[0] = 0;
167                         if (rlen > 0)
168                                 __dns_parse(reply, rlen, dns_parse_callback, buf);
169                 }
170                 if (!*buf) {
171                         if (flags & NI_NAMEREQD) return EAI_NONAME;
172                         inet_ntop(af, a, buf, sizeof buf);
173                         if (scopeid) {
174                                 char *p = 0, tmp[IF_NAMESIZE+1];
175                                 if (!(flags & NI_NUMERICSCOPE) &&
176                                     (IN6_IS_ADDR_LINKLOCAL(a) ||
177                                      IN6_IS_ADDR_MC_LINKLOCAL(a)))
178                                         p = if_indextoname(scopeid, tmp+1);
179                                 if (!p)
180                                         p = itoa(num, scopeid);
181                                 *--p = '%';
182                                 strcat(buf, p);
183                         }
184                 }
185                 if (strlen(buf) >= nodelen) return EAI_OVERFLOW;
186                 strcpy(node, buf);
187         }
188
189         if (serv && servlen) {
190                 char *p = buf;
191                 int port = ntohs(((struct sockaddr_in *)sa)->sin_port);
192                 buf[0] = 0;
193                 if (!(flags & NI_NUMERICSERV))
194                         reverse_services(buf, port, flags & NI_DGRAM);
195                 if (!*p)
196                         p = itoa(num, port);
197                 if (strlen(p) >= servlen)
198                         return EAI_OVERFLOW;
199                 strcpy(serv, p);
200         }
201
202         return 0;
203 }