getnameinfo.c 5.0 KB

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