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