ConnectionFromClient.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2021, Sergey Bugaev <bugaevc@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ConnectionFromClient.h"
  7. #include "DNSPacket.h"
  8. #include "LookupServer.h"
  9. #include <AK/IPv4Address.h>
  10. namespace LookupServer {
  11. static HashMap<int, RefPtr<ConnectionFromClient>> s_connections;
  12. ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket> socket, int client_id)
  13. : IPC::ConnectionFromClient<LookupClientEndpoint, LookupServerEndpoint>(*this, move(socket), client_id)
  14. {
  15. s_connections.set(client_id, *this);
  16. }
  17. ConnectionFromClient::~ConnectionFromClient()
  18. {
  19. }
  20. void ConnectionFromClient::die()
  21. {
  22. s_connections.remove(client_id());
  23. }
  24. Messages::LookupServer::LookupNameResponse ConnectionFromClient::lookup_name(String const& name)
  25. {
  26. auto maybe_answers = LookupServer::the().lookup(name, DNSRecordType::A);
  27. if (maybe_answers.is_error()) {
  28. dbgln("LookupServer: Failed to lookup PTR record: {}", maybe_answers.error());
  29. }
  30. auto answers = maybe_answers.release_value();
  31. Vector<String> addresses;
  32. for (auto& answer : answers) {
  33. addresses.append(answer.record_data());
  34. }
  35. return { 0, move(addresses) };
  36. }
  37. Messages::LookupServer::LookupAddressResponse ConnectionFromClient::lookup_address(String const& address)
  38. {
  39. if (address.length() != 4)
  40. return { 1, String() };
  41. IPv4Address ip_address { (const u8*)address.characters() };
  42. auto name = String::formatted("{}.{}.{}.{}.in-addr.arpa",
  43. ip_address[3],
  44. ip_address[2],
  45. ip_address[1],
  46. ip_address[0]);
  47. auto maybe_answers = LookupServer::the().lookup(name, DNSRecordType::PTR);
  48. if (maybe_answers.is_error()) {
  49. dbgln("LookupServer: Failed to lookup PTR record: {}", maybe_answers.error());
  50. }
  51. auto answers = maybe_answers.release_value();
  52. if (answers.is_empty())
  53. return { 1, String() };
  54. return { 0, answers[0].record_data() };
  55. }
  56. }