netdb.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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/ByteBuffer.h>
  28. #include <AK/ScopeGuard.h>
  29. #include <AK/String.h>
  30. #include <Kernel/Net/IPv4.h>
  31. #include <arpa/inet.h>
  32. #include <netdb.h>
  33. #include <netinet/in.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. extern "C" {
  38. int h_errno;
  39. static hostent __gethostbyname_buffer;
  40. static char __gethostbyname_name_buffer[512];
  41. static in_addr_t __gethostbyname_address;
  42. static in_addr_t* __gethostbyname_address_list_buffer[2];
  43. static hostent __gethostbyaddr_buffer;
  44. static char __gethostbyaddr_name_buffer[512];
  45. static in_addr_t* __gethostbyaddr_address_list_buffer[2];
  46. static FILE* services_file = nullptr;
  47. static const char* services_path = "/etc/services";
  48. static bool fill_getserv_buffers(char* line, ssize_t read);
  49. static servent __getserv_buffer;
  50. static char __getserv_name_buffer[512];
  51. static char __getserv_protocol_buffer[10];
  52. static int __getserv_port_buffer;
  53. static Vector<ByteBuffer> __getserv_alias_list_buffer;
  54. static Vector<char*> __getserv_alias_list;
  55. static bool keep_service_file_open = false;
  56. static ssize_t service_file_offset = 0;
  57. static int connect_to_lookup_server()
  58. {
  59. int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
  60. if (fd < 0) {
  61. perror("socket");
  62. return -1;
  63. }
  64. sockaddr_un address;
  65. address.sun_family = AF_LOCAL;
  66. strcpy(address.sun_path, "/tmp/portal/lookup");
  67. if (connect(fd, (const sockaddr*)&address, sizeof(address)) < 0) {
  68. perror("connect_to_lookup_server");
  69. close(fd);
  70. return -1;
  71. }
  72. return fd;
  73. }
  74. hostent* gethostbyname(const char* name)
  75. {
  76. auto ipv4_address = IPv4Address::from_string(name);
  77. if (ipv4_address.has_value()) {
  78. sprintf(__gethostbyname_name_buffer, "%s", ipv4_address.value().to_string().characters());
  79. __gethostbyname_buffer.h_name = __gethostbyname_name_buffer;
  80. __gethostbyname_buffer.h_aliases = nullptr;
  81. __gethostbyname_buffer.h_addrtype = AF_INET;
  82. new (&__gethostbyname_address) IPv4Address(ipv4_address.value());
  83. __gethostbyname_address_list_buffer[0] = &__gethostbyname_address;
  84. __gethostbyname_address_list_buffer[1] = nullptr;
  85. __gethostbyname_buffer.h_addr_list = (char**)__gethostbyname_address_list_buffer;
  86. __gethostbyname_buffer.h_length = 4;
  87. return &__gethostbyname_buffer;
  88. }
  89. int fd = connect_to_lookup_server();
  90. if (fd < 0)
  91. return nullptr;
  92. auto close_fd_on_exit = ScopeGuard([fd] {
  93. close(fd);
  94. });
  95. auto line = String::format("L%s\n", name);
  96. int nsent = write(fd, line.characters(), line.length());
  97. if (nsent < 0) {
  98. perror("write");
  99. return nullptr;
  100. }
  101. ASSERT((size_t)nsent == line.length());
  102. char buffer[1024];
  103. int nrecv = read(fd, buffer, sizeof(buffer) - 1);
  104. if (nrecv < 0) {
  105. perror("recv");
  106. return nullptr;
  107. }
  108. if (!memcmp(buffer, "Not found.", sizeof("Not found.") - 1))
  109. return nullptr;
  110. auto responses = String(buffer, nrecv).split('\n');
  111. if (responses.is_empty())
  112. return nullptr;
  113. auto& response = responses[0];
  114. int rc = inet_pton(AF_INET, response.characters(), &__gethostbyname_address);
  115. if (rc <= 0)
  116. return nullptr;
  117. strncpy(__gethostbyname_name_buffer, name, sizeof(__gethostbyaddr_name_buffer) - 1);
  118. __gethostbyname_buffer.h_name = __gethostbyname_name_buffer;
  119. __gethostbyname_buffer.h_aliases = nullptr;
  120. __gethostbyname_buffer.h_addrtype = AF_INET;
  121. __gethostbyname_address_list_buffer[0] = &__gethostbyname_address;
  122. __gethostbyname_address_list_buffer[1] = nullptr;
  123. __gethostbyname_buffer.h_addr_list = (char**)__gethostbyname_address_list_buffer;
  124. __gethostbyname_buffer.h_length = 4;
  125. return &__gethostbyname_buffer;
  126. }
  127. hostent* gethostbyaddr(const void* addr, socklen_t addr_size, int type)
  128. {
  129. if (type != AF_INET) {
  130. errno = EAFNOSUPPORT;
  131. return nullptr;
  132. }
  133. if (addr_size < sizeof(in_addr)) {
  134. errno = EINVAL;
  135. return nullptr;
  136. }
  137. int fd = connect_to_lookup_server();
  138. if (fd < 0)
  139. return nullptr;
  140. auto close_fd_on_exit = ScopeGuard([fd] {
  141. close(fd);
  142. });
  143. IPv4Address ipv4_address((const u8*)&((const in_addr*)addr)->s_addr);
  144. auto line = String::format("R%d.%d.%d.%d.in-addr.arpa\n",
  145. ipv4_address[3],
  146. ipv4_address[2],
  147. ipv4_address[1],
  148. ipv4_address[0]);
  149. int nsent = write(fd, line.characters(), line.length());
  150. if (nsent < 0) {
  151. perror("write");
  152. return nullptr;
  153. }
  154. ASSERT((size_t)nsent == line.length());
  155. char buffer[1024];
  156. int nrecv = read(fd, buffer, sizeof(buffer) - 1);
  157. if (nrecv < 0) {
  158. perror("recv");
  159. return nullptr;
  160. }
  161. if (!memcmp(buffer, "Not found.", sizeof("Not found.") - 1))
  162. return nullptr;
  163. auto responses = String(buffer, nrecv).split('\n');
  164. if (responses.is_empty())
  165. return nullptr;
  166. auto& response = responses[0];
  167. strncpy(__gethostbyaddr_name_buffer, response.characters(), max(sizeof(__gethostbyaddr_name_buffer), response.length()));
  168. __gethostbyaddr_buffer.h_name = __gethostbyaddr_name_buffer;
  169. __gethostbyaddr_buffer.h_aliases = nullptr;
  170. __gethostbyaddr_buffer.h_addrtype = AF_INET;
  171. // FIXME: Should we populate the hostent's address list here with a sockaddr_in for the provided host?
  172. __gethostbyaddr_address_list_buffer[0] = nullptr;
  173. __gethostbyaddr_buffer.h_addr_list = (char**)__gethostbyaddr_address_list_buffer;
  174. __gethostbyaddr_buffer.h_length = 4;
  175. return &__gethostbyaddr_buffer;
  176. }
  177. struct servent* getservent()
  178. {
  179. //If the services file is not open, attempt to open it and return null if it fails.
  180. if (!services_file) {
  181. services_file = fopen(services_path, "r");
  182. if (!services_file) {
  183. perror("error opening services file");
  184. return nullptr;
  185. }
  186. }
  187. if (fseek(services_file, service_file_offset, SEEK_SET) != 0) {
  188. perror("error seeking file");
  189. fclose(services_file);
  190. return nullptr;
  191. }
  192. char* line = nullptr;
  193. size_t len = 0;
  194. ssize_t read;
  195. auto free_line_on_exit = ScopeGuard([line] {
  196. if (line) {
  197. free(line);
  198. }
  199. });
  200. //Read lines from services file until an actual service name is found.
  201. do {
  202. read = getline(&line, &len, services_file);
  203. service_file_offset += read;
  204. if (read > 0 && (line[0] >= 65 && line[0] <= 122)) {
  205. break;
  206. }
  207. } while (read != -1);
  208. if (read == -1) {
  209. fclose(services_file);
  210. services_file = nullptr;
  211. service_file_offset = 0;
  212. return nullptr;
  213. }
  214. servent* service_entry = nullptr;
  215. if (!fill_getserv_buffers(line, read))
  216. return nullptr;
  217. __getserv_buffer.s_name = __getserv_name_buffer;
  218. __getserv_buffer.s_port = __getserv_port_buffer;
  219. __getserv_buffer.s_proto = __getserv_protocol_buffer;
  220. for (auto& alias : __getserv_alias_list_buffer) {
  221. __getserv_alias_list.append((char*)alias.data());
  222. }
  223. __getserv_buffer.s_aliases = __getserv_alias_list.data();
  224. service_entry = &__getserv_buffer;
  225. if (!keep_service_file_open) {
  226. endservent();
  227. }
  228. return service_entry;
  229. }
  230. struct servent* getservbyname(const char* name, const char* protocol)
  231. {
  232. bool previous_file_open_setting = keep_service_file_open;
  233. setservent(1);
  234. struct servent* current_service = nullptr;
  235. auto service_file_handler = ScopeGuard([previous_file_open_setting] {
  236. if (!previous_file_open_setting) {
  237. endservent();
  238. }
  239. });
  240. while (true) {
  241. current_service = getservent();
  242. if (current_service == nullptr)
  243. break;
  244. else if (!protocol && strcmp(current_service->s_name, name) == 0)
  245. break;
  246. else if (strcmp(current_service->s_name, name) == 0 && strcmp(current_service->s_proto, protocol) == 0)
  247. break;
  248. }
  249. return current_service;
  250. }
  251. struct servent* getservbyport(int port, const char* protocol)
  252. {
  253. bool previous_file_open_setting = keep_service_file_open;
  254. setservent(1);
  255. struct servent* current_service = nullptr;
  256. auto service_file_handler = ScopeGuard([previous_file_open_setting] {
  257. if (!previous_file_open_setting) {
  258. endservent();
  259. }
  260. });
  261. while (true) {
  262. current_service = getservent();
  263. if (current_service == nullptr)
  264. break;
  265. else if (!protocol && current_service->s_port == port)
  266. break;
  267. else if (current_service->s_port == port && (strcmp(current_service->s_proto, protocol) == 0))
  268. break;
  269. }
  270. return current_service;
  271. }
  272. void setservent(int stay_open)
  273. {
  274. if (!services_file) {
  275. services_file = fopen(services_path, "r");
  276. if (!services_file) {
  277. perror("error opening services file");
  278. return;
  279. }
  280. }
  281. rewind(services_file);
  282. keep_service_file_open = stay_open;
  283. service_file_offset = 0;
  284. }
  285. void endservent()
  286. {
  287. if (!services_file) {
  288. return;
  289. }
  290. fclose(services_file);
  291. services_file = nullptr;
  292. }
  293. //Fill the service entry buffer with the information contained in the currently read line, returns true if successfull, false if failure occurs.
  294. static bool fill_getserv_buffers(char* line, ssize_t read)
  295. {
  296. //Splitting the line by tab delimiter and filling the servent buffers name, port, and protocol members.
  297. String string_line = String(line, read);
  298. string_line.replace(" ", "\t", true);
  299. auto split_line = string_line.split('\t');
  300. //This indicates an incorrect file format. Services file entries should always at least contain name and port/protocol, seperated by tabs.
  301. if (split_line.size() < 2) {
  302. perror("malformed services file: entry");
  303. return false;
  304. }
  305. if (sizeof(__getserv_name_buffer) >= split_line[0].length() + 1) {
  306. strncpy(__getserv_name_buffer, split_line[0].characters(), split_line[0].length() + 1);
  307. } else {
  308. perror("invalid buffer length: service name");
  309. return false;
  310. }
  311. auto port_protocol_split = String(split_line[1]).split('/');
  312. if (port_protocol_split.size() < 2) {
  313. perror("malformed services file: port/protocol");
  314. return false;
  315. }
  316. bool conversion_checker;
  317. __getserv_port_buffer = port_protocol_split[0].to_int(conversion_checker);
  318. if (!conversion_checker) {
  319. return false;
  320. }
  321. //Removing any annoying whitespace at the end of the protocol.
  322. port_protocol_split[1].replace(" ", "", true);
  323. port_protocol_split[1].replace("\t", "", true);
  324. port_protocol_split[1].replace("\n", "", true);
  325. if (sizeof(__getserv_protocol_buffer) >= port_protocol_split[1].length()) {
  326. strncpy(__getserv_protocol_buffer, port_protocol_split[1].characters(), port_protocol_split[1].length() + 1);
  327. } else {
  328. perror("malformed services file: protocol");
  329. return false;
  330. }
  331. __getserv_alias_list_buffer.clear();
  332. //If there are aliases for the service, we will fill the alias list buffer.
  333. if (split_line.size() > 2 && !split_line[2].starts_with('#')) {
  334. for (size_t i = 2; i < split_line.size(); i++) {
  335. if (split_line[i].starts_with('#')) {
  336. break;
  337. }
  338. __getserv_alias_list_buffer.append(split_line[i].to_byte_buffer());
  339. }
  340. }
  341. return true;
  342. }
  343. }