1
0

getnameinfo.c 4.7 KB

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