netdb.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Assertions.h>
  27. #include <AK/ScopeGuard.h>
  28. #include <AK/String.h>
  29. #include <Kernel/Net/IPv4.h>
  30. #include <arpa/inet.h>
  31. #include <netdb.h>
  32. #include <netinet/in.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <unistd.h>
  36. extern "C" {
  37. static hostent __gethostbyname_buffer;
  38. static char __gethostbyname_name_buffer[512];
  39. static in_addr_t __gethostbyname_address;
  40. static in_addr_t* __gethostbyname_address_list_buffer[2];
  41. static hostent __gethostbyaddr_buffer;
  42. static char __gethostbyaddr_name_buffer[512];
  43. static in_addr_t* __gethostbyaddr_address_list_buffer[2];
  44. static int connect_to_lookup_server()
  45. {
  46. int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
  47. if (fd < 0) {
  48. perror("socket");
  49. return -1;
  50. }
  51. sockaddr_un address;
  52. address.sun_family = AF_LOCAL;
  53. strcpy(address.sun_path, "/tmp/portal/lookup");
  54. if (connect(fd, (const sockaddr*)&address, sizeof(address)) < 0) {
  55. perror("connect_to_lookup_server");
  56. close(fd);
  57. return -1;
  58. }
  59. return fd;
  60. }
  61. hostent* gethostbyname(const char* name)
  62. {
  63. auto ipv4_address = IPv4Address::from_string(name);
  64. if (ipv4_address.has_value()) {
  65. sprintf(__gethostbyname_name_buffer, "%s", ipv4_address.value().to_string().characters());
  66. __gethostbyname_buffer.h_name = __gethostbyname_name_buffer;
  67. __gethostbyname_buffer.h_aliases = nullptr;
  68. __gethostbyname_buffer.h_addrtype = AF_INET;
  69. new (&__gethostbyname_address) IPv4Address(ipv4_address.value());
  70. __gethostbyname_address_list_buffer[0] = &__gethostbyname_address;
  71. __gethostbyname_address_list_buffer[1] = nullptr;
  72. __gethostbyname_buffer.h_addr_list = (char**)__gethostbyname_address_list_buffer;
  73. __gethostbyname_buffer.h_length = 4;
  74. return &__gethostbyname_buffer;
  75. }
  76. int fd = connect_to_lookup_server();
  77. if (fd < 0)
  78. return nullptr;
  79. auto close_fd_on_exit = ScopeGuard([fd] {
  80. close(fd);
  81. });
  82. auto line = String::format("L%s\n", name);
  83. int nsent = write(fd, line.characters(), line.length());
  84. if (nsent < 0) {
  85. perror("write");
  86. return nullptr;
  87. }
  88. ASSERT((size_t)nsent == line.length());
  89. char buffer[1024];
  90. int nrecv = read(fd, buffer, sizeof(buffer) - 1);
  91. if (nrecv < 0) {
  92. perror("recv");
  93. return nullptr;
  94. }
  95. if (!memcmp(buffer, "Not found.", sizeof("Not found.") - 1))
  96. return nullptr;
  97. auto responses = String(buffer, nrecv).split('\n');
  98. if (responses.is_empty())
  99. return nullptr;
  100. auto& response = responses[0];
  101. int rc = inet_pton(AF_INET, response.characters(), &__gethostbyname_address);
  102. if (rc <= 0)
  103. return nullptr;
  104. strncpy(__gethostbyname_name_buffer, name, strlen(name));
  105. __gethostbyname_buffer.h_name = __gethostbyname_name_buffer;
  106. __gethostbyname_buffer.h_aliases = nullptr;
  107. __gethostbyname_buffer.h_addrtype = AF_INET;
  108. __gethostbyname_address_list_buffer[0] = &__gethostbyname_address;
  109. __gethostbyname_address_list_buffer[1] = nullptr;
  110. __gethostbyname_buffer.h_addr_list = (char**)__gethostbyname_address_list_buffer;
  111. __gethostbyname_buffer.h_length = 4;
  112. return &__gethostbyname_buffer;
  113. }
  114. hostent* gethostbyaddr(const void* addr, socklen_t addr_size, int type)
  115. {
  116. if (type != AF_INET) {
  117. errno = EAFNOSUPPORT;
  118. return nullptr;
  119. }
  120. if (addr_size < sizeof(in_addr)) {
  121. errno = EINVAL;
  122. return nullptr;
  123. }
  124. int fd = connect_to_lookup_server();
  125. if (fd < 0)
  126. return nullptr;
  127. auto close_fd_on_exit = ScopeGuard([fd] {
  128. close(fd);
  129. });
  130. IPv4Address ipv4_address((const u8*)&((const in_addr*)addr)->s_addr);
  131. auto line = String::format("R%d.%d.%d.%d.in-addr.arpa\n",
  132. ipv4_address[3],
  133. ipv4_address[2],
  134. ipv4_address[1],
  135. ipv4_address[0]);
  136. int nsent = write(fd, line.characters(), line.length());
  137. if (nsent < 0) {
  138. perror("write");
  139. return nullptr;
  140. }
  141. ASSERT((size_t)nsent == line.length());
  142. char buffer[1024];
  143. int nrecv = read(fd, buffer, sizeof(buffer) - 1);
  144. if (nrecv < 0) {
  145. perror("recv");
  146. return nullptr;
  147. }
  148. if (!memcmp(buffer, "Not found.", sizeof("Not found.") - 1))
  149. return nullptr;
  150. auto responses = String(buffer, nrecv).split('\n');
  151. if (responses.is_empty())
  152. return nullptr;
  153. auto& response = responses[0];
  154. strncpy(__gethostbyaddr_name_buffer, response.characters(), max(sizeof(__gethostbyaddr_name_buffer), response.length()));
  155. __gethostbyaddr_buffer.h_name = __gethostbyaddr_name_buffer;
  156. __gethostbyaddr_buffer.h_aliases = nullptr;
  157. __gethostbyaddr_buffer.h_addrtype = AF_INET;
  158. // FIXME: Should we populate the hostent's address list here with a sockaddr_in for the provided host?
  159. __gethostbyaddr_address_list_buffer[0] = nullptr;
  160. __gethostbyaddr_buffer.h_addr_list = (char**)__gethostbyaddr_address_list_buffer;
  161. __gethostbyaddr_buffer.h_length = 4;
  162. return &__gethostbyaddr_buffer;
  163. }
  164. }