6 #include <sys/socket.h>
7 #include <netinet/in.h>
13 #include "stdio_impl.h"
15 #define PTR_MAX (64 + sizeof ".in-addr.arpa")
18 static char *itoa(char *p
, unsigned x
) {
28 static void mkptr4(char *s
, const unsigned char *ip
)
30 sprintf(s
, "%d.%d.%d.%d.in-addr.arpa",
31 ip
[3], ip
[2], ip
[1], ip
[0]);
34 static void mkptr6(char *s
, const unsigned char *ip
)
36 static const char xdigits
[] = "0123456789abcdef";
38 for (i
=15; i
>=0; i
--) {
39 *s
++ = xdigits
[ip
[i
]&15]; *s
++ = '.';
40 *s
++ = xdigits
[ip
[i
]>>4]; *s
++ = '.';
42 strcpy(s
, "ip6.arpa");
45 static void reverse_hosts(char *buf
, const unsigned char *a
, unsigned scopeid
, int family
)
47 char line
[512], *p
, *z
;
48 unsigned char _buf
[1032], atmp
[16];
50 FILE _f
, *f
= __fopen_rb_ca("/etc/hosts", &_f
, _buf
, sizeof _buf
);
52 if (family
== AF_INET
) {
53 memcpy(atmp
+12, a
, 4);
54 memcpy(atmp
, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
57 while (fgets(line
, sizeof line
, f
)) {
58 if ((p
=strchr(line
, '#'))) *p
++='\n', *p
=0;
60 for (p
=line
; *p
&& !isspace(*p
); p
++);
63 if (__lookup_ipliteral(&iplit
, line
, AF_UNSPEC
)<=0)
66 if (iplit
.family
== AF_INET
) {
67 memcpy(iplit
.addr
+12, iplit
.addr
, 4);
68 memcpy(iplit
.addr
, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
72 if (memcmp(a
, iplit
.addr
, 16) || iplit
.scopeid
!= scopeid
)
75 for (; *p
&& isspace(*p
); p
++);
76 for (z
=p
; *z
&& !isspace(*z
); z
++);
79 memcpy(buf
, p
, z
-p
+1);
86 static void reverse_services(char *buf
, int port
, int dgram
)
89 char line
[128], *p
, *z
;
90 unsigned char _buf
[1032];
91 FILE _f
, *f
= __fopen_rb_ca("/etc/services", &_f
, _buf
, sizeof _buf
);
93 while (fgets(line
, sizeof line
, f
)) {
94 if ((p
=strchr(line
, '#'))) *p
++='\n', *p
=0;
96 for (p
=line
; *p
&& !isspace(*p
); p
++);
99 svport
= strtoul(p
, &z
, 10);
101 if (svport
!= port
|| z
==p
) continue;
102 if (dgram
&& strncmp(z
, "/udp", 4)) continue;
103 if (!dgram
&& strncmp(z
, "/tcp", 4)) continue;
104 if (p
-line
> 32) continue;
106 memcpy(buf
, line
, p
-line
);
112 static int dns_parse_callback(void *c
, int rr
, const void *data
, int len
, const void *packet
, int plen
)
114 if (rr
!= RR_PTR
) return 0;
115 if (__dn_expand(packet
, (const unsigned char *)packet
+ plen
,
122 int getnameinfo(const struct sockaddr
*restrict sa
, socklen_t sl
,
123 char *restrict node
, socklen_t nodelen
,
124 char *restrict serv
, socklen_t servlen
,
128 char buf
[256], num
[3*sizeof(int)+1];
129 int af
= sa
->sa_family
;
135 a
= (void *)&((struct sockaddr_in
*)sa
)->sin_addr
;
136 if (sl
< sizeof(struct sockaddr_in
)) return EAI_FAMILY
;
141 a
= (void *)&((struct sockaddr_in6
*)sa
)->sin6_addr
;
142 if (sl
< sizeof(struct sockaddr_in6
)) return EAI_FAMILY
;
143 if (memcmp(a
, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12))
147 scopeid
= ((struct sockaddr_in6
*)sa
)->sin6_scope_id
;
153 if (node
&& nodelen
) {
155 if (!(flags
& NI_NUMERICHOST
)) {
156 reverse_hosts(buf
, a
, scopeid
, af
);
158 if (!*buf
&& !(flags
& NI_NUMERICHOST
)) {
159 unsigned char query
[18+PTR_MAX
], reply
[512];
160 int qlen
= __res_mkquery(0, ptr
, 1, RR_PTR
,
161 0, 0, 0, query
, sizeof query
);
162 query
[3] = 0; /* don't need AD flag */
163 int rlen
= __res_send(query
, qlen
, reply
, sizeof reply
);
166 if (rlen
> sizeof reply
) rlen
= sizeof reply
;
167 __dns_parse(reply
, rlen
, dns_parse_callback
, buf
);
171 if (flags
& NI_NAMEREQD
) return EAI_NONAME
;
172 inet_ntop(af
, a
, buf
, sizeof buf
);
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);
180 p
= itoa(num
, scopeid
);
185 if (strlen(buf
) >= nodelen
) return EAI_OVERFLOW
;
189 if (serv
&& servlen
) {
191 int port
= ntohs(((struct sockaddr_in
*)sa
)->sin_port
);
193 if (!(flags
& NI_NUMERICSERV
))
194 reverse_services(buf
, port
, flags
& NI_DGRAM
);
197 if (strlen(p
) >= servlen
)