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