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