getaddrinfo.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <netdb.h>
  4. #include <netinet/in.h>
  5. #include <sys/socket.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include "__dns.h"
  10. #include "stdio_impl.h"
  11. static int is_valid(const char *host)
  12. {
  13. const unsigned char *s;
  14. if (strlen(host)-1 > 254 || mbstowcs(0, host, 0) > 255) return 0;
  15. for (s=(void *)host; *s>=0x80 || *s=='.' || *s=='-' || isalnum(*s); s++);
  16. return !*s;
  17. }
  18. #if 0
  19. static int have_af(int family)
  20. {
  21. struct sockaddr_in6 sin6 = { .sin6_family = family };
  22. socklen_t sl = family == AF_INET
  23. ? sizeof(struct sockaddr_in)
  24. : sizeof(struct sockaddr_in6);
  25. int sock = socket(family, SOCK_STREAM, 0);
  26. int have = !bind(sock, (void *)&sin6, sl);
  27. close(sock);
  28. return have;
  29. }
  30. #endif
  31. #include <stdlib.h>
  32. #include <netdb.h>
  33. union sa {
  34. struct sockaddr_in sin;
  35. struct sockaddr_in6 sin6;
  36. };
  37. struct aibuf {
  38. struct addrinfo ai;
  39. union sa sa;
  40. };
  41. /* Extra slots needed for storing canonical name */
  42. #define EXTRA ((256+sizeof(struct aibuf)-1)/sizeof(struct aibuf))
  43. int getaddrinfo(const char *host, const char *serv, const struct addrinfo *hint, struct addrinfo **res)
  44. {
  45. int flags = hint ? hint->ai_flags : 0;
  46. int family = hint ? hint->ai_family : AF_UNSPEC;
  47. int type = hint ? hint->ai_socktype : 0;
  48. int proto = hint ? hint->ai_protocol : 0;
  49. unsigned long port = 0;
  50. struct aibuf *buf;
  51. union sa sa = {{0}};
  52. unsigned char reply[1024];
  53. int i, j;
  54. //char hostbuf[256];
  55. char line[512];
  56. FILE *f, _f;
  57. unsigned char _buf[1024];
  58. char *z;
  59. int result;
  60. int cnt;
  61. if (host && strlen(host)>255) return EAI_NONAME;
  62. if (serv && strlen(serv)>32) return EAI_SERVICE;
  63. if (type && !proto)
  64. proto = type==SOCK_DGRAM ? IPPROTO_UDP : IPPROTO_TCP;
  65. if (!type && proto)
  66. type = proto==IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
  67. if (serv) {
  68. if (!*serv) return EAI_SERVICE;
  69. port = strtoul(serv, &z, 10);
  70. if (!*z && port > 65535) return EAI_SERVICE;
  71. if (!port) {
  72. if (flags & AI_NUMERICSERV) return EAI_SERVICE;
  73. //f = fopen("/etc/services", "rb");
  74. return EAI_SERVICE;
  75. }
  76. port = htons(port);
  77. }
  78. if (!host) {
  79. if (family == AF_UNSPEC) family = AF_INET;
  80. buf = calloc(sizeof *buf, 1+EXTRA);
  81. if (!buf) return EAI_MEMORY;
  82. buf->ai.ai_protocol = proto;
  83. buf->ai.ai_socktype = type;
  84. buf->ai.ai_addr = (void *)&buf->sa;
  85. buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
  86. buf->ai.ai_family = family;
  87. buf->sa.sin.sin_family = family;
  88. buf->sa.sin.sin_port = port;
  89. if (!(flags & AI_PASSIVE)) {
  90. if (family == AF_INET) {
  91. 0[(uint8_t*)&buf->sa.sin.sin_addr.s_addr]=127;
  92. 3[(uint8_t*)&buf->sa.sin.sin_addr.s_addr]=1;
  93. } else buf[0].sa.sin6.sin6_addr.s6_addr[15] = 1;
  94. }
  95. *res = &buf->ai;
  96. return 0;
  97. }
  98. if (!*host) return EAI_NONAME;
  99. /* Try as a numeric address */
  100. if (__ipparse(&sa, family, host) >= 0) {
  101. buf = calloc(sizeof *buf, 1+EXTRA);
  102. if (!buf) return EAI_MEMORY;
  103. family = sa.sin.sin_family;
  104. buf->ai.ai_protocol = proto;
  105. buf->ai.ai_socktype = type;
  106. buf->ai.ai_addr = (void *)&buf->sa;
  107. buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
  108. buf->ai.ai_family = family;
  109. buf->ai.ai_canonname = (char *)host;
  110. buf->sa = sa;
  111. buf->sa.sin.sin_port = port;
  112. *res = &buf->ai;
  113. return 0;
  114. }
  115. if (flags & AI_NUMERICHOST) return EAI_NONAME;
  116. f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
  117. if (f) while (fgets(line, sizeof line, f)) {
  118. char *p;
  119. size_t l = strlen(host);
  120. if ((p=strchr(line, '#'))) *p++='\n', *p=0;
  121. for(p=line+1; (p=strstr(p, host)) &&
  122. (!isspace(p[-1]) || !isspace(p[l])); p++);
  123. if (!p) continue;
  124. __fclose_ca(f);
  125. /* Isolate IP address to parse */
  126. for (p=line; *p && !isspace(*p); p++);
  127. *p++ = 0;
  128. if (__ipparse(&sa, family, line) < 0) return EAI_NONAME;
  129. /* Allocate and fill result buffer */
  130. buf = calloc(sizeof *buf, 1+EXTRA);
  131. if (!buf) return EAI_MEMORY;
  132. family = sa.sin.sin_family;
  133. buf->ai.ai_protocol = proto;
  134. buf->ai.ai_socktype = type;
  135. buf->ai.ai_addr = (void *)&buf->sa;
  136. buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
  137. buf->ai.ai_family = family;
  138. buf->sa = sa;
  139. buf->sa.sin.sin_port = port;
  140. /* Extract first name as canonical name */
  141. for (; *p && isspace(*p); p++);
  142. buf->ai.ai_canonname = (void *)(buf+1);
  143. snprintf(buf->ai.ai_canonname, 256, "%s", p);
  144. for (p=buf->ai.ai_canonname; *p && !isspace(*p); p++);
  145. *p = 0;
  146. if (!is_valid(buf->ai.ai_canonname))
  147. buf->ai.ai_canonname = 0;
  148. *res = &buf->ai;
  149. return 0;
  150. }
  151. if (f) __fclose_ca(f);
  152. #if 0
  153. f = __fopen_rb_ca("/etc/resolv.conf", &_f, _buf, sizeof _buf);
  154. if (f) while (fgets(line, sizeof line, f)) {
  155. if (!isspace(line[10]) || (strncmp(line, "search", 6)
  156. && strncmp(line, "domain", 6))) continue;
  157. }
  158. if (f) __fclose_ca(f);
  159. #endif
  160. /* Perform one or more DNS queries for host */
  161. memset(reply, 0, sizeof reply);
  162. result = __dns_query(reply, host, family, 0);
  163. if (result < 0) return result;
  164. cnt = __dns_count_addrs(reply, result);
  165. if (cnt <= 0) return EAI_NONAME;
  166. buf = calloc(sizeof *buf, cnt+EXTRA);
  167. if (!buf) return EAI_MEMORY;
  168. i = 0;
  169. if (family != AF_INET6) {
  170. j = __dns_get_rr(&buf[i].sa.sin.sin_addr, sizeof *buf, 4, cnt-i, reply, RR_A, 0);
  171. while (j--) buf[i++].sa.sin.sin_family = AF_INET;
  172. }
  173. if (family != AF_INET) {
  174. j = __dns_get_rr(&buf[i].sa.sin6.sin6_addr, sizeof *buf, 16, cnt-i, reply, RR_AAAA, 0);
  175. while (j--) buf[i++].sa.sin.sin_family = AF_INET6;
  176. }
  177. if (result>1) {
  178. j = __dns_get_rr(&buf[i].sa.sin.sin_addr, sizeof *buf, 4, cnt-i, reply+512, RR_A, 0);
  179. while (j--) buf[i++].sa.sin.sin_family = AF_INET;
  180. j = __dns_get_rr(&buf[i].sa.sin6.sin6_addr, sizeof *buf, 16, cnt-i, reply+512, RR_AAAA, 0);
  181. while (j--) buf[i++].sa.sin.sin_family = AF_INET6;
  182. }
  183. if (__dns_get_rr((void *)&buf[cnt], 0, 256, 1, reply, RR_CNAME, 1) < 0)
  184. strcpy((void *)&buf[cnt], host);
  185. for (i=0; i<cnt; i++) {
  186. buf[i].ai.ai_protocol = proto;
  187. buf[i].ai.ai_socktype = type;
  188. buf[i].ai.ai_addr = (void *)&buf[i].sa;
  189. buf[i].ai.ai_addrlen = buf[i].sa.sin.sin_family==AF_INET6
  190. ? sizeof sa.sin6 : sizeof sa.sin;
  191. buf[i].ai.ai_family = buf[i].sa.sin.sin_family;
  192. buf[i].sa.sin.sin_port = port;
  193. buf[i].ai.ai_next = &buf[i+1].ai;
  194. buf[i].ai.ai_canonname = (void *)&buf[cnt];
  195. }
  196. buf[cnt-1].ai.ai_next = 0;
  197. *res = &buf->ai;
  198. return 0;
  199. }