netdb.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include <AK/String.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/portal/lookup");
  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. close(fd);
  64. });
  65. auto line = String::format("L%s\n", name);
  66. int nsent = write(fd, line.characters(), line.length());
  67. if (nsent < 0) {
  68. perror("write");
  69. return nullptr;
  70. }
  71. ASSERT(nsent == line.length());
  72. char buffer[1024];
  73. int nrecv = read(fd, buffer, sizeof(buffer) - 1);
  74. if (nrecv < 0) {
  75. perror("recv");
  76. return nullptr;
  77. }
  78. buffer[nrecv] = '\0';
  79. if (!memcmp(buffer, "Not found.", sizeof("Not found.") - 1))
  80. return nullptr;
  81. int rc = inet_pton(AF_INET, buffer, &__gethostbyname_address);
  82. if (rc <= 0)
  83. return nullptr;
  84. strncpy(__gethostbyname_name_buffer, name, strlen(name));
  85. __gethostbyname_buffer.h_name = __gethostbyname_name_buffer;
  86. __gethostbyname_buffer.h_aliases = nullptr;
  87. __gethostbyname_buffer.h_addrtype = AF_INET;
  88. __gethostbyname_address_list_buffer[0] = &__gethostbyname_address;
  89. __gethostbyname_address_list_buffer[1] = nullptr;
  90. __gethostbyname_buffer.h_addr_list = (char**)__gethostbyname_address_list_buffer;
  91. __gethostbyname_buffer.h_length = 4;
  92. return &__gethostbyname_buffer;
  93. }
  94. hostent* gethostbyaddr(const void* addr, socklen_t addr_size, int type)
  95. {
  96. if (type != AF_INET) {
  97. errno = EAFNOSUPPORT;
  98. return nullptr;
  99. }
  100. if (addr_size < sizeof(in_addr)) {
  101. errno = EINVAL;
  102. return nullptr;
  103. }
  104. int fd = connect_to_lookup_server();
  105. if (fd < 0)
  106. return nullptr;
  107. auto close_fd_on_exit = ScopeGuard([fd] {
  108. close(fd);
  109. });
  110. IPv4Address ipv4_address((const u8*)&((const in_addr*)addr)->s_addr);
  111. auto line = String::format("R%d.%d.%d.%d.in-addr.arpa\n",
  112. ipv4_address[3],
  113. ipv4_address[2],
  114. ipv4_address[1],
  115. ipv4_address[0]);
  116. int nsent = write(fd, line.characters(), line.length());
  117. if (nsent < 0) {
  118. perror("write");
  119. return nullptr;
  120. }
  121. ASSERT(nsent == line.length());
  122. char buffer[1024];
  123. int nrecv = read(fd, buffer, sizeof(buffer) - 1);
  124. if (nrecv < 0) {
  125. perror("recv");
  126. return nullptr;
  127. }
  128. if (nrecv > 1) {
  129. // Strip newline.
  130. buffer[nrecv - 1] = '\0';
  131. }
  132. buffer[nrecv] = '\0';
  133. if (!memcmp(buffer, "Not found.", sizeof("Not found.") - 1))
  134. return nullptr;
  135. strncpy(__gethostbyaddr_name_buffer, buffer, max(sizeof(__gethostbyaddr_name_buffer), (size_t)nrecv));
  136. __gethostbyaddr_buffer.h_name = __gethostbyaddr_name_buffer;
  137. __gethostbyaddr_buffer.h_aliases = nullptr;
  138. __gethostbyaddr_buffer.h_addrtype = AF_INET;
  139. // FIXME: Should we populate the hostent's address list here with a sockaddr_in for the provided host?
  140. __gethostbyaddr_address_list_buffer[0] = nullptr;
  141. __gethostbyaddr_buffer.h_addr_list = (char**)__gethostbyaddr_address_list_buffer;
  142. __gethostbyaddr_buffer.h_length = 4;
  143. return &__gethostbyaddr_buffer;
  144. }
  145. }