getaddrinfo.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 *restrict host, const char *restrict serv, const struct addrinfo *restrict hint, struct addrinfo **restrict 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 line[512];
  55. FILE *f, _f;
  56. unsigned char _buf[1024];
  57. char *z;
  58. int result;
  59. int cnt;
  60. if (host && strlen(host)>255) return EAI_NONAME;
  61. if (serv && strlen(serv)>32) return EAI_SERVICE;
  62. if (type && !proto)
  63. proto = type==SOCK_DGRAM ? IPPROTO_UDP : IPPROTO_TCP;
  64. if (!type && proto)
  65. type = proto==IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
  66. if (serv) {
  67. if (!*serv) return EAI_SERVICE;
  68. port = strtoul(serv, &z, 10);
  69. if (*z) {
  70. size_t servlen = strlen(serv);
  71. char *end = line;
  72. if (flags & AI_NUMERICSERV) return EAI_SERVICE;
  73. f = __fopen_rb_ca("/etc/services", &_f, _buf, sizeof _buf);
  74. if (!f) return EAI_SERVICE;
  75. while (fgets(line, sizeof line, f)) {
  76. if (strncmp(line, serv, servlen) || !isspace(line[servlen]))
  77. continue;
  78. port = strtoul(line+servlen, &end, 10);
  79. if (strncmp(end, proto==IPPROTO_UDP ? "/udp" : "/tcp", 4))
  80. continue;
  81. break;
  82. }
  83. __fclose_ca(f);
  84. if (feof(f)) return EAI_SERVICE;
  85. }
  86. if (port > 65535) return EAI_SERVICE;
  87. port = htons(port);
  88. }
  89. if (!host) {
  90. if (family == AF_UNSPEC) family = AF_INET;
  91. buf = calloc(sizeof *buf, 1+EXTRA);
  92. if (!buf) return EAI_MEMORY;
  93. buf->ai.ai_protocol = proto;
  94. buf->ai.ai_socktype = type;
  95. buf->ai.ai_addr = (void *)&buf->sa;
  96. buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
  97. buf->ai.ai_family = family;
  98. buf->sa.sin.sin_family = family;
  99. buf->sa.sin.sin_port = port;
  100. if (!(flags & AI_PASSIVE)) {
  101. if (family == AF_INET) {
  102. 0[(uint8_t*)&buf->sa.sin.sin_addr.s_addr]=127;
  103. 3[(uint8_t*)&buf->sa.sin.sin_addr.s_addr]=1;
  104. } else buf[0].sa.sin6.sin6_addr.s6_addr[15] = 1;
  105. }
  106. *res = &buf->ai;
  107. return 0;
  108. }
  109. if (!*host) return EAI_NONAME;
  110. /* Try as a numeric address */
  111. if (__ipparse(&sa, family, host) >= 0) {
  112. buf = calloc(sizeof *buf, 1+EXTRA);
  113. if (!buf) return EAI_MEMORY;
  114. family = sa.sin.sin_family;
  115. buf->ai.ai_protocol = proto;
  116. buf->ai.ai_socktype = type;
  117. buf->ai.ai_addr = (void *)&buf->sa;
  118. buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
  119. buf->ai.ai_family = family;
  120. buf->ai.ai_canonname = (char *)host;
  121. buf->sa = sa;
  122. buf->sa.sin.sin_port = port;
  123. *res = &buf->ai;
  124. return 0;
  125. }
  126. if (flags & AI_NUMERICHOST) return EAI_NONAME;
  127. f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
  128. if (f) while (fgets(line, sizeof line, f)) {
  129. char *p;
  130. size_t l = strlen(host);
  131. if ((p=strchr(line, '#'))) *p++='\n', *p=0;
  132. for(p=line+1; (p=strstr(p, host)) &&
  133. (!isspace(p[-1]) || !isspace(p[l])); p++);
  134. if (!p) continue;
  135. __fclose_ca(f);
  136. /* Isolate IP address to parse */
  137. for (p=line; *p && !isspace(*p); p++);
  138. *p++ = 0;
  139. if (__ipparse(&sa, family, line) < 0) return EAI_NONAME;
  140. /* Allocate and fill result buffer */
  141. buf = calloc(sizeof *buf, 1+EXTRA);
  142. if (!buf) return EAI_MEMORY;
  143. family = sa.sin.sin_family;
  144. buf->ai.ai_protocol = proto;
  145. buf->ai.ai_socktype = type;
  146. buf->ai.ai_addr = (void *)&buf->sa;
  147. buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin;
  148. buf->ai.ai_family = family;
  149. buf->sa = sa;
  150. buf->sa.sin.sin_port = port;
  151. /* Extract first name as canonical name */
  152. for (; *p && isspace(*p); p++);
  153. buf->ai.ai_canonname = (void *)(buf+1);
  154. snprintf(buf->ai.ai_canonname, 256, "%s", p);
  155. for (p=buf->ai.ai_canonname; *p && !isspace(*p); p++);
  156. *p = 0;
  157. if (!is_valid(buf->ai.ai_canonname))
  158. buf->ai.ai_canonname = 0;
  159. *res = &buf->ai;
  160. return 0;
  161. }
  162. if (f) __fclose_ca(f);
  163. #if 0
  164. f = __fopen_rb_ca("/etc/resolv.conf", &_f, _buf, sizeof _buf);
  165. if (f) while (fgets(line, sizeof line, f)) {
  166. if (!isspace(line[10]) || (strncmp(line, "search", 6)
  167. && strncmp(line, "domain", 6))) continue;
  168. }
  169. if (f) __fclose_ca(f);
  170. #endif
  171. /* Perform one or more DNS queries for host */
  172. memset(reply, 0, sizeof reply);
  173. result = __dns_query(reply, host, family, 0);
  174. if (result < 0) return result;
  175. cnt = __dns_count_addrs(reply, result);
  176. if (cnt <= 0) return EAI_NONAME;
  177. buf = calloc(sizeof *buf, cnt+EXTRA);
  178. if (!buf) return EAI_MEMORY;
  179. i = 0;
  180. if (family != AF_INET6) {
  181. j = __dns_get_rr(&buf[i].sa.sin.sin_addr, sizeof *buf, 4, cnt-i, reply, RR_A, 0);
  182. while (j--) buf[i++].sa.sin.sin_family = AF_INET;
  183. }
  184. if (family != AF_INET) {
  185. j = __dns_get_rr(&buf[i].sa.sin6.sin6_addr, sizeof *buf, 16, cnt-i, reply, RR_AAAA, 0);
  186. while (j--) buf[i++].sa.sin.sin_family = AF_INET6;
  187. }
  188. if (result>1) {
  189. j = __dns_get_rr(&buf[i].sa.sin.sin_addr, sizeof *buf, 4, cnt-i, reply+512, RR_A, 0);
  190. while (j--) buf[i++].sa.sin.sin_family = AF_INET;
  191. j = __dns_get_rr(&buf[i].sa.sin6.sin6_addr, sizeof *buf, 16, cnt-i, reply+512, RR_AAAA, 0);
  192. while (j--) buf[i++].sa.sin.sin_family = AF_INET6;
  193. }
  194. if (__dns_get_rr((void *)&buf[cnt], 0, 256, 1, reply, RR_CNAME, 1) <= 0)
  195. strcpy((void *)&buf[cnt], host);
  196. for (i=0; i<cnt; i++) {
  197. buf[i].ai.ai_protocol = proto;
  198. buf[i].ai.ai_socktype = type;
  199. buf[i].ai.ai_addr = (void *)&buf[i].sa;
  200. buf[i].ai.ai_addrlen = buf[i].sa.sin.sin_family==AF_INET6
  201. ? sizeof sa.sin6 : sizeof sa.sin;
  202. buf[i].ai.ai_family = buf[i].sa.sin.sin_family;
  203. buf[i].sa.sin.sin_port = port;
  204. buf[i].ai.ai_next = &buf[i+1].ai;
  205. buf[i].ai.ai_canonname = (void *)&buf[cnt];
  206. }
  207. buf[cnt-1].ai.ai_next = 0;
  208. *res = &buf->ai;
  209. return 0;
  210. }