netdb.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include <AK/AKString.h>
  2. #include <AK/Assertions.h>
  3. #include <AK/ScopeGuard.h>
  4. #include <Kernel/Net/IPv4.h>
  5. #include <arpa/inet.h>
  6. #include <netdb.h>
  7. #include <netinet/in.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. extern "C" {
  12. static hostent __gethostbyname_buffer;
  13. static char __gethostbyname_name_buffer[512];
  14. static in_addr_t __gethostbyname_address;
  15. static in_addr_t* __gethostbyname_address_list_buffer[2];
  16. static hostent __gethostbyaddr_buffer;
  17. static char __gethostbyaddr_name_buffer[512];
  18. static in_addr_t* __gethostbyaddr_address_list_buffer[2];
  19. static int connect_to_lookup_server()
  20. {
  21. int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
  22. if (fd < 0) {
  23. perror("socket");
  24. return -1;
  25. }
  26. sockaddr_un address;
  27. address.sun_family = AF_LOCAL;
  28. strcpy(address.sun_path, "/tmp/.LookupServer-socket");
  29. int retries = 3;
  30. int rc = 0;
  31. while (retries) {
  32. rc = connect(fd, (const sockaddr*)&address, sizeof(address));
  33. if (rc == 0)
  34. break;
  35. --retries;
  36. sleep(1);
  37. }
  38. if (rc < 0) {
  39. close(fd);
  40. return -1;
  41. }
  42. return fd;
  43. }
  44. hostent* gethostbyname(const char* name)
  45. {
  46. auto ipv4_address = IPv4Address::from_string(name);
  47. if (ipv4_address.has_value()) {
  48. sprintf(__gethostbyname_name_buffer, "%s", ipv4_address.value().to_string().characters());
  49. __gethostbyname_buffer.h_name = __gethostbyname_name_buffer;
  50. __gethostbyname_buffer.h_aliases = nullptr;
  51. __gethostbyname_buffer.h_addrtype = AF_INET;
  52. new (&__gethostbyname_address) IPv4Address(ipv4_address.value());
  53. __gethostbyname_address_list_buffer[0] = &__gethostbyname_address;
  54. __gethostbyname_address_list_buffer[1] = nullptr;
  55. __gethostbyname_buffer.h_addr_list = (char**)__gethostbyname_address_list_buffer;
  56. __gethostbyname_buffer.h_length = 4;
  57. return &__gethostbyname_buffer;
  58. }
  59. int fd = connect_to_lookup_server();
  60. if (fd < 0)
  61. return nullptr;
  62. auto close_fd_on_exit = ScopeGuard([fd] {
  63. dbgprintf("closing fd\n");
  64. close(fd);
  65. });
  66. auto line = String::format("L%s\n", name);
  67. int nsent = write(fd, line.characters(), line.length());
  68. if (nsent < 0) {
  69. perror("write");
  70. return nullptr;
  71. }
  72. ASSERT(nsent == line.length());
  73. char buffer[1024];
  74. int nrecv = read(fd, buffer, sizeof(buffer) - 1);
  75. if (nrecv < 0) {
  76. perror("recv");
  77. return nullptr;
  78. }
  79. buffer[nrecv] = '\0';
  80. if (!memcmp(buffer, "Not found.", sizeof("Not found.") - 1))
  81. return nullptr;
  82. int rc = inet_pton(AF_INET, buffer, &__gethostbyname_address);
  83. if (rc <= 0)
  84. return nullptr;
  85. strncpy(__gethostbyname_name_buffer, name, strlen(name));
  86. __gethostbyname_buffer.h_name = __gethostbyname_name_buffer;
  87. __gethostbyname_buffer.h_aliases = nullptr;
  88. __gethostbyname_buffer.h_addrtype = AF_INET;
  89. __gethostbyname_address_list_buffer[0] = &__gethostbyname_address;
  90. __gethostbyname_address_list_buffer[1] = nullptr;
  91. __gethostbyname_buffer.h_addr_list = (char**)__gethostbyname_address_list_buffer;
  92. __gethostbyname_buffer.h_length = 4;
  93. return &__gethostbyname_buffer;
  94. }
  95. hostent* gethostbyaddr(const void* addr, socklen_t addr_size, int type)
  96. {
  97. if (type != AF_INET) {
  98. errno = EAFNOSUPPORT;
  99. return nullptr;
  100. }
  101. if (addr_size < sizeof(in_addr)) {
  102. errno = EINVAL;
  103. return nullptr;
  104. }
  105. int fd = connect_to_lookup_server();
  106. if (fd < 0)
  107. return nullptr;
  108. auto close_fd_on_exit = ScopeGuard([fd] {
  109. close(fd);
  110. });
  111. IPv4Address ipv4_address((const u8*)&((const in_addr*)addr)->s_addr);
  112. auto line = String::format("R%d.%d.%d.%d.in-addr.arpa\n",
  113. ipv4_address[3],
  114. ipv4_address[2],
  115. ipv4_address[1],
  116. ipv4_address[0]);
  117. int nsent = write(fd, line.characters(), line.length());
  118. if (nsent < 0) {
  119. perror("write");
  120. return nullptr;
  121. }
  122. ASSERT(nsent == line.length());
  123. char buffer[1024];
  124. int nrecv = read(fd, buffer, sizeof(buffer) - 1);
  125. if (nrecv < 0) {
  126. perror("recv");
  127. return nullptr;
  128. }
  129. if (nrecv > 1) {
  130. // Strip newline.
  131. buffer[nrecv - 1] = '\0';
  132. }
  133. buffer[nrecv] = '\0';
  134. if (!memcmp(buffer, "Not found.", sizeof("Not found.") - 1))
  135. return nullptr;
  136. strncpy(__gethostbyaddr_name_buffer, buffer, max(sizeof(__gethostbyaddr_name_buffer), (size_t)nrecv));
  137. __gethostbyaddr_buffer.h_name = __gethostbyaddr_name_buffer;
  138. __gethostbyaddr_buffer.h_aliases = nullptr;
  139. __gethostbyaddr_buffer.h_addrtype = AF_INET;
  140. // FIXME: Should we populate the hostent's address list here with a sockaddr_in for the provided host?
  141. __gethostbyaddr_address_list_buffer[0] = nullptr;
  142. __gethostbyaddr_buffer.h_addr_list = (char**)__gethostbyaddr_address_list_buffer;
  143. __gethostbyaddr_buffer.h_length = 4;
  144. return &__gethostbyaddr_buffer;
  145. }
  146. }